| 38 | |
| 39 | |
| 40 | class TestChunkIds(unittest.TestCase): |
| 41 | def _ids(self, source: str) -> list: |
| 42 | tree = ast.parse(source) |
| 43 | seen: dict = {} |
| 44 | return [_make_chunk_id(n, seen) for n in tree.body] |
| 45 | |
| 46 | def test_function(self): |
| 47 | self.assertEqual(self._ids("def foo(): pass"), ["FunctionDef:foo"]) |
| 48 | |
| 49 | def test_async_function(self): |
| 50 | self.assertEqual(self._ids("async def bar(): pass"), ["AsyncFunctionDef:bar"]) |
| 51 | |
| 52 | def test_class(self): |
| 53 | self.assertEqual(self._ids("class MyClass: pass"), ["ClassDef:MyClass"]) |
| 54 | |
| 55 | def test_bare_statement(self): |
| 56 | self.assertEqual(self._ids("x = 1"), ["stmt:1"]) |
| 57 | |
| 58 | def test_duplicate_names_get_suffix(self): |
| 59 | ids = self._ids("def foo(): pass\ndef foo(): pass") |
| 60 | self.assertEqual(ids, ["FunctionDef:foo", "FunctionDef:foo:1"]) |
| 61 | |
| 62 | def test_mixed(self): |
| 63 | ids = self._ids("x = 1\ndef foo(): pass\nclass Bar: pass") |
| 64 | self.assertEqual(ids, ["stmt:1", "FunctionDef:foo", "ClassDef:Bar"]) |
| 65 | |
| 66 | |
| 67 | # ── TestBuildAstJson ───────────────────────────────────────────────────────── |
nothing calls this directly
no outgoing calls
no test coverage detected