use super::*;
#[test]
fn violation_effort_score_is_the_weighted_sum() {
let a = raw_analysis_of("fn g() {} fn f() { if true { g(); } }", "f");
assert_eq!(a.effort_score, Some(4.5), "effort = 1*1.0 + 1*1.5 + 1*2.0");
}
#[test]
fn violation_with_total_two_is_low_severity() {
let a = raw_analysis_of("fn g() {} fn f() { if true { g(); } }", "f");
assert_eq!(
a.severity,
Some(Severity::Low),
"total == 2 is Low, not Medium"
);
}
#[test]
fn violation_with_total_five_is_medium_severity() {
let a = raw_analysis_of(
"fn g() {} fn f() { if true {} if true {} if true {} if true {} g(); }",
"f",
);
assert_eq!(
a.severity,
Some(Severity::Medium),
"total == 5 is Medium, not High"
);
}
#[test]
fn single_line_function_has_one_line_count() {
let m = metrics_of("fn f() { if true { let _ = 1; } }");
assert_eq!(m.function_lines, 1, "a single-line body is 1 line");
}
#[test]
fn for_loop_with_return_delegation_is_not_logic() {
let m = metrics_of("fn g() {} fn f() { for _ in 0..1 { return g(); } }");
assert_eq!(
m.logic_count, 0,
"a return-delegating for body is not logic"
);
}
#[test]
fn for_loop_with_paren_delegation_is_not_logic() {
let m = metrics_of("fn g() {} fn f() { for _ in 0..1 { (g()); } }");
assert_eq!(m.logic_count, 0, "a paren-delegating for body is not logic");
}
#[test]
fn match_with_block_arms_keeps_them_trivial() {
let m = metrics_of("fn f(x: i32) -> i32 { match x { 1 => { 0 }, _ => { 1 } } }");
assert_eq!(
m.cyclomatic_complexity, 1,
"single-statement block match arms are trivial"
);
}
#[test]
fn impl_method_in_nested_module_is_analysed() {
let names = analysed_names("mod inner { struct S; impl S { fn m(&self) {} } }");
assert!(names.iter().any(|n| n == "m"), "got {names:?}");
}
#[test]
fn trait_method_in_nested_module_is_analysed() {
let names = analysed_names("mod inner { trait T { fn m(&self) {} } }");
assert!(names.iter().any(|n| n == "m"), "got {names:?}");
}
#[test]
fn function_in_doubly_nested_module_is_analysed() {
let names = analysed_names("mod inner { mod deeper { fn m() {} } }");
assert!(names.iter().any(|n| n == "m"), "got {names:?}");
}
#[test]
fn method_in_top_level_cfg_test_impl_is_marked_test() {
let m = analysis_of(
"struct Keep; struct S; #[cfg(test)] impl S { fn m(&self) {} }",
"m",
);
assert!(m.is_test, "a method in a #[cfg(test)] impl is test code");
}
#[test]
fn default_method_in_top_level_cfg_test_trait_is_marked_test() {
let m = analysis_of("struct Keep; #[cfg(test)] trait T { fn m(&self) {} }", "m");
assert!(
m.is_test,
"a default method in a #[cfg(test)] trait is test code"
);
}
#[test]
fn method_in_modular_cfg_test_impl_is_marked_test() {
let m = analysis_of(
"struct Keep; struct S; mod inner { #[cfg(test)] impl super::S { fn m(&self) {} } }",
"m",
);
assert!(
m.is_test,
"a method in a nested #[cfg(test)] impl is test code"
);
}