-
Notifications
You must be signed in to change notification settings - Fork 5
/
tests.py
344 lines (245 loc) · 7.93 KB
/
tests.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
import pickle
from sys import version_info
from unittest import TestCase, TestLoader, TextTestRunner
from typedecorator import (params, returns, void, setup_typecheck, Union,
Nullable)
class TestTypeSignatures(TestCase):
def setUp(self):
setup_typecheck()
def test_params_builtin_type(self):
@params(a=int)
def foo(a):
pass
# should not raise anything
foo(1)
self.assertRaises(TypeError, lambda: foo('a'))
def test_params_custom_type(self):
class MyType(object):
pass
@params(a=MyType)
def foo(a):
pass
@params(a='MyType')
def bar(a):
pass
# should not raise anything
foo(MyType())
bar(MyType())
self.assertRaises(TypeError, lambda: foo(1))
self.assertRaises(TypeError, lambda: bar(1))
def test_params_custom_subtype(self):
class MyBaseType(object):
pass
class MyType(MyBaseType):
pass
@params(a=MyBaseType)
def foo(a):
pass
@params(a='MyBaseType')
def bar(a):
pass
# should not raise anything
foo(MyType())
bar(MyType())
self.assertRaises(TypeError, lambda: foo(1))
self.assertRaises(TypeError, lambda: bar(1))
def test_params_list(self):
@params(a=[int])
def foo(a):
pass
# should not raise anything
foo([])
foo([1, 2, 3])
self.assertRaises(TypeError, lambda: foo(1))
self.assertRaises(TypeError, lambda: foo(['a']))
self.assertRaises(TypeError, lambda: foo([1, 2, 'a', 3]))
def test_params_dict(self):
@params(a={str: int})
def foo(a):
pass
# should not raise anything
foo({})
foo({'a': 1, 'b': 2})
self.assertRaises(TypeError, lambda: foo(set()))
self.assertRaises(TypeError, lambda: foo({'a': 'b'}))
self.assertRaises(TypeError, lambda: foo({1: 1}))
def test_params_set(self):
@params(a=set([int])) # 2.6 compat
def foo(a):
pass
# should not raise anything
foo(set())
foo(set([1, 2, 3])) # 2.6 compat
self.assertRaises(TypeError, lambda: foo({}))
self.assertRaises(TypeError, lambda: foo(set([1, 2, 'a'])))
def test_params_xrange(self):
try:
range_type = xrange
except NameError:
range_type = range
@params(a=range_type)
def foo(a):
pass
# should not raise anything
foo(range_type(10))
foo(i for i in [1, 2])
foo(iter([1, 2]))
foo([]) # works because list supports the iterator protocol
def test_params_tuple(self):
@params(a=(int, bool))
def foo(a):
pass
# should not raise anything
foo((1, True))
foo((0, False))
self.assertRaises(TypeError, lambda: foo(1, True, None))
self.assertRaises(TypeError, lambda: foo(True, 1))
def test_params_union_types(self):
@params(a=Union(int, str))
def foo(a):
pass
# should not raise anything
foo(42)
foo('hello')
self.assertRaises(TypeError, lambda: foo(3.14))
self.assertRaises(TypeError, lambda: foo(None))
def test_params_nullable_type(self):
@params(a=Nullable(int))
def foo(a=None):
pass
# should not raise anything
foo(0)
foo(None)
self.assertRaises(TypeError, lambda: foo('a'))
def test_params_variadic(self):
@params(a=int, b=str)
def foo(a, b=None, *args, **kwargs):
pass
foo(0)
foo(0, 'b', 1, 2)
foo(0, c='a')
self.assertRaises(TypeError, lambda: foo('a'))
self.assertRaises(TypeError, lambda: foo(0, 1, 2, 3))
self.assertRaises(TypeError, lambda: foo(b=2, a=1))
def test_invalid_signatures_throw_error(self):
def foo(a):
pass
self.assertRaises(TypeError, lambda: params(a=int, b=int)(foo))
self.assertRaises(TypeError, lambda: params(a=[int, int]))
self.assertRaises(TypeError, lambda: params(a=None))
class TestParams(TestCase):
def setUp(self):
setup_typecheck()
def test_params_args_kwargs(self):
@params(a=int, b=str)
def foo(a, b):
pass
# should not raise anything
foo(1, 'test')
foo(0, '')
foo(b='foo', a=2)
self.assertRaises(TypeError, lambda: foo(1, 1))
self.assertRaises(TypeError, lambda: foo(b=0, a='x'))
def test_invalid_signatures_throw_error(self):
def foo(a):
pass
# sanity check that params() works
params(a=int)(foo)
self.assertRaises(TypeError, lambda: params()(foo))
self.assertRaises(TypeError, lambda: params(int)(foo))
class TestReturns(TestCase):
def setUp(self):
setup_typecheck()
def test_returns(self):
@returns(int)
def foo(x):
return x
# should not raise anything
foo(1)
self.assertRaises(TypeError, lambda: foo('a'))
self.assertRaises(TypeError, lambda: foo(None))
def test_void(self):
@void
def foo(x):
return x
# shoud not raise anything
foo(None)
self.assertRaises(TypeError, lambda: foo(1))
def test_returns_nullable(self):
@returns(Nullable(int))
def foo(x):
return x
# should not raise anything
foo(1)
foo(None)
self.assertRaises(TypeError, lambda: foo('a'))
class TestSetup(TestCase):
def test_custom_exceptions(self):
class MyError(Exception):
pass
setup_typecheck(exception=MyError)
@returns(int)
def foo():
pass
self.assertRaises(MyError, lambda: foo())
def test_disabled_exception(self):
setup_typecheck()
@returns(int)
def foo():
pass
self.assertRaises(TypeError, lambda: foo())
setup_typecheck(exception=None)
# should not raise anything any more
foo()
class TestMethodAnnotation(TestCase):
def setUp(self):
setup_typecheck()
def test_class_instance_methods(self):
class Accumulator(object):
sum = 0
@void
@params(self=object, a=int)
def add(self, a):
self.sum = self.sum + a
@classmethod
@returns(int)
@params(cls=type, a=int, b=int)
def add2(cls, a, b):
return a + b
@property
@returns(int)
def total(self):
return self.sum
@returns('Accumulator')
@params(self='Accumulator', other='Accumulator')
def __add__(self, other):
acc = Accumulator()
acc.sum = self.sum + other.sum
return acc
a = Accumulator()
# should not raise anything
Accumulator.add2(1, 2)
a.add(1)
self.assertEqual(a.total, 1)
c = a + Accumulator()
self.assertRaises(TypeError, lambda: Accumulator.add2(1, 'a'))
self.assertRaises(TypeError, lambda: a.add(None))
self.assertRaises(TypeError, lambda: a + 1)
@returns(int)
@params(a=int, b=int)
def pickle_test_function(a, b):
"""Used for TestWrapping.test_wrapped_function_remains_pickleable"""
return a + b
class TestWrapping(TestCase):
def test_wrapped_function_remains_pickleable(self):
dump = pickle.dumps(pickle_test_function)
fn = pickle.loads(dump)
self.assertEqual(fn(1, 1), 2)
def main():
test_mods = [__name__]
if version_info[0] == 3:
test_mods.append('tests3')
suite = TestLoader().loadTestsFromNames(test_mods)
TextTestRunner().run(suite)
if __name__ == '__main__':
main()