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

40 statements  

« prev     ^ index     » next       coverage.py v7.13.3, created at 2026-02-08 15:04 -0800

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

2 

3from __future__ import annotations 

4 

5from pathlib import Path 

6import tempfile 

7 

8from call_graph import build_call_graph 

9from models import CallGraph 

10 

11 

12def test_local_call_resolution(): 

13 code = """ 

14def foo(): 

15 return bar() 

16  

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

35def test_external_call_resolution(): 

36 code = """ 

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

55def test_method_call_resolution(): 

56 code = """ 

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