-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtest.py
106 lines (80 loc) · 2.63 KB
/
test.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
from compose import compose
def test_compose():
def f(s):
return s + 'f'
def g(s):
return s + 'g'
f_g = compose(f, g)
assert f_g('') == 'gf' # Remember that it's f(g(...)) so g runs first
assert f_g.functions == (g, f) # functions exposed in execution order
def test_inlining():
def f(_):
return None
f_f = compose(f, f)
f_f_f = compose(f_f, f)
assert f_f_f.functions == (f, f, f)
def test_reject_empty():
try:
compose()
except TypeError:
return
assert False, 'compose() should have raised'
def _test_reject_non_callable(*args):
try:
compose(*args)
except TypeError:
return
assert False, 'compose(..., None, ...) should have raised'
def test_reject_non_callable():
def f(_):
return None
_test_reject_non_callable(None)
_test_reject_non_callable(f, None)
_test_reject_non_callable(None, f)
_test_reject_non_callable(None, None)
_test_reject_non_callable(f, None, f)
_test_reject_non_callable(None, f, None)
def test_pickle():
import pickle
# `pickle.HIGHEST_PROTOCOL` was added in Python 2.3, but so was
# `list.extend`, and the main code relies on that already.
highest_protocol = pickle.HIGHEST_PROTOCOL
protocol = 0
compose_in_compose_instance = compose(compose)
while protocol <= highest_protocol:
pickle.loads(pickle.dumps(compose_in_compose_instance, protocol))
protocol += 1
# Finishing the loop without exception is a pass.
def test_repr_eval():
class CallableWithEvaluatableRepr(object):
def __repr__(self):
return type(self).__name__ + '()'
def __call__(self, *args, **kwargs):
return None
c = CallableWithEvaluatableRepr()
c1 = compose(c)
c2 = eval(repr(c1))
assert repr(c1) == repr(c2), 'repr eval failed on compose(c)'
c1 = compose(c, c)
c2 = eval(repr(c1))
assert repr(c1) == repr(c2), 'repr eval failed on compose(c, c)'
c1 = compose(c, c, c)
c2 = eval(repr(c1))
assert repr(c1) == repr(c2), 'repr eval failed on compose(c, c, c)'
def test_positional_only_self_in_call():
value = 'whatever'
d = compose(dict)(self=value)
assert d['self'] == value
# Now check that making the above work didn't break standard errors:
raised = False
try:
compose.__call__()
except TypeError:
raised = True
assert raised, 'compose.__call__() should have raised'
raised = False
try:
compose.__call__(self=None)
except TypeError:
raised = True
assert raised, 'compose.__call__(self=...) should have raised'