/
NoInlineCodeTest.py
363 lines (334 loc) · 9.82 KB
/
NoInlineCodeTest.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
# Tests for the implementatuion of --no-inline-code
# Note that the use of int/float/lon as variable
# is not allowed with google closure compile
import sys
import UnitTest
import urllib
def test(a):
return None
class NoInlineCodeTest(UnitTest.UnitTest):
def test_bool(self):
i1 = bool(1)
def fn():
i2 = True
bool = test
i3 = bool(1)
i4 = True
self.assertEqual(i1, True)
self.assertEqual(i1, i2)
self.assertNotEqual(i1, i3)
self.assertEqual(i1, i4)
fn()
def test_int(self):
i1 = int(1)
def fn():
i2 = 1
int = test
i3 = int(1)
i4 = 1
self.assertEqual(i1, 1)
self.assertEqual(i1, i2)
self.assertNotEqual(i1, i3)
self.assertEqual(i1, i4)
fn()
def test_hexint(self):
i1 = int(1)
def fn():
i2 = 0x1
int = hex = test
i3 = int(0x1)
i4 = 0x1
self.assertEqual(i1, 1)
self.assertEqual(i1, i2)
self.assertNotEqual(i1, i3)
self.assertEqual(i1, i4)
fn()
def test_long(self):
i1 = long(1)
def fn():
i2 = 1L
long = test
i3 = long(1)
i4 = 1L
self.assertEqual(i1, 1L)
self.assertEqual(i1, i2)
self.assertNotEqual(i1, i3)
self.assertEqual(i1, i4)
fn()
def test_float(self):
i1 = float(1.0)
def fn():
i2 = 1.0
float = test
i3 = float(1.0)
i4 = 1.0
self.assertEqual(i1, 1.0)
self.assertEqual(i1, i2)
self.assertNotEqual(i1, i3)
self.assertEqual(i1, i4)
fn()
def test_tuple(self):
i1 = tuple((1,))
def fn():
i2 = (1,)
tuple = test
i3 = tuple((1,))
i4 = (1,)
self.assertEqual(i1, (1,))
self.assertEqual(i1, i2)
self.assertNotEqual(i1, i3)
self.assertEqual(i1, i4)
fn()
def test_list(self):
i1 = list([1])
def fn():
i2 = [1]
list = test
i3 = list([1])
i4 = [1]
self.assertEqual(i1, [1])
self.assertEqual(i1, i2)
self.assertNotEqual(i1, i3)
self.assertEqual(i1, i4)
fn()
def test_dict(self):
i1 = dict(a=1)
def fn():
i2 = {'a':1}
dict = test
i3 = dict(a=1)
i4 = {'a':1}
self.assertEqual(i1, {'a':1})
self.assertEqual(i1, i2)
self.assertNotEqual(i1, i3)
self.assertEqual(i1, i4)
fn()
def test_set(self):
i1 = set([1])
_set = set
def fn():
set = test
i3 = set([1])
i4 = _set([1])
self.assertNotEqual(i1, i3)
self.assertEqual(i1, i4)
fn()
def test_ArithmeticError(self):
e1 = ArithmeticError
def fn():
ArithmeticError = bool
try:
a = 1/0
except e1, e:
self.assertTrue(isinstance(e, e1))
self.assertFalse(isinstance(e, ArithmeticError))
else:
self.fail("Failed to raise ArithmeticError")
fn()
def test_AttributeError(self):
e1 = AttributeError
def fn():
AttributeError = bool
try:
a = e1.noooo
except e1, e:
self.assertTrue(isinstance(e, e1))
self.assertFalse(isinstance(e, AttributeError))
else:
self.fail("Failed to raise AttributeError")
fn()
def test_BaseException(self):
e1 = BaseException
def fn():
BaseException = bool
try:
a = 1/0
except e1, e:
self.assertTrue(isinstance(e, e1))
self.assertFalse(isinstance(e, BaseException))
else:
self.fail("Failed to raise BaseException")
fn()
def test_Exception(self):
e1 = Exception
def fn():
Exception = bool
try:
a = 1/0
except e1, e:
self.assertTrue(isinstance(e, e1))
self.assertFalse(isinstance(e, Exception))
else:
self.fail("Failed to raise Exception")
fn()
def _test_GeneratorExit(self):
e1 = GeneratorExit
def fn():
GeneratorExit = bool
try:
a = 1/0
except e1, e:
self.assertTrue(isinstance(e, e1))
self.assertFalse(isinstance(e, GeneratorExit))
else:
self.fail("Failed to raise GeneratorExit")
fn()
def test_ImportError(self):
e1 = ImportError
def fn():
ImportError = bool
try:
import nosuchmodule
except e1, e:
self.assertTrue(isinstance(e, e1))
self.assertFalse(isinstance(e, ImportError))
else:
self.fail("Failed to raise ImportError")
fn()
def test_IndexError(self):
e1 = IndexError
def fn():
IndexError = bool
try:
a = [0][1]
except e1, e:
self.assertTrue(isinstance(e, e1))
self.assertFalse(isinstance(e, IndexError))
else:
self.fail("Failed to raise IndexError")
fn()
def test_KeyError(self):
e1 = KeyError
def fn():
KeyError = bool
try:
a = dict(a=1)['b']
except e1, e:
self.assertTrue(isinstance(e, e1))
self.assertFalse(isinstance(e, KeyError))
else:
self.fail("Failed to raise KeyError")
fn()
def test_LookupError(self):
e1 = LookupError
def fn():
LookupError = bool
try:
a = set([1]).remove(2)
except e1, e:
self.assertTrue(isinstance(e, e1))
self.assertFalse(isinstance(e, LookupError))
else:
self.fail("Failed to raise LookupError")
fn()
def _test_NameError(self):
# This fails anyway
e1 = NameError
def fn():
NameError = bool
try:
a = nosuchname
except e1, e:
self.assertTrue(isinstance(e, e1))
self.assertFalse(isinstance(e, NameError))
else:
self.fail("Failed to raise NameError")
fn()
def _test_RuntimeError(self):
# This fails anyway...
e1 = RuntimeError
def fn():
RuntimeError = bool
try:
a = dict(a=1,b=2,c=3)
for k, v in a.iteritems():
a['_%s' % k] = v
except e1, e:
self.assertTrue(isinstance(e, e1))
self.assertFalse(isinstance(e, RuntimeError))
else:
self.fail("Failed to raise RuntimeError")
fn()
def test_StandardError(self):
e1 = StandardError
def fn():
StandardError = bool
try:
a = 1/0
except e1, e:
self.assertTrue(isinstance(e, e1))
self.assertFalse(isinstance(e, StandardError))
else:
self.fail("Failed to raise StandardError")
fn()
def _test_StopIteration(self):
#This fails anyway...
e1 = StopIteration
def fn():
def g():
yield None
StopIteration = bool
try:
a = g()
a.next()
a.next()
except e1, e:
self.assertTrue(isinstance(e, e1))
self.assertFalse(isinstance(e, StopIteration))
else:
self.fail("Failed to raise StopIteration")
fn()
def test_TypeError(self):
e1 = TypeError
def fn():
TypeError = bool
try:
a = 1 + 'a'
except e1, e:
self.assertTrue(isinstance(e, e1))
self.assertFalse(isinstance(e, TypeError))
else:
self.fail("Failed to raise TypeError")
fn()
def test_ValueError(self):
e1 = ValueError
def fn():
ValueError = bool
try:
a = list([1]).index(2)
except e1, e:
self.assertTrue(isinstance(e, e1))
self.assertFalse(isinstance(e, ValueError))
else:
self.fail("Failed to raise ValueError")
fn()
def test_ZeroDivisionError(self):
e1 = ZeroDivisionError
def fn():
ZeroDivisionError = bool
try:
a = 1/0
except e1, e:
self.assertTrue(isinstance(e, e1))
self.assertFalse(isinstance(e, ZeroDivisionError))
else:
self.fail("Failed to raise ZeroDivisionError")
fn()
def test_ArgsScoping(self):
collection = []
def fn(i, *args, **kwargs):
if i < 2:
fn(i+1)
collection.append((i, args, kwargs))
args = (2,3)
kwargs = dict(a='a', b='b')
collection.append((0, args, kwargs))
fn(1, *args, **kwargs)
self.assertEqual(
collection,
[
(0, (2, 3), {'a': 'a', 'b': 'b'}),
(2, (), {}),
(1, (2, 3), {'a': 'a', 'b': 'b'}),
],
)