use super::*;
#[test]
fn test_cognitive_simple_if() {
let v = visit_code("if true { let _ = 1; }");
assert_eq!(v.cognitive_complexity, 1);
}
#[test]
fn test_cognitive_nested_if() {
let v = visit_code("if true { if false { let _ = 1; } }");
assert_eq!(v.cognitive_complexity, 3);
}
#[test]
fn test_cognitive_deep_nesting() {
let v = visit_code("if true { if false { if true { let _ = 1; } } }");
assert_eq!(v.cognitive_complexity, 6);
}
#[test]
fn test_cognitive_match() {
let v = visit_code("match 1 { 1 => {}, 2 => {}, _ => {} }");
assert_eq!(v.cognitive_complexity, 1);
}
#[test]
fn test_cognitive_for_while_loop() {
let v = visit_code("for _ in 0..10 { while true { loop { break; } } }");
assert_eq!(v.cognitive_complexity, 6);
}
#[test]
fn test_cognitive_boolean_alternation() {
let v = visit_code("let _ = true && false || true;");
assert!(
v.cognitive_complexity >= 1,
"Expected alternation to add to cognitive, got {}",
v.cognitive_complexity
);
}
#[test]
fn test_cognitive_no_alternation() {
let v = visit_code("let _ = true && false && true;");
assert_eq!(v.cognitive_complexity, 0);
}
#[test]
fn test_cyclomatic_basic() {
let v = visit_code("if true {} if false {}");
assert_eq!(v.cyclomatic_complexity, 3);
}
#[test]
fn test_cyclomatic_match_arms_all_trivial() {
let v = visit_code("match x { 1 => \"a\", 2 => \"b\", 3 => \"c\", _ => \"d\" }");
assert_eq!(v.cyclomatic_complexity, 1);
}
#[test]
fn test_cyclomatic_match_arms_with_control_flow() {
let v = visit_code("match x { 1 => if y { 1 } else { 2 }, 2 => 0, _ => 0 }");
assert_eq!(v.cyclomatic_complexity, 2);
}
#[test]
fn test_cyclomatic_match_all_nontrivial() {
let v = visit_code(
"match x { 1 => { let a = 1; a }, 2 => { let b = 2; b }, _ => { let c = 3; c } }",
);
assert_eq!(v.cyclomatic_complexity, 3);
}
#[test]
fn test_cyclomatic_lookup_table_match() {
let v = visit_code(
r#"match op {
0 => "+", 1 => "-", 2 => "*", 3 => "/",
4 => "%", 5 => "&&", 6 => "||", 7 => "^",
8 => "&", 9 => "|", _ => "?"
}"#,
);
assert_eq!(v.cyclomatic_complexity, 1);
}
#[test]
fn test_cyclomatic_match_mixed_trivial_nontrivial() {
let v = visit_code(
"match x { 1 => true, 2 => if y { true } else { false }, 3 => false, _ => false }",
);
assert_eq!(v.cyclomatic_complexity, 2);
}
#[test]
fn test_cyclomatic_boolean_ops() {
let v = visit_code("let _ = true && false || true;");
assert_eq!(v.cyclomatic_complexity, 3);
}
#[test]
fn test_complexity_hotspot_at_deep_nesting() {
let v = visit_code("if true { if false { if true { if false { let _ = 1; } } } }");
assert!(
!v.complexity_hotspots.is_empty(),
"Expected hotspot at nesting >= 3"
);
assert_eq!(v.complexity_hotspots[0].nesting_depth, 3);
assert_eq!(v.complexity_hotspots[0].construct, "if");
}
#[test]
fn test_no_hotspot_at_shallow_nesting() {
let v = visit_code("if true { if false { let _ = 1; } }");
assert!(v.complexity_hotspots.is_empty());
}