forked from beeware/batavia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_utils.py
455 lines (375 loc) · 12.8 KB
/
test_utils.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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
import unittest
from .utils import adjust, JSCleaner, PYCleaner, TranspileTestCase
class AdjustTests(unittest.TestCase):
def assertEqualOutput(self, actual, expected):
self.assertEqual(adjust(actual), adjust(expected))
def test_adjust(self):
"Test input can be stripped of leading spaces."
self.assertEqual("""for i in range(0, 10):
print('hello, world')
print('Done.')
""", adjust("""
for i in range(0, 10):
print('hello, world')
print('Done.')
"""))
def test_adjust_no_leading_space(self):
self.assertEqual("""for i in range(0, 10):
print('hello, world')
print('Done.')
""", adjust("""for i in range(0, 10):
print('hello, world')
print('Done.')
"""))
class JavaScriptNormalizationTests(unittest.TestCase):
def assertNormalized(self, actual, expected, js_cleaner=JSCleaner()):
self.assertEqual(js_cleaner.cleanse(adjust(actual), None), adjust(expected))
def test_no_exception(self):
self.assertNormalized(
"""
Hello, world.
""",
"""
Hello, world.
"""
)
def test_exception(self):
self.assertNormalized(
"""
Traceback (most recent call last):
File "test.py", line 3, in <module>
TypeError: unsupported operand type(s) for &: 'float' and 'bool'
""",
"""
### EXCEPTION ###
TypeError: unsupported operand type(s) for &: 'float' and 'bool'
test.py:3
"""
)
def test_multi_frame_exception(self):
self.assertNormalized(
"""
Traceback (most recent call last):
File "test.py", line 3, in <module>
File "test.py", line 6, in <module>
File "test.py", line 9, in <module>
TypeError: unsupported operand type(s) for &: 'float' and 'bool'
""",
"""
### EXCEPTION ###
TypeError: unsupported operand type(s) for &: 'float' and 'bool'
test.py:3
test.py:6
test.py:9
"""
)
def test_exception_with_other_text(self):
self.assertNormalized(
"""
Hello, world.
Traceback (most recent call last):
File "test.py", line 3, in <module>
TypeError: unsupported operand type(s) for &: 'float' and 'bool'
""",
"""
Hello, world.
### EXCEPTION ###
TypeError: unsupported operand type(s) for &: 'float' and 'bool'
test.py:3
"""
)
def test_bool(self):
self.assertNormalized('true', 'True')
self.assertNormalized('false', 'False')
def test_float(self):
self.assertNormalized('7.95089e-06', '7.95089e-6')
self.assertNormalized('7.950899e-06', '7.95089...e-6')
self.assertNormalized('7.950899459780156e-06', '7.95089...e-6')
self.assertNormalized('0.000002653582035', '2.65358...e-6')
self.assertNormalized('321956420358983230.0', '3.21956...e+17')
self.assertNormalized('(18446744073709552000-4j)', '(1.84467...e+19-4j)')
self.assertNormalized('(18446744073709552000+4j)', '(1.84467...e+19+4j)')
def test_memory_reference(self):
self.assertNormalized(
"""
Class is <class 'com.example.MyClass'>
Method is <native function com.example.MyClass.method>
Method from instance is <bound native method com.example.MyClass.method of <Native class com.example.MyClass object at 0x1eb19f4e>>
Hello from the instance!
Done.
""",
"""
Class is <class 'com.example.MyClass'>
Method is <native function com.example.MyClass.method>
Method from instance is <bound native method com.example.MyClass.method of <Native class com.example.MyClass object at 0xXXXXXXXX>>
Hello from the instance!
Done.
"""
)
class PythonNormalizationTests(unittest.TestCase):
def assertNormalized(self, actual, expected, py_cleaner=PYCleaner()):
self.assertEqual(py_cleaner.cleanse(adjust(actual), None), adjust(expected))
def test_no_exception(self):
self.assertNormalized(
"""
Hello, world.
""",
"""
Hello, world.
"""
)
def test_exception(self):
self.assertNormalized(
"""
Traceback (most recent call last):
File "test.py", line 3, in <module>
print(x & y)
TypeError: unsupported operand type(s) for &: 'float' and 'bool'
""",
"""
### EXCEPTION ###
TypeError: unsupported operand type(s) for &: 'float' and 'bool'
test.py:3
"""
)
def test_multi_frame_exception(self):
self.assertNormalized(
"""
Traceback (most recent call last):
File "test.py", line 3, in <module>
print(x & y)
File "test.py", line 6, in <module>
print(x & y)
File "test.py", line 9, in <module>
print(x & y)
TypeError: unsupported operand type(s) for &: 'float' and 'bool'
""",
"""
### EXCEPTION ###
TypeError: unsupported operand type(s) for &: 'float' and 'bool'
test.py:3
test.py:6
test.py:9
"""
)
def test_exception_with_other_text(self):
self.assertNormalized(
"""
Hello, world.
Traceback (most recent call last):
File "test.py", line 3, in <module>
print(x & y)
TypeError: unsupported operand type(s) for &: 'float' and 'bool'
""",
"""
Hello, world.
### EXCEPTION ###
TypeError: unsupported operand type(s) for &: 'float' and 'bool'
test.py:3
"""
)
def test_float(self):
self.assertNormalized('7.95089e-06', '7.95089e-6')
self.assertNormalized('7.950899e-06', '7.95089...e-6')
self.assertNormalized('7.950899459780156e-06', '7.95089...e-6')
def test_memory_reference(self):
self.assertNormalized(
"""
Class is <class 'com.example.MyClass'>
Method is <native function com.example.MyClass.method>
Method from instance is <bound native method com.example.MyClass.method of <Native class com.example.MyClass object at 0x1eb19f4e>>
Hello from the instance!
Done.
""",
"""
Class is <class 'com.example.MyClass'>
Method is <native function com.example.MyClass.method>
Method from instance is <bound native method com.example.MyClass.method of <Native class com.example.MyClass object at 0xXXXXXXXX>>
Hello from the instance!
Done.
"""
)
class JavaScriptBootstrapTests(TranspileTestCase):
def test_js_code(self):
"You can supply JavaScript code and use it from within Python"
self.assertJavaScriptExecution(
"""
from example import myfunc
myfunc()
print("Done.")
""",
js={
'example': """
var example = function(mod) {
mod.myfunc = function() {
console.log("Hello from JavaScript function.");
};
return mod;
}({});
"""
},
out="""
Hello from JavaScript function.
Done.
""",
)
class JSCleanerTests(TranspileTestCase):
cleaner = JSCleaner()
def test_cleanse_err_msg(self):
js_in = adjust("""
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'a' is not defined
""")
expected_out = adjust("""
### EXCEPTION ###
NameError: name 'a' is not defined
<stdin>:1
""")
self.assertEqual(expected_out, self.cleaner.cleanse(js_in, {}))
def test_cleanse_memory_ref(self):
js_in = adjust("""
<turtle.Turtle object at 0x10299c588>
""")
expected_out = adjust("""
<turtle.Turtle object at 0xXXXXXXXX>
""")
self.assertEqual(expected_out, self.cleaner.cleanse(js_in, {}))
def test_cleanse_bool(self):
js_in = adjust("""
true
false
""")
expected_out = adjust("""
True
False
""")
self.assertEqual(expected_out, self.cleaner.cleanse(js_in, {}))
def test_cleanse_decimal(self):
js_in = adjust("""
3.000
""")
expected_out = adjust("""
3.0
""")
self.assertEqual(expected_out, self.cleaner.cleanse(js_in, {}))
def test_cleanse_float_exp(self):
js_in = adjust("""
55e-5
55e-05
55e-005
""")
expected_out = adjust("""
55e-5
55e-5
55e-05
""")
self.assertEqual(expected_out, self.cleaner.cleanse(js_in, {}))
def test_complex_num(self):
js_in = adjust("""
(1234567890123456-
(1234567890123456+
""")
expected_out = adjust("""
(1234567890123456.0.0-
(1234567890123456.0.0+
""")
self.assertEqual(expected_out, self.cleaner.cleanse(js_in, {}))
def test_high_precision_float(self):
js_in = adjust("""
4.00000000000000000001
""")
expected_out = adjust("""
4.0
""")
self.assertEqual(expected_out, self.cleaner.cleanse(js_in, {}))
def test_memory_ref(self):
js_in = adjust("""
'test.py'
""")
expected_out = adjust("""
***EXECUTABLE***
""")
self.assertEqual(expected_out, self.cleaner.cleanse(js_in, {}))
def test_custom(self):
js_in = adjust("""
'we are the knights'
""")
expected_out = adjust("""
'we are the knights who go NI'
""")
custom_replacement = {
'we are the knights who go NI': ['we are the knights']
}
self.assertEqual(expected_out, self.cleaner.cleanse(js_in, custom_replacement))
class PYCleanerTests(TranspileTestCase):
cleaner = PYCleaner()
def test_cleanse_err_msg(self):
py_in = adjust("""
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'a' is not defined
""")
expected_out = adjust("""
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'a' is not defined
""")
self.assertEqual(expected_out, self.cleaner.cleanse(py_in, {}))
def test_cleanse_memory_ref(self):
py_in = adjust("""
<turtle.Turtle object at 0x10299c588>
""")
expected_out = adjust("""
<turtle.Turtle object at 0xXXXXXXXX>
""")
self.assertEqual(expected_out, self.cleaner.cleanse(py_in, {}))
def test_cleanse_float_exp(self):
py_in = adjust("""
55e-5
55e-05
55e-005
""")
expected_out = adjust("""
55e-5
55e-5
55e-05
""")
self.assertEqual(expected_out, self.cleaner.cleanse(py_in, {}))
def test_complex_num(self):
py_in = adjust("""
(1234567890123456-
(1234567890123456+
""")
expected_out = adjust("""
(1234567890123456-
(1234567890123456+
""")
self.assertEqual(expected_out, self.cleaner.cleanse(py_in, {}))
def test_high_precision_float(self):
py_in = adjust("""
4.00000000000000000001
""")
expected_out = adjust("""
4.00000...
""")
self.assertEqual(expected_out, self.cleaner.cleanse(py_in, {}))
def test_ref(self):
py_in = adjust("""
'test.py'
""")
expected_out = adjust("""
***EXECUTABLE***
""")
self.assertEqual(expected_out, self.cleaner.cleanse(py_in, {}))
def test_custom(self):
js_in = adjust("""
'we are the knights who say NI'
""")
expected_out = adjust("""
'We are now the Knights Who Say Ecky-ecky-ecky-ecky-pikang-zoop-boing-goodem-zoo-owli-zhiv'
""")
custom_replacement = {
'We are now the Knights Who Say Ecky-ecky-ecky-ecky-pikang-zoop-boing-goodem-zoo-owli-zhiv': ['we are the knights who say NI']
}
self.assertEqual(expected_out, self.cleaner.cleanse(js_in, custom_replacement))