forked from wearpants/factory
/
test_Factory.py
executable file
·192 lines (159 loc) · 4.77 KB
/
test_Factory.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
import operator
from nose.tools import *
import Factory
def test_alias():
assert Factory.Factory is Factory.bind
def test_callable_object():
class CallMe(object):
def __init__(self, x):
self.x = x
def __call__(self, y):
return self.x + y
fac = Factory.Factory(CallMe(1))
assert fac(1) == 2
def test_missing_attr():
fac=Factory.Factory(lambda x: x)
assert_raises(AttributeError, getattr, fac, 'x')
assert_raises(AttributeError, getattr, fac, 'y')
fac.x = 'x'
obj = fac()
assert obj == 'x'
class A(object):
"""This class doesn't define a cooperative constructor, but it doesn't call
its super anyway."""
def __init__(self, x):
self.x = x
class B(A):
def __init__(self, y, **kwargs):
self.y = y
super(B, self).__init__(**kwargs)
class C(B):
def __init__(self, z, **kwargs):
self.z = z
super(C, self).__init__(**kwargs)
def test_basic_use_with_cooperative_constructors():
fac = Factory.Factory(C)
fac.z = 'z'
fac.y = 'y'
fac.x = 'x'
obj = fac()
assert obj.x == 'x'
assert obj.y == 'y'
assert obj.z == 'z'
fac.bind(x='xx', y='yy')
obj = fac()
assert obj.x == 'xx'
assert obj.y == 'yy'
assert obj.z == 'z'
class Uncooperative(A):
def __init__(self, y='Uncooperative'): # Note there's no x=
super(Uncooperative, self).__init__(x='Uncooperative')
self.y = 'Uncooperative'
def test_bind_constructor_does_not_use_kwargs():
fac = Factory.Factory(Uncooperative)
fac.bind(y='Factory') # This is normal, and should succeed.
assert_raises(AttributeError, fac.bind, x='Factory')
assert_raises(AttributeError, setattr, fac, 'x', 'Factory')
def test_bind_varargs():
def mysum(*args):
return sum(args)
fac = Factory.Factory(mysum)
fac.bind(1, 2, 3)
fac.bind(4, 5, 6)
fac.bind(7, 8, 9)
assert fac() == 45
assert fac(10) == 55
def test_bind_varargs_preserves_order():
# Now for a non-commutative operation.
def join(*args):
return ''.join(args)
fac = Factory.Factory(join)
fac.bind('a', 'b', 'c')
fac.bind('d', 'e', 'f')
fac.bind('g', 'h', 'i')
assert fac() == 'abcdefghi'
assert fac('j', 'k', 'l') == 'abcdefghijkl'
def test_bind_varargs_fails_when_callee_does_not_support_varargs():
def add(a, b):
return a + b
fac = Factory.Factory(add)
assert_raises(TypeError, fac.bind, 1, 2)
def test_permits_varkwargs():
def f(**kwargs):
return kwargs # This accepts any and all args.
fac = Factory.Factory(f)
fac.bind(x='x', y='y')
assert fac() == {'x': 'x', 'y': 'y'}
class A(object):
def __init__(self, **kwargs):
pass
class B(A):
def __init__(self, **kwargs):
super(B, self).__init__(**kwargs)
class C(B):
def __init__(self, b=None):
super(C, self).__init__({'b': b})
B_fac = Factory.Factory(B) # Should accept variable kwargs.
C_fac = Factory.Factory(C) # Should not accept variable kwargs.
assert B_fac(x=1)
assert_raises(AttributeError, C_fac.bind, x=1)
def test_builtin_function():
assert Factory.Factory(hex, 42)() == '0x2a'
def test_builtin_class():
fac = Factory.Factory(list)
fac.bind((1, 2, 3))
L = fac()
assert L == [1, 2, 3]
def test_subclass_of_builtin():
class MyList(list):
pass
fac = Factory.Factory(MyList)
fac.bind((1, 2, 3))
L = fac()
assert L == [1, 2, 3]
assert isinstance(L, MyList)
def test_bultin_instancemethod():
L = [1, 2, 3]
fac = Factory.Factory(L.append)
fac(4)
assert L == [1, 2, 3, 4]
fac.bind(5)
fac()
assert L == [1, 2, 3, 4, 5]
def test_delattr():
def called_with(x='pants'):
return x
fac = Factory.Factory(called_with)
fac.x = 'shirt'
assert fac() == 'shirt'
del fac.x
assert fac() == 'pants'
def test_getattr():
def do_stuff(x, y):
return x, y
fac = Factory.Factory(do_stuff, x=1)
assert fac.x == 1
assert_raises(AttributeError, getattr, fac, 'y')
def test_bunch_equals():
b = Factory.Bunch(pants="jeans", shirt="dressy")
c = b.harden()
assert c is not b
assert c == b
c.shirt = "sloppy"
assert c != b
def test_bunch_repr():
b = Factory.Bunch(pants="jeans")
assert repr(b) == """Bunch(pants='jeans')"""
def test_bunch_get():
b = Factory.Bunch(pants="jeans")
assert b.get('pants', 'foo') == 'jeans'
assert b.get('nonesuch', 'foo') == 'foo'
assert b.get('nonesuch') is None
def test_bunch_recursive():
b = Factory.Bunch(pants="jeans")
b.sub = Factory.Bunch(shirt="buttons")
b2 = b.harden()
assert b is not b2
assert b == b2
assert b.sub is not b2.sub
assert b.sub == b2.sub