use stern4rust::registry_item::RegistryItem;
use stern4rust::registry_parser::RegistryParser;
use stern4rust::registry_policy::RegistryPolicy;
use stern4rust::source_file::SourceFile;
fn labels(body: &str) -> Vec<String> {
strays(body).into_iter().map(|stray| stray.label).collect()
}
fn strays(body: &str) -> Vec<RegistryItem> {
RegistryParser::strays(
&SourceFile::new("tests/all_tests.rs", body),
RegistryPolicy::tests(),
)
.expect("parses")
}
#[test]
fn strays_names_a_constant_with_its_identifier() {
let found = labels("const LIMIT: usize = 1;\n");
assert_eq!(found, ["the constant `LIMIT`"]);
}
#[test]
fn strays_names_a_function_with_its_identifier() {
let found = labels("fn helper() {}\n");
assert_eq!(found, ["the function `helper`"]);
}
#[test]
fn strays_names_a_static_with_its_identifier() {
let found = labels("static LIMIT: usize = 1;\n");
assert_eq!(found, ["the static `LIMIT`"]);
}
#[test]
fn strays_names_a_struct_with_its_identifier() {
let found = labels("struct Recorder;\n");
assert_eq!(found, ["the struct `Recorder`"]);
}
#[test]
fn strays_names_a_type_alias_with_its_identifier() {
let found = labels("type Pair = (usize, usize);\n");
assert_eq!(found, ["the type alias `Pair`"]);
}
#[test]
fn strays_names_an_impl_block_by_the_line_as_written() {
let found = labels("struct Recorder;\nimpl Recorder {}\n");
assert_eq!(found[1], "the impl block `impl Recorder {}`");
}
#[test]
fn strays_names_an_import_by_the_line_as_written() {
let found = labels("use std::fmt::Debug;\n");
assert_eq!(found, ["the import `use std::fmt::Debug;`"]);
}
#[test]
fn strays_names_an_inline_module_with_its_identifier() {
let found = labels("mod inner { }\n");
assert_eq!(found, ["the inline module `inner`"]);
}
#[test]
fn strays_of_a_file_that_does_not_parse_returns_nothing() {
let found = RegistryParser::strays(
&SourceFile::new("tests/all_tests.rs", "mod broken {\n"),
RegistryPolicy::tests(),
);
assert!(found.is_none());
}
#[test]
fn strays_of_a_private_mod_declaration_returns_nothing() {
let found = strays("mod alpha_tests;\n");
assert!(found.is_empty(), "expected none, got {found:?}");
}
#[test]
fn strays_of_a_registry_of_only_declarations_returns_nothing() {
let found = strays("pub mod alpha_tests;\npub mod beta_tests;\n");
assert!(found.is_empty(), "expected none, got {found:?}");
}
#[test]
fn strays_of_an_empty_file_returns_nothing() {
let found = strays("");
assert!(found.is_empty(), "expected none, got {found:?}");
}
#[test]
fn strays_reports_each_stray_at_its_own_line() {
let body =
"use std::fmt;\n\nconst LIMIT: usize = 1;\n\nfn helper() {}\n\npub mod alpha_tests;\n";
let found = strays(body);
assert_eq!(found.len(), 3);
assert_eq!(
found.iter().map(|stray| stray.line).collect::<Vec<usize>>(),
[1, 3, 5]
);
}