use stern4rust::finding::section::Section;
use stern4rust::finding::test_file_parser::TestFileParser;
use stern4rust::source_file::SourceFile;
fn items(contents: &str) -> Vec<stern4rust::finding::test_file_item::TestFileItem> {
TestFileParser::parse(&SourceFile::new("tests/a_tests.rs", contents)).expect("parses")
}
fn sections(contents: &str) -> Vec<Section> {
items(contents)
.into_iter()
.map(|item| item.section)
.collect()
}
#[test]
fn parse_classifies_a_const_and_a_static_as_constants() {
let found = sections("const A: usize = 1;\nstatic B: usize = 2;\n");
assert_eq!(found, [Section::Constants, Section::Constants]);
}
#[test]
fn parse_classifies_a_function_carrying_a_qualified_test_attribute_as_a_test() {
let found = sections("#[tokio::test]\nasync fn a() {}\n");
assert_eq!(found, [Section::Tests]);
}
#[test]
fn parse_classifies_a_function_carrying_the_test_attribute_as_a_test() {
let found = sections("#[test]\nfn a() {}\n");
assert_eq!(found, [Section::Tests]);
}
#[test]
fn parse_classifies_a_plain_function_as_a_helper() {
let found = sections("fn a() {}\n");
assert_eq!(found, [Section::Helpers]);
}
#[test]
fn parse_classifies_a_struct_an_impl_and_a_type_alias_as_helpers() {
let found = sections("struct A;\nimpl A {}\ntype B = A;\n");
assert_eq!(
found,
[Section::Helpers, Section::Helpers, Section::Helpers]
);
}
#[test]
fn parse_classifies_a_use_item_as_an_import() {
let found = sections("use alpha::One;\n");
assert_eq!(found, [Section::Imports]);
}
#[test]
fn parse_folds_a_leading_comment_into_the_item_below_it() {
let found = items("fn a() {}\n\n// Why this one matters.\n#[test]\nfn b() {}\n");
assert_eq!(found[1].first_line, 3);
}
#[test]
fn parse_folds_several_leading_comment_lines_into_the_item_below_them() {
let found = items("fn a() {}\n\n// One.\n// Two.\n#[test]\nfn b() {}\n");
assert_eq!(found[1].first_line, 3);
}
#[test]
fn parse_names_a_constant_by_its_identifier() {
let found = items("const SOME_LIMIT: usize = 1;\n");
assert_eq!(found[0].name, "SOME_LIMIT");
}
#[test]
fn parse_names_a_function_by_its_identifier() {
let found = items("fn helper_name() {}\n");
assert_eq!(found[0].name, "helper_name");
}
#[test]
fn parse_names_an_impl_block_after_the_type_it_implements() {
let found = items("impl Recorder {}\n");
assert_eq!(found[0].name, "Recorder");
}
#[test]
fn parse_names_an_import_as_it_was_written() {
let found = items("use alpha::One;\n");
assert!(found[0].name.contains("alpha::One"));
}
#[test]
fn parse_of_a_file_that_does_not_parse_returns_nothing() {
let found = TestFileParser::parse(&SourceFile::new("tests/a_tests.rs", "fn broken( {\n"));
assert!(found.is_none());
}
#[test]
fn parse_of_an_empty_file_returns_no_items() {
let found = items("");
assert!(found.is_empty());
}
#[test]
fn parse_reports_the_line_an_item_ends_on() {
let found = items("fn a() {\n let _ = 1;\n}\n");
assert_eq!(found[0].last_line, 3);
}
#[test]
fn parse_reports_the_line_an_item_starts_on() {
let found = items("\n\nfn a() {}\n");
assert_eq!(found[0].first_line, 3);
}
#[test]
fn parse_starts_a_test_block_at_its_attribute() {
let found = items("#[test]\nfn a() {}\n");
assert_eq!(found[0].first_line, 1);
}