-
-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathtest_remove_debug.py
More file actions
227 lines (176 loc) · 4.75 KB
/
test_remove_debug.py
File metadata and controls
227 lines (176 loc) · 4.75 KB
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
import ast
import pytest
from python_minifier.ast_annotation import add_parent
from python_minifier.ast_compare import compare_ast
from python_minifier.rename import add_namespace, bind_names, resolve_names
from python_minifier.transforms.remove_debug import RemoveDebug
def remove_debug(source):
module = ast.parse(source, 'remove_debug')
add_parent(module)
add_namespace(module)
bind_names(module)
resolve_names(module)
return RemoveDebug()(module)
def test_remove_debug_empty_module():
source = 'if __debug__: pass'
expected = ''
expected_ast = ast.parse(expected)
actual_ast = remove_debug(source)
compare_ast(expected_ast, actual_ast)
def test_remove_debug_module():
source = '''import collections
if __debug__: pass
a = 1
if __debug__: pass'''
expected = '''import collections
a=1'''
expected_ast = ast.parse(expected)
actual_ast = remove_debug(source)
compare_ast(expected_ast, actual_ast)
def test_remove_if_empty():
source = '''if True:
if __debug__: pass'''
expected = '''if True: 0'''
expected_ast = ast.parse(expected)
actual_ast = remove_debug(source)
compare_ast(expected_ast, actual_ast)
def test_remove_suite():
source = '''if True:
if __debug__: pass
a=1
if __debug__: pass
return None'''
expected = '''if True:
a=1
return None'''
expected_ast = ast.parse(expected)
actual_ast = remove_debug(source)
compare_ast(expected_ast, actual_ast)
def test_remove_from_class():
source = '''class A:
if __debug__: pass
a = 1
if __debug__: pass
def b():
if __debug__: pass
return 1
if __debug__: pass
'''
expected = '''class A:
a=1
def b():
return 1
'''
expected_ast = ast.parse(expected)
actual_ast = remove_debug(source)
compare_ast(expected_ast, actual_ast)
def test_remove_from_class_empty():
source = '''class A:
if __debug__: pass
'''
expected = 'class A:0'
expected_ast = ast.parse(expected)
actual_ast = remove_debug(source)
compare_ast(expected_ast, actual_ast)
def test_remove_from_class_func_empty():
source = '''class A:
def b():
if __debug__: pass
'''
expected = '''class A:
def b(): 0'''
expected_ast = ast.parse(expected)
actual_ast = remove_debug(source)
compare_ast(expected_ast, actual_ast)
@pytest.mark.parametrize(
'condition', [
'__debug__',
'__debug__ is True',
'__debug__ is not False',
'__debug__ == True'
]
)
def test_remove_truthy_debug(condition):
source = '''
value = 10
# Truthy
if ''' + condition + ''':
value += 1
print(value)
'''
expected = '''
value = 10
print(value)
'''
expected_ast = ast.parse(expected)
actual_ast = remove_debug(source)
compare_ast(expected_ast, actual_ast)
@pytest.mark.parametrize(
'condition', [
'not __debug__',
'__debug__ is False',
'__debug__ is not True',
'__debug__ == False',
'not __debug__ is True',
'not __debug__ is not False',
'not __debug__ == True'
]
)
def test_no_remove_falsy_debug(condition):
source = '''
value = 10
# Truthy
if ''' + condition + ''':
value += 1
print(value)
'''
expected = source
expected_ast = ast.parse(expected)
actual_ast = remove_debug(source)
compare_ast(expected_ast, actual_ast)
@pytest.mark.parametrize(
'condition', [
'__sandwich__',
'__sandwich__ is True',
'__sandwich__ is False',
'__sandwich__ is not False',
'__sandwich__ == True',
'__sandwich__ == __debug__',
'__sandwich() == True',
'func() is True',
'some_call(a, b) is True',
'obj.method() is True',
'obj.attr is True',
'True is something',
'True == something',
]
)
def test_no_remove_not_debug(condition):
source = '''
value = 10
# Not a __debug__ test
if ''' + condition + ''':
value += 1
print(value)
'''
expected = source
expected_ast = ast.parse(expected)
actual_ast = remove_debug(source)
compare_ast(expected_ast, actual_ast)
def test_no_remove_is_true_in_elif_chain():
"""Regression test for issue #142 - if/elif/else with 'is True' comparisons"""
source = '''
def check_is_internet_working(c):
url, url_hostname = get_url_and_url_hostname(c)
if is_internet_working_socket_test(c, url_hostname) is True:
c.is_internet_connected = True
elif is_internet_working_urllib_open(c, url) is True:
c.is_internet_connected = True
else:
c.is_internet_connected = False
return c.is_internet_connected
'''
expected = source
expected_ast = ast.parse(expected)
actual_ast = remove_debug(source)
compare_ast(expected_ast, actual_ast)