def test_cancellable_preparer(self): hook = HookMultiplexer(preparer=CancellableHook()) @hook def derp(event): assert getattr(event, "cancel", None) == None assert event.cancelled == False hook.fire(name="derp")
def test_missing_hook(self): hook = HookMultiplexer(missing=Hook()) missings = [] @hook.missing def on_missing(event): missings.append((event.name, event.name)) hook.fire(name="nonexistant") assert missings == [("nonexistant", "nonexistant")]
def test_command_nameinfer(self): hook = HookMultiplexer() counter = Counter() @hook def derp(event): counter.tick() hook.fire(name="derp") assert counter.count == 1
def test_custom_childarg(self): hook = HookMultiplexer(childarg="subject_home_town_here") counter = Counter() @hook def child(event): counter.tick() hook.fire(subject_home_town_here="child") assert counter.incremented(1)
def test_explicit_name(self): hook = HookMultiplexer() counter = Counter() @hook("herp") def derp(event): counter.tick() hook.fire(name="herp") assert counter.count == 1 with pytest.raises(NameResolutionError): hook.fire(name="derp")
def test_unregister_deletion(self): hook = HookMultiplexer() @hook def handler(event): event.handled = True event = hook.fire(name="handler") assert event.handled hook.unregister(handler) with pytest.raises(NameResolutionError): hook.fire(name="handler")
def test_register_once_explicitname(self): hook = HookMultiplexer() counter = Counter() def handler(event): counter.tick() hook.register_once(handler, "handler_name") hook.fire(name="handler_name") assert counter.incremented(1) with pytest.raises(NameResolutionError): hook.fire(name="handler_name")
def test_preparer(self): hook = HookMultiplexer(preparer=Hook()) counter1 = Counter() counter2 = Counter() counter_mapped = Counter() counter_preparer = Counter() @hook def derp(event): counter1.tick() @hook("herp") def herp(event): counter2.tick() mapping = { "dink": "herp", "donk": "derp" } @hook.preparer def prepare(event): if event.name in mapping: event.name = mapping[event.name] counter_mapped.tick() counter_preparer.tick() assert counter1.count == 0 assert counter2.count == 0 assert counter_preparer.count == 0 assert counter_mapped.count == 0 hook.fire(name="dink") assert counter1.count == 0 assert counter2.count == 1 assert counter_preparer.count == 1 assert counter_mapped.count == 1 hook.fire(name="donk") assert counter1.count == 1 assert counter2.count == 1 assert counter_preparer.count == 2 assert counter_mapped.count == 2 hook.fire(name="herp") assert counter1.count == 1 assert counter2.count == 2 assert counter_preparer.count == 3 assert counter_mapped.count == 2 hook.fire(name="derp") assert counter1.count == 2 assert counter2.count == 2 assert counter_preparer.count == 4 assert counter_mapped.count == 2
def test_preparer(self): hook = HookMultiplexer(preparer=Hook()) counter1 = Counter() counter2 = Counter() counter_mapped = Counter() counter_preparer = Counter() @hook def derp(event): counter1.tick() @hook("herp") def herp(event): counter2.tick() mapping = {"dink": "herp", "donk": "derp"} @hook.preparer def prepare(event): if event.name in mapping: event.name = mapping[event.name] counter_mapped.tick() counter_preparer.tick() assert counter1.count == 0 assert counter2.count == 0 assert counter_preparer.count == 0 assert counter_mapped.count == 0 hook.fire(name="dink") assert counter1.count == 0 assert counter2.count == 1 assert counter_preparer.count == 1 assert counter_mapped.count == 1 hook.fire(name="donk") assert counter1.count == 1 assert counter2.count == 1 assert counter_preparer.count == 2 assert counter_mapped.count == 2 hook.fire(name="herp") assert counter1.count == 1 assert counter2.count == 2 assert counter_preparer.count == 3 assert counter_mapped.count == 2 hook.fire(name="derp") assert counter1.count == 2 assert counter2.count == 2 assert counter_preparer.count == 4 assert counter_mapped.count == 2
def test_multi_unregister(self): hook = HookMultiplexer() @hook def handler1(event): should_never_run() @hook def handler2(event): event.handler2 = True hook.unregister(handler1) event = hook.fire(name="handler2") assert event.handler2 with pytest.raises(NameResolutionError): hook.fire(name="handler1")
def test_missing_hook_event_return(self): hook = HookMultiplexer(missing=Hook()) @hook.missing def missing(event): event.result = "result" event = hook.fire(name="nonexistant") assert event.result == "result"
def test_missing_preparer_event_return(self): hook = HookMultiplexer(raise_on_missing=False, preparer=Hook()) @hook.preparer def preparer(event): event.result = "result" event = hook.fire(name="missing") assert event.result == "result"
def test_command_event_return(self): hook = HookMultiplexer() @hook def command(event): event.result = "result" event = hook.fire(name="command") assert event.result == "result"
def test_childhook_repr(): hook = HookMultiplexer(name="special_testing_name") @hook def handler(event): assert "special_testing_name" in repr(event.calling_hook) assert "handler" in repr(event.calling_hook) event.ran = True event = hook.fire(name="handler") assert event.ran
def test_cancelled_preparer(self): hook = HookMultiplexer(preparer=CancellableHook()) @hook def derp(event): should_never_run() @hook.preparer def preparer(event): event.cancel() result = hook.fire(name="derp") assert result.cancelled
def test_not_registered_unregister(self): hook = HookMultiplexer() @hook def handler1(event): event.handler1 = True def handler2(event): should_never_run() assert hook.fire(name="handler1").handler1 with pytest.raises(NotRegisteredError): hook.unregister(handler2)
def test_beforeafter_handlers(self): hook = HookMultiplexer(hook_class=CommandHook) @hook("command", before="main") def first(event): event.first = True @hook def command(event): assert event.first event.command = True @hook("command", after="main") def after(event): assert event.command event.after = True event = hook.fire(name="command") assert event.after
def test_multi_unregister_samehook(self): hook = HookMultiplexer() counter = Counter() @hook("derp") def handler1(event): counter.tick() @hook("derp") def handler2(event): counter.tick() hook.fire(name="derp") assert counter.incremented(2) hook.unregister(handler1) hook.fire(name="derp") assert counter.incremented(1) hook.unregister(handler2) with pytest.raises(NameResolutionError): hook.fire(name="derp")
def test_required_childname(self): hook = HookMultiplexer() with pytest.raises(TypeError): hook.fire()
def test_optional_childname(self): hook = HookMultiplexer(raise_on_noname=False) with pytest.raises(NameResolutionError): hook.fire()
def test_pass_on_missing(self): hook = HookMultiplexer(raise_on_missing=False) hook.fire(name="missing")
def test_missing_exception(self): hook = HookMultiplexer() with pytest.raises(NameResolutionError): hook.fire(name="")