forked from samuelcolvin/python-devtools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_expr_render.py
More file actions
320 lines (267 loc) · 7.56 KB
/
test_expr_render.py
File metadata and controls
320 lines (267 loc) · 7.56 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
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
import asyncio
import sys
import pytest
from devtools import Debug, debug
from .utils import normalise_output
def foobar(a, b, c):
return a + b + c
def test_simple():
a = [1, 2, 3]
v = debug.format(len(a))
s = normalise_output(str(v))
# print(s)
assert (
'tests/test_expr_render.py:<line no> test_simple\n'
' len(a): 3 (int)'
) == s
def test_subscription():
a = {1: 2}
v = debug.format(a[1])
s = normalise_output(str(v))
assert (
'tests/test_expr_render.py:<line no> test_subscription\n'
' a[1]: 2 (int)'
) == s
def test_exotic_types():
aa = [1, 2, 3]
v = debug.format(
sum(aa),
1 == 2,
1 < 2,
1 << 2,
't' if True else 'f',
1 or 2,
[a for a in aa],
{a for a in aa},
{a: a + 1 for a in aa},
(a for a in aa),
)
s = normalise_output(str(v))
print(f'\n---\n{v}\n---')
# Generator expression source changed in 3.8 to include parentheses, see:
# https://github.com/gristlabs/asttokens/pull/50
# https://bugs.python.org/issue31241
genexpr_source = 'a for a in aa'
if sys.version_info[:2] > (3, 7):
genexpr_source = f'({genexpr_source})'
assert (
"tests/test_expr_render.py:<line no> test_exotic_types\n"
" sum(aa): 6 (int)\n"
" 1 == 2: False (bool)\n"
" 1 < 2: True (bool)\n"
" 1 << 2: 4 (int)\n"
" 't' if True else 'f': 't' (str) len=1\n"
" 1 or 2: 1 (int)\n"
" [a for a in aa]: [1, 2, 3] (list) len=3\n"
" {a for a in aa}: {1, 2, 3} (set) len=3\n"
" {a: a + 1 for a in aa}: {\n"
" 1: 2,\n"
" 2: 3,\n"
" 3: 4,\n"
" } (dict) len=3\n"
f" {genexpr_source}: (\n"
" 1,\n"
" 2,\n"
" 3,\n"
" ) (generator)"
) == s
def test_newline():
v = debug.format(
foobar(1, 2, 3))
s = normalise_output(str(v))
# print(s)
assert (
'tests/test_expr_render.py:<line no> test_newline\n'
' foobar(1, 2, 3): 6 (int)'
) == s
def test_trailing_bracket():
v = debug.format(
foobar(1, 2, 3)
)
s = normalise_output(str(v))
# print(s)
assert (
'tests/test_expr_render.py:<line no> test_trailing_bracket\n'
' foobar(1, 2, 3): 6 (int)'
) == s
def test_multiline():
v = debug.format(
foobar(1,
2,
3)
)
s = normalise_output(str(v))
# print(s)
assert (
'tests/test_expr_render.py:<line no> test_multiline\n'
' foobar(1, 2, 3): 6 (int)'
) == s
def test_multiline_trailing_bracket():
v = debug.format(
foobar(1, 2, 3
))
s = normalise_output(str(v))
# print(s)
assert (
'tests/test_expr_render.py:<line no> test_multiline_trailing_bracket\n'
' foobar(1, 2, 3 ): 6 (int)'
) == s
@pytest.mark.skipif(sys.version_info < (3, 6), reason='kwarg order is not guaranteed for 3.5')
def test_kwargs():
v = debug.format(
foobar(1, 2, 3),
a=6,
b=7
)
s = normalise_output(str(v))
assert (
'tests/test_expr_render.py:<line no> test_kwargs\n'
' foobar(1, 2, 3): 6 (int)\n'
' a: 6 (int)\n'
' b: 7 (int)'
) == s
@pytest.mark.skipif(sys.version_info < (3, 6), reason='kwarg order is not guaranteed for 3.5')
def test_kwargs_multiline():
v = debug.format(
foobar(1, 2,
3),
a=6,
b=7
)
s = normalise_output(str(v))
assert (
'tests/test_expr_render.py:<line no> test_kwargs_multiline\n'
' foobar(1, 2, 3): 6 (int)\n'
' a: 6 (int)\n'
' b: 7 (int)'
) == s
def test_multiple_trailing_lines():
v = debug.format(
foobar(
1, 2, 3
),
)
s = normalise_output(str(v))
assert (
'tests/test_expr_render.py:<line no> test_multiple_trailing_lines\n foobar( 1, 2, 3 ): 6 (int)'
) == s
def test_very_nested_last_statement():
def func():
return debug.format(
abs(
abs(
abs(
abs(
-1
)
)
)
)
)
v = func()
# check only the original code is included in the warning
s = normalise_output(str(v))
assert s == (
'tests/test_expr_render.py:<line no> test_very_nested_last_statement.<locals>.func\n'
' abs( abs( abs( abs( -1 ) ) ) ): 1 (int)'
)
def test_syntax_warning():
def func():
return debug.format(
abs(
abs(
abs(
abs(
abs(
-1
)
)
)
)
)
)
v = func()
# check only the original code is included in the warning
s = normalise_output(str(v))
assert s == (
'tests/test_expr_render.py:<line no> test_syntax_warning.<locals>.func\n'
' abs( abs( abs( abs( abs( -1 ) ) ) ) ): 1 (int)'
)
def test_no_syntax_warning():
# exceed the 4 extra lines which are normally checked
debug_ = Debug(warnings=False)
def func():
return debug_.format(
abs(
abs(
abs(
abs(
abs(
-1
)
)
)
)
)
)
v = func()
s = normalise_output(str(v))
assert s == (
'tests/test_expr_render.py:<line no> test_no_syntax_warning.<locals>.func\n'
' abs( abs( abs( abs( abs( -1 ) ) ) ) ): 1 (int)'
)
def test_await():
async def foo():
return 1
async def bar():
return debug.format(await foo())
loop = asyncio.new_event_loop()
v = loop.run_until_complete(bar())
loop.close()
s = normalise_output(str(v))
assert (
'tests/test_expr_render.py:<line no> test_await.<locals>.bar\n'
' await foo(): 1 (int)'
) == s
def test_other_debug_arg():
debug.timer()
v = debug.format([1, 2])
# check only the original code is included in the warning
s = normalise_output(str(v))
assert s == (
'tests/test_expr_render.py:<line no> test_other_debug_arg\n'
' [1, 2] (list) len=2'
)
def test_other_debug_arg_not_literal():
debug.timer()
x = 1
y = 2
v = debug.format([x, y])
s = normalise_output(str(v))
assert s == (
'tests/test_expr_render.py:<line no> test_other_debug_arg_not_literal\n'
' [x, y]: [1, 2] (list) len=2'
)
def test_executing_failure():
debug.timer()
x = 1
y = 2
# executing fails inside a pytest assert ast the AST is modified
assert normalise_output(str(debug.format([x, y]))) == (
'tests/test_expr_render.py:<line no> test_executing_failure '
'(executing failed to find the calling node)\n'
' [1, 2] (list) len=2'
)
def test_format_inside_error():
debug.timer()
x = 1
y = 2
try:
raise RuntimeError(debug.format([x, y]))
except RuntimeError as e:
v = str(e)
s = normalise_output(str(v))
assert s == (
'tests/test_expr_render.py:<line no> test_format_inside_error\n'
' [x, y]: [1, 2] (list) len=2'
)