use super::*;
#[test]
fn flags_a_fn_only_tests_call() {
let f = unwired(
"pub fn helper(x: u8) -> bool { true }\n",
"assert!(helper(1));",
&["helper".into()],
);
assert_eq!(f.len(), 1, "is_ignored_dir landed exactly like this");
assert_eq!(f.first().map(|x| x.rule), Some("unwired"));
}
#[test]
fn the_crate_source_is_assembled_in_a_stable_order() {
let root = Path::new(env!("CARGO_MANIFEST_DIR")).join("src");
let read = || -> String {
crate::fed::rust_files(&root)
.iter()
.filter_map(|p| std::fs::read_to_string(p).ok())
.map(|s| split_module(&s).0.to_string())
.collect()
};
assert_eq!(read(), read(), "two reads of one tree are one string");
assert!(
!crate::fed::rust_files(&root)
.iter()
.any(|p| p.components().any(|c| c.as_os_str() == "target")),
"a build product is not crate source"
);
}
#[test]
fn a_new_fn_recognising_an_existing_fn_s_markers_is_flagged() {
let src = "fn old(s: &str) -> bool {\n \
s.starts_with(\"## \u{a7}T\") && s.contains(\"status\")\n}\n\
fn fresh(s: &str) -> bool {\n \
s.contains(\"## \u{a7}T\") || s.contains(\"status\")\n}\n";
let f = duplication(src, &["fresh".to_string()]);
assert_eq!(f.len(), 1, "{f:?}");
assert_eq!(f.first().map(|x| x.rule), Some("duplication"));
assert!(
f.first().is_some_and(|x| x.detail.contains("`old`")),
"the report names what it duplicates: {f:?}"
);
}
#[test]
fn calling_the_existing_fn_is_reuse_and_not_a_finding() {
let src = "fn old(s: &str) -> bool {\n \
s.starts_with(\"## \u{a7}T\") && s.contains(\"status\")\n}\n\
fn fresh(s: &str) -> bool {\n \
old(s) && s.contains(\"## \u{a7}T\") && s.contains(\"status\")\n}\n";
assert!(duplication(src, &["fresh".to_string()]).is_empty());
}
#[test]
fn one_shared_marker_is_below_the_threshold() {
let src = "fn old(s: &str) -> bool {\n \
s.starts_with(\"## \u{a7}T\") && s.contains(\"other\")\n}\n\
fn fresh(s: &str) -> bool {\n \
s.contains(\"## \u{a7}T\") && s.contains(\"unrelated\")\n}\n";
assert!(duplication(src, &["fresh".to_string()]).is_empty());
}
#[test]
fn a_length_check_beside_positional_indexes_names_the_slice_pattern() {
let src = "fn t() {\n let e = edges(F);\n \
assert_eq!(e.len(), 2);\n assert_eq!(e[0].dir, \"src\");\n \
assert_eq!(e[1].tokens, None);\n}\n";
let f = fix_shapes(src, &["t".to_string()]);
assert_eq!(f.first().map(|x| x.rule), Some("positional-index"), "{f:?}");
assert!(
f.first().is_some_and(|x| x.detail.contains("as_slice()")),
"the finding names the shape to write: {f:?}"
);
}
#[test]
fn a_single_index_without_a_length_check_is_not_the_shape() {
let src = "fn t() {\n let v = go();\n use_it(v[0]);\n}\n";
assert!(fix_shapes(src, &["t".to_string()]).is_empty());
}
#[test]
fn a_long_byte_scanner_names_the_named_boundary_shape() {
let mut src = String::from("fn t(s: &str) {\n let b = s.as_bytes();\n");
for _ in 0..16 {
src.push_str(" if b[i] == b'x' { i += 1; }\n");
}
src.push_str("}\n");
let f = fix_shapes(&src, &["t".to_string()]);
assert!(f.iter().any(|x| x.rule == "byte-scanner"), "{f:?}");
}
#[test]
fn a_subprocess_whose_status_is_never_read_is_named() {
let src = "fn t() {\n let out = Command::new(\"git\").output();\n \
let Ok(out) = out else { return };\n \
let diff = String::from_utf8_lossy(&out.stdout);\n}\n";
let f = fix_shapes(src, &["t".to_string()]);
assert!(f.iter().any(|x| x.rule == "status-unread"), "{f:?}");
let ok = src.replace(
"let Ok(out) = out",
"let Ok(out) = out.filter(|o| o.status.success())",
);
assert!(
!fix_shapes(&ok, &["t".to_string()])
.iter()
.any(|x| x.rule == "status-unread")
);
}
#[test]
fn every_rule_that_runs_is_named_in_the_report() {
assert_eq!(RULES.len(), 8);
for r in [
"unwired",
"negative-only",
"ignored-input",
"undocumented",
"duplication",
"positional-index",
"byte-scanner",
"status-unread",
] {
assert!(RULES.contains(&r), "{r} is not named");
}
}
#[test]
fn accepts_a_fn_the_impl_actually_calls() {
let f = unwired(
"pub fn helper(x: u8) -> bool { true }\nfn go() { helper(2); }\n",
"assert!(helper(1));",
&["helper".into()],
);
assert!(f.is_empty(), "{f:?}");
}
#[test]
fn a_generic_declaration_is_still_a_declaration() {
let crate_src =
"pub fn helper<'a>(x: &'a str) -> bool { true }\nlet v = helper(s);\n";
assert!(
unwired(crate_src, "assert!(helper(1));", &["helper".into()])
.is_empty()
);
}
#[test]
fn flags_a_generic_fn_nothing_calls() {
let crate_src = "pub fn helper<'a>(x: &'a str) -> bool { true }\n";
assert_eq!(
unwired(crate_src, "assert!(helper(1));", &["helper".into()]).len(),
1
);
}
#[test]
fn accepts_a_fn_called_from_a_sibling_node() {
let crate_src = "pub fn helper(x: u8) -> bool { true }\n\
// ... src/cli/mod.rs ...\nlet v = helper(3);\n";
assert!(
unwired(crate_src, "assert!(helper(1));", &["helper".into()])
.is_empty()
);
}
const DETECTOR: &str = "pub fn detect(e: &[u8]) -> Vec<String> { vec![] }";
#[test]
fn a_new_pub_fn_without_a_doc_is_flagged() {
let f = undocumented(
"pub fn post_with_retry(t: &T) -> u8 { 1 }",
&["post_with_retry".into()],
);
assert_eq!(f.len(), 1, "{f:?}");
}
#[test]
fn a_documented_one_is_not() {
let f = undocumented(
"/// Retries a failing post.\npub fn post_with_retry() -> u8 { 1 }",
&["post_with_retry".into()],
);
assert!(f.is_empty(), "{f:?}");
}
#[test]
fn an_attribute_between_the_doc_and_the_fn_still_counts_as_documented() {
let f = undocumented(
"/// Retries.\n#[must_use]\npub fn go() -> u8 { 1 }",
&["go".into()],
);
assert!(f.is_empty(), "{f:?}");
}
#[test]
fn a_fn_the_commit_did_not_add_is_not_judged() {
let f = undocumented("pub fn old() -> u8 { 1 }", &["new_one".into()]);
assert!(f.is_empty(), "{f:?}");
}
#[test]
fn flags_a_detector_tested_only_on_empty() {
let f = negative_only(
DETECTOR,
"let c = detect(&e); assert!(c.is_empty());",
&["detect".into()],
);
assert_eq!(f.len(), 1, "detect_cycles passed exactly like this");
}
#[test]
fn accepts_a_detector_with_a_positive_case() {
let f = negative_only(
DETECTOR,
"let c = detect(&e); assert!(!c.is_empty());",
&["detect".into()],
);
assert!(f.is_empty(), "{f:?}");
}
#[test]
fn a_scalar_returning_fn_is_not_a_detector() {
let f = negative_only(
"pub fn double(n: u8) -> u8 { n * 2 }",
"assert_eq!(double(2), 4);",
&["double".into()],
);
assert!(f.is_empty(), "a scalar cannot 'find nothing': {f:?}");
}
#[test]
fn an_option_returning_fn_is_still_a_detector() {
let f = negative_only(
"pub fn find(k: &str) -> Option<u8> { None }",
"assert!(find(\"x\").is_none());",
&["find".into()],
);
assert_eq!(f.len(), 1, "None-only is the same defect: {f:?}");
}
#[test]
fn flags_a_new_fn_that_ignores_an_input() {
let src =
"pub fn hint(root: &Path, _budget: u64) -> Vec<PathBuf> { vec![] }\n";
let f = ignored_input(src, &["hint".into()]);
assert_eq!(
f.len(),
1,
"check_split_hint ignored _budget exactly like this"
);
assert_eq!(f.first().map(|x| x.rule), Some("ignored-input"));
}
#[test]
fn accepts_a_fn_that_uses_every_input() {
let src =
"pub fn hint(root: &Path, budget: u64) -> Vec<PathBuf> { vec![] }\n";
assert!(ignored_input(src, &["hint".into()]).is_empty());
}