-
-
Notifications
You must be signed in to change notification settings - Fork 114
/
Copy pathtest_multistrategy_dispatch.py
50 lines (34 loc) · 1.21 KB
/
test_multistrategy_dispatch.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
from cattrs import BaseConverter
from cattrs.dispatch import MultiStrategyDispatch
class Foo:
pass
def _fallback():
pass
def _foo_func():
pass
def _foo_cls():
pass
c = BaseConverter()
def test_multistrategy_dispatch_register_cls():
_fallback()
_foo_func()
_foo_cls()
dispatch = MultiStrategyDispatch(lambda _: _fallback, c)
assert dispatch.dispatch(Foo) == _fallback
dispatch.register_cls_list([(Foo, _foo_cls)])
assert dispatch.dispatch(Foo) == _foo_cls
def test_multistrategy_dispatch_register_func():
dispatch = MultiStrategyDispatch(lambda _: _fallback, c)
assert dispatch.dispatch(Foo) == _fallback
dispatch.register_func_list([(lambda cls: issubclass(cls, Foo), _foo_func)])
assert dispatch.dispatch(Foo) == _foo_func
def test_multistrategy_dispatch_conflict_class_wins():
"""
When a class dispatch and a function dispatch
are registered which handle the same type, the
class dispatch should return.
"""
dispatch = MultiStrategyDispatch(lambda _: _fallback, c)
dispatch.register_func_list([(lambda cls: issubclass(cls, Foo), _foo_func)])
dispatch.register_cls_list([(Foo, _foo_cls)])
assert dispatch.dispatch(Foo) == _foo_cls