forked from beeware/voc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_utils.py
359 lines (324 loc) · 12.2 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
import unittest
from .utils import adjust, cleanse_java, cleanse_python, 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 AssertCodeExecutionTests(TranspileTestCase):
@unittest.expectedFailure
def test_fail_unexpected_exception(self):
self.assertCodeExecution("""
raise ValueError
""")
def test_allow_expected_exception(self):
self.assertCodeExecution("""
raise ValueError
""", exits_early=True)
@unittest.expectedFailure
def test_fail_missing_expected_exception(self):
self.assertCodeExecution("""
print('Done.')
""", exits_early=True)
class JavaNormalizationTests(unittest.TestCase):
def assertNormalized(self, actual, expected):
self.assertEqual(cleanse_java(adjust(actual), None), adjust(expected))
def test_no_exception(self):
self.assertNormalized(
"""
Hello, world.
""",
"""
Hello, world.
"""
)
def test_exception_in_clinit(self):
self.assertNormalized(
"""
Exception in thread "main" java.lang.ExceptionInInitializerError
Caused by: IndexError: list index out of range
at org.python.types.List.__getitem__(List.java:100)
at org.python.types.List.__getitem__(List.java:85)
at python.test.<clinit>(test.py:2)
""",
"""
### EXCEPTION ###
IndexError: list index out of range
test.py:2
"""
)
def test_exception_in_module_init(self):
self.assertNormalized(
"""
Exception in thread "main" NameError: name 'y' is not defined
at org.python.types.Module.__getattribute__(Module.java:32)
at python.example.__init__.module$import(example.py:2)
at python.example.__init__.main(example.py)
""",
"""
### EXCEPTION ###
NameError: name 'y' is not defined
example.py:2
"""
)
def test_exception_in_clinit_after_output(self):
self.assertNormalized(
"""
Hello, world.
Exception in thread "main" java.lang.ExceptionInInitializerError
Caused by: IndexError: list index out of range
at org.python.types.List.__getitem__(List.java:100)
at org.python.types.List.__getitem__(List.java:85)
at python.test.<clinit>(test.py:2)
""",
"""
Hello, world.
### EXCEPTION ###
IndexError: list index out of range
test.py:2
"""
)
def test_exception_in_clinit_after_output_windows(self):
self.assertNormalized(
"""
Hello, world.
java.lang.ExceptionInInitializerError
Caused by: IndexError: list index out of range
at org.python.types.List.__getitem__(List.java:100)
at org.python.types.List.__getitem__(List.java:85)
at python.test.<clinit>(test.py:2)
""",
"""
Hello, world.
### EXCEPTION ###
IndexError: list index out of range
test.py:2
"""
)
def test_exception_in_clinit_after_output_testdaemon(self):
self.assertNormalized(
"""
False
True
Exception in thread "main" java.lang.ExceptionInInitializerError
\tat sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
\tat sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
\tat sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
\tat java.lang.reflect.Method.invoke(Method.java:497)
\tat python.testdaemon.TestDaemon.main(TestDaemon.java:66)
Caused by: KeyError: 'c'
at org.python.types.Dict.__getitem__(Dict.java:142)
at python.test.__init__.<clinit>(test.py:4)
... 5 more
""",
"""
False
True
### EXCEPTION ###
KeyError: 'c'
test.py:4
"""
)
def test_exception_in_method(self):
self.assertNormalized(
"""
Exception in thread "main" IndexError: list index out of range
at org.python.types.List.__getitem__(List.java:100)
at org.python.types.List.__getitem__(List.java:85)
at python.test.main(test.py:3)
""",
"""
### EXCEPTION ###
IndexError: list index out of range
test.py:3
"""
)
def test_exception_in_method_windows(self):
self.assertNormalized(
"""
IndexError: list index out of range
at org.python.types.List.__getitem__(List.java:100)
at org.python.types.List.__getitem__(List.java:85)
at python.test.main(test.py:3)
""",
"""
### EXCEPTION ###
IndexError: list index out of range
test.py:3
"""
)
def test_exception_in_method_after_output(self):
self.assertNormalized(
"""
Hello, world.
Exception in thread "main" IndexError: list index out of range
at org.python.types.List.__getitem__(List.java:100)
at org.python.types.List.__getitem__(List.java:85)
at python.test.main(test.py:3)
""",
"""
Hello, world.
### EXCEPTION ###
IndexError: list index out of range
test.py:3
"""
)
def test_exception_in_method_after_output_windows(self):
self.assertNormalized(
"""
Hello, world.
IndexError: list index out of range
at org.python.types.List.__getitem__(List.java:100)
at org.python.types.List.__getitem__(List.java:85)
at python.test.main(test.py:3)
""",
"""
Hello, world.
### EXCEPTION ###
IndexError: list index out of range
test.py:3
"""
)
def test_exception_in_constructor(self):
self.assertNormalized(
"""
Exception in thread "main" java.lang.ExceptionInInitializerError
Caused by: UnboundLocalError: local variable 'x' referenced before assignment
at python.example.Foo.__init__(example.py:44)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.python.types.Method.invoke(Method.java:66)
at python.example.Foo.<init>(example.py)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
at org.python.types.Constructor.invoke(Constructor.java:25)
at python.example.<clinit>(example.py:51)
""",
"""
### EXCEPTION ###
UnboundLocalError: local variable 'x' referenced before assignment
example.py:51
example.py:44
"""
)
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.
""", # noqa
"""
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.
""" # noqa
)
class PythonNormalizationTests(unittest.TestCase):
def assertNormalized(self, actual, expected):
self.assertEqual(cleanse_python(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_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_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.
""", # noqa
"""
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.
""" # noqa
)
class JavaBootstrapTests(TranspileTestCase):
def test_java_code(self):
"You can supply Java code and use it from within Python"
self.assertJavaExecution(
"""
from com.example import MyClass
obj = MyClass()
obj.doStuff()
print("Done.")
""",
java={
'com/example/MyClass': """
package com.example;
public class MyClass {
public void doStuff() {
System.out.println("Hello from Java");
}
}
"""
},
out="""
Hello from Java
Done.
""",
)