-
Notifications
You must be signed in to change notification settings - Fork 10
/
test_parse.py
416 lines (351 loc) · 11.6 KB
/
test_parse.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
"""Tests for `ast_comments.parse`."""
import ast
import dis
from textwrap import dedent
import pytest
from ast_comments import Comment, parse, pre_compile_fixer
def test_compile_parse_output():
"""
Output of `ast_comments.parse` can be compiled after applying the compile_fix helper function.
The tree resulting from this compilation should be the same as the tree resulting from
compiling the same code without comments.
"""
with_comments = dedent(
"""
func(1, 2) # comment
# Another comment
x = 5 * "s"
"""
)
without_comments = dedent(
"""
func(1, 2)
x = 5 * "s"
"""
)
compiled_with = compile(pre_compile_fixer(parse(with_comments)), "<string>", "exec")
compiled_without = compile(parse(without_comments), "<string>", "exec")
assert list(dis.get_instructions(compiled_with)) == list(
dis.get_instructions(compiled_without)
)
def test_comment_at_start_of_inner_block_getting_correctly_parsed():
"""Comment at the start of a new inlined block/interval"""
source = dedent(
"""
def test():
# Comment at start of block
hello = 'hello'
"""
)
nodes = parse(source).body
assert isinstance(nodes[0].body[0], Comment)
def test_comment_at_start_of_inner_block_with_wrong_indentation_is_still_inside_the_block():
"""Comment at the start of a new inlined block/interval with wrong indentation"""
source = dedent(
"""
def test():
# Comment at start of block
hello = 'hello'
"""
)
nodes = parse(source).body
assert isinstance(nodes[0].body[0], Comment)
def test_comment_at_end_of_inner_block_getting_correctly_parsed():
"""Comment at the end of a new inlined block/interval"""
source = dedent(
"""
def test():
hello = 'hello'
# Comment at end of block
"""
)
nodes = parse(source).body
assert isinstance(nodes[0].body[1], Comment)
def test_comment_at_end_of_inner_block_with_wrong_indentation_gets_moved_to_next_block():
"""Comment at the end of a new inlined block/interval with wrong indentation should get assigned
to next block
"""
source = dedent(
"""
if 1 == 1:
hello = 'hello'
# Comment at end of block
else:
hello = 'hello'
"""
)
nodes = parse(source).body
assert isinstance(nodes[0].orelse[0], Comment)
def test_single_comment_in_tree():
"""Parsed tree has Comment node."""
source = """# comment"""
nodes = parse(source).body
assert len(nodes) == 1
assert isinstance(nodes[0], Comment)
assert not nodes[0].inline
def test_comment_ends_with_space():
"""Spaces at the end of a comment does not change its inlined value."""
source = """# comment """
nodes = parse(source).body
assert not nodes[0].inline
def test_separate_line_single_line():
"""Comment to the following line. Order in which nodes appears is preserved."""
source = dedent(
"""
# comment to hello
hello = 'hello'
"""
)
nodes = parse(source).body
assert len(nodes) == 2
assert isinstance(nodes[0], Comment)
assert not nodes[0].inline
def test_inline_comment_after_statement():
"""Inlined comment goes after statement."""
source = """hello = 'hello' # comment to hello"""
nodes = parse(source).body
assert len(nodes) == 2
assert isinstance(nodes[1], Comment)
assert nodes[1].inline
def test_separate_line_multiline():
"""Multiple comments to the following line."""
source = dedent(
"""
# comment to hello 1
# comment to hello 2
hello = 'hello'
"""
)
nodes = parse(source).body
assert len(nodes) == 3
assert isinstance(nodes[0], Comment)
assert isinstance(nodes[1], Comment)
assert nodes[0].value == "# comment to hello 1"
assert nodes[1].value == "# comment to hello 2"
assert not nodes[0].inline
assert not nodes[1].inline
def test_multiline_and_inline_combined():
"""Multiple comments to the following line combined with inline comment."""
source = dedent(
"""
# comment to hello 1
# comment to hello 2
hello = 'hello' # comment to hello 3
"""
)
nodes = parse(source).body
assert nodes[0].value == "# comment to hello 1"
assert not nodes[0].inline
assert nodes[1].value == "# comment to hello 2"
assert not nodes[1].inline
assert nodes[3].value == "# comment to hello 3"
assert nodes[3].inline
def test_unrelated_comment():
"""Comment after statement"""
source = dedent(
"""
hello = 'hello'
# unrelated comment
"""
)
nodes = parse(source).body
assert len(nodes) == 2
assert isinstance(nodes[1], Comment)
assert not nodes[1].inline
def test_comment_to_function():
"""Comments to function and expression inside."""
source = dedent(
"""
# comment to function 'foo'
def foo(*args, **kwargs):
print(args, kwargs) # comment to print
"""
)
nodes = parse(source).body
assert len(nodes) == 2
assert nodes[0].value == "# comment to function 'foo'"
assert not nodes[0].inline
function_node = nodes[1]
assert function_node.body[1].value == "# comment to print"
assert function_node.body[1].inline
def test_comment_to_class():
"""Comments to class, its method and variable."""
source = dedent(
"""
# comment to class 'Foo'
class Foo:
var = "Foo var" # comment to 'Foo.var'
# comment to method 'foo'
def foo(self):
...
"""
)
nodes = parse(source).body
assert len(nodes) == 2
assert nodes[0].value == "# comment to class 'Foo'"
assert not nodes[0].inline
class_body = nodes[1].body
assert isinstance(class_body[0], ast.Assign)
assert isinstance(class_body[1], Comment)
assert class_body[1].inline
assert isinstance(class_body[2], Comment)
assert not class_body[2].inline
assert isinstance(class_body[3], ast.FunctionDef)
def test_parse_again():
"""We can parse AstNode objects."""
source = """hello = 'hello' # comment to hello"""
nodes = parse(parse(source)).body
assert isinstance(nodes[1], Comment)
assert nodes[1].inline
def test_parse_ast():
"""We can parse ast.AST objects."""
source = """hello = 'hello' # comment to hello"""
assert parse(ast.parse(source))
def test_multiple_statements_in_line():
"""It's possible to parse multiple statements in one line."""
source = """hello = 'hello'; hello += ' world?'"""
tree = parse(source)
assert len(tree.body) == 2
def test_comment_to_multiple_statements():
"""Comment goes behind statements."""
source = """a=1; b=2 # hello"""
nodes = parse(source).body
assert len(nodes) == 3
assert isinstance(nodes[0], ast.Assign)
assert isinstance(nodes[1], ast.Assign)
assert isinstance(nodes[2], Comment)
assert nodes[2].inline
def test_comments_to_if():
"""Comments to if/elif/else blocks."""
source = dedent(
"""
if a > b: # if comment
print('bigger')
elif a == b: # elif comment
print('equal')
else: # else comment
print('less')
"""
)
nodes = parse(source).body
assert len(nodes) == 1 # IF node
body_nodes = nodes[0].body
assert len(body_nodes) == 2
assert isinstance(body_nodes[0], Comment)
assert body_nodes[0].inline
assert isinstance(body_nodes[1], ast.Expr)
orelse_nodes = nodes[0].orelse
assert len(orelse_nodes) == 1 # ast.parse changes elif to nested if-else
orelse_if_nodes = orelse_nodes[0].body
assert len(orelse_if_nodes) == 2
assert isinstance(orelse_if_nodes[0], Comment)
assert orelse_if_nodes[0].inline
assert isinstance(orelse_if_nodes[1], ast.Expr)
orelse_else_nodes = orelse_nodes[0].orelse
assert len(orelse_else_nodes) == 2
assert isinstance(orelse_else_nodes[0], Comment)
assert orelse_else_nodes[0].inline
assert isinstance(orelse_else_nodes[1], ast.Expr)
def test_comments_to_for():
"""Comments to for/else blocks."""
source = dedent(
"""
for i in range(10): # for comment
continue # continue comment
else: # else comment
pass # pass comment
"""
)
nodes = parse(source).body
assert len(nodes) == 1 # FOR node
body_nodes = nodes[0].body
assert len(body_nodes) == 3
assert isinstance(body_nodes[0], Comment)
assert isinstance(body_nodes[1], ast.Continue)
assert isinstance(body_nodes[2], Comment)
orelse_nodes = nodes[0].orelse
assert len(orelse_nodes) == 3
assert isinstance(orelse_nodes[0], Comment)
assert isinstance(orelse_nodes[1], ast.Pass)
assert isinstance(orelse_nodes[2], Comment)
def test_comments_to_try():
"""Comments to try/except/else/finally blocks."""
source = dedent(
"""
try: # try comment
1 / 0 # expr comment
except ValueError: # except ValueError comment
pass # pass comment
except KeyError: # except KeyError
pass # pass comment
else: # else comment
print() # print comment
finally: # finally comment
print() # print comment
"""
)
nodes = parse(source).body
assert len(nodes) == 1 # TRY node
body_nodes = nodes[0].body
assert len(body_nodes) == 3
assert isinstance(body_nodes[0], Comment)
assert isinstance(body_nodes[1], ast.Expr)
assert isinstance(body_nodes[2], Comment)
handlers_nodes = nodes[0].handlers
assert len(handlers_nodes) == 2
for h_node in handlers_nodes:
handler_nodes = h_node.body
assert len(handler_nodes) == 3
assert isinstance(handler_nodes[0], Comment)
assert isinstance(handler_nodes[1], ast.Pass)
assert isinstance(handler_nodes[2], Comment)
else_nodes = nodes[0].orelse
assert len(else_nodes) == 3
assert isinstance(else_nodes[0], Comment)
assert isinstance(else_nodes[1], ast.Expr)
assert isinstance(else_nodes[2], Comment)
finalbody_nodes = nodes[0].finalbody
assert len(finalbody_nodes) == 3
assert isinstance(finalbody_nodes[0], Comment)
assert isinstance(finalbody_nodes[1], ast.Expr)
assert isinstance(finalbody_nodes[2], Comment)
def test_comment_to_multiline_expr():
"""Comment to multilined expr goes first."""
source = dedent(
"""
if a:
(b if b >=
0 else 1) # some comment
"""
)
if_node = parse(source).body[0]
body_nodes = if_node.body
assert len(body_nodes) == 2
assert isinstance(body_nodes[0], ast.Expr)
assert isinstance(body_nodes[1], Comment)
assert body_nodes[1].inline
def test_empty_line_not_affect_comment_placement():
"""Empty line doesn't mess with indendation intervals."""
source = dedent(
"""
# comment 1
if a: # comment 2
pass
"""
)
body = parse(source).body
assert len(body) == 2
assert isinstance(body[0], Comment)
if_body = body[1].body
assert isinstance(if_body[0], Comment)
@pytest.mark.xfail(reason="https://github.com/t3rn0/ast-comments/issues/13")
def test_comment_in_multilined_list():
"""Comment to element of the list stays inside the list."""
source = dedent(
"""
lst = [
1 # comment to element
]
"""
)
assert len(parse(source).body) == 1