use stern4rust::rule::Rule;
use stern4rust::rules::readable_source_rule::ReadableSourceRule;
use stern4rust::source_file::SourceFile;
const RULE: &str = "readable-source";
fn check(contents: &str) -> Vec<stern4rust::reporting::offence::Offence> {
ReadableSourceRule::new().check(&SourceFile::new("src/subject.rs", contents))
}
#[test]
fn check_a_file_of_nul_bytes_reports_it() {
let corrupted = "\0".repeat(41);
let offences = check(&corrupted);
assert_eq!(offences.len(), 1);
assert_eq!(offences[0].rule, RULE);
}
#[test]
fn check_a_file_that_does_not_parse_reports_it() {
let offences = check("fn broken( {\n");
assert_eq!(offences.len(), 1);
assert!(
offences[0].description.contains("does not parse"),
"got {}",
offences[0].description
);
}
#[test]
fn check_a_file_that_parses_reports_nothing() {
let offences = check("pub struct Subject;\n");
assert!(offences.is_empty(), "expected none, got {offences:?}");
}
#[test]
fn check_an_empty_file_reports_nothing() {
let offences = check("");
assert!(offences.is_empty(), "expected none, got {offences:?}");
}
#[test]
fn check_names_the_file_it_judged() {
let offences = check("fn broken( {\n");
assert_eq!(offences[0].file, "src/subject.rs");
}
#[test]
fn check_reports_the_line_the_parse_failed_on() {
let offences = check("pub struct Fine;\n\npub struct Also;\n\nfn broken( {\n");
assert!(offences[0].line >= 5, "got line {}", offences[0].line);
}
#[test]
fn name_is_the_kebab_case_rule_name_used_in_the_report() {
let name = ReadableSourceRule::new().name();
assert_eq!(name, RULE);
}