Esempio n. 1
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"
Esempio n. 2
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"
Esempio n. 3
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"
Esempio n. 4
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"
Esempio n. 5
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"
Esempio n. 6
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")
Esempio n. 7
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"
Esempio n. 8
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")
Esempio n. 9
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
Esempio n. 10
0
    def test_command_nameinfer(self):
        hook = HookMultiplexer()
        counter = Counter()

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

        hook.fire(name="derp")
        assert counter.count == 1
Esempio n. 11
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")]
Esempio n. 12
0
    def test_command_nameinfer(self):
        hook = HookMultiplexer()
        counter = Counter()

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

        hook.fire(name="derp")
        assert counter.count == 1
Esempio n. 13
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)
Esempio n. 14
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
Esempio n. 15
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")]
Esempio n. 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
Esempio n. 17
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)
Esempio n. 18
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
Esempio n. 19
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")
Esempio n. 20
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
Esempio n. 21
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
Esempio n. 22
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)
Esempio n. 23
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)
Esempio n. 24
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
Esempio n. 25
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
Esempio n. 26
0
    def test_single_main_handler(self):
        hook = HookMultiplexer(hook_class=CommandHook)

        @hook("my_command")
        def my_command(event):
            should_never_run()

        with pytest.raises(AlreadyRegisteredError):

            @hook("my_command")
            def my_conflict(event):
                should_never_run()
Esempio n. 27
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")
Esempio n. 28
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")
Esempio n. 29
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")
Esempio n. 30
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")
Esempio n. 31
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")
Esempio n. 32
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")
Esempio n. 33
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")
Esempio n. 34
0
def test_hookmultiplexer_repr():
    hook = HookMultiplexer(name="special_testing_name")

    assert "special_testing_name" in repr(hook)
Esempio n. 35
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")
Esempio n. 36
0
    def test_optional_childname(self):
        hook = HookMultiplexer(raise_on_noname=False)

        with pytest.raises(NameResolutionError):
            hook.fire()
Esempio n. 37
0
    def test_optional_childname(self):
        hook = HookMultiplexer(raise_on_noname=False)

        with pytest.raises(NameResolutionError):
            hook.fire()
Esempio n. 38
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")
Esempio n. 39
0
    def test_required_childname(self):
        hook = HookMultiplexer()

        with pytest.raises(TypeError):
            hook.fire()
Esempio n. 40
0
    def test_required_childname(self):
        hook = HookMultiplexer()

        with pytest.raises(TypeError):
            hook.fire()
Esempio n. 41
0
    def test_pass_on_missing(self):
        hook = HookMultiplexer(raise_on_missing=False)

        hook.fire(name="missing")
Esempio n. 42
0
    def test_missing_exception(self):
        hook = HookMultiplexer()

        with pytest.raises(NameResolutionError):
            hook.fire(name="")
Esempio n. 43
0
    def test_pass_on_missing(self):
        hook = HookMultiplexer(raise_on_missing=False)

        hook.fire(name="missing")
Esempio n. 44
0
 class HasHook(object):
     hook = InstanceHook(
         hook_class=lambda: HookMultiplexer(preparer=Hook()))
Esempio n. 45
0
    def test_missing_exception(self):
        hook = HookMultiplexer()

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