Exemplo n.º 1
0
    def test_hooks_extra_plugins(self):
        registry = Registry()

        class Api:
            def hello(self, arg):
                pass

        hookrelay = HookRelay(hookspecs=Api, registry=registry)
        hook_hello = hookrelay.hello

        class Plugin:
            def hello(self, arg):
                return arg + 1

        registry.register(Plugin())

        class Plugin2:
            def hello(self, arg):
                return arg + 2

        newhook = hookrelay._makecall("hello", extralookup=Plugin2())
        l = newhook(arg=3)
        assert l == [5, 4]
        l2 = hook_hello(arg=3)
        assert l2 == [4]
Exemplo n.º 2
0
    def test_listattr(self):
        plugins = Registry()

        class api1:
            x = 41

        class api2:
            x = 42

        class api3:
            x = 43

        plugins.register(api1())
        plugins.register(api2())
        plugins.register(api3())
        l = list(plugins.listattr('x'))
        assert l == [41, 42, 43]
        l = list(plugins.listattr('x', reverse=True))
        assert l == [43, 42, 41]

        class api4:
            x = 44

        l = list(plugins.listattr('x', extra=(api4, )))
        assert l == range(41, 45)
        assert len(list(plugins)) == 3  # otherwise extra added
Exemplo n.º 3
0
    def test_firstresult_definition(self):
        registry = Registry()
        class Api:
            def hello(self, arg): pass
            hello.firstresult = True

        mcm = HookRelay(hookspecs=Api, registry=registry)
        class Plugin:
            def hello(self, arg):
                return arg + 1
        registry.register(Plugin())
        res = mcm.hello(arg=3)
        assert res == 4
Exemplo n.º 4
0
    def test_register(self):
        registry = Registry()
        class MyPlugin:
            pass
        my = MyPlugin()
        registry.register(my)
        assert list(registry) == [my]
        my2 = MyPlugin()
        registry.register(my2)
        assert list(registry) == [my, my2]

        assert registry.isregistered(my)
        assert registry.isregistered(my2)
        registry.unregister(my)
        assert not registry.isregistered(my)
        assert list(registry) == [my2]
Exemplo n.º 5
0
    def test_happypath(self):
        registry = Registry()
        class Api:
            def hello(self, arg):
                pass

        mcm = HookRelay(hookspecs=Api, registry=registry)
        assert hasattr(mcm, 'hello')
        assert repr(mcm.hello).find("hello") != -1
        class Plugin:
            def hello(self, arg):
                return arg + 1
        registry.register(Plugin())
        l = mcm.hello(arg=3)
        assert l == [4]
        assert not hasattr(mcm, 'world')
Exemplo n.º 6
0
    def test_firstresult_definition(self):
        registry = Registry()

        class Api:
            def hello(self, arg):
                pass

            hello.firstresult = True

        mcm = HookRelay(hookspecs=Api, registry=registry)

        class Plugin:
            def hello(self, arg):
                return arg + 1

        registry.register(Plugin())
        res = mcm.hello(arg=3)
        assert res == 4
Exemplo n.º 7
0
    def test_only_kwargs(self):
        registry = Registry()

        class Api:
            def hello(self, arg):
                pass

        mcm = HookRelay(hookspecs=Api, registry=registry)
        py.test.raises(TypeError, "mcm.hello(3)")
Exemplo n.º 8
0
    def test_happypath(self):
        registry = Registry()

        class Api:
            def hello(self, arg):
                pass

        mcm = HookRelay(hookspecs=Api, registry=registry)
        assert hasattr(mcm, 'hello')
        assert repr(mcm.hello).find("hello") != -1

        class Plugin:
            def hello(self, arg):
                return arg + 1

        registry.register(Plugin())
        l = mcm.hello(arg=3)
        assert l == [4]
        assert not hasattr(mcm, 'world')
Exemplo n.º 9
0
 def test_hooks_extra_plugins(self):
     registry = Registry()
     class Api:
         def hello(self, arg):
             pass
     hookrelay = HookRelay(hookspecs=Api, registry=registry)
     hook_hello = hookrelay.hello
     class Plugin:
         def hello(self, arg):
             return arg + 1
     registry.register(Plugin())
     class Plugin2:
         def hello(self, arg):
             return arg + 2
     newhook = hookrelay._makecall("hello", extralookup=Plugin2())
     l = newhook(arg=3)
     assert l == [5, 4]
     l2 = hook_hello(arg=3)
     assert l2 == [4]
Exemplo n.º 10
0
    def test_listattr(self):
        plugins = Registry()
        class api1:
            x = 41
        class api2:
            x = 42
        class api3:
            x = 43
        plugins.register(api1())
        plugins.register(api2())
        plugins.register(api3())
        l = list(plugins.listattr('x'))
        assert l == [41, 42, 43]
        l = list(plugins.listattr('x', reverse=True))
        assert l == [43, 42, 41]

        class api4: 
            x = 44
        l = list(plugins.listattr('x', extra=(api4,)))
        assert l == range(41, 45)
        assert len(list(plugins)) == 3  # otherwise extra added
Exemplo n.º 11
0
    def test_register(self):
        registry = Registry()

        class MyPlugin:
            pass

        my = MyPlugin()
        registry.register(my)
        assert list(registry) == [my]
        my2 = MyPlugin()
        registry.register(my2)
        assert list(registry) == [my, my2]

        assert registry.isregistered(my)
        assert registry.isregistered(my2)
        registry.unregister(my)
        assert not registry.isregistered(my)
        assert list(registry) == [my2]