forked from baverman/supplement
-
Notifications
You must be signed in to change notification settings - Fork 3
/
test_linter.py
253 lines (205 loc) · 5 KB
/
test_linter.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
# -*- coding: utf-8 -*-
import re
from supplement.linter import lint
from .helpers import cleantabs
def get_source_and_expected_result(source):
source = cleantabs(source)
result = []
while True:
m = re.search(r'([$!])(.+?)\1', source)
if not m: break
lineno = source.count('\n', 0, m.start()) + 1
offset = m.start() - len(''.join(source.splitlines(True)[:lineno-1]))
c = m.group(1)
if c == '$':
message = 'Unused name (W01): ' + m.group(2)
else:
message = 'Unknown name (E01): ' + m.group(2)
result.append(((lineno, offset), m.group(2), message))
source= source.replace(c, '', 2)
return source, result
def assert_names(source):
source, expected = get_source_and_expected_result(source)
result = lint(source)
assert set(result) == set(expected)
def test_module_level_variables_must_be_skipped_as_unused():
assert_names('''
var = 1
''')
def test_unused_imports():
assert_names('''
import $package1$.module, package1.module as $modulo$
from ..package import *
from .package.module import $boo$, $foo$
from . import $bar$
''')
def test_module_level_unused_names():
assert_names('''
for $a$, b in []:
b
''')
def test_underscore_names():
assert_names('''
for _, _test in []:
pass
''')
def test_unknown_names():
assert_names('''
!name!
''')
def test_builtin_names():
assert_names('''
True
None
__builtins__
Exception
''')
def test_func_and_arg_names():
assert_names('''
def boo($bar$):
def $bar$():
pass
def foo($arg$, *args, **kwargs):
!bar!
boo()
args
kwargs
foo()
''')
def test_class_defenitions():
assert_names('''
class Boo(object):
def boo($self$):
pass
class Foo(object): pass
def foo($self$):
class $Bar$(object):
pass
Boo()
''')
def test_declaration_action_scope():
assert_names('''
def bar():
boo = 1
$boo$ = boo + 1
$foo$ = !foo! + 1
''')
def test_self_changed_names():
assert_names('''
def bar():
boo = 1
$boo$ += 1
''')
def test_looped_name_usage_in_for_statements():
assert_names('''
def bar():
for i in []:
i += 1
found = False
while not found:
found, $boo$ = True, False
boo = 1
return boo
''')
def test_generator_and_list_expressions():
assert_names('''
def bar():
(r for r in (1,2,3))
[rr for rr in (1,2,3)]
''')
def test_class_scope_must_not_hide_global_names():
assert_names('''
from . import bar
class Bar(object):
def bar($self$):
return bar()
''')
def test_unicode():
assert_names(u'''
s = "юникод %s" % !test!
''')
def test_unused_function_arguments():
assert_names('''
def bar(boo, $foo$):
return boo
''')
def test_simple_if():
assert_names('''
def foo():
boo = 1
if True:
boo = 2
return boo
''')
def test_complex_if():
assert_names('''
def foo():
boo = 1
if True:
boo = 2
elif False:
boo = 1
else:
if True:
boo = 0
return boo
''')
def test_near_branches():
assert_names('''
def foo():
if True:
boo = 2
if True:
boo = 0
return boo
''')
def test_parent_subbranches1():
assert_names('''
def foo():
if True:
boo = 2
if True:
return boo
''')
def test_parent_subbranches2():
assert_names('''
def foo():
if True:
boo = 1
if True:
boo = 2
if True:
return boo
''')
def test_parent_subbranches3():
assert_names('''
def foo():
if True:
$boo$ = 1
else:
return !boo!
''')
def test_lambda():
assert_names('''
def foo():
return lambda boo, bar: boo + bar
''')
def test_lambda_assigned_to_var():
assert_names('''
def foo():
$result$ = lambda boo, bar: boo + bar
''')
def test_lambda_assigned_to_var():
assert_names('''
from __future__ import absolute_import
''')
def test_for_loop_alt_logic_branches():
assert_names('''
def foo():
for _ in []:
bar = 1
break
else:
bar = 2
print bar
''')