Exemple #1
0
def test_dispatch():
    global called_default
    called_default = False

    def handler_default(*args, **kwargs):
        global called_default
        called_default = True
        return

    global called
    called = False

    def handler(*args, **kwargs):
        global called
        called = True
        return args[0]

    hooks.register_default("test_hook", handler_default)
    hooks.register("test_hook", handler)

    hooks.dispatch("test_hook", 1)
    assert not called_default
    assert called
    called_default = False
    called = False
    hooks.dispatch("test_hook", None)
    assert called_default
    assert called
Exemple #2
0
def test_register():
    assert "test_hook" not in hooks._handlers
    assert "test_hook" not in hooks._defaults

    with assert_raises(error.InvalidHook):
        hooks.register("test_hook", lambda x: x)

    assert "test_hook" not in hooks._handlers
    assert "test_hook" not in hooks._defaults

    hooks.register_default("test_hook", lambda x: x)

    hooks.register("test_hook", lambda x: x)

    assert "test_hook" in hooks._handlers