-
Notifications
You must be signed in to change notification settings - Fork 3
/
test_pyq.py
275 lines (206 loc) · 10.1 KB
/
test_pyq.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
from pyq.astmatch import ASTMatchEngine
import unittest
import os.path
import ast
class TestASTMatchEngine(unittest.TestCase):
def setUp(self):
self.m = ASTMatchEngine()
def filepath(self, filename):
return os.path.join(os.path.dirname(__file__), 'testfiles', filename)
def test_classes(self):
matches = list(self.m.match('class', self.filepath('classes.py')))
self.assertEqual(len(matches), 4)
# check instances
self.assertIsInstance(matches[0][0], ast.ClassDef)
self.assertIsInstance(matches[1][0], ast.ClassDef)
self.assertIsInstance(matches[2][0], ast.ClassDef)
self.assertIsInstance(matches[3][0], ast.ClassDef)
# check lines
self.assertEqual(matches[0][1], 1)
self.assertEqual(matches[1][1], 9)
self.assertEqual(matches[2][1], 13)
self.assertEqual(matches[3][1], 14)
def test_classes_with_specific_method(self):
matches1 = list(self.m.match('class:not(:has(def))',
self.filepath('classes.py')))
matches2 = list(self.m.match('class:has(def[name=bar])',
self.filepath('classes.py')))
matches3 = list(self.m.match('class:has(> def)',
self.filepath('classes.py')))
self.assertEqual(len(matches1), 1)
self.assertIsInstance(matches1[0][0], ast.ClassDef)
self.assertEqual(len(matches2), 3)
self.assertIsInstance(matches2[0][0], ast.ClassDef)
self.assertIsInstance(matches2[1][0], ast.ClassDef)
self.assertIsInstance(matches2[2][0], ast.ClassDef)
self.assertEqual(len(matches3), 2)
self.assertIsInstance(matches3[0][0], ast.ClassDef)
self.assertIsInstance(matches3[1][0], ast.ClassDef)
self.assertEqual(matches3[0][1], 1)
self.assertEqual(matches3[1][1], 14)
def test_methods(self):
matches = list(self.m.match('class def', self.filepath('classes.py')))
self.assertEqual(len(matches), 3)
# check instances
self.assertIsInstance(matches[0][0], ast.FunctionDef)
self.assertIsInstance(matches[1][0], ast.FunctionDef)
self.assertIsInstance(matches[2][0], ast.FunctionDef)
# check lines
self.assertEqual(matches[0][1], 2)
self.assertEqual(matches[1][1], 5)
self.assertEqual(matches[2][1], 15)
def test_methods_by_name(self):
matches1 = list(self.m.match('class def[name=baz]',
self.filepath('classes.py')))
matches2 = list(self.m.match('class def[name!=baz]',
self.filepath('classes.py')))
self.assertEqual(len(matches1), 1)
self.assertEqual(matches1[0][1], 5)
self.assertEqual(len(matches2), 2)
self.assertEqual(matches2[0][1], 2)
self.assertEqual(matches2[1][1], 15)
def test_import(self):
matches = list(self.m.match('import', self.filepath('imports.py')))
self.assertEqual(len(matches), 6)
# check instances
self.assertIsInstance(matches[0][0], ast.ImportFrom)
self.assertIsInstance(matches[1][0], ast.ImportFrom)
self.assertIsInstance(matches[2][0], ast.ImportFrom)
self.assertIsInstance(matches[3][0], ast.ImportFrom)
self.assertIsInstance(matches[4][0], ast.Import)
self.assertIsInstance(matches[5][0], ast.Import)
def test_import_from(self):
matches = list(self.m.match('import[from=foo]',
self.filepath('imports.py')))
self.assertEqual(len(matches), 2)
# check instances
self.assertIsInstance(matches[0][0], ast.ImportFrom)
self.assertIsInstance(matches[1][0], ast.ImportFrom)
# check lines
self.assertEqual(matches[0][1], 1)
self.assertEqual(matches[1][1], 2)
def test_import_not_from(self):
matches = list(self.m.match('import:not([from^=foo])',
self.filepath('imports.py')))
self.assertEqual(len(matches), 3)
# check instances
self.assertIsInstance(matches[0][0], ast.ImportFrom)
self.assertIsInstance(matches[1][0], ast.Import)
self.assertIsInstance(matches[2][0], ast.Import)
def test_import_name(self):
matches = list(self.m.match('import[name=example2]',
self.filepath('imports.py')))
self.assertEqual(len(matches), 1)
matches = list(self.m.match('import[name=bar], import[name=bar2]',
self.filepath('imports.py')))
self.assertEqual(len(matches), 2)
def test_import_multiple(self):
matches1 = list(self.m.match('import[name=xyz]',
self.filepath('imports.py')))
matches2 = list(self.m.match('import[name=xyz][name=bar2]',
self.filepath('imports.py')))
matches3 = list(self.m.match('import[name=foo.baz]',
self.filepath('imports.py')))
matches4 = list(self.m.match('import[name^=foo]',
self.filepath('imports.py')))
matches5 = list(self.m.match('import[from^=foo]',
self.filepath('imports.py')))
self.assertEqual(len(matches1), 1)
self.assertEqual(len(matches2), 1)
self.assertEqual(len(matches3), 1)
self.assertEqual(len(matches4), 1)
self.assertEqual(len(matches5), 3)
def test_import_special_attr(self):
matches1 = list(self.m.match('import[full=foo.bar2]',
self.filepath('imports.py')))
matches2 = list(self.m.match('import[full=foo.xyz]',
self.filepath('imports.py')))
self.assertEqual(len(matches1), 1)
self.assertEqual(len(matches2), 1)
def test_match_id(self):
matches = list(self.m.match('#foo,#bar', self.filepath('ids.py')))
self.assertEqual(len(matches), 5)
def test_assign(self):
matches1 = list(self.m.match('assign', self.filepath('assign.py')))
matches2 = list(self.m.match('assign#foo', self.filepath('assign.py')))
matches3 = list(
self.m.match('assign[name=bar]', self.filepath('assign.py')))
matches4 = list(self.m.match('assign#b', self.filepath('assign.py')))
matches5 = list(
self.m.match('#abc,[name=abc]', self.filepath('assign.py')))
self.assertEqual(len(matches1), 6)
self.assertEqual(len(matches2), 1)
self.assertEqual(matches2[0][1], 1)
self.assertEqual(len(matches3), 1)
self.assertEqual(matches3[0][1], 2)
self.assertEqual(len(matches4), 1)
self.assertEqual(matches4[0][1], 4)
self.assertEqual(len(matches5), 1)
self.assertEqual(matches5[0][1], 5)
def test_calls(self):
matches1 = list(
self.m.match('[name=print]', self.filepath('calls.py')))
matches2 = list(self.m.match('call#foo', self.filepath('calls.py')))
matches3 = list(self.m.match('call', self.filepath('calls.py')))
matches4 = list(self.m.match('[name=foo]', self.filepath('calls.py')))
self.assertEqual(len(matches1), 1)
self.assertEqual(len(matches2), 2)
self.assertEqual(len(matches3), 5)
self.assertEqual(len(matches4), 2)
def test_call_arg_kwarg(self):
matches1 = list(self.m.match('call[kwarg=a]',
self.filepath('calls.py')))
matches2 = list(self.m.match('call[kwarg=x]',
self.filepath('calls.py')))
matches3 = list(self.m.match('call[arg=bar]',
self.filepath('calls.py')))
matches4 = list(self.m.match('[arg=bang]', self.filepath('calls.py')))
self.assertEqual(len(matches1), 1)
self.assertEqual(len(matches2), 2)
self.assertEqual(len(matches3), 2)
self.assertEqual(len(matches4), 2)
self.assertIsInstance(matches1[0][0], ast.Call)
self.assertIsInstance(matches2[0][0], ast.Call)
self.assertIsInstance(matches2[1][0], ast.Call)
self.assertIsInstance(matches3[0][0], ast.Call)
self.assertIsInstance(matches3[1][0], ast.Call)
self.assertIsInstance(matches4[0][0], ast.Call)
def test_attrs(self):
matches1 = list(self.m.match('#bang', self.filepath('attrs.py')))
matches2 = list(self.m.match('attr#z', self.filepath('attrs.py')))
matches3 = list(self.m.match('attr', self.filepath('attrs.py')))
matches4 = list(self.m.match('#y', self.filepath('attrs.py')))
self.assertEqual(len(matches1), 1)
self.assertEqual(len(matches2), 1)
self.assertEqual(len(matches3), 4)
self.assertEqual(len(matches4), 1)
self.assertEqual(matches3[0][0].attr, 'bar')
self.assertEqual(matches3[1][0].attr, 'bang')
self.assertEqual(matches3[2][0].attr, 'y')
self.assertEqual(matches3[3][0].attr, 'z')
def test_pseudo_extends(self):
matches1 = list(self.m.match(':extends(#object)',
self.filepath('classes.py')))
matches2 = list(self.m.match(':extends()',
self.filepath('classes.py')))
matches3 = list(self.m.match(':extends(#Unknown)',
self.filepath('classes.py')))
matches4 = list(self.m.match(':extends(#object, #X)',
self.filepath('classes.py')))
matches5 = list(self.m.match(':extends(#X):extends(#Y)',
self.filepath('classes.py')))
matches6 = list(self.m.match(':extends(#foo)',
self.filepath('classes.py')))
matches7 = list(self.m.match(':extends(attr#B)',
self.filepath('classes.py')))
matches8 = list(self.m.match(':extends(#A):extends(attr#B)',
self.filepath('classes.py')))
self.assertEqual(len(matches1), 3)
self.assertEqual(len(matches2), 1)
self.assertEqual(len(matches3), 0)
self.assertEqual(len(matches4), 3)
self.assertEqual(len(matches5), 1)
self.assertEqual(len(matches6), 1)
self.assertEqual(len(matches7), 1)
self.assertEqual(len(matches8), 1)
unittest.main(failfast=True)