Exemplo n.º 1
0
    def test_name_collision(self):
        hooktree = HookTree()

        hooktree.createhook("child")

        with pytest.raises(AlreadyRegisteredError):
            hooktree.createhook("child")
Exemplo n.º 2
0
    def test_lazycall_attributeerror(self):
        hooktree = HookTree(name="hooktree_name", start_lazy=True)

        hooktree.doesnt_exist.createhook("arg1", 2, three="four")

        with pytest.raises(AttributeError):
            hooktree._unlazy()
Exemplo n.º 3
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)
Exemplo n.º 4
0
    def test_createhook_lazy(self):
        hooktree = HookTree(start_lazy=True)
        hooktree.createhook("child_hook")
        assert not isinstance(hooktree.child_hook, Hook)

        hooktree._unlazy()
        assert isinstance(hooktree.child_hook, Hook)
Exemplo n.º 5
0
    def test_lazycall_attributeerror(self):
        hooktree = HookTree(name="hooktree_name", start_lazy=True)

        hooktree.doesnt_exist.createhook("arg1", 2, three="four")

        with pytest.raises(AttributeError):
            hooktree._unlazy()
Exemplo n.º 6
0
    def test_hook_use(self):
        hooktree = HookTree(start_lazy=True)

        hooktree.createhook("child_hook", default_tags=("first", "third"))
        @hooktree.child_hook(tag="first")
        def first_handler(event):
            event.first = True

        hooktree.child_hook.tag("second", after="first", before="third")
        @hooktree.child_hook(tag="second")
        def second_handler(event):
            assert event.first
            event.second = True

        @hooktree.child_hook(tag="third")
        def third_handler(event):
            assert event.second
            event.third = True

        @hooktree.child_hook(after="third", before="simple_handler")
        def fourth_handler(event):
            assert event.third
            event.fourth = True

        @hooktree.child_hook
        def simple_handler(event):
            assert event.fourth
            event.simple = True

        hooktree._unlazy()

        event = hooktree.child_hook.fire()
        assert event.simple
Exemplo n.º 7
0
    def test_hook_misplaced(self):
        hooktree = HookTree(name="named_hooktree")

        # TODO: this is actually a Hook test too
        hooktree.createhook("child", hook_class=Hook)

        assert "named_hooktree.child" in repr(hooktree.child)
Exemplo n.º 8
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)
Exemplo n.º 9
0
    def test_hook_misplaced(self):
        hooktree = HookTree(name="named_hooktree")

        # TODO: this is actually a Hook test too
        hooktree.createhook("child", hook_class=Hook)

        assert "named_hooktree.child" in repr(hooktree.child)
Exemplo n.º 10
0
    def test_createhook_customclass_lazy(self):
        hooktree = HookTree(start_lazy=True)
        hooktree.createhook("child_hook", hook_class=CancellableHook)
        assert not isinstance(hooktree.child_hook, CancellableHook)

        hooktree._unlazy()
        assert isinstance(hooktree.child_hook, CancellableHook)
Exemplo n.º 11
0
    def test_invalid_ordering_special(self):
        hooktree = HookTree(start_lazy=True)

        hooktree.herp.createsub("derp")
        hooktree.createsub("herp")

        with pytest.raises(AttributeError):
            hooktree._unlazy()
Exemplo n.º 12
0
    def test_createhook_lazyness(self):
        counter = Counter()
        hooktree = HookTree(start_lazy=True, hook_class=counter.tick)
        hooktree.createhook('child_hook')
        assert counter.incremented(0)

        hooktree._unlazy()
        assert counter.incremented(1)
Exemplo n.º 13
0
    def test_addhook(self):
        hooktree = HookTree(start_lazy=True)
        sentinel = object()
        result = hooktree.addhook("child_hook", sentinel)
        assert result is sentinel
        assert hooktree.child_hook is not sentinel

        hooktree._unlazy()
        assert hooktree.child_hook is sentinel
Exemplo n.º 14
0
    def test_addhook(self):
        hooktree = HookTree(start_lazy=True)
        sentinel = object()
        result = hooktree.addhook("child_hook", sentinel)
        assert result is sentinel
        assert hooktree.child_hook is not sentinel

        hooktree._unlazy()
        assert hooktree.child_hook is sentinel
Exemplo n.º 15
0
    def test_instantiatehook(self):
        hooktree = HookTree(start_lazy=True)

        @hooktree.instantiatehook("instantiated")
        class HookToInstantiate(object):
            pass

        assert not isinstance(hooktree.instantiated, HookToInstantiate)

        hooktree._unlazy()
        assert isinstance(hooktree.instantiated, HookToInstantiate)
Exemplo n.º 16
0
    def test_instantiatehook(self):
        hooktree = HookTree(start_lazy=True)

        @hooktree.instantiatehook("instantiated")
        class HookToInstantiate(object):
            pass

        assert not isinstance(hooktree.instantiated, HookToInstantiate)

        hooktree._unlazy()
        assert isinstance(hooktree.instantiated, HookToInstantiate)
Exemplo n.º 17
0
    def test_sub_instantiatehook(self):
        hooktree = HookTree(start_lazy=True)

        hooktree.createsub("child")

        @hooktree.child.instantiatehook("somehook")
        class Herp(object):
            pass

        hooktree._unlazy()

        assert isinstance(hooktree.child.somehook, Herp)
Exemplo n.º 18
0
    def test_exception_in_hookinit(self):
        class SentinelException(Exception):
            pass

        def raiseexception():
            raise SentinelException()

        hooktree = HookTree(hook_class=raiseexception, start_lazy=True)

        hooktree.createhook("child")

        with pytest.raises(ExceptionInCallError):
            hooktree._unlazy()
Exemplo n.º 19
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)
Exemplo n.º 20
0
    def test_ordering(self):
        hooktree = HookTree(start_lazy=True)
        
        @hooktree.doesnt.exist.yet.hook
        def handler(event):
            event.ran = True

        hooktree.createsub("doesnt")
        hooktree.doesnt.createsub("exist")
        hooktree.doesnt.exist.createsub("yet")
        hooktree.doesnt.exist.yet.createhook("hook")

        hooktree._unlazy()
        
        event = hooktree.doesnt.exist.yet.hook.fire()
        assert event.ran
Exemplo n.º 21
0
    def test_name_collision(self):
        hooktree = HookTree()

        hooktree.createhook("child")

        with pytest.raises(AlreadyRegisteredError):
            hooktree.createhook("child")
Exemplo n.º 22
0
    def test_createhook_lazy(self):
        hooktree = HookTree(start_lazy=True)
        hooktree.createhook("child_hook")
        assert not isinstance(hooktree.child_hook, Hook)

        hooktree._unlazy()
        assert isinstance(hooktree.child_hook, Hook)
Exemplo n.º 23
0
    def test_hook_use(self):
        hooktree = HookTree(start_lazy=True)

        hooktree.createhook("child_hook", default_tags=("first", "third"))

        @hooktree.child_hook(tag="first")
        def first_handler(event):
            event.first = True

        hooktree.child_hook.tag("second", after="first", before="third")

        @hooktree.child_hook(tag="second")
        def second_handler(event):
            assert event.first
            event.second = True

        @hooktree.child_hook(tag="third")
        def third_handler(event):
            assert event.second
            event.third = True

        @hooktree.child_hook(after="third", before="simple_handler")
        def fourth_handler(event):
            assert event.third
            event.fourth = True

        @hooktree.child_hook
        def simple_handler(event):
            assert event.fourth
            event.simple = True

        hooktree._unlazy()

        event = hooktree.child_hook.fire()
        assert event.simple
Exemplo n.º 24
0
    def test_createhook_customclass_lazy(self):
        hooktree = HookTree(start_lazy=True)
        hooktree.createhook("child_hook", hook_class=CancellableHook)
        assert not isinstance(hooktree.child_hook, CancellableHook)

        hooktree._unlazy()
        assert isinstance(hooktree.child_hook, CancellableHook)
Exemplo n.º 25
0
    def test_invalid_ordering_special(self):
        hooktree = HookTree(start_lazy=True)

        hooktree.herp.createsub("derp")
        hooktree.createsub("herp")

        with pytest.raises(AttributeError):
            hooktree._unlazy()
Exemplo n.º 26
0
    def test_createhook_lazyness(self):
        counter = Counter()
        hooktree = HookTree(start_lazy=True, hook_class=counter.tick)
        hooktree.createhook('child_hook')
        assert counter.incremented(0)

        hooktree._unlazy()
        assert counter.incremented(1)
Exemplo n.º 27
0
def test_main(monkeypatch):
    # don't want plugins messing with it to make permanent changes
    hook = HookTree(start_lazy=True)

    trackers = []
    monkeypatch.setattr(crow2.plugin, "Tracker",
                        functools.partial(DummyTracker, trackers))

    main = crow2.main.Main(hook, "core", ["pluginset", "pluginset2"])

    assert len(trackers) == 3
    core, pluginset, pluginset2 = trackers
    assert core.name == "core"
    assert pluginset.name == "pluginset"
    assert pluginset2.name == "pluginset2"
    assert all(tracker.loaded for tracker in trackers)

    mainloop_exitcodes = []

    @hook.init
    def init(event):
        assert event.main == main
        event.main.initialized = True

    @hook.mainloop
    def mainloop(event):
        assert event.main.initialized
        do_stuff(event)
        assert event.didnt_stop_immediately
        assert mainloop_exitcodes == [0]
        event.main.mainloop_ran = True

    def do_stuff(event):
        event.main.quit()
        event.didnt_stop_immediately = True

    @hook.stopmainloop
    def stopmainloop(event):
        mainloop_exitcodes.append(event.exitcode)

    @hook.deinit
    def deinit(event):
        assert event.main.mainloop_ran
        event.main.deinit_ran = True

    main.run()
    assert main.deinit_ran
Exemplo n.º 28
0
    def test_instantiatehook(self):
        hooktree = HookTree()

        @hooktree.instantiatehook("child_hook")
        @hooktree.instantiatehook("child_hook_2",
                                  "herp",
                                  "derp",
                                  "whee",
                                  dink="donk")
        class WhackyHookSubclass(object):
            def __init__(self, *args, **keywords):
                self.args = args
                self.keywords = keywords

        assert isinstance(hooktree.child_hook, WhackyHookSubclass)
        assert hooktree.child_hook_2.args == ("herp", "derp", "whee")
        assert hooktree.child_hook_2.keywords == {"dink": "donk"}
Exemplo n.º 29
0
    def test_sub_instantiatehook(self):
        hooktree = HookTree(start_lazy=True)

        hooktree.createsub("child")

        @hooktree.child.instantiatehook("somehook")
        class Herp(object):
            pass

        hooktree._unlazy()

        assert isinstance(hooktree.child.somehook, Herp)
Exemplo n.º 30
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)
Exemplo n.º 31
0
    def test_exception_in_hookinit(self):
        class SentinelException(Exception):
            pass

        def raiseexception():
            raise SentinelException()

        hooktree = HookTree(hook_class=raiseexception, start_lazy=True)

        hooktree.createhook("child")

        with pytest.raises(ExceptionInCallError):
            hooktree._unlazy()
Exemplo n.º 32
0
    def test_ordering(self):
        hooktree = HookTree(start_lazy=True)

        @hooktree.doesnt.exist.yet.hook
        def handler(event):
            event.ran = True

        hooktree.createsub("doesnt")
        hooktree.doesnt.createsub("exist")
        hooktree.doesnt.exist.createsub("yet")
        hooktree.doesnt.exist.yet.createhook("hook")

        hooktree._unlazy()

        event = hooktree.doesnt.exist.yet.hook.fire()
        assert event.ran
Exemplo n.º 33
0
 def test_createsub(self):
     hooktree = HookTree()
     hooktree.createsub("child_hooktree")
     assert isinstance(hooktree.child_hooktree, HookTree)
     assert hooktree.child_hooktree is not hooktree
Exemplo n.º 34
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)
Exemplo n.º 35
0
    def test_invalid_unlazy(self):
        hooktree = HookTree()

        with pytest.raises(AlreadyRegisteredError):
            hooktree._unlazy()
Exemplo n.º 36
0
 def test_createhook_customclass(self):
     hooktree = HookTree()
     hooktree.createhook("child_hook", hook_class=CancellableHook)
     assert isinstance(hooktree.child_hook, CancellableHook)
Exemplo n.º 37
0
 def test_createhook(self):
     hooktree = HookTree()
     hooktree.createhook("child_hook")
     assert isinstance(hooktree.child_hook, Hook)
Exemplo n.º 38
0
 def test_createhook(self):
     hooktree = HookTree()
     hooktree.createhook("child_hook")
     assert isinstance(hooktree.child_hook, Hook)
Exemplo n.º 39
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)
Exemplo n.º 40
0
 def test_missing_child(self):
     hooktree = HookTree()
     with pytest.raises(AttributeError):
         hooktree.derp
Exemplo n.º 41
0
    def test_name(self):
        hooktree = HookTree(name="named_hooktree")

        assert "named_hooktree" in repr(hooktree)
Exemplo n.º 42
0
 def test_createhook_customclass(self):
     hooktree = HookTree()
     hooktree.createhook("child_hook", hook_class=CancellableHook)
     assert isinstance(hooktree.child_hook, CancellableHook)
Exemplo n.º 43
0
 def test_addhook(self):
     hooktree = HookTree()
     sentinel = object()
     result = hooktree.addhook("child_hook", sentinel)
     assert hooktree.child_hook is sentinel
     assert result is sentinel
Exemplo n.º 44
0
 def test_createsub(self):
     hooktree = HookTree()
     hooktree.createsub("child_hooktree")
     assert isinstance(hooktree.child_hooktree, HookTree)
     assert hooktree.child_hooktree is not hooktree
Exemplo n.º 45
0
    def test_invalid_unlazy(self):
        hooktree = HookTree()

        with pytest.raises(AlreadyRegisteredError):
            hooktree._unlazy()
Exemplo n.º 46
0
 def test_addhook(self):
     hooktree = HookTree()
     sentinel = object()
     result = hooktree.addhook("child_hook", sentinel)
     assert hooktree.child_hook is sentinel
     assert result is sentinel