test_rust_ast.py¶
tests/test_rust_ast.py
Rust tree-sitter dialect tests — the AST/call-graph half of Rust support (clew/rustdoc.py covers the structural half; see tests/test_rustdoc.py and tests/test_cli_rustdoc.py).
test_call_edges_resolves_direct_and_qualified_calls
test_rust_ast.test_call_edges_resolves_direct_and_qualified_calls()
Checks
assert "helper" in namesassert "spawn" in namesassert spawn_site[3] == "std::thread::spawn"
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-PIPE-003 | measured | test_call_edges_resolves_direct_and_qualified_calls → _ast_harvest_calls → is_python_tree |
Source · 18 lines
def test_call_edges_resolves_direct_and_qualified_calls():
from clew.call_edges import _ast_harvest_calls
tree, src = _parse(
"fn helper(x: i32) -> i32 { x + 1 }\n"
"fn main() {\n"
" helper(1);\n"
" std::thread::spawn(|| {});\n"
"}\n"
)
sites = _ast_harvest_calls(tree, src)
names = {s[0] for s in sites}
assert "helper" in names
assert "spawn" in names
spawn_site = next(s for s in sites if s[0] == "spawn")
assert spawn_site[3] == "std::thread::spawn"
Activity · 8 steps
test_call_edges_resolves_method_calls
test_rust_ast.test_call_edges_resolves_method_calls()
Checks
assert any(s[0] == "go" and s[2] == "ast_member" for s in sites)
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-PIPE-003 | measured | test_call_edges_resolves_method_calls → _ast_harvest_calls → is_python_tree |
Source · 13 lines
def test_call_edges_resolves_method_calls():
from clew.call_edges import _ast_harvest_calls
tree, src = _parse(
"struct Foo;\nimpl Foo {\n fn go(&self) {}\n}\nfn main() {\n let f = Foo;\n f.go();\n}\n"
)
sites = _ast_harvest_calls(tree, src)
assert any(s[0] == "go" and s[2] == "ast_member" for s in sites)
# ─── threads: std::thread::spawn ────────────────────────────────────────────
Activity · 4 steps
test_callback_edges_resolves_assignment_registration
test_rust_ast.test_callback_edges_resolves_assignment_registration()
Checks
assert any(r[1] == "Widget::callback" and r[2] == "h" for r in out["regs"])
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-PIPE-003 | measured |
Source · 13 lines
def test_callback_edges_resolves_assignment_registration():
from clew.callback_edges import _harvest_callback_file
tree, src = _parse(
"struct Widget { callback: fn() }\n"
"impl Widget {\n"
" fn set(&mut self, h: fn()) {\n"
" self.callback = h;\n"
" }\n"
"}\n"
)
out = _harvest_callback_file(tree, src)
assert any(r[1] == "Widget::callback" and r[2] == "h" for r in out["regs"])1 more lines, not shown
Activity · 4 steps
test_callback_edges_resolves_struct_literal_registration
test_rust_ast.test_callback_edges_resolves_struct_literal_registration()
Checks
assert "Widget::callback" in reg_globalsassert "new" in call_namesassert "Widget::callback" in call_names
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-PIPE-003 | measured |
Source · 26 lines
def test_callback_edges_resolves_struct_literal_registration():
from clew.callback_edges import _harvest_callback_file
tree, src = _parse(
"fn handler(x: i32) -> i32 { x }\n"
"struct Widget { callback: fn(i32) -> i32 }\n"
"impl Widget {\n"
" fn new(h: fn(i32) -> i32) -> Self {\n"
" Self { callback: h }\n"
" }\n"
" fn fire(&self, x: i32) -> i32 {\n"
" (self.callback)(x)\n"
" }\n"
"}\n"
"fn build() -> Widget {\n"
" Widget::new(handler)\n"
"}\n"
)
out = _harvest_callback_file(tree, src)
reg_globals = {r[1] for r in out["regs"]}
assert "Widget::callback" in reg_globals
call_names = {c[1] for c in out["calls"]}
assert "new" in call_names
assert "Widget::callback" in call_names
Activity · 8 steps
test_critical_section_excludes_call_after_early_return
test_rust_ast.test_critical_section_excludes_call_after_early_return()
Checks
assert len(sites) == 1assert "do_thing" in called_names
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-PIPE-003 | measured | test_critical_section_excludes_call_after_early_return → _walk_lock_sites → _visit_call → _append_site → _class_scope → enclosing |
| REQ-DDB-SCHEMA-011 | measured | test_critical_section_excludes_call_after_early_return → load_lock_patterns |
| REQ-DDB-SCHEMA-012 | measured | test_critical_section_excludes_call_after_early_return → load_lock_patterns → declaration_origin |
Source · 23 lines
def test_critical_section_excludes_call_after_early_return():
from clew.locks import _walk_lock_sites, load_lock_patterns
tree, src = _parse(
"fn f(m: &std::sync::Mutex<i32>, bad: bool) {\n"
" let g = m.lock().unwrap();\n"
" if bad {\n"
" return;\n"
" }\n"
" do_thing();\n"
"}\n"
)
patterns = {p.name: p for p in load_lock_patterns(None)}
sites = _walk_lock_sites(tree, src, patterns)
assert len(sites) == 1
calls = sites[0][10]
called_names = {c[0] for c in calls}
assert "do_thing" in called_names
# ─── callback_edges: closure/fn-pointer field registration ──────────────────
Activity · 8 steps
test_lock_site_detects_mutex_lock_binding
test_rust_ast.test_lock_site_detects_mutex_lock_binding()
Checks
assert len(sites) == 1assert name == "lock"assert operand == "self.mutex"assert scope == "class:Counter"assert form == "raii"assert kind == "mutex"assert mode == "exclusive"assert confidence == "high"
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-PIPE-003 | measured | test_lock_site_detects_mutex_lock_binding → _walk_lock_sites → _visit_call → _append_site → _class_scope → enclosing |
| REQ-DDB-SCHEMA-011 | measured | test_lock_site_detects_mutex_lock_binding → load_lock_patterns |
| REQ-DDB-SCHEMA-012 | measured | test_lock_site_detects_mutex_lock_binding → load_lock_patterns → declaration_origin |
Source · 25 lines
def test_lock_site_detects_mutex_lock_binding():
from clew.locks import _walk_lock_sites, load_lock_patterns
tree, src = _parse(
"struct Counter { mutex: std::sync::Mutex<i32> }\n"
"impl Counter {\n"
" fn bump(&self) {\n"
" let mut g = self.mutex.lock().unwrap();\n"
" *g += 1;\n"
" }\n"
"}\n"
)
patterns = {p.name: p for p in load_lock_patterns(None)}
sites = _walk_lock_sites(tree, src, patterns)
assert len(sites) == 1
name, operand, scope, line, end_line, form, kind, mode, role, confidence, calls = sites[0]
assert name == "lock"
assert operand == "self.mutex"
assert scope == "class:Counter"
assert form == "raii"
assert kind == "mutex"
assert mode == "exclusive"
assert confidence == "high"
Activity · 13 steps
test_lock_site_detects_mutex_lock_with_poison_recovery
test_rust_ast.test_lock_site_detects_mutex_lock_with_poison_recovery()
`.lock().unwrap_or_else(|e| e.into_inner())` is the standard poison-recovery idiom (vs. plain `.unwrap()`) and must still be recognized as a lock acquisition — found missing against a real codebase (tools_sqc/src/progress.rs), where every guard used this form and none were detected.
Checks
assert len(sites) == 1assert name == "lock"assert operand == "self.mutex"assert form == "raii"assert kind == "mutex"
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-PIPE-003 | measured | test_lock_site_detects_mutex_lock_with_poison_recovery → _walk_lock_sites → _visit_call → _append_site → _class_scope → enclosing |
| REQ-DDB-SCHEMA-011 | measured | test_lock_site_detects_mutex_lock_with_poison_recovery → load_lock_patterns |
| REQ-DDB-SCHEMA-012 | measured | test_lock_site_detects_mutex_lock_with_poison_recovery → load_lock_patterns → declaration_origin |
Source · 29 lines
def test_lock_site_detects_mutex_lock_with_poison_recovery():
"""`.lock().unwrap_or_else(|e| e.into_inner())` is the standard
poison-recovery idiom (vs. plain `.unwrap()`) and must still be
recognized as a lock acquisition — found missing against a real
codebase (tools_sqc/src/progress.rs), where every guard used this form
and none were detected."""
from clew.locks import _walk_lock_sites, load_lock_patterns
tree, src = _parse(
"struct Counter { mutex: std::sync::Mutex<i32> }\n"
"impl Counter {\n"
" fn bump(&self) {\n"
" let mut g = self.mutex.lock().unwrap_or_else(|e| e.into_inner());\n"
" *g += 1;\n"
" }\n"
"}\n"
)
patterns = {p.name: p for p in load_lock_patterns(None)}
sites = _walk_lock_sites(tree, src, patterns)
assert len(sites) == 1
name, operand, _scope, _line, _end_line, form, kind, _mode, _role, _confidence, _calls = sites[
0
]
assert name == "lock"
assert operand == "self.mutex"
assert form == "raii"
assert kind == "mutex"
Activity · 10 steps
test_lock_site_detects_rwlock_read_and_write
test_rust_ast.test_lock_site_detects_rwlock_read_and_write()
Checks
assert ("read", "shared") in kinds_modesassert ("write", "exclusive") in kinds_modes
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-PIPE-003 | measured | test_lock_site_detects_rwlock_read_and_write → _walk_lock_sites → _visit_call → _append_site → _class_scope → enclosing |
| REQ-DDB-SCHEMA-011 | measured | test_lock_site_detects_rwlock_read_and_write → load_lock_patterns |
| REQ-DDB-SCHEMA-012 | measured | test_lock_site_detects_rwlock_read_and_write → load_lock_patterns → declaration_origin |
Source · 19 lines
def test_lock_site_detects_rwlock_read_and_write():
from clew.locks import _walk_lock_sites, load_lock_patterns
tree, src = _parse(
"fn f(rw: &std::sync::RwLock<i32>) {\n"
" let r = rw.read().unwrap();\n"
" let w = rw.write().unwrap();\n"
"}\n"
)
patterns = {p.name: p for p in load_lock_patterns(None)}
sites = _walk_lock_sites(tree, src, patterns)
kinds_modes = {(s[0], s[7]) for s in sites}
assert ("read", "shared") in kinds_modes
assert ("write", "exclusive") in kinds_modes
# ─── critical_sections: block/jump node names generalized for Rust ─────────
Activity · 7 steps
test_thread_spawn_aliased_import_form
test_rust_ast.test_thread_spawn_aliased_import_form()
Called by none
Calls _parse, _walk_spawn_sites, load_thread_patterns, patterns_by_name
Checks
assert len(sites) == 1assert sites[0][1] == "worker"
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-PIPE-003 | measured | test_thread_spawn_aliased_import_form → _walk_spawn_sites → is_python_tree |
| REQ-DDB-SCHEMA-001 | measured | test_thread_spawn_aliased_import_form → load_thread_patterns |
| REQ-DDB-SCHEMA-012 | inferred | test_thread_spawn_aliased_import_form → load_thread_patterns → declaration_origin |
Source · 15 lines
def test_thread_spawn_aliased_import_form():
from clew.threads import _walk_spawn_sites, load_thread_patterns, patterns_by_name
tree, src = _parse(
"use std::thread;\nfn worker() {}\nfn main() {\n thread::spawn(|| { worker(); });\n}\n"
)
patterns = patterns_by_name(load_thread_patterns(None))
sites = _walk_spawn_sites(tree, src, patterns)
assert len(sites) == 1
assert sites[0][1] == "worker"
# ─── locks: Mutex/RwLock guard bindings ─────────────────────────────────────
Activity · 6 steps
test_thread_spawn_resolves_closure_entry
test_rust_ast.test_thread_spawn_resolves_closure_entry()
Called by none
Calls _parse, _walk_spawn_sites, load_thread_patterns, patterns_by_name
Checks
assert len(sites) == 1assert len(sites[0]) == 10assert entry_name == "worker"assert kind == "pthread"assert spawn_fn == "main"assert line == 3
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-PIPE-003 | measured | test_thread_spawn_resolves_closure_entry → _walk_spawn_sites → is_python_tree |
| REQ-DDB-SCHEMA-001 | measured | test_thread_spawn_resolves_closure_entry → load_thread_patterns |
| REQ-DDB-SCHEMA-012 | inferred | test_thread_spawn_resolves_closure_entry → load_thread_patterns → declaration_origin |
Source · 18 lines
def test_thread_spawn_resolves_closure_entry():
from clew.threads import _walk_spawn_sites, load_thread_patterns, patterns_by_name
tree, src = _parse(
"fn worker() {}\nfn main() {\n std::thread::spawn(|| { worker(); });\n}\n"
)
patterns = patterns_by_name(load_thread_patterns(None))
sites = _walk_spawn_sites(tree, src, patterns)
assert len(sites) == 1
## Ten elements since 2.1: the last two are the lifecycle and its line (`threadlife`).
thread_name, entry_name, kind, qualified_entry, _sep, line, spawn_fn, _source = sites[0][:8]
assert len(sites[0]) == 10
assert entry_name == "worker"
assert kind == "pthread"
assert spawn_fn == "main"
assert line == 3
Activity · 11 steps
test_ts_language_for_routes_rust
test_rust_ast.test_ts_language_for_routes_rust()
Checks
assert _ts_language_for("dir/file.rs") is not Noneassert _ts_language_for("foo.txt") is None
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-PIPE-012 | inferred | test_ts_language_for_routes_rust → _ts_language_for → _grammar_available → has_language |
Source · 8 lines
def test_ts_language_for_routes_rust():
assert _ts_language_for("dir/file.rs") is not None
assert _ts_language_for("foo.txt") is None
# ─── call_edges: Rust needs no dedicated branch ─────────────────────────────