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()

tests/test_rust_ast.py:41

Show on the map →

Called by none

Calls _ast_harvest_calls, _parse

Checks

  • assert "helper" in names
  • assert "spawn" in names
  • assert spawn_site[3] == "std::thread::spawn"

Result: passed

Verifies

requirementlinkedpath to the implementer
REQ-DDB-PIPE-003measuredtest_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
lines 42–42from clew.call_edges import _ast_harvest_callslines 44–50tree, src = _parse( "fn helper(x: i32) -> i32 { x + 1 }\n" "fn main() {\n" " helper(1);\n" " std::thread::spawn(|| {});\n" "}\n" )lines 51–51sites = _ast_harvest_calls(tree, src)lines 52–52names = {s[0] for s in sites}lines 53–53assert "helper" in nameslines 54–54assert "spawn" in nameslines 55–55spawn_site = next(s for s in sites if s[0] == "spawn")lines 56–56assert spawn_site[3] == "std::thread::spawn"

test_call_edges_resolves_method_calls

test_rust_ast.test_call_edges_resolves_method_calls()

tests/test_rust_ast.py:59

Show on the map →

Called by none

Calls _ast_harvest_calls, _parse

Checks

  • assert any(s[0] == "go" and s[2] == "ast_member" for s in sites)

Result: passed

Verifies

requirementlinkedpath to the implementer
REQ-DDB-PIPE-003measuredtest_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
lines 60–60from clew.call_edges import _ast_harvest_callslines 62–64tree, src = _parse( "struct Foo;\nimpl Foo {\n fn go(&self) {}\n}\nfn main() {\n let f = Foo;\n f.go();\n}\n" )lines 65–65sites = _ast_harvest_calls(tree, src)lines 66–66assert any(s[0] == "go" and s[2] == "ast_member" for s in sites)

test_callback_edges_resolves_assignment_registration

test_rust_ast.test_callback_edges_resolves_assignment_registration()

tests/test_rust_ast.py:227

Show on the map →

Called by none

Calls _harvest_callback_file, _parse

Checks

  • assert any(r[1] == "Widget::callback" and r[2] == "h" for r in out["regs"])

Result: passed

Verifies

requirementlinkedpath to the implementer
REQ-DDB-PIPE-003measured
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
lines 228–228from clew.callback_edges import _harvest_callback_filelines 230–237tree, src = _parse( "struct Widget { callback: fn() }\n" "impl Widget {\n" " fn set(&mut self, h: fn()) {\n" " self.callback = h;\n" " }\n" "}\n" )lines 238–238out = _harvest_callback_file(tree, src)lines 239–239assert any(r[1] == "Widget::callback" and r[2] == "h" for r in out["regs"])

test_callback_edges_resolves_struct_literal_registration

test_rust_ast.test_callback_edges_resolves_struct_literal_registration()

tests/test_rust_ast.py:201

Show on the map →

Called by none

Calls _harvest_callback_file, _parse

Checks

  • assert "Widget::callback" in reg_globals
  • assert "new" in call_names
  • assert "Widget::callback" in call_names

Result: passed

Verifies

requirementlinkedpath to the implementer
REQ-DDB-PIPE-003measured
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
lines 202–202from clew.callback_edges import _harvest_callback_filelines 204–218tree, 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" )lines 219–219out = _harvest_callback_file(tree, src)lines 220–220reg_globals = {r[1] for r in out["regs"]}lines 221–221assert "Widget::callback" in reg_globalslines 222–222call_names = {c[1] for c in out["calls"]}lines 223–223assert "new" in call_nameslines 224–224assert "Widget::callback" in call_names

test_critical_section_excludes_call_after_early_return

test_rust_ast.test_critical_section_excludes_call_after_early_return()

tests/test_rust_ast.py:178

Show on the map →

Called by none

Calls _parse, _walk_lock_sites, load_lock_patterns

Checks

  • assert len(sites) == 1
  • assert "do_thing" in called_names

Result: passed

Verifies

requirementlinkedpath to the implementer
REQ-DDB-PIPE-003measuredtest_critical_section_excludes_call_after_early_return → _walk_lock_sites → _visit_call → _append_site → _class_scope → enclosing
REQ-DDB-SCHEMA-011measuredtest_critical_section_excludes_call_after_early_return → load_lock_patterns
REQ-DDB-SCHEMA-012measuredtest_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
lines 179–179from clew.locks import _walk_lock_sites, load_lock_patternslines 181–189tree, 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" )lines 190–190patterns = {p.name: p for p in load_lock_patterns(None)}lines 191–191sites = _walk_lock_sites(tree, src, patterns)lines 192–192assert len(sites) == 1lines 193–193calls = sites[0][10]lines 194–194called_names = {c[0] for c in calls}lines 195–195assert "do_thing" in called_names

test_lock_site_detects_mutex_lock_binding

test_rust_ast.test_lock_site_detects_mutex_lock_binding()

tests/test_rust_ast.py:105

Show on the map →

Called by none

Calls _parse, _walk_lock_sites, load_lock_patterns

Checks

  • assert len(sites) == 1
  • assert 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

requirementlinkedpath to the implementer
REQ-DDB-PIPE-003measuredtest_lock_site_detects_mutex_lock_binding → _walk_lock_sites → _visit_call → _append_site → _class_scope → enclosing
REQ-DDB-SCHEMA-011measuredtest_lock_site_detects_mutex_lock_binding → load_lock_patterns
REQ-DDB-SCHEMA-012measuredtest_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
lines 106–106from clew.locks import _walk_lock_sites, load_lock_patternslines 108–116tree, 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" )lines 117–117patterns = {p.name: p for p in load_lock_patterns(None)}lines 118–118sites = _walk_lock_sites(tree, src, patterns)lines 119–119assert len(sites) == 1lines 120–120name, operand, scope, line, end_line, form, kind, mode, role, confidence, calls = sites[0]lines 121–121assert name == "lock"lines 122–122assert operand == "self.mutex"lines 123–123assert scope == "class:Counter"lines 124–124assert form == "raii"lines 125–125assert kind == "mutex"lines 126–126assert mode == "exclusive"lines 127–127assert confidence == "high"

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.

tests/test_rust_ast.py:130

Show on the map →

Called by none

Calls _parse, _walk_lock_sites, load_lock_patterns

Checks

  • assert len(sites) == 1
  • assert name == "lock"
  • assert operand == "self.mutex"
  • assert form == "raii"
  • assert kind == "mutex"

Result: passed

Verifies

requirementlinkedpath to the implementer
REQ-DDB-PIPE-003measuredtest_lock_site_detects_mutex_lock_with_poison_recovery → _walk_lock_sites → _visit_call → _append_site → _class_scope → enclosing
REQ-DDB-SCHEMA-011measuredtest_lock_site_detects_mutex_lock_with_poison_recovery → load_lock_patterns
REQ-DDB-SCHEMA-012measuredtest_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
lines 136–136from clew.locks import _walk_lock_sites, load_lock_patternslines 138–146tree, 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" )lines 147–147patterns = {p.name: p for p in load_lock_patterns(None)}lines 148–148sites = _walk_lock_sites(tree, src, patterns)lines 149–149assert len(sites) == 1lines 150–152name, operand, _scope, _line, _end_line, form, kind, _mode, _role, _confidence, _calls = sites[ 0 ]lines 153–153assert name == "lock"lines 154–154assert operand == "self.mutex"lines 155–155assert form == "raii"lines 156–156assert kind == "mutex"

test_lock_site_detects_rwlock_read_and_write

test_rust_ast.test_lock_site_detects_rwlock_read_and_write()

tests/test_rust_ast.py:159

Show on the map →

Called by none

Calls _parse, _walk_lock_sites, load_lock_patterns

Checks

  • assert ("read", "shared") in kinds_modes
  • assert ("write", "exclusive") in kinds_modes

Result: passed

Verifies

requirementlinkedpath to the implementer
REQ-DDB-PIPE-003measuredtest_lock_site_detects_rwlock_read_and_write → _walk_lock_sites → _visit_call → _append_site → _class_scope → enclosing
REQ-DDB-SCHEMA-011measuredtest_lock_site_detects_rwlock_read_and_write → load_lock_patterns
REQ-DDB-SCHEMA-012measuredtest_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
lines 160–160from clew.locks import _walk_lock_sites, load_lock_patternslines 162–167tree, src = _parse( "fn f(rw: &std::sync::RwLock<i32>) {\n" " let r = rw.read().unwrap();\n" " let w = rw.write().unwrap();\n" "}\n" )lines 168–168patterns = {p.name: p for p in load_lock_patterns(None)}lines 169–169sites = _walk_lock_sites(tree, src, patterns)lines 170–170kinds_modes = {(s[0], s[7]) for s in sites}lines 171–171assert ("read", "shared") in kinds_modeslines 172–172assert ("write", "exclusive") in kinds_modes

test_thread_spawn_aliased_import_form

test_rust_ast.test_thread_spawn_aliased_import_form()

tests/test_rust_ast.py:90

Show on the map →

Called by none

Calls _parse, _walk_spawn_sites, load_thread_patterns, patterns_by_name

Checks

  • assert len(sites) == 1
  • assert sites[0][1] == "worker"

Result: passed

Verifies

requirementlinkedpath to the implementer
REQ-DDB-PIPE-003measuredtest_thread_spawn_aliased_import_form → _walk_spawn_sites → is_python_tree
REQ-DDB-SCHEMA-001measuredtest_thread_spawn_aliased_import_form → load_thread_patterns
REQ-DDB-SCHEMA-012inferredtest_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
lines 91–91from clew.threads import _walk_spawn_sites, load_thread_patterns, patterns_by_namelines 93–95tree, src = _parse( "use std::thread;\nfn worker() {}\nfn main() {\n thread::spawn(|| { worker(); });\n}\n" )lines 96–96patterns = patterns_by_name(load_thread_patterns(None))lines 97–97sites = _walk_spawn_sites(tree, src, patterns)lines 98–98assert len(sites) == 1lines 99–99assert sites[0][1] == "worker"

test_thread_spawn_resolves_closure_entry

test_rust_ast.test_thread_spawn_resolves_closure_entry()

tests/test_rust_ast.py:72

Show on the map →

Called by none

Calls _parse, _walk_spawn_sites, load_thread_patterns, patterns_by_name

Checks

  • assert len(sites) == 1
  • assert len(sites[0]) == 10
  • assert entry_name == "worker"
  • assert kind == "pthread"
  • assert spawn_fn == "main"
  • assert line == 3

Result: passed

Verifies

requirementlinkedpath to the implementer
REQ-DDB-PIPE-003measuredtest_thread_spawn_resolves_closure_entry → _walk_spawn_sites → is_python_tree
REQ-DDB-SCHEMA-001measuredtest_thread_spawn_resolves_closure_entry → load_thread_patterns
REQ-DDB-SCHEMA-012inferredtest_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
lines 73–73from clew.threads import _walk_spawn_sites, load_thread_patterns, patterns_by_namelines 75–77tree, src = _parse( "fn worker() {}\nfn main() {\n std::thread::spawn(|| { worker(); });\n}\n" )lines 78–78patterns = patterns_by_name(load_thread_patterns(None))lines 79–79sites = _walk_spawn_sites(tree, src, patterns)lines 80–80assert len(sites) == 1lines 82–82thread_name, entry_name, kind, qualified_entry, _sep, line, spawn_fn, _source = sites[0][:8]lines 83–83assert len(sites[0]) == 10lines 84–84assert entry_name == "worker"lines 85–85assert kind == "pthread"lines 86–86assert spawn_fn == "main"lines 87–87assert line == 3

test_ts_language_for_routes_rust

test_rust_ast.test_ts_language_for_routes_rust()

tests/test_rust_ast.py:33

Checks

  • assert _ts_language_for("dir/file.rs") is not None
  • assert _ts_language_for("foo.txt") is None

Result: passed

Verifies

requirementlinkedpath to the implementer
REQ-DDB-PIPE-012inferredtest_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 ─────────────────────────────

Activity · 2 steps
lines 34–34assert _ts_language_for("dir/file.rs") is not Nonelines 35–35assert _ts_language_for("foo.txt") is None