Coverage for tests/test_call_graph/test_cycles.py: 100%

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

2

3 from __future__ import annotations

4

5 from pathlib import Path

6 import tempfile

7

8 from call_graph import build_call_graph, find_call_cycles

9

10

11 def test_recursive_cycle():

12 code = """

13 def 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

28 def test_mutual_recursion():

29 code = """

30 def foo():

31 return bar()

32

33 def 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

48 def test_no_cycle():

49 code = """

50 def foo():

51 return bar()

52

53 def bar():

54 return 42

55

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