|
| 1 | +import ast |
| 2 | +from pathlib import Path |
| 3 | + |
| 4 | + |
| 5 | +TOOLS_MODULE = Path("hyperbrowser/tools/__init__.py") |
| 6 | + |
| 7 | + |
| 8 | +def _collect_keys_calls(module: ast.AST) -> list[int]: |
| 9 | + keys_calls: list[int] = [] |
| 10 | + for node in ast.walk(module): |
| 11 | + if not isinstance(node, ast.Call): |
| 12 | + continue |
| 13 | + if not isinstance(node.func, ast.Attribute): |
| 14 | + continue |
| 15 | + if node.func.attr == "keys": |
| 16 | + keys_calls.append(node.lineno) |
| 17 | + return keys_calls |
| 18 | + |
| 19 | + |
| 20 | +def _collect_helper_calls(module: ast.AST, helper_name: str) -> list[int]: |
| 21 | + helper_calls: list[int] = [] |
| 22 | + for node in ast.walk(module): |
| 23 | + if not isinstance(node, ast.Call): |
| 24 | + continue |
| 25 | + if not isinstance(node.func, ast.Name): |
| 26 | + continue |
| 27 | + if node.func.id == helper_name: |
| 28 | + helper_calls.append(node.lineno) |
| 29 | + return helper_calls |
| 30 | + |
| 31 | + |
| 32 | +def test_tools_module_uses_shared_mapping_read_helpers(): |
| 33 | + source = TOOLS_MODULE.read_text(encoding="utf-8") |
| 34 | + module = ast.parse(source, filename=str(TOOLS_MODULE)) |
| 35 | + |
| 36 | + keys_calls = _collect_keys_calls(module) |
| 37 | + read_key_calls = _collect_helper_calls(module, "read_string_mapping_keys") |
| 38 | + copy_value_calls = _collect_helper_calls( |
| 39 | + module, "copy_mapping_values_by_string_keys" |
| 40 | + ) |
| 41 | + |
| 42 | + assert keys_calls == [] |
| 43 | + assert read_key_calls != [] |
| 44 | + assert copy_value_calls != [] |
0 commit comments