示例#1
0
    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")
示例#2
0
    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")
示例#3
0
    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")]
示例#4
0
    def test_command_nameinfer(self):
        hook = HookMultiplexer()
        counter = Counter()

        @hook
        def derp(event):
            counter.tick()

        hook.fire(name="derp")
        assert counter.count == 1
示例#5
0
    def test_command_nameinfer(self):
        hook = HookMultiplexer()
        counter = Counter()

        @hook
        def derp(event):
            counter.tick()

        hook.fire(name="derp")
        assert counter.count == 1
示例#6
0
    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)
示例#7
0
    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")]
示例#8
0
    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)
示例#9
0
    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")
示例#10
0
    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")
示例#11
0
    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")
示例#12
0
    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")
示例#13
0
    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")
示例#14
0
    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")
示例#15
0
    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
示例#16
0
    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
示例#17
0
    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")
示例#18
0
    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")
示例#19
0
    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"
示例#20
0
    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"
示例#21
0
    def test_command_event_return(self):
        hook = HookMultiplexer()

        @hook
        def command(event):
            event.result = "result"

        event = hook.fire(name="command")
        assert event.result == "result"
示例#22
0
    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"
示例#23
0
    def test_command_event_return(self):
        hook = HookMultiplexer()

        @hook
        def command(event):
            event.result = "result"

        event = hook.fire(name="command")
        assert event.result == "result"
示例#24
0
    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"
示例#25
0
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
示例#26
0
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
示例#27
0
    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
示例#28
0
    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
示例#29
0
    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)
示例#30
0
    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)
示例#31
0
    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
示例#32
0
    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
示例#33
0
    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")
示例#34
0
    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")
示例#35
0
    def test_required_childname(self):
        hook = HookMultiplexer()

        with pytest.raises(TypeError):
            hook.fire()
示例#36
0
    def test_optional_childname(self):
        hook = HookMultiplexer(raise_on_noname=False)

        with pytest.raises(NameResolutionError):
            hook.fire()
示例#37
0
    def test_pass_on_missing(self):
        hook = HookMultiplexer(raise_on_missing=False)

        hook.fire(name="missing")
示例#38
0
    def test_required_childname(self):
        hook = HookMultiplexer()

        with pytest.raises(TypeError):
            hook.fire()
示例#39
0
    def test_optional_childname(self):
        hook = HookMultiplexer(raise_on_noname=False)

        with pytest.raises(NameResolutionError):
            hook.fire()
示例#40
0
    def test_pass_on_missing(self):
        hook = HookMultiplexer(raise_on_missing=False)

        hook.fire(name="missing")
示例#41
0
    def test_missing_exception(self):
        hook = HookMultiplexer()

        with pytest.raises(NameResolutionError):
            hook.fire(name="")
示例#42
0
    def test_missing_exception(self):
        hook = HookMultiplexer()

        with pytest.raises(NameResolutionError):
            hook.fire(name="")