use super::*;
use crate::adapters::analyzers::iosp::MagicNumberOccurrence;
fn cx_config(max_cognitive: usize, max_cyclomatic: usize) -> Config {
let mut c = Config::default();
c.complexity.max_cognitive = max_cognitive;
c.complexity.max_cyclomatic = max_cyclomatic;
c
}
fn apply_cx(metrics: ComplexityMetrics, config: &Config) -> (bool, bool, usize, usize) {
let mut fa = make_func_with_metrics(metrics);
let mut summary = Summary::from_results(&[]);
apply_complexity_warnings(
std::slice::from_mut(&mut fa),
config,
&mut summary,
&std::collections::HashMap::new(),
);
(
fa.cognitive_warning,
fa.cyclomatic_warning,
summary.complexity_warnings,
summary.magic_number_warnings,
)
}
#[test]
fn complexity_exactly_at_threshold_does_not_warn() {
let m = ComplexityMetrics {
cognitive_complexity: 5,
cyclomatic_complexity: 5,
..Default::default()
};
assert_eq!(apply_cx(m, &cx_config(5, 5)), (false, false, 0, 0));
}
#[test]
fn cognitive_over_threshold_warns_alone() {
let m = ComplexityMetrics {
cognitive_complexity: 6,
cyclomatic_complexity: 5,
..Default::default()
};
assert_eq!(apply_cx(m, &cx_config(5, 5)), (true, false, 1, 0));
}
#[test]
fn cyclomatic_over_threshold_warns_alone() {
let m = ComplexityMetrics {
cognitive_complexity: 5,
cyclomatic_complexity: 6,
..Default::default()
};
assert_eq!(apply_cx(m, &cx_config(5, 5)), (false, true, 1, 0));
}
#[test]
fn magic_numbers_are_summed() {
let m = ComplexityMetrics {
magic_numbers: vec![
MagicNumberOccurrence {
line: 1,
value: "42".into(),
},
MagicNumberOccurrence {
line: 2,
value: "7".into(),
},
MagicNumberOccurrence {
line: 3,
value: "13".into(),
},
],
..Default::default()
};
assert_eq!(apply_cx(m, &cx_config(5, 5)).3, 3);
}
#[test]
fn disabled_complexity_emits_no_warnings() {
let mut config = cx_config(5, 5);
config.complexity.enabled = false;
let m = ComplexityMetrics {
cognitive_complexity: 99,
cyclomatic_complexity: 99,
..Default::default()
};
assert_eq!(apply_cx(m, &config), (false, false, 0, 0));
}