2009-02-27 18:18:27 +08:00
|
|
|
|
|
|
|
import py
|
|
|
|
import os
|
2009-04-09 22:03:09 +08:00
|
|
|
from py._com import Registry, MultiCall
|
2009-05-08 00:01:53 +08:00
|
|
|
from py._com import Hooks
|
2009-02-27 18:18:27 +08:00
|
|
|
|
|
|
|
pytest_plugins = "xfail"
|
|
|
|
|
|
|
|
class TestMultiCall:
|
2009-03-25 06:00:07 +08:00
|
|
|
def test_uses_copy_of_methods(self):
|
|
|
|
l = [lambda: 42]
|
|
|
|
mc = MultiCall(l)
|
|
|
|
l[:] = []
|
|
|
|
res = mc.execute()
|
|
|
|
return res == 42
|
|
|
|
|
2009-02-27 18:18:27 +08:00
|
|
|
def test_call_passing(self):
|
|
|
|
class P1:
|
|
|
|
def m(self, __call__, x):
|
|
|
|
assert __call__.currentmethod == self.m
|
|
|
|
assert len(__call__.results) == 1
|
|
|
|
assert not __call__.methods
|
|
|
|
return 17
|
|
|
|
|
|
|
|
class P2:
|
|
|
|
def m(self, __call__, x):
|
|
|
|
assert __call__.currentmethod == self.m
|
|
|
|
assert __call__.args
|
|
|
|
assert __call__.results == []
|
|
|
|
assert __call__.methods
|
|
|
|
return 23
|
|
|
|
|
|
|
|
p1 = P1()
|
|
|
|
p2 = P2()
|
|
|
|
multicall = MultiCall([p1.m, p2.m], 23)
|
|
|
|
reslist = multicall.execute()
|
|
|
|
assert len(reslist) == 2
|
|
|
|
# ensure reversed order
|
|
|
|
assert reslist == [23, 17]
|
|
|
|
|
|
|
|
def test_optionalcallarg(self):
|
|
|
|
class P1:
|
|
|
|
def m(self, x):
|
|
|
|
return x
|
|
|
|
call = MultiCall([P1().m], 23)
|
|
|
|
assert call.execute() == [23]
|
|
|
|
assert call.execute(firstresult=True) == 23
|
|
|
|
|
|
|
|
def test_call_subexecute(self):
|
|
|
|
def m(__call__):
|
|
|
|
subresult = __call__.execute(firstresult=True)
|
|
|
|
return subresult + 1
|
|
|
|
|
|
|
|
def n():
|
|
|
|
return 1
|
|
|
|
|
|
|
|
call = MultiCall([n, m])
|
|
|
|
res = call.execute(firstresult=True)
|
|
|
|
assert res == 2
|
2009-03-06 06:35:35 +08:00
|
|
|
|
|
|
|
def test_call_exclude_other_results(self):
|
|
|
|
def m(__call__):
|
|
|
|
__call__.exclude_other_results()
|
|
|
|
return 10
|
|
|
|
|
|
|
|
def n():
|
|
|
|
return 1
|
|
|
|
|
|
|
|
call = MultiCall([n, n, m, n])
|
|
|
|
res = call.execute()
|
|
|
|
assert res == [10]
|
|
|
|
# doesn't really make sense for firstresult-mode - because
|
|
|
|
# we might not have had a chance to run at all.
|
|
|
|
#res = call.execute(firstresult=True)
|
|
|
|
#assert res == 10
|
2009-05-21 04:02:08 +08:00
|
|
|
|
|
|
|
def test_call_none_is_no_result(self):
|
|
|
|
def m1():
|
|
|
|
return 1
|
|
|
|
def m2():
|
|
|
|
return None
|
|
|
|
mc = MultiCall([m1, m2])
|
|
|
|
res = mc.execute(firstresult=True)
|
|
|
|
assert res == 1
|
2009-02-27 18:18:27 +08:00
|
|
|
|
2009-04-09 22:03:09 +08:00
|
|
|
class TestRegistry:
|
2009-02-27 18:18:27 +08:00
|
|
|
def test_MultiCall(self):
|
2009-04-09 22:03:09 +08:00
|
|
|
plugins = Registry()
|
2009-02-27 18:18:27 +08:00
|
|
|
assert hasattr(plugins, "MultiCall")
|
|
|
|
|
|
|
|
def test_register(self):
|
2009-04-09 22:21:07 +08:00
|
|
|
registry = Registry()
|
2009-02-27 18:18:27 +08:00
|
|
|
class MyPlugin:
|
|
|
|
pass
|
|
|
|
my = MyPlugin()
|
2009-04-09 22:21:07 +08:00
|
|
|
registry.register(my)
|
|
|
|
assert list(registry) == [my]
|
2009-02-27 18:18:27 +08:00
|
|
|
my2 = MyPlugin()
|
2009-04-09 22:21:07 +08:00
|
|
|
registry.register(my2)
|
|
|
|
assert list(registry) == [my, my2]
|
2009-02-27 18:18:27 +08:00
|
|
|
|
2009-04-09 22:21:07 +08:00
|
|
|
assert registry.isregistered(my)
|
|
|
|
assert registry.isregistered(my2)
|
|
|
|
registry.unregister(my)
|
|
|
|
assert not registry.isregistered(my)
|
|
|
|
assert list(registry) == [my2]
|
2009-02-27 18:18:27 +08:00
|
|
|
|
|
|
|
def test_listattr(self):
|
2009-04-09 22:03:09 +08:00
|
|
|
plugins = Registry()
|
2009-02-27 18:18:27 +08:00
|
|
|
class api1:
|
|
|
|
x = 41
|
2009-03-21 23:14:22 +08:00
|
|
|
class api2:
|
|
|
|
x = 42
|
|
|
|
class api3:
|
|
|
|
x = 43
|
2009-02-27 18:18:27 +08:00
|
|
|
plugins.register(api1())
|
|
|
|
plugins.register(api2())
|
2009-03-21 23:14:22 +08:00
|
|
|
plugins.register(api3())
|
2009-02-27 18:18:27 +08:00
|
|
|
l = list(plugins.listattr('x'))
|
2009-03-21 23:14:22 +08:00
|
|
|
assert l == [41, 42, 43]
|
|
|
|
l = list(plugins.listattr('x', reverse=True))
|
|
|
|
assert l == [43, 42, 41]
|
2009-02-27 18:18:27 +08:00
|
|
|
|
2009-04-14 23:25:59 +08:00
|
|
|
class api4:
|
|
|
|
x = 44
|
|
|
|
l = list(plugins.listattr('x', extra=(api4,)))
|
|
|
|
assert l == range(41, 45)
|
|
|
|
assert len(list(plugins)) == 3 # otherwise extra added
|
|
|
|
|
2009-02-27 18:18:27 +08:00
|
|
|
def test_api_and_defaults():
|
2009-04-09 22:03:09 +08:00
|
|
|
assert isinstance(py._com.comregistry, Registry)
|
2009-02-27 18:18:27 +08:00
|
|
|
|
2009-05-08 00:01:53 +08:00
|
|
|
class TestHooks:
|
2009-04-08 04:22:52 +08:00
|
|
|
def test_happypath(self):
|
2009-04-09 22:34:53 +08:00
|
|
|
registry = Registry()
|
2009-04-08 04:22:52 +08:00
|
|
|
class Api:
|
2009-04-08 04:46:50 +08:00
|
|
|
def hello(self, arg):
|
2009-04-08 04:22:52 +08:00
|
|
|
pass
|
|
|
|
|
2009-05-08 00:01:53 +08:00
|
|
|
mcm = Hooks(hookspecs=Api, registry=registry)
|
2009-04-08 04:22:52 +08:00
|
|
|
assert hasattr(mcm, 'hello')
|
2009-04-08 04:46:50 +08:00
|
|
|
assert repr(mcm.hello).find("hello") != -1
|
2009-04-08 04:22:52 +08:00
|
|
|
class Plugin:
|
2009-04-08 04:46:50 +08:00
|
|
|
def hello(self, arg):
|
2009-04-08 04:22:52 +08:00
|
|
|
return arg + 1
|
2009-04-09 22:34:53 +08:00
|
|
|
registry.register(Plugin())
|
2009-04-10 00:55:11 +08:00
|
|
|
l = mcm.hello(arg=3)
|
2009-04-08 04:22:52 +08:00
|
|
|
assert l == [4]
|
|
|
|
assert not hasattr(mcm, 'world')
|
2009-04-08 23:15:56 +08:00
|
|
|
|
2009-04-10 00:55:11 +08:00
|
|
|
def test_needskeywordargs(self):
|
|
|
|
registry = Registry()
|
|
|
|
class Api:
|
|
|
|
def hello(self, arg):
|
|
|
|
pass
|
2009-05-08 00:01:53 +08:00
|
|
|
mcm = Hooks(hookspecs=Api, registry=registry)
|
2009-04-10 00:55:11 +08:00
|
|
|
excinfo = py.test.raises(TypeError, "mcm.hello(3)")
|
|
|
|
assert str(excinfo.value).find("only keyword arguments") != -1
|
|
|
|
assert str(excinfo.value).find("hello(self, arg)")
|
|
|
|
|
2009-04-08 23:15:56 +08:00
|
|
|
def test_firstresult(self):
|
2009-04-09 22:34:53 +08:00
|
|
|
registry = Registry()
|
2009-04-08 23:15:56 +08:00
|
|
|
class Api:
|
|
|
|
def hello(self, arg): pass
|
|
|
|
hello.firstresult = True
|
|
|
|
|
2009-05-08 00:01:53 +08:00
|
|
|
mcm = Hooks(hookspecs=Api, registry=registry)
|
2009-04-08 23:15:56 +08:00
|
|
|
class Plugin:
|
|
|
|
def hello(self, arg):
|
|
|
|
return arg + 1
|
2009-04-09 22:34:53 +08:00
|
|
|
registry.register(Plugin())
|
2009-04-10 00:55:11 +08:00
|
|
|
res = mcm.hello(arg=3)
|
2009-04-08 23:15:56 +08:00
|
|
|
assert res == 4
|
|
|
|
|
|
|
|
def test_default_plugins(self):
|
|
|
|
class Api: pass
|
2009-05-08 00:01:53 +08:00
|
|
|
mcm = Hooks(hookspecs=Api)
|
2009-04-09 22:34:53 +08:00
|
|
|
assert mcm.registry == py._com.comregistry
|
2009-05-12 01:23:57 +08:00
|
|
|
|
|
|
|
def test_hooks_extra_plugins(self):
|
|
|
|
registry = Registry()
|
|
|
|
class Api:
|
|
|
|
def hello(self, arg):
|
|
|
|
pass
|
|
|
|
hook_hello = Hooks(hookspecs=Api, registry=registry).hello
|
|
|
|
class Plugin:
|
|
|
|
def hello(self, arg):
|
|
|
|
return arg + 1
|
|
|
|
registry.register(Plugin())
|
|
|
|
class Plugin2:
|
|
|
|
def hello(self, arg):
|
|
|
|
return arg + 2
|
|
|
|
newhook = hook_hello.clone(extralookup=Plugin2())
|
|
|
|
l = newhook(arg=3)
|
|
|
|
assert l == [5, 4]
|
|
|
|
l2 = hook_hello(arg=3)
|
|
|
|
assert l2 == [4]
|
|
|
|
|