-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest_weakmethod.py
More file actions
210 lines (176 loc) · 5.8 KB
/
test_weakmethod.py
File metadata and controls
210 lines (176 loc) · 5.8 KB
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
# test_weakref
import weakref
import unittest
import stacklesslib.weakmethod as weakmethod
try:
from weakref import ReferenceError
except:
pass # a python 3 built int
class TestClass(object):
def method(self, *args):
return args
def func():
pass
class TestWeakMethod(unittest.TestCase):
def setUp(self):
self.c = TestClass()
def test_create(self):
r = weakmethod.WeakMethod(TestClass().method)
def test_weak(self):
r = weakmethod.WeakMethod(TestClass().method)
self.assertEqual(r(), None)
def test_strong(self):
r = weakmethod.WeakMethod(self.c.method)
self.assertEqual(r(), self.c.method)
del self.c
self.assertEqual(r(), None)
def test_fail(self):
self.assertRaises(TypeError, weakmethod.WeakMethod, func)
def test_eq(self):
m = self.c.method
r = weakmethod.WeakMethod(m)
self.assertEqual(m, r())
def test_repr(self):
m = self.c.method
r = weakmethod.WeakMethod(m)
self.assertEqual(repr(m), repr(r()))
def test_ref(self):
m = self.c.method
r = weakmethod.WeakMethod(m)
weakref.ref(r)
class TestProxy(unittest.TestCase):
"""Test some properties of callable proxies that we want to emulate"""
def setUp(self):
def dude():
pass
self.dude = dude
def test_callback(self):
foo = [None]
def cb(arg):
foo[0] = arg
p = weakref.proxy(self.dude, cb)
del self.dude
self.assertEqual(id(foo[0]), id(p))
def test_hash(self):
p = weakref.proxy(self.dude)
self.assertRaises(TypeError, hash, p)
del self.dude
self.assertRaises(TypeError, hash, p)
def test_cmp(self):
def cb1(arg): pass
def cb2(arg): pass
p1 = weakref.proxy(self.dude, cb1)
p2 = weakref.proxy(self.dude, cb2)
self.assertNotEqual(id(p1), id(p2))
self.assertEqual(p1, p2)
del self.dude
def cmp(a, b): return a == b
self.assertRaises(ReferenceError, cmp, p1, p2)
class TestWeakMethodProxy(unittest.TestCase):
def setUp(self):
self.c = TestClass()
def test_create(self):
r = weakmethod.WeakMethodProxy(self.c.method)
def test_weak(self):
r = weakmethod.WeakMethodProxy(TestClass().method)
self.assertRaises(ReferenceError, r)
def test_strong(self):
r = weakmethod.WeakMethodProxy(self.c.method)
result = r(1,2,3)
self.assertEqual(result, (1, 2, 3))
del self.c
self.assertRaises(ReferenceError, r)
def test_strong_callback(self):
foo = [None]
def cb(arg):
foo[0] = arg
r = weakmethod.WeakMethodProxy(self.c.method, callback=cb)
result = r(1,2,3)
self.assertEqual(result, (1, 2, 3))
self.assertEqual(foo[0], None)
del self.c
self.assertRaises(ReferenceError, r)
self.assertEqual(id(foo[0]), id(r))
def test_fallback(self):
def fb(*arg):
return arg+arg
r = weakmethod.WeakMethodProxy(self.c.method, fallback=fb)
result = r(1,2,3)
self.assertEqual(result, (1, 2, 3))
del self.c
result = r(1,2,3)
self.assertEqual(result, (1, 2, 3)*2)
def test_hash(self):
r = weakmethod.WeakMethodProxy(self.c.method)
self.assertRaises(TypeError, hash, r)
del self.c
self.assertRaises(TypeError, hash, r)
def test_repr(self):
r = weakmethod.WeakMethodProxy(self.c.method)
inner = repr(self.c.method)
outer = repr(r)
self.assertTrue(inner in outer)
del self.c
outer = repr(r)
self.assertFalse(inner in outer)
self.assertTrue("None" in outer)
def test_cmp(self):
def cb1(arg): pass
def cb2(arg): pass
p1 = weakmethod.WeakMethodProxy(self.c.method)
p2 = weakmethod.WeakMethodProxy(self.c.method)
self.assertNotEqual(id(p1), id(p2))
self.assertEqual(p1, p2)
del self.c
def cmp(a, b): return a == b
self.assertRaises(ReferenceError, cmp, p1, p2)
class TestRef(unittest.TestCase):
def test_object(self):
o = TestClass()
p1 = weakref.ref(o)
p2 = weakmethod.ref(o)
self.assertEqual(p1, p2)
def test_object_cb(self):
def cb1(arg): pass
o = TestClass()
p1 = weakref.ref(o, cb1)
p2 = weakmethod.ref(o, cb1)
self.assertEqual(p1, p2)
def test_func(self):
def o(): pass
p1 = weakref.ref(o)
p2 = weakmethod.ref(o)
self.assertEqual(p1, p2)
def test_func(self):
c = TestClass()
o = c.method
p1 = weakref.ref(o)
p2 = weakmethod.ref(o)
self.assertNotEqual(p1, p2)
p3 = weakmethod.WeakMethod(o)
self.assertNotEqual(p1, p3)
class TestProxy(unittest.TestCase):
def test_object(self):
o = TestClass()
p1 = weakref.proxy(o)
p2 = weakmethod.proxy(o)
self.assertEqual(p1, p2)
def test_object_cb(self):
def cb1(arg): pass
o = TestClass()
p1 = weakref.proxy(o, cb1)
p2 = weakmethod.proxy(o, cb1)
self.assertEqual(p1, p2)
def test_func(self):
def o(): pass
p1 = weakref.proxy(o)
p2 = weakmethod.proxy(o)
self.assertEqual(p1, p2)
def test_func(self):
c = TestClass()
o = c.method
p1 = weakref.proxy(o)
p2 = weakmethod.proxy(o)
self.assertNotEqual(p1, p2)
p3 = weakmethod.WeakMethodProxy(o)
self.assertNotEqual(p1, p3)