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

1"""Tests for callee resolution."""

2

3 from __future__ import annotations

4

5 from pathlib import Path

6 import tempfile

7

8 from call_graph import build_call_graph

9 from models import CallGraph

10

11

12 def test_local_call_resolution():

13 code = """

14 def foo():

15 return bar()

16

17 def bar():

18 return 42

19"""

20 with tempfile.NamedTemporaryFile(mode="w", suffix=".py", delete=False) as f:

21 f.write(code)

22 f.flush()

23

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

25

26 foo_calls = [c for c in graph.calls if c.caller_function == "foo"]

27 assert len(foo_calls) == 1

28 assert foo_calls[0].callee_function == "bar"

29 assert foo_calls[0].call_type == "local"

30 assert foo_calls[0].callee_file is not None

31

32 Path(f.name).unlink()

33

34

35 def test_external_call_resolution():

36 code = """

37 def foo():

38 return print("hello")

39"""

40 with tempfile.NamedTemporaryFile(mode="w", suffix=".py", delete=False) as f:

41 f.write(code)

42 f.flush()

43

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

45

46 foo_calls = [c for c in graph.calls if c.caller_function == "foo"]

47 assert len(foo_calls) == 1

48 assert foo_calls[0].callee_function == "print"

49 assert foo_calls[0].call_type == "external"

50 assert foo_calls[0].callee_file is None

51

52 Path(f.name).unlink()

53

54

55 def test_method_call_resolution():

56 code = """

57 class MyClass:

58 def foo(self):

59 self.bar()

60

61 def bar(self):

62 return 42

63"""

64 with tempfile.NamedTemporaryFile(mode="w", suffix=".py", delete=False) as f:

65 f.write(code)

66 f.flush()

67

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

69

70 foo_calls = [c for c in graph.calls if c.caller_function == "foo"]

71 assert len(foo_calls) == 1

72 assert foo_calls[0].callee_function == "bar"

73 assert foo_calls[0].call_type == "method"

74

75 Path(f.name).unlink()