Debian Codegraph @

test/test_api/test_context.py

raw symbols SCIP
1 import pytest
2
3
4 def _iter_hierarchy(context):
5 def iter(context):
6 while context is not None:
7 yield context
8 context = context.parent()
9
10 return reversed(list(iter(context)))
11
12
13 func_code = '''\
14 def func1(x, y):
15 pass
16
17 def func2():
18 what ?
19 i = 3
20
21 def func3():
22 1'''
23 cls_code = '''\
24 class Foo:
25 def x():
26 def y():
27 pass
28 '''
29 cls_nested = '''\
30 class C:
31 class D:
32 def f():
33 pass
34 '''
35 lambda_ = '''\
36 def x():
37 (lambda x:
38 lambda: y
39 )
40 '''
41 comprehension = '''
42 def f(x):
43 [x
44 for
45 x
46 in x
47 ]'''
48
49 with_brackets = '''\
50 def x():
51 [
52
53 ]
54 '''
55
56
57 @pytest.mark.parametrize(
58 'code, line, column, full_name, expected_parents', [
59 ('', None, None, 'myfile', []),
60 (' ', None, 0, 'myfile', []),
61
62 (func_code, 1, 0, 'myfile', []),
63 (func_code, 1, None, 'myfile.func1', ['func1']),
64 (func_code, 1, 1, 'myfile.func1', ['func1']),
65 (func_code, 1, 4, 'myfile.func1', ['func1']),
66 (func_code, 1, 10, 'myfile.func1', ['func1']),
67
68 (func_code, 3, 0, 'myfile', []),
69 (func_code, 5, None, 'myfile.func2', ['func2']),
70 (func_code, 6, None, 'myfile', []),
71 (func_code, 7, None, 'myfile', []),
72 (func_code, 9, None, 'myfile.func3', ['func3']),
73
74 (cls_code, None, None, 'myfile', []),
75 (cls_code + ' ', None, None, 'myfile.Foo', ['Foo']),
76 (cls_code + ' ' * 3, None, None, 'myfile.Foo', ['Foo']),
77 (cls_code + ' ' * 4, None, None, 'myfile.Foo', ['Foo']),
78 (cls_code + ' ' * 5, None, None, 'myfile.Foo.x', ['Foo', 'x']),
79 (cls_code + ' ' * 8, None, None, 'myfile.Foo.x', ['Foo', 'x']),
80 (cls_code + ' ' * 12, None, None, None, ['Foo', 'x', 'y']),
81
82 (cls_code, 4, 0, 'myfile', []),
83 (cls_code, 4, 3, 'myfile.Foo', ['Foo']),
84 (cls_code, 4, 4, 'myfile.Foo', ['Foo']),
85 (cls_code, 4, 5, 'myfile.Foo.x', ['Foo', 'x']),
86 (cls_code, 4, 8, 'myfile.Foo.x', ['Foo', 'x']),
87 (cls_code, 4, 12, None, ['Foo', 'x', 'y']),
88 (cls_code, 1, 1, 'myfile.Foo', ['Foo']),
89
90 (cls_nested, 4, None, 'myfile.C.D.f', ['C', 'D', 'f']),
91 (cls_nested, 4, 3, 'myfile.C', ['C']),
92
93 (lambda_, 2, 9, 'myfile.x', ['x']), # the lambda keyword
94 (lambda_, 2, 13, 'myfile.x', ['x']), # the lambda param
95 (lambda_, 3, 0, 'myfile', []), # Within brackets, but they are ignored.
96 (lambda_, 3, 8, 'myfile.x', ['x']),
97 (lambda_, 3, None, 'myfile.x', ['x']),
98
99 (comprehension, 2, None, 'myfile.f', ['f']),
100 (comprehension, 3, None, 'myfile.f', ['f']),
101 (comprehension, 4, None, 'myfile.f', ['f']),
102 (comprehension, 5, None, 'myfile.f', ['f']),
103 (comprehension, 6, None, 'myfile.f', ['f']),
104
105 # Brackets are just ignored.
106 (with_brackets, 3, None, 'myfile', []),
107 (with_brackets, 4, 4, 'myfile.x', ['x']),
108 (with_brackets, 4, 5, 'myfile.x', ['x']),
109 ]
110 )
111 def test_context(Script, code, line, column, full_name, expected_parents):
112 context = Script(code, path='/foo/myfile.py').get_context(line, column)
113 assert context.full_name == full_name
114 parent_names = [d.name for d in _iter_hierarchy(context)]
115 assert parent_names == ['myfile'] + expected_parents