-
Notifications
You must be signed in to change notification settings - Fork 9
/
tests.py
381 lines (313 loc) · 12.5 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
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
"""
Test suite originally copied from test_collections.py in Python 3.5.
"""
import backports_abc
FROM_MODULE = [
backports_abc.Generator,
backports_abc.Coroutine,
backports_abc.Awaitable,
backports_abc.isawaitable,
]
backports_abc.patch()
try:
from collections.abc import (
Generator, Coroutine, Awaitable, Iterator, Iterable)
except ImportError:
from collections import (
Generator, Coroutine, Awaitable, Iterator, Iterable)
import sys
import inspect
import unittest
import operator
from gc import collect as gc_collect
IS_PY3 = sys.version_info[0] >= 3
class BackportsAbcTest(unittest.TestCase):
def validate_module_namespace(self):
self.assertEqual(FROM_MODULE[0], Generator)
self.assertEqual(FROM_MODULE[1], Coroutine)
self.assertEqual(FROM_MODULE[2], Awaitable)
self.assertEqual(FROM_MODULE[3], inspect.isawaitable)
################################################################################
### Abstract Base Classes
################################################################################
class ABCTestCase(unittest.TestCase):
def validate_abstract_methods(self, abc, *names):
methodstubs = dict.fromkeys(names, lambda s, *args: 0)
# everything should work will all required methods are present
C = type('C', (abc,), methodstubs)
C()
# instantiation should fail if a required method is missing
for name in names:
stubs = methodstubs.copy()
del stubs[name]
C = type('C', (abc,), stubs)
self.assertRaises(TypeError, C, name)
def validate_isinstance(self, abc, name):
stub = lambda s, *args: 0
C = type('C', (object,), {'__hash__': None})
setattr(C, name, stub)
self.assertIsInstance(C(), abc)
self.assertTrue(issubclass(C, abc))
C = type('C', (object,), {'__hash__': None})
self.assertNotIsInstance(C(), abc)
self.assertFalse(issubclass(C, abc))
def validate_comparison(self, instance):
ops = ['lt', 'gt', 'le', 'ge', 'ne', 'or', 'and', 'xor', 'sub']
operators = {}
for op in ops:
name = '__' + op + '__'
operators[name] = getattr(operator, name)
class Other(object):
def __init__(self):
self.right_side = False
def __eq__(self, other):
self.right_side = True
return True
__lt__ = __eq__
__gt__ = __eq__
__le__ = __eq__
__ge__ = __eq__
__ne__ = __eq__
__ror__ = __eq__
__rand__ = __eq__
__rxor__ = __eq__
__rsub__ = __eq__
for name, op in operators.items():
if not hasattr(instance, name):
continue
other = Other()
op(instance, other)
self.assertTrue(other.right_side,'Right side not called for %s.%s'
% (type(instance), name))
if not hasattr(unittest.TestCase, 'assertRaisesRegex'):
def assertRaisesRegex(self, exc, regex, func, *args, **kwargs):
try:
func(*args, **kwargs)
except exc:
self.assertTrue(True)
else:
self.assertFalse(True)
if not hasattr(unittest.TestCase, 'assertIs'):
def assertIs(self, obj, t):
self.assertTrue(obj is t, repr(obj))
if not hasattr(unittest.TestCase, 'assertIsNone'):
def assertIsNone(self, obj):
self.assertTrue(obj is None, repr(obj))
if not hasattr(unittest.TestCase, 'assertIsInstance'):
def assertIsInstance(self, obj, t):
self.assertTrue(isinstance(obj, t), repr(obj))
if not hasattr(unittest.TestCase, 'assertNotIsInstance'):
def assertNotIsInstance(self, obj, t):
self.assertFalse(isinstance(obj, t), repr(obj))
class TestOneTrickPonyABCs(ABCTestCase):
def test_Awaitable(self):
def gen():
yield
#@types.coroutine
#def coro():
# yield
#async def new_coro():
# pass
class Bar(object):
def __await__(self):
yield
class MinimalCoro(Coroutine):
def send(self, value):
return value
def throw(self, typ, val=None, tb=None):
super(MinimalCoro, self).throw(typ, val, tb)
def __await__(self):
yield
non_samples = [None, int(), gen(), object()]
for x in non_samples:
self.assertNotIsInstance(x, Awaitable)
self.assertFalse(issubclass(type(x), Awaitable), repr(type(x)))
self.assertFalse(inspect.isawaitable(x))
samples = [Bar(), MinimalCoro()]
for x in samples:
self.assertIsInstance(x, Awaitable)
self.assertTrue(issubclass(type(x), Awaitable))
self.assertTrue(inspect.isawaitable(x))
#c = coro()
#self.assertIsInstance(c, Awaitable)
#c.close() # avoid RuntimeWarning that coro() was not awaited
#c = new_coro()
#self.assertIsInstance(c, Awaitable)
#c.close() # avoid RuntimeWarning that coro() was not awaited
class CoroLike(object): pass
Coroutine.register(CoroLike)
self.assertTrue(inspect.isawaitable(CoroLike()))
self.assertTrue(isinstance(CoroLike(), Awaitable))
self.assertTrue(issubclass(CoroLike, Awaitable))
CoroLike = None
gc_collect() # Kill CoroLike to clean-up ABCMeta cache
def test_Coroutine(self):
def gen():
yield
#@types.coroutine
#def coro():
# yield
#async def new_coro():
# pass
class Bar(object):
def __await__(self):
yield
class MinimalCoro(Coroutine):
def send(self, value):
return value
def throw(self, typ, val=None, tb=None):
super(MinimalCoro, self).throw(typ, val, tb)
def __await__(self):
yield
non_samples = [None, int(), gen(), object(), Bar()]
for x in non_samples:
self.assertNotIsInstance(x, Coroutine)
self.assertFalse(issubclass(type(x), Coroutine), repr(type(x)))
samples = [MinimalCoro()]
for x in samples:
self.assertIsInstance(x, Awaitable)
self.assertTrue(issubclass(type(x), Awaitable))
#c = coro()
#self.assertIsInstance(c, Coroutine)
#c.close() # avoid RuntimeWarning that coro() was not awaited
#c = new_coro()
#self.assertIsInstance(c, Coroutine)
#c.close() # avoid RuntimeWarning that coro() was not awaited
class CoroLike(object):
def send(self, value):
pass
def throw(self, typ, val=None, tb=None):
pass
def close(self):
pass
def __await__(self):
pass
self.assertTrue(isinstance(CoroLike(), Coroutine))
self.assertTrue(issubclass(CoroLike, Coroutine))
class CoroLike(object):
def send(self, value):
pass
def close(self):
pass
def __await__(self):
pass
self.assertFalse(isinstance(CoroLike(), Coroutine))
self.assertFalse(issubclass(CoroLike, Coroutine))
def test_Iterable(self):
# Check some non-iterables
class OldStyleNonIterable: pass
non_samples = [None, 42, 3.14, 1j, OldStyleNonIterable()]
for x in non_samples:
self.assertNotIsInstance(x, Iterable)
self.assertFalse(issubclass(x.__class__, Iterable),
repr(x.__class__))
# Check some iterables
class OldStyleIterableBase:
def __iter__(self):
yield None
class OldStyleIterable(OldStyleIterableBase): pass
samples = [bytes(), str(),
tuple(), list(), set(), frozenset(), dict(),
dict().keys(), dict().items(), dict().values(),
(lambda: (yield))(),
(x for x in []),
OldStyleIterable(),
]
for x in samples:
self.assertIsInstance(x, Iterable)
self.assertTrue(issubclass(x.__class__, Iterable),
repr(x.__class__))
# Check direct subclassing
class I(Iterable):
def __iter__(self):
return super(I, self).__iter__()
self.assertEqual(list(I()), [])
self.assertFalse(issubclass(str, I))
self.validate_abstract_methods(Iterable, '__iter__')
self.validate_isinstance(Iterable, '__iter__')
def test_Iterator(self):
non_samples = [None, 42, 3.14, 1j, b"", "", (), [], {}, set()]
for x in non_samples:
self.assertNotIsInstance(x, Iterator)
self.assertFalse(issubclass(type(x), Iterator), repr(type(x)))
samples = [iter(bytes()), iter(str()),
iter(tuple()), iter(list()), iter(dict()),
iter(set()), iter(frozenset()),
iter(dict().keys()), iter(dict().items()),
iter(dict().values()),
(lambda: (yield))(),
(x for x in []),
]
for x in samples:
self.assertIsInstance(x, Iterator)
self.assertTrue(issubclass(type(x), Iterator), repr(type(x)))
self.validate_abstract_methods(Iterator, '__next__' if IS_PY3 else 'next', '__iter__')
# Issue 10565
class NextOnly(object):
def __next__(self):
yield 1
return
self.assertNotIsInstance(NextOnly(), Iterator)
def test_Generator(self):
class NonGen1(object):
def __iter__(self): return self
def __next__(self): return None
def close(self): pass
def throw(self, typ, val=None, tb=None): pass
class NonGen2(object):
def __iter__(self): return self
def __next__(self): return None
def close(self): pass
def send(self, value): return value
class NonGen3(object):
def close(self): pass
def send(self, value): return value
def throw(self, typ, val=None, tb=None): pass
non_samples = [
None, 42, 3.14, 1j, b"", "", (), [], {}, set(),
iter(()), iter([]), NonGen1(), NonGen2(), NonGen3()]
for x in non_samples:
self.assertNotIsInstance(x, Generator)
self.assertFalse(issubclass(type(x), Generator), repr(type(x)))
class Gen(object):
def __iter__(self): return self
if IS_PY3:
def __next__(self): return None
else:
def next(self): return None
def close(self): pass
def send(self, value): return value
def throw(self, typ, val=None, tb=None): pass
class MinimalGen(Generator):
def send(self, value):
return value
def throw(self, typ, val=None, tb=None):
super(MinimalGen, self).throw(typ, val, tb)
def gen():
yield 1
samples = [gen(), (lambda: (yield))(), Gen(), MinimalGen()]
for x in samples:
self.assertIsInstance(x, Iterator)
self.assertIsInstance(x, Generator)
self.assertTrue(issubclass(type(x), Generator), repr(type(x)))
self.validate_abstract_methods(Generator, 'send', 'throw')
# mixin tests
mgen = MinimalGen()
self.assertIs(mgen, iter(mgen))
self.assertIs(mgen.send(None), next(mgen))
self.assertEqual(2, mgen.send(2))
self.assertIsNone(mgen.close())
self.assertRaises(ValueError, mgen.throw, ValueError)
self.assertRaisesRegex(ValueError, "^huhu$",
mgen.throw, ValueError, ValueError("huhu"))
self.assertRaises(StopIteration, mgen.throw, StopIteration())
class FailOnClose(Generator):
def send(self, value): return value
def throw(self, *args): raise ValueError
self.assertRaises(ValueError, FailOnClose().close)
class IgnoreGeneratorExit(Generator):
def send(self, value): return value
def throw(self, *args): pass
self.assertRaises(RuntimeError, IgnoreGeneratorExit().close)
if __name__ == '__main__':
unittest.main()