use stern4rust::reporting::offence::Offence;
#[test]
fn new_keeps_every_field_it_was_given() {
let offence = Offence::new(
"src/a.rs",
12,
"header",
"expected something".to_string(),
"fix it like this".to_string(),
);
assert_eq!(offence.file, "src/a.rs");
assert_eq!(offence.line, 12);
assert_eq!(offence.rule, "header");
assert_eq!(offence.description, "expected something");
assert_eq!(offence.correction, "fix it like this");
}
#[test]
fn new_leaves_the_machine_readable_fields_empty() {
let offence = Offence::new(
"src/a.rs",
12,
"header",
"expected something".to_string(),
"fix".to_string(),
);
assert_eq!(offence.subject, None);
assert_eq!(offence.expected, None);
}
#[test]
fn offences_differing_only_by_line_are_not_equal() {
let first = Offence::new(
"src/a.rs",
1,
"header",
"same".to_string(),
"fix".to_string(),
);
let second = Offence::new(
"src/a.rs",
2,
"header",
"same".to_string(),
"fix".to_string(),
);
assert_ne!(first, second);
}
#[test]
fn sort_key_groups_offences_by_file_before_line() {
let first = Offence::new("src/a.rs", 90, "header", "x".to_string(), "fix".to_string());
let second = Offence::new("src/b.rs", 2, "header", "x".to_string(), "fix".to_string());
assert!(first.sort_key() < second.sort_key());
}
#[test]
fn sort_key_orders_two_offences_in_the_same_file_by_line() {
let first = Offence::new("src/a.rs", 2, "header", "x".to_string(), "fix".to_string());
let second = Offence::new("src/a.rs", 10, "header", "x".to_string(), "fix".to_string());
assert!(first.sort_key() < second.sort_key());
}
#[test]
fn with_expected_attaches_the_text_the_rule_knows_is_correct() {
let offence = Offence::new("src/a.rs", 1, "header", "x".to_string(), "fix".to_string())
.with_expected("// Copyright\n// MIT");
assert_eq!(offence.expected, Some("// Copyright\n// MIT".to_string()));
}
#[test]
fn with_subject_names_the_thing_the_offence_is_about() {
let offence = Offence::new("src/a.rs", 1, "header", "x".to_string(), "fix".to_string())
.with_subject("the constant `LIMIT`");
assert_eq!(offence.subject, Some("the constant `LIMIT`".to_string()));
}