forked from nocarryr/python-dispatch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_properties.py
More file actions
440 lines (341 loc) · 13.7 KB
/
Copy pathtest_properties.py
File metadata and controls
440 lines (341 loc) · 13.7 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
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
def test_properties(listener):
from pydispatch import Dispatcher, Property
class A(Dispatcher):
test_prop = Property('default')
name = Property('')
something = Property()
def __init__(self, name):
self.name = name
self.something = 'stuff'
a = A('foo')
assert a.something == 'stuff'
assert a.name == 'foo'
a.bind(test_prop=listener.on_prop)
assert 'test_prop' in a._Dispatcher__property_events
assert len(a._Dispatcher__property_events['test_prop'].listeners) == 1
assert a.test_prop == 'default'
a.test_prop = 'a'
a.test_prop = 'a'
a.test_prop = 'b'
assert listener.property_events == ['a', 'b']
def test_container_properties(listener):
from pydispatch import Dispatcher
from pydispatch.properties import ListProperty, DictProperty
class A(Dispatcher):
test_dict = DictProperty({'defaultkey':'defaultval'})
test_list = ListProperty(['defaultitem'])
a = A()
a.bind(test_dict=listener.on_prop, test_list=listener.on_prop)
assert a.test_dict['defaultkey'] == 'defaultval'
assert a.test_list[0] == 'defaultitem'
a.test_dict = {'changed':True}
a.test_list = ['changed']
assert a.test_dict['changed'] is True and len(a.test_dict) == 1
assert a.test_list[0] == 'changed' and len(a.test_list) == 1
assert listener.property_events == [{'changed':True}, ['changed']]
listener.property_events = []
listener.property_event_kwargs = []
a.test_dict['nested_dict'] = {'foo':'bar'}
a.test_dict['nested_dict']['foo'] = 'baz'
a.test_dict['nested_dict']['nested_list'] = [0]
a.test_dict['nested_dict']['nested_list'].append(1)
assert len(listener.property_events) == 4
assert a.test_dict['nested_dict']['nested_list'] == [0, 1]
assert 'nested_dict' in listener.property_event_kwargs[0]['keys']
listener.property_events = []
a.test_list.append({'nested_dict':{'foo':'bar'}})
d = a.test_list[-1]
d['nested_dict']['foo'] = 'baz'
assert len(listener.property_events) == 2
assert a.test_list[-1] == {'nested_dict':{'foo':'baz'}}
listener.property_events = []
del a.test_list[:]
assert len(listener.property_events) == 1
assert len(a.test_list) == 0
if hasattr(list, 'clear'):
listener.property_events = []
a.test_list.append(42)
a.test_list.clear()
a.test_list.append(True)
assert len(listener.property_events) == 3
def test_list_property_ops(listener):
from pydispatch import Dispatcher
from pydispatch.properties import ListProperty
class A(Dispatcher):
test_list = ListProperty()
a = A()
a.bind(test_list=listener.on_prop)
# Test slicing
listener.property_events = []
a.test_list[1:4] = ['a', 'b', 'c', 'd']
assert len(listener.property_events) == 1
assert a.test_list == ['a', 'b', 'c', 'd']
# Test __setitem__
listener.property_events = []
listener.property_event_kwargs = []
a.test_list[0] = 'z'
assert len(listener.property_events) == 1
assert a.test_list == ['z', 'b', 'c', 'd']
assert listener.property_event_kwargs[0]['keys'] == [0]
# Test __delitem__
listener.property_events = []
del a.test_list[0]
assert len(listener.property_events) == 1
assert a.test_list == ['b', 'c', 'd']
# Test remove
listener.property_events = []
a.test_list = ['a', 'b', 'c', 'd']
a.test_list.remove('d')
assert len(listener.property_events) == 2
assert a.test_list == ['a', 'b', 'c']
# Test __iadd__
listener.property_events = []
a.test_list = ['a', 'b', 'c', 'd']
a.test_list += ['e', 'f', 'g']
assert a.test_list == ['a', 'b', 'c', 'd', 'e', 'f', 'g']
assert len(listener.property_events) == 2
def test_dict_property_ops(listener):
from pydispatch import Dispatcher
from pydispatch.properties import DictProperty
class A(Dispatcher):
test_dict = DictProperty({'a':1, 'b':2, 'c':3, 'd':4})
a = A()
a.bind(test_dict=listener.on_prop)
v = a.test_dict.pop('a')
assert len(listener.property_events) == 1
assert 'a' not in a.test_dict
listener.property_events = []
del a.test_dict['b']
assert len(listener.property_events) == 1
assert 'b' not in a.test_dict
listener.property_events = []
listener.property_event_kwargs = []
a.test_dict.update({'c':3, 'e':5, 'f':6, 'g':7})
assert len(listener.property_events) == 1
assert a.test_dict == {'c':3, 'd':4, 'e':5, 'f':6, 'g':7}
assert sorted(listener.property_event_kwargs[0]['keys']) == ['e', 'f', 'g']
listener.property_events = []
a.test_dict.clear()
assert len(listener.property_events) == 1
assert len(a.test_dict) == 0
listener.property_events = []
a.test_dict.setdefault('foo', 'bar')
assert len(listener.property_events) == 1
assert a.test_dict['foo'] == 'bar'
def test_empty_defaults(listener):
from pydispatch import Dispatcher
from pydispatch.properties import (
ListProperty, DictProperty, ObservableList, ObservableDict,
)
class A(Dispatcher):
test_dict = DictProperty()
test_list = ListProperty()
a = A()
a.bind(test_dict=listener.on_prop, test_list=listener.on_prop)
assert isinstance(a.test_dict, ObservableDict)
assert isinstance(a.test_list, ObservableList)
a.test_dict['foo'] = 'bar'
a.test_list.append('baz')
assert isinstance(a.test_dict, ObservableDict)
assert isinstance(a.test_list, ObservableList)
assert len(listener.property_events) == 2
def test_unbind(listener):
from pydispatch import Dispatcher, Property
class A(Dispatcher):
test_prop = Property()
a = A()
a.bind(test_prop=listener.on_prop)
a.test_prop = 1
assert len(listener.property_events) == 1
listener.property_events = []
# unbind by method
a.unbind(listener.on_prop)
a.test_prop = 2
assert len(listener.property_events) == 0
# rebind and make sure events still work
a.bind(test_prop=listener.on_prop)
a.test_prop = 3
assert len(listener.property_events) == 1
listener.property_events = []
# unbind by instance
a.unbind(listener)
a.test_prop = 4
assert len(listener.property_events) == 0
def test_removal():
from pydispatch import Dispatcher, Property
class Listener(object):
def __init__(self):
self.property_events = []
def on_prop(self, obj, value, **kwargs):
self.property_events.append(value)
class A(Dispatcher):
test_prop = Property()
listener = Listener()
a = A()
a.bind(test_prop=listener.on_prop)
a.test_prop = 1
assert len(listener.property_events) == 1
del listener
e = a._Dispatcher__property_events['test_prop']
l = [m for m in e.listeners]
assert len(l) == 0
prop = A._PROPERTIES_['test_prop']
del a
assert len(prop._Property__weakrefs) == 0
assert len(prop._Property__storage) == 0
def test_self_binding():
from pydispatch import Dispatcher
from pydispatch.properties import Property, ListProperty, DictProperty
class A(Dispatcher):
test_prop = Property()
test_dict = DictProperty()
test_list = ListProperty()
def __init__(self):
self.received = []
self.bind(
test_prop=self.on_test_prop,
test_dict=self.on_test_dict,
test_list=self.on_test_list,
)
def on_test_prop(self, *args, **kwargs):
self.received.append('test_prop')
def on_test_dict(self, *args, **kwargs):
self.received.append('test_dict')
def on_test_list(self, *args, **kwargs):
self.received.append('test_list')
a = A()
a.test_prop = 'foo'
a.test_dict['foo'] = 'bar'
a.test_list.append('baz')
assert a.received == ['test_prop', 'test_dict', 'test_list']
def test_emission_lock(listener):
from pydispatch import Dispatcher, Property
from pydispatch.properties import ListProperty, DictProperty
class A(Dispatcher):
test_prop = Property()
test_dict = DictProperty()
test_list = ListProperty()
a = A()
a.bind(test_prop=listener.on_prop, test_list=listener.on_prop, test_dict=listener.on_prop)
letters = 'abcdefghijkl'
a.test_prop = 'foo'
a.test_list = [-1] * 4
a.test_dict = {'a':0, 'b':1, 'c':2, 'd':3}
assert len(listener.property_events) == 3
listener.property_events = []
listener.property_event_kwargs = []
with a.emission_lock('test_prop'):
for i in range(4):
a.test_prop = i
assert len(listener.property_events) == 1
assert listener.property_event_kwargs[0]['property'].name == 'test_prop'
assert listener.property_events[0] == i
listener.property_events = []
listener.property_event_kwargs = []
with a.emission_lock('test_list'):
a.test_prop = 'foo'
for i in range(4):
a.test_list = [i] * 4
assert len(listener.property_events) == 2
assert listener.property_event_kwargs[0]['property'].name == 'test_prop'
assert listener.property_events[0] == 'foo'
assert listener.property_event_kwargs[1]['property'].name == 'test_list'
assert listener.property_events[1] == [i] * 4
listener.property_events = []
listener.property_event_kwargs = []
with a.emission_lock('test_dict'):
a.test_prop = 'bar'
a.test_list[0] = 'a'
for i in range(4):
for key in a.test_dict.keys():
a.test_dict[key] = i
assert len(listener.property_events) == 3
assert listener.property_event_kwargs[0]['property'].name == 'test_prop'
assert listener.property_events[0] == 'bar'
assert listener.property_event_kwargs[1]['property'].name == 'test_list'
assert listener.property_events[1][0] == 'a'
assert listener.property_event_kwargs[2]['property'].name == 'test_dict'
assert listener.property_events[2] == {k:i for k in a.test_dict.keys()}
listener.property_events = []
listener.property_event_kwargs = []
with a.emission_lock('test_prop'):
with a.emission_lock('test_list'):
with a.emission_lock('test_dict'):
for i in range(4):
a.test_prop = i
a.test_list[0] = i
a.test_dict[i] = 'foo'
assert len(listener.property_events) == 3
assert listener.property_event_kwargs[0]['property'].name == 'test_dict'
for k in range(4):
assert listener.property_events[0][k] == 'foo'
assert listener.property_event_kwargs[1]['property'].name == 'test_list'
assert listener.property_events[1][0] == i
assert listener.property_event_kwargs[2]['property'].name == 'test_prop'
assert listener.property_events[2] == i
def test_copy_on_change(listener):
from pydispatch import Dispatcher
from pydispatch.properties import ListProperty, DictProperty
class A(Dispatcher):
test_dict = DictProperty(copy_on_change=True)
test_list = ListProperty(copy_on_change=True)
no_cp_dict = DictProperty()
no_cp_list = ListProperty()
a = A()
a.bind(
test_dict=listener.on_prop, test_list=listener.on_prop,
no_cp_dict=listener.on_prop, no_cp_list=listener.on_prop,
)
a.test_dict['foo'] = 'bar'
assert listener.property_event_kwargs[0]['old'] == {}
a.test_dict['foo'] = None
assert listener.property_event_kwargs[1]['old'] == {'foo':'bar'}
a.test_dict['nested_dict'] = {'a':1}
assert listener.property_event_kwargs[2]['old'] == {'foo':None}
a.test_dict['nested_dict']['b'] = 2
assert listener.property_event_kwargs[3]['old'] == {'foo':None, 'nested_dict':{'a':1}}
a.test_dict['nested_list'] = ['a', 'b']
assert listener.property_event_kwargs[4]['old'] == {
'foo':None, 'nested_dict':{'a':1, 'b':2}
}
a.test_dict['nested_list'].append('c')
assert listener.property_event_kwargs[5]['old'] == {
'foo':None, 'nested_dict':{'a':1, 'b':2}, 'nested_list':['a', 'b']
}
listener.property_event_kwargs = []
a.test_list.append('a')
assert listener.property_event_kwargs[0]['old'] == []
a.test_list.extend(['b', 'c'])
assert listener.property_event_kwargs[1]['old'] == ['a']
a.test_list[0] = {}
assert listener.property_event_kwargs[2]['old'] == ['a', 'b', 'c']
a.test_list[0]['foo'] = 'bar'
assert listener.property_event_kwargs[3]['old'] == [{}, 'b', 'c']
a.test_list[1] = [0]
assert listener.property_event_kwargs[4]['old'] == [{'foo':'bar'}, 'b', 'c']
a.test_list[1].append(1)
assert listener.property_event_kwargs[5]['old'] == [{'foo':'bar'}, [0], 'c']
listener.property_event_kwargs = []
a.no_cp_dict['foo'] = 'bar'
assert listener.property_event_kwargs[0]['old'] == None
a.no_cp_dict['nested_dict'] = {}
assert listener.property_event_kwargs[1]['old'] == None
a.no_cp_dict['nested_dict']['a'] = 1
assert listener.property_event_kwargs[2]['old'] == None
a.no_cp_dict['nested_list'] = []
assert listener.property_event_kwargs[3]['old'] == None
a.no_cp_dict['nested_list'].append(1)
assert listener.property_event_kwargs[4]['old'] == None
listener.property_event_kwargs = []
a.no_cp_list.append('a')
assert listener.property_event_kwargs[0]['old'] == None
a.no_cp_list.extend(['b', 'c'])
assert listener.property_event_kwargs[1]['old'] == None
a.no_cp_list[0] = {}
assert listener.property_event_kwargs[2]['old'] == None
a.no_cp_list[0]['foo'] = 'bar'
assert listener.property_event_kwargs[3]['old'] == None
a.no_cp_list[1] = []
assert listener.property_event_kwargs[4]['old'] == None
a.no_cp_list[1].append(1)
assert listener.property_event_kwargs[5]['old'] == None