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"
|