def test_interceptable_override_none(): foo = make_interceptable(lambda: 'nope') with signals.plugin.interceptable_function.connected_to( lambda *a, **kw: None, interceptable_sender(foo)): # simply returning None won't work assert foo() == 'nope' with signals.plugin.interceptable_function.connected_to( lambda *a, RETURN_NONE, **kw: RETURN_NONE, interceptable_sender(foo)): assert foo() is None
def test_interceptable_override_multi(): foo = make_interceptable(lambda a: f'{a=}') with ( signals.plugin.interceptable_function.connected_to( lambda *a, **kw: 'x', interceptable_sender(foo)), signals.plugin.interceptable_function.connected_to( lambda *a, **kw: 'y', interceptable_sender(foo)), ): with pytest.raises(RuntimeError) as exc_info: foo(1) assert str( exc_info.value ) == f'Multiple results returned for interceptable {interceptable_sender(foo)}'
def test_interceptable_multi(): def _handler(sender, func, args, **kwargs): args.arguments['a'] *= 2 def _handler2(sender, func, args, **kwargs): args.arguments['a'] *= 3 foo = make_interceptable(lambda a: a) with signals.plugin.interceptable_function.connected_to( _handler, interceptable_sender(foo)): assert foo(1) == 2 with ( signals.plugin.interceptable_function.connected_to( _handler, interceptable_sender(foo)), signals.plugin.interceptable_function.connected_to( _handler2, interceptable_sender(foo)), ): assert foo(1) == 6
def test_interceptable_key(caller_key, receiver_key): called = False def _handler(sender, func, args, **kwargs): nonlocal called called = True foo = make_interceptable(lambda a: f'{a=}', caller_key) with signals.plugin.interceptable_function.connected_to( _handler, interceptable_sender(foo, receiver_key)): assert foo(1) == 'a=1' assert called == (caller_key == receiver_key)
def test_interceptable_starargs(): def foo(a, *va, **kw): return f'{a=} {va=} {kw=}' def _handler(sender, func, args, **kwargs): args.apply_defaults() assert args.arguments == {'a': 1, 'va': (2, 3), 'kw': {'x': 4}} args.arguments['va'] = (*args.arguments['va'], 33) args.arguments['kw'] = {**args.arguments['kw'], 'y': 5} foo = make_interceptable(foo, 'test') with signals.plugin.interceptable_function.connected_to( _handler, interceptable_sender(foo, 'test')): assert foo(1, 2, 3, x=4) == "a=1 va=(2, 3, 33) kw={'x': 4, 'y': 5}"
def test_interceptable_method_decorated(): class Foo: def __init__(self, x): self.x = x @make_interceptable def bar(self, a): return f'{self.x=} {a=}' def _handler(sender, func, args, ctx, **kwargs): args.arguments['a'] = 'x' with signals.plugin.interceptable_function.connected_to( _handler, interceptable_sender(Foo.bar)): assert Foo(1).bar(2) == "self.x=1 a='x'"
def test_interceptable(): def foo(a, /, b, c=1, *, d=123, **kw): return f'{a=} {b=} {c=} {d=} {kw=}' def _handler(sender, func, args, ctx, **kwargs): args.apply_defaults() args.arguments['c'] = 'ccc' args.arguments['kw'].setdefault('lol', 'funny') if ctx: return {'return': 'value', 'orig': func(*args.args, **args.kwargs)} foo = make_interceptable(foo) foo2 = make_interceptable(foo, ctx=True) with signals.plugin.interceptable_function.connected_to( _handler, interceptable_sender(foo)): assert foo(1, 2) == "a=1 b=2 c='ccc' d=123 kw={'lol': 'funny'}" assert foo(1, 2, lol='test') == "a=1 b=2 c='ccc' d=123 kw={'lol': 'test'}" assert foo2(3, 4) == { 'return': 'value', 'orig': "a=3 b=4 c='ccc' d=123 kw={'lol': 'funny'}" } def test_interceptable_method(): class Foo: def __init__(self, x): self.x = x def bar(self, a):