Coverage for tests / test_call_graph / test_cycles.py: 100%
31 statements
« prev ^ index » next coverage.py v7.13.3, created at 2026-02-08 15:04 -0800
« prev ^ index » next coverage.py v7.13.3, created at 2026-02-08 15:04 -0800
1"""Tests for call graph cycle detection."""
3from __future__ import annotations
5from pathlib import Path
6import tempfile
8from call_graph import build_call_graph, find_call_cycles
11def test_recursive_cycle():
12 code = """
13def foo():
14 return foo()
15"""
16 with tempfile.NamedTemporaryFile(mode="w", suffix=".py", delete=False) as f:
17 f.write(code)
18 f.flush()
20 graph = build_call_graph([Path(f.name)])
21 cycles = find_call_cycles(graph)
23 assert len(cycles) > 0
25 Path(f.name).unlink()
28def test_mutual_recursion():
29 code = """
30def foo():
31 return bar()
33def bar():
34 return foo()
35"""
36 with tempfile.NamedTemporaryFile(mode="w", suffix=".py", delete=False) as f:
37 f.write(code)
38 f.flush()
40 graph = build_call_graph([Path(f.name)])
41 cycles = find_call_cycles(graph)
43 assert len(cycles) > 0
45 Path(f.name).unlink()
48def test_no_cycle():
49 code = """
50def foo():
51 return bar()
53def bar():
54 return 42
56def baz():
57 return 1
58"""
59 with tempfile.NamedTemporaryFile(mode="w", suffix=".py", delete=False) as f:
60 f.write(code)
61 f.flush()
63 graph = build_call_graph([Path(f.name)])
64 cycles = find_call_cycles(graph)
66 assert len(cycles) == 0
68 Path(f.name).unlink()