Esempio n. 1
0
def test_partial_adaptation():
    hook1 = Hook()
    hook2 = Hook()

    @hook2
    def first(event):
        event.before_called = True

    @hook2
    def third(event):
        assert event.handler_called
        event.after_called = True

    @hook1
    @yielding
    def handler(event):
        event.handler_called = True

        event = yield hook2(after=first, before=third)

        assert event.before_called
        event.handler_called = True
        
    first_event = hook1.fire()
    assert first_event.handler_called

    second_event = hook2.fire()
    assert second_event.after_called
Esempio n. 2
0
def test_deferred_adaptation():
    hook1 = Hook()

    @hook1
    @yielding
    def handler(event):
        event.handler_called = True
        deferred_result = yield event.deferred
        deferred_result.handler_called = True

    event = hook1.fire(deferred=Deferred())

    newevent = AttrDict()
    event.deferred.callback(newevent)
    assert newevent.handler_called
Esempio n. 3
0
def test_deferred_adaptation():
    hook1 = Hook()

    @hook1
    @yielding
    def handler(event):
        event.handler_called = True
        deferred_result = yield event.deferred
        deferred_result.handler_called = True

    event = hook1.fire(deferred=Deferred())

    newevent = AttrDict()
    event.deferred.callback(newevent)
    assert newevent.handler_called
Esempio n. 4
0
    def test_name_added_hook(self):
        hooktree = HookTree(name="hooktree_name")

        hook = Hook()
        assert "hooktree_name" not in repr(hook)
        hooktree.addhook("hook", hook)
        assert "hooktree_name.hook" in repr(hook)
Esempio n. 5
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. 6
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. 7
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. 8
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. 9
0
def test_protocol(monkeypatch):
    conn_hooks = AttrDict()
    monkeypatch.setattr(hook, "connection", conn_hooks)

    delimiter_sentinel = object()
    server = AttrDict(delimiter=delimiter_sentinel)
    factory = main.ConnectionFactory(server)
    assert factory.server is server

    protocol = factory.buildProtocol("irc.example.net")
    assert server._reactor_connection is protocol
    assert protocol.server is server
    assert protocol.delimiter is server.delimiter

    conn_hooks.made = Hook()
    result = AttrDict()
    updated = Counter()

    @conn_hooks.made
    def onmade(event):
        result.update(event)
        updated.tick()

    protocol.connectionMade()
    assert updated.incremented(1)

    # goes out of its way to not assert that the connection
    # object is actually what is passed into the event call
    @result.conn.received
    def received(event):
        assert event.line == "incoming line"
        event.received = True
        event.command = "command"

    commands = Counter()

    @result.conn.received("command")
    def received_command(event):
        assert event.received == True
        commands.tick()

    protocol.lineReceived("incoming line")
    assert commands.incremented(1)

    disconnect_count = Counter()
    reason_sentinel = object()

    @result.conn.disconnect
    def disconnected(event):
        assert event.reason is reason_sentinel
        disconnect_count.tick()

    protocol.connectionLost(reason_sentinel)
    assert disconnect_count.incremented(1)
Esempio n. 10
0
def test_partial_adaptation():
    hook1 = Hook()
    hook2 = Hook()

    @hook2
    def first(event):
        event.before_called = True

    @hook2
    def third(event):
        assert event.handler_called
        event.after_called = True

    @hook1
    @yielding
    def handler(event):
        event.handler_called = True

        event = yield hook2(after=first, before=third)

        assert event.before_called
        event.handler_called = True

    first_event = hook1.fire()
    assert first_event.handler_called

    second_event = hook2.fire()
    assert second_event.after_called
Esempio n. 11
0
    def test_child_addhook(self):
        hooktree = HookTree(name="hooktree_name", start_lazy=True)

        thehook = Hook()
        hooktree.createsub("herp")
        hooktree.herp.addhook("child", thehook)

        assert "hooktree_name.child" not in repr(thehook)

        hooktree._unlazy()

        assert "hooktree_name.child" not in repr(thehook)
        assert isinstance(hooktree.herp.child, Hook)
Esempio n. 12
0
 def __init__(self):
     super(ProtocolMultiplexer, self).__init__(preparer=Hook(),
                                               hook_class=CommandHook,
                                               raise_on_noname=False,
                                               childarg="command")
Esempio n. 13
0
    def test_dont_name_added_hook(self):
        hooktree = HookTree()

        hook = Hook()
        hooktree.addhook("hook", hook, name_child=False)
        assert "hooktree" not in repr(hook)
Esempio n. 14
0
from crow2 import hook, log
from crow2.lib import config
from crow2.events.hook import Hook
from crow2.events.hooktree import InstanceHook, HookMultiplexer, CommandHook

example_connection = {
    "server": "irc.example.net",
    "nick": "example",
    "user": "******",
    "realname": "example",
    "channels": ["#changeme"]
}

hook.createsub("connection")
hook.connection.addhook("made", Hook())


class ProtocolMultiplexer(HookMultiplexer):
    def __init__(self):
        super(ProtocolMultiplexer, self).__init__(preparer=Hook(),
                                                  hook_class=CommandHook,
                                                  raise_on_noname=False,
                                                  childarg="command")

    def _get_or_create_child(self, handler, name):
        if not name:
            return self.preparer
        else:
            return super(ProtocolMultiplexer,
                         self)._get_or_create_child(handler, name)
Esempio n. 15
0
def test_integration():
    hook1 = Hook()
    hook2 = Hook()
    hook_method = Hook()

    @handlerclass(hook1)
    @handlerclass(hook2)
    class Clazz(object):
        def __init__(self, event):
            event.counter.tick()

        @handlermethod(hook_method)
        def a_method(self, event):
            event.other_counter.tick()

        @instancehandler.hook(tag="derp")
        def a_handler(self, event):
            event.counter2.tick()

        @instancehandler.hook_destroy
        def destroy(self, event):
            self.delete()

    result = hook_method.fire(other_counter=Counter())
    assert result.other_counter.incremented(0)

    hook1_result = hook1.fire(hook=Hook(), hook_destroy=Hook(), counter=Counter())
    hook2_result = hook2.fire(hook=Hook(), hook_destroy=Hook(), counter=Counter())
    assert hook1_result.counter.incremented(1)
    assert hook2_result.counter.incremented(1)

    result = hook_method.fire(other_counter=Counter())
    assert result.other_counter.incremented(2)

    print hook2_result.hook
    hook3_result = hook2_result.hook.fire(counter2=Counter())
    assert hook3_result.counter2.incremented(1)

    hook1_result.hook_destroy.fire()
    result = hook_method.fire(other_counter=Counter())
    assert result.other_counter.incremented(1)

    hook2_result.hook_destroy.fire()
    result = hook_method.fire(other_counter=Counter())
    assert result.other_counter.incremented(0)
    hook3_result = hook2_result.hook.fire(counter2=Counter())
    assert hook3_result.counter2.incremented(0)
Esempio n. 16
0
 class HasHook(object):
     hook = InstanceHook(
         hook_class=lambda: HookMultiplexer(preparer=Hook()))
Esempio n. 17
0
    def test_class_registration(self):
        class ProxiedClass(object):
            def __init__(self):
                self.counter = Counter()
            proxied_method = proxied_method

        class ProxiedClass2(object):
            def __init__(self):
                self.counter = Counter()
            proxied_method = proxied_method

        proxy = HookMethodProxy(proxied_method)

        hook0 = Hook()
        hook1 = Hook()

        instance1 = ProxiedClass()
        instance2 = ProxiedClass2()

        # simply adding the hook just indicates that
        # when a class is ready to use the proxy,
        # the hook will be registered; it doesn't
        # do anything else
        proxy.addhook(hook0, (), {})
        hook0.fire()
        assert instance1.counter.incremented(0)

        proxy.register(ProxiedClass, "proxied_method")
        proxy.add_bound_method(instance1.proxied_method, None)
        hook0.fire()
        assert instance1.counter.incremented(1)

        with pytest.raises(exceptions.AlreadyRegisteredError):
            proxy.register(ProxiedClass, "proxied_method")

        proxy.addhook(hook1, (), {})
        hook1.fire()
        assert instance1.counter.incremented(1)

        proxy.register(ProxiedClass2, "proxied_method")
        proxy.add_bound_method(instance2.proxied_method, None)
        hook1.fire()
        assert instance2.counter.incremented(1)
        assert instance1.counter.incremented(1)

        proxy.remove_bound_method(instance1.proxied_method)
        proxy.unregister(ProxiedClass, "proxied_method")
        hook1.fire()
        assert instance2.counter.incremented(1)
        assert instance1.counter.incremented(0)

        proxy.remove_bound_method(instance2.proxied_method)
        proxy.unregister(ProxiedClass2, "proxied_method")

        with pytest.raises(exceptions.NotRegisteredError):
            proxy.unregister(ProxiedClass2, "proxied_method")

        hook1.fire()
        assert instance2.counter.incremented(0)
        assert instance1.counter.incremented(0)
Esempio n. 18
0
def test_simple():
    """
    "Simple" test of yielding
    """
    hook1 = Hook()
    hook2 = Hook()

    @hook1
    @yielding
    def handler(event):
        "yielding handler"
        while "derp" in event:
            event = yield event.derp
        event.was_called = True

    assert handler #shut up, pylint

    # when we pass nothing in, it doesn't see "derp" in event and so just returns
    assert "was_called" in hook1.fire()

    # nothing is registered to hook2 at the moment
    assert not "was_called" in hook2.fire()

    # when we pass hook2 into hook1's handler, it yields it, and so hook1's event never gets modified
    assert not "was_called" in hook1.fire(derp=hook2)
    # now, since hook2 was yielded from hook1's handler, when we fire hook2
    # with no arguments it's event gets modified
    assert "was_called" in hook2.fire()

    # but if we call hook2 again, nothing happens, since the yield handler finished
    assert not "was_called" in hook2.fire()

    # now we pass back and forth just to be sure it works
    assert not "was_called" in hook1.fire(derp=hook2)
    assert not "was_called" in hook2.fire(derp=hook1)
    assert not "was_called" in hook1.fire(derp=hook2)
    # aaand, call without arguments. are you still there, handler?
    assert "was_called" in hook2.fire()
Esempio n. 19
0
def test_simple():
    """
    "Simple" test of yielding
    """
    hook1 = Hook()
    hook2 = Hook()

    @hook1
    @yielding
    def handler(event):
        "yielding handler"
        while "derp" in event:
            event = yield event.derp
        event.was_called = True

    assert handler  #shut up, pylint

    # when we pass nothing in, it doesn't see "derp" in event and so just returns
    assert "was_called" in hook1.fire()

    # nothing is registered to hook2 at the moment
    assert not "was_called" in hook2.fire()

    # when we pass hook2 into hook1's handler, it yields it, and so hook1's event never gets modified
    assert not "was_called" in hook1.fire(derp=hook2)
    # now, since hook2 was yielded from hook1's handler, when we fire hook2
    # with no arguments it's event gets modified
    assert "was_called" in hook2.fire()

    # but if we call hook2 again, nothing happens, since the yield handler finished
    assert not "was_called" in hook2.fire()

    # now we pass back and forth just to be sure it works
    assert not "was_called" in hook1.fire(derp=hook2)
    assert not "was_called" in hook2.fire(derp=hook1)
    assert not "was_called" in hook1.fire(derp=hook2)
    # aaand, call without arguments. are you still there, handler?
    assert "was_called" in hook2.fire()