#[cfg(feature = "rust")]
#[test]
fn the_exclude_tests_prune_resolves_no_sibling_from_a_node() {
let source = "#[cfg(test)]\nmod tests {\nfn t() {}\n}\n\
#[inline]\nfn kept() {\n#[allow(dead_code)]\nfn nested() {}\nlet x = 1;\n}\n";
let ast = crate::test_support::parse_named(crate::LANG::Rust, "lib.rs", source);
let root = ast.root_node();
let last = root.children().last().expect("the file has items");
let _ = last.previous_sibling();
let seeded = crate::node::node_resolved_sibling_lookups::observed();
assert!(seeded > 0, "the seed call must be counted");
ast.metrics(crate::MetricsOptions::default().with_exclude_tests(true))
.expect("the walk must yield a top-level space");
assert_eq!(
crate::node::node_resolved_sibling_lookups::observed(),
seeded,
"the metric walk resolved a sibling from a node; \
read it off the ancestor chain instead (#1096 / #1100)"
);
}
#[cfg(feature = "rust")]
#[test]
fn the_exclude_tests_prune_reads_forward_up_to_its_depth_scaled_budget() {
let at_budget = "#[cfg(test)]\nfn a() {}\n#[inline]\nfn b() {}\n#[cfg(test)]\nfn c() {}\n";
let past_budget = format!("{at_budget}fn d() {{}}\n");
let nested = format!("mod m {{\n{past_budget}}}\n");
for (shape, source, resolves_siblings) in [
("six children at depth 1", at_budget.to_string(), false),
("seven children at depth 1", past_budget, true),
("nine children at depth 3", nested, false),
] {
let before = crate::node::node_resolved_sibling_lookups::observed();
crate::test_support::parse_named(crate::LANG::Rust, "lib.rs", &source)
.metrics(crate::MetricsOptions::default().with_exclude_tests(true))
.expect("the walk must yield a top-level space");
let resolved = crate::node::node_resolved_sibling_lookups::observed() > before;
assert_eq!(
resolved, resolves_siblings,
"{shape}: the prune took the wrong dispatch arm"
);
}
}
#[cfg(all(feature = "c", feature = "python", feature = "rust"))]
#[test]
fn the_metric_and_suppression_walks_scan_a_tree_on_one_cursor() {
use crate::ParserTrait;
let seed = crate::CParser::new(
b"int main() { int a; }".to_vec(),
std::path::Path::new("a.c"),
None,
);
let _ = seed.root().children().count();
assert!(
crate::node::child_scan_cursors::observed() > 0,
"the seed scan must be counted"
);
let source = "class C:\n def a(self):\n self.x = 1\n self.y = [1, 2]\n\
\n def b(self):\n self.z, self.w = 1, 2\n \
if self.x:\n self.v = self.y\n";
let ast = crate::test_support::parse_named(crate::LANG::Python, "c.py", source);
let nodes = ast.root_node().preorder().count();
let before = crate::node::child_scan_cursors::observed();
ast.metrics(crate::MetricsOptions::default())
.expect("the walk must yield a top-level space");
let scans = crate::node::child_scan_cursors::observed() - before;
assert!(nodes > 60, "fixture is too small to prove much");
assert!(
scans < nodes / 2,
"the Python metric walk built {scans} cursors over {nodes} nodes; the \
instance-attribute scan is meant to hold one for the subtree (#1112)"
);
let parser = crate::langs::RustParser::new(
b"// bca: suppress(cognitive)\nfn f() { if a { g(1, 2); } }\n".to_vec(),
std::path::Path::new("lib.rs"),
None,
);
let nodes = parser.root().preorder().count();
let before = crate::node::child_scan_cursors::observed();
let markers = crate::suppression::suppression_markers(&parser);
let scans = crate::node::child_scan_cursors::observed() - before;
assert_eq!(markers.len(), 1, "fixture carries one marker");
assert!(nodes > 20, "fixture is too small to prove much");
assert!(
scans < nodes / 2,
"the suppression scan built {scans} cursors over {nodes} nodes (#1112)"
);
}