def test_name_collision(self): hooktree = HookTree() hooktree.createhook("child") with pytest.raises(AlreadyRegisteredError): hooktree.createhook("child")
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()
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)
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)
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
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)
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)
def test_invalid_ordering_special(self): hooktree = HookTree(start_lazy=True) hooktree.herp.createsub("derp") hooktree.createsub("herp") with pytest.raises(AttributeError): hooktree._unlazy()
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)
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
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)
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)
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()
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)
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
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
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"}
def test_createsub(self): hooktree = HookTree() hooktree.createsub("child_hooktree") assert isinstance(hooktree.child_hooktree, HookTree) assert hooktree.child_hooktree is not hooktree
def test_dont_name_added_hook(self): hooktree = HookTree() hook = Hook() hooktree.addhook("hook", hook, name_child=False) assert "hooktree" not in repr(hook)
def test_invalid_unlazy(self): hooktree = HookTree() with pytest.raises(AlreadyRegisteredError): hooktree._unlazy()
def test_createhook_customclass(self): hooktree = HookTree() hooktree.createhook("child_hook", hook_class=CancellableHook) assert isinstance(hooktree.child_hook, CancellableHook)
def test_createhook(self): hooktree = HookTree() hooktree.createhook("child_hook") assert isinstance(hooktree.child_hook, Hook)
def test_missing_child(self): hooktree = HookTree() with pytest.raises(AttributeError): hooktree.derp
def test_name(self): hooktree = HookTree(name="named_hooktree") assert "named_hooktree" in repr(hooktree)
def test_addhook(self): hooktree = HookTree() sentinel = object() result = hooktree.addhook("child_hook", sentinel) assert hooktree.child_hook is sentinel assert result is sentinel