File: test_function_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 (33 lines) | stat: -rw-r--r-- 754 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
from cattrs import BaseConverter
from cattrs.dispatch import FunctionDispatch


def test_function_dispatch():
    dispatch = FunctionDispatch(BaseConverter())

    assert dispatch.dispatch(float) is None

    test_func = object()

    dispatch.register(lambda cls: issubclass(cls, float), test_func)

    assert dispatch.dispatch(float) == test_func


def test_function_clears_cache_after_function_added():
    dispatch = FunctionDispatch(BaseConverter())

    class Foo:
        pass

    Foo()

    class Bar(Foo):
        pass

    Bar()

    dispatch.register(lambda cls: issubclass(cls, Foo), "foo")
    assert dispatch.dispatch(Bar) == "foo"
    dispatch.register(lambda cls: issubclass(cls, Bar), "bar")
    assert dispatch.dispatch(Bar) == "bar"