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

1"""Tests for call graph cycle detection.""" 

2 

3from __future__ import annotations 

4 

5from pathlib import Path 

6import tempfile 

7 

8from call_graph import build_call_graph, find_call_cycles 

9 

10 

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

19 

20 graph = build_call_graph([Path(f.name)]) 

21 cycles = find_call_cycles(graph) 

22 

23 assert len(cycles) > 0 

24 

25 Path(f.name).unlink() 

26 

27 

28def test_mutual_recursion(): 

29 code = """ 

30def foo(): 

31 return bar() 

32 

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

39 

40 graph = build_call_graph([Path(f.name)]) 

41 cycles = find_call_cycles(graph) 

42 

43 assert len(cycles) > 0 

44 

45 Path(f.name).unlink() 

46 

47 

48def test_no_cycle(): 

49 code = """ 

50def foo(): 

51 return bar() 

52 

53def bar(): 

54 return 42 

55 

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

62 

63 graph = build_call_graph([Path(f.name)]) 

64 cycles = find_call_cycles(graph) 

65 

66 assert len(cycles) == 0 

67 

68 Path(f.name).unlink()