-
Notifications
You must be signed in to change notification settings - Fork 774
Expand file tree
/
Copy pathtest_delegate.py
More file actions
453 lines (317 loc) · 11.8 KB
/
test_delegate.py
File metadata and controls
453 lines (317 loc) · 11.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
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
441
442
443
444
445
446
447
448
449
450
451
452
453
# -*- coding: utf-8 -*-
"""Test CLR delegate support."""
import Python.Test as Test
import System
import pytest
from Python.Test import DelegateTest, StringDelegate
from .utils import HelloClass, hello_func, MultipleHandler, DictProxyType
def test_delegate_standard_attrs():
"""Test standard delegate attributes."""
from Python.Test import PublicDelegate
assert PublicDelegate.__name__ == 'PublicDelegate'
assert PublicDelegate.__module__ == 'Python.Test'
assert isinstance(PublicDelegate.__dict__, DictProxyType)
assert PublicDelegate.__doc__ is None
def test_global_delegate_visibility():
"""Test visibility of module-level delegates."""
from Python.Test import PublicDelegate
assert PublicDelegate.__name__ == 'PublicDelegate'
assert Test.PublicDelegate.__name__ == 'PublicDelegate'
with pytest.raises(ImportError):
from Python.Test import InternalDelegate
_ = InternalDelegate
with pytest.raises(AttributeError):
_ = Test.InternalDelegate
def test_nested_delegate_visibility():
"""Test visibility of nested delegates."""
ob = DelegateTest.PublicDelegate
assert ob.__name__ == 'PublicDelegate'
ob = DelegateTest.ProtectedDelegate
assert ob.__name__ == 'ProtectedDelegate'
with pytest.raises(AttributeError):
_ = DelegateTest.InternalDelegate
with pytest.raises(AttributeError):
_ = DelegateTest.PrivateDelegate
def test_delegate_from_function():
"""Test delegate implemented with a Python function."""
d = StringDelegate(hello_func)
ob = DelegateTest()
assert ob.CallStringDelegate(d) == "hello"
assert d() == "hello"
ob.stringDelegate = d
assert ob.CallStringDelegate(ob.stringDelegate) == "hello"
assert ob.stringDelegate() == "hello"
def test_delegate_from_method():
"""Test delegate implemented with a Python instance method."""
inst = HelloClass()
d = StringDelegate(inst.hello)
ob = DelegateTest()
assert ob.CallStringDelegate(d) == "hello"
assert d() == "hello"
ob.stringDelegate = d
assert ob.CallStringDelegate(ob.stringDelegate) == "hello"
assert ob.stringDelegate() == "hello"
def test_delegate_from_unbound_method():
"""Test failure mode for unbound methods."""
with pytest.raises(TypeError):
d = StringDelegate(HelloClass.hello)
d()
def test_delegate_from_static_method():
"""Test delegate implemented with a Python static method."""
d = StringDelegate(HelloClass.s_hello)
ob = DelegateTest()
assert ob.CallStringDelegate(d) == "hello"
assert d() == "hello"
ob.stringDelegate = d
assert ob.CallStringDelegate(ob.stringDelegate) == "hello"
assert ob.stringDelegate() == "hello"
inst = HelloClass()
d = StringDelegate(inst.s_hello)
ob = DelegateTest()
assert ob.CallStringDelegate(d) == "hello"
assert d() == "hello"
ob.stringDelegate = d
assert ob.CallStringDelegate(ob.stringDelegate) == "hello"
assert ob.stringDelegate() == "hello"
def test_delegate_from_class_method():
"""Test delegate implemented with a Python class method."""
d = StringDelegate(HelloClass.c_hello)
ob = DelegateTest()
assert ob.CallStringDelegate(d) == "hello"
assert d() == "hello"
ob.stringDelegate = d
assert ob.CallStringDelegate(ob.stringDelegate) == "hello"
assert ob.stringDelegate() == "hello"
inst = HelloClass()
d = StringDelegate(inst.c_hello)
ob = DelegateTest()
assert ob.CallStringDelegate(d) == "hello"
assert d() == "hello"
ob.stringDelegate = d
assert ob.CallStringDelegate(ob.stringDelegate) == "hello"
assert ob.stringDelegate() == "hello"
def test_delegate_from_callable():
"""Test delegate implemented with a Python callable object."""
inst = HelloClass()
d = StringDelegate(inst)
ob = DelegateTest()
assert ob.CallStringDelegate(d) == "hello"
assert d() == "hello"
ob.stringDelegate = d
assert ob.CallStringDelegate(ob.stringDelegate) == "hello"
assert ob.stringDelegate() == "hello"
def test_delegate_from_managed_instance_method():
"""Test delegate implemented with a managed instance method."""
ob = DelegateTest()
d = StringDelegate(ob.SayHello)
assert ob.CallStringDelegate(d) == "hello"
assert d() == "hello"
ob.stringDelegate = d
assert ob.CallStringDelegate(ob.stringDelegate) == "hello"
assert ob.stringDelegate() == "hello"
def test_delegate_from_managed_static_method():
"""Test delegate implemented with a managed static method."""
d = StringDelegate(DelegateTest.StaticSayHello)
ob = DelegateTest()
assert ob.CallStringDelegate(d) == "hello"
assert d() == "hello"
ob.stringDelegate = d
assert ob.CallStringDelegate(ob.stringDelegate) == "hello"
assert ob.stringDelegate() == "hello"
def test_delegate_from_delegate():
"""Test delegate implemented with another delegate."""
d1 = StringDelegate(hello_func)
d2 = StringDelegate(d1)
ob = DelegateTest()
assert ob.CallStringDelegate(d2) == "hello"
assert d2() == "hello"
ob.stringDelegate = d2
assert ob.CallStringDelegate(ob.stringDelegate) == "hello"
assert ob.stringDelegate() == "hello"
def test_delegate_with_invalid_args():
"""Test delegate instantiation with invalid (non-callable) args."""
with pytest.raises(TypeError):
_ = StringDelegate(None)
with pytest.raises(TypeError):
_ = StringDelegate("spam")
with pytest.raises(TypeError):
_ = StringDelegate(1)
def test_multicast_delegate():
"""Test multicast delegates."""
inst = MultipleHandler()
d1 = StringDelegate(inst.count)
d2 = StringDelegate(inst.count)
md = System.Delegate.Combine(d1, d2)
ob = DelegateTest()
assert ob.CallStringDelegate(md) == "ok"
assert inst.value == 2
assert md() == "ok"
assert inst.value == 4
def test_subclass_delegate_fails():
"""Test that subclassing of a delegate type fails."""
from Python.Test import PublicDelegate
with pytest.raises(TypeError):
class Boom(PublicDelegate):
pass
_ = Boom
def test_delegate_equality():
"""Test delegate equality."""
d = StringDelegate(hello_func)
ob = DelegateTest()
ob.stringDelegate = d
assert ob.stringDelegate == d
def test_bool_delegate():
"""Test boolean delegate."""
from Python.Test import BoolDelegate
def always_so_negative():
return False
d = BoolDelegate(always_so_negative)
ob = DelegateTest()
ob.CallBoolDelegate(d)
assert not d()
assert not ob.CallBoolDelegate(d)
def always_so_positive():
return 1
bad = BoolDelegate(always_so_positive)
with pytest.raises(TypeError):
ob.CallBoolDelegate(bad)
def test_object_delegate():
"""Test object delegate."""
from Python.Test import ObjectDelegate
def create_object():
return DelegateTest()
d = ObjectDelegate(create_object)
ob = DelegateTest()
ob.CallObjectDelegate(d)
def test_invalid_object_delegate():
"""Test invalid object delegate with mismatched return type."""
from Python.Test import ObjectDelegate
d = ObjectDelegate(hello_func)
ob = DelegateTest()
with pytest.raises(TypeError):
ob.CallObjectDelegate(d)
def test_out_int_delegate():
"""Test delegate with an out int parameter."""
from Python.Test import OutIntDelegate
value = 7
def out_hello_func(ignored):
return 5
d = OutIntDelegate(out_hello_func)
result = d(value)
assert result == 5
ob = DelegateTest()
result = ob.CallOutIntDelegate(d, value)
assert result == 5
def invalid_handler(ignored):
return '5'
d = OutIntDelegate(invalid_handler)
with pytest.raises(TypeError):
result = d(value)
def test_out_string_delegate():
"""Test delegate with an out string parameter."""
from Python.Test import OutStringDelegate
value = 'goodbye'
def out_hello_func(ignored):
return 'hello'
d = OutStringDelegate(out_hello_func)
result = d(value)
assert result == 'hello'
ob = DelegateTest()
result = ob.CallOutStringDelegate(d, value)
assert result == 'hello'
def test_ref_int_delegate():
"""Test delegate with a ref string parameter."""
from Python.Test import RefIntDelegate
value = 7
def ref_hello_func(data):
assert data == value
return data + 1
d = RefIntDelegate(ref_hello_func)
result = d(value)
assert result == value + 1
ob = DelegateTest()
result = ob.CallRefIntDelegate(d, value)
assert result == value + 1
def test_ref_string_delegate():
"""Test delegate with a ref string parameter."""
from Python.Test import RefStringDelegate
value = 'goodbye'
def ref_hello_func(data):
assert data == value
return 'hello'
d = RefStringDelegate(ref_hello_func)
result = d(value)
assert result == 'hello'
ob = DelegateTest()
result = ob.CallRefStringDelegate(d, value)
assert result == 'hello'
def test_ref_int_ref_string_delegate():
"""Test delegate with a ref int and ref string parameter."""
from Python.Test import RefIntRefStringDelegate
intData = 7
stringData = 'goodbye'
def ref_hello_func(intValue, stringValue):
assert intData == intValue
assert stringData == stringValue
return (intValue + 1, stringValue + '!')
d = RefIntRefStringDelegate(ref_hello_func)
result = d(intData, stringData)
assert result == (intData + 1, stringData + '!')
ob = DelegateTest()
result = ob.CallRefIntRefStringDelegate(d, intData, stringData)
assert result == (intData + 1, stringData + '!')
def not_a_tuple(intValue, stringValue):
return 'a'
d = RefIntRefStringDelegate(not_a_tuple)
with pytest.raises(TypeError):
result = d(intData, stringData)
def short_tuple(intValue, stringValue):
return (5,)
d = RefIntRefStringDelegate(short_tuple)
with pytest.raises(TypeError):
result = d(intData, stringData)
def long_tuple(intValue, stringValue):
return (5, 'a', 'b')
d = RefIntRefStringDelegate(long_tuple)
with pytest.raises(TypeError):
result = d(intData, stringData)
def wrong_tuple_item(intValue, stringValue):
return ('a', 'b')
d = RefIntRefStringDelegate(wrong_tuple_item)
with pytest.raises(TypeError):
result = d(intData, stringData)
def test_int_ref_int_ref_string_delegate():
"""Test delegate with a ref int and ref string parameter."""
from Python.Test import IntRefIntRefStringDelegate
intData = 7
stringData = 'goodbye'
def ref_hello_func(intValue, stringValue):
assert intData == intValue
assert stringData == stringValue
return (intValue + len(stringValue), intValue + 1, stringValue + '!')
d = IntRefIntRefStringDelegate(ref_hello_func)
result = d(intData, stringData)
assert result == (intData + len(stringData), intData + 1, stringData + '!')
ob = DelegateTest()
result = ob.CallIntRefIntRefStringDelegate(d, intData, stringData)
assert result == (intData + len(stringData), intData + 1, stringData + '!')
def not_a_tuple(intValue, stringValue):
return 'a'
d = IntRefIntRefStringDelegate(not_a_tuple)
with pytest.raises(TypeError):
result = d(intData, stringData)
def short_tuple(intValue, stringValue):
return (5,)
d = IntRefIntRefStringDelegate(short_tuple)
with pytest.raises(TypeError):
result = d(intData, stringData)
def wrong_return_type(intValue, stringValue):
return ('a', 7, 'b')
d = IntRefIntRefStringDelegate(wrong_return_type)
with pytest.raises(TypeError):
result = d(intData, stringData)
# test async delegates
# test multicast delegates
# test explicit op_
# test sig mismatch, both on managed and Python side
# test return wrong type