File: test_multistrategy_dispatch.py

package info (click to toggle)
python-cattrs 25.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,812 kB
  • sloc: python: 12,236; makefile: 155
file content (50 lines) | stat: -rw-r--r-- 1,244 bytes parent folder | download | duplicates (2)
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