use crate::services::cargo_dead_code_analyzer::{
estimated_dead_lines, estimated_dead_lines_bounded, DeadCodeKind, DeadItem,
};
fn functions(n: usize) -> Vec<DeadItem> {
(0..n)
.map(|i| DeadItem {
name: format!("d{i}"),
kind: DeadCodeKind::Function,
line: i,
column: 1,
message: "never used".to_string(),
})
.collect()
}
#[test]
fn the_estimate_never_exceeds_the_file_it_describes() {
let items = functions(4);
assert_eq!(
estimated_dead_lines(&items),
20,
"the raw estimator is unchanged"
);
assert_eq!(
estimated_dead_lines_bounded(&items, Some(5)),
5,
"a 5-line file cannot hold 20 dead lines"
);
}
#[test]
fn an_estimate_below_the_file_length_is_left_alone() {
let items = functions(1);
assert_eq!(estimated_dead_lines_bounded(&items, Some(100)), 5);
}
#[test]
fn an_unknown_file_length_keeps_the_raw_estimate() {
let items = functions(3);
assert_eq!(
estimated_dead_lines_bounded(&items, None),
estimated_dead_lines(&items)
);
}
#[test]
fn no_items_is_no_dead_lines_at_any_length() {
assert_eq!(estimated_dead_lines_bounded(&[], Some(0)), 0);
assert_eq!(estimated_dead_lines_bounded(&[], None), 0);
}
#[test]
fn default_timeout_covers_a_cold_cargo_check() {
use crate::services::cargo_dead_code_analyzer::{
CargoDeadCodeAnalyzer, DEFAULT_ANALYSIS_TIMEOUT_SECS,
};
assert_eq!(
CargoDeadCodeAnalyzer::new(".").timeout(),
std::time::Duration::from_secs(DEFAULT_ANALYSIS_TIMEOUT_SECS),
"the constructor must use the one named default, not a second literal"
);
assert!(
DEFAULT_ANALYSIS_TIMEOUT_SECS >= 245,
"the default must cover a cold `cargo check` on the repo that ships it (measured 245s)"
);
}