|
| 1 | +"""Tests for leaf-node identifier filtering. |
| 2 | +
|
| 3 | +Covers filter_leaf_nodes: identifiers that merely contain a word like |
| 4 | +"invalid" must survive, while strings that are not known component ids |
| 5 | +(error messages reaching leaf-node selection, malformed entries) are dropped. |
| 6 | +""" |
| 7 | + |
| 8 | +from __future__ import annotations |
| 9 | + |
| 10 | +from codewiki.src.be.dependency_analyzer.leaf_selection import filter_leaf_nodes |
| 11 | +from codewiki.src.be.dependency_analyzer.models.core import Node |
| 12 | + |
| 13 | + |
| 14 | +def _component(component_id: str, component_type: str = "function") -> Node: |
| 15 | + file_path = component_id.split("::")[0] |
| 16 | + return Node( |
| 17 | + id=component_id, |
| 18 | + name=component_id.split("::")[-1], |
| 19 | + component_type=component_type, |
| 20 | + file_path=file_path, |
| 21 | + relative_path=file_path, |
| 22 | + ) |
| 23 | + |
| 24 | + |
| 25 | +def _components(*component_ids: str) -> dict[str, Node]: |
| 26 | + return {cid: _component(cid) for cid in component_ids} |
| 27 | + |
| 28 | + |
| 29 | +def test_identifiers_containing_error_words_are_kept() -> None: |
| 30 | + components = _components( |
| 31 | + "src/input.cpp::handleInvalidInput", |
| 32 | + "src/game.cpp::gameFailedCheck", |
| 33 | + "src/log.cpp::ErrorLog", |
| 34 | + "src/parser.cpp::parseExceptionTable", |
| 35 | + ) |
| 36 | + |
| 37 | + kept = filter_leaf_nodes(list(components), components, {"function"}) |
| 38 | + |
| 39 | + assert set(kept) == set(components) |
| 40 | + |
| 41 | + |
| 42 | +def test_error_messages_are_dropped() -> None: |
| 43 | + components = _components("src/player.cpp::playerMove") |
| 44 | + candidates = [ |
| 45 | + "src/player.cpp::playerMove", |
| 46 | + "Error: could not parse file", |
| 47 | + "invalid syntax at line 3", |
| 48 | + "Analysis failed for this component", |
| 49 | + ] |
| 50 | + |
| 51 | + kept = filter_leaf_nodes(candidates, components, {"function"}) |
| 52 | + |
| 53 | + assert kept == ["src/player.cpp::playerMove"] |
| 54 | + |
| 55 | + |
| 56 | +def test_unknown_and_malformed_entries_are_dropped() -> None: |
| 57 | + components = _components("src/player.cpp::playerMove") |
| 58 | + candidates = [ |
| 59 | + "src/player.cpp::playerMove", |
| 60 | + "src/player.cpp::doesNotExist", |
| 61 | + "", |
| 62 | + " ", |
| 63 | + None, |
| 64 | + ] |
| 65 | + |
| 66 | + kept = filter_leaf_nodes(candidates, components, {"function"}) |
| 67 | + |
| 68 | + assert kept == ["src/player.cpp::playerMove"] |
| 69 | + |
| 70 | + |
| 71 | +def test_components_of_other_types_are_dropped() -> None: |
| 72 | + components = { |
| 73 | + "src/game.cpp::runGame": _component("src/game.cpp::runGame", "function"), |
| 74 | + "src/game.h::GameState": _component("src/game.h::GameState", "struct"), |
| 75 | + } |
| 76 | + |
| 77 | + kept = filter_leaf_nodes(list(components), components, {"function"}) |
| 78 | + |
| 79 | + assert kept == ["src/game.cpp::runGame"] |
0 commit comments