2009-09-06 22:59:39 +08:00
|
|
|
import py
|
2009-12-29 19:36:17 +08:00
|
|
|
import sys
|
2010-01-14 00:15:54 +08:00
|
|
|
from py._plugin.pytest__pytest import HookRecorder
|
|
|
|
from py._test.pluginmanager import Registry
|
2009-09-06 22:59:39 +08:00
|
|
|
|
|
|
|
def test_hookrecorder_basic():
|
2009-12-29 19:36:17 +08:00
|
|
|
rec = HookRecorder(Registry())
|
2009-09-06 22:59:39 +08:00
|
|
|
class ApiClass:
|
|
|
|
def xyz(self, arg):
|
|
|
|
pass
|
|
|
|
rec.start_recording(ApiClass)
|
|
|
|
rec.hook.xyz(arg=123)
|
|
|
|
call = rec.popcall("xyz")
|
|
|
|
assert call.arg == 123
|
|
|
|
assert call._name == "xyz"
|
|
|
|
py.test.raises(ValueError, "rec.popcall('abc')")
|
|
|
|
|
|
|
|
def test_hookrecorder_basic_no_args_hook():
|
2009-12-29 19:36:17 +08:00
|
|
|
rec = HookRecorder(Registry())
|
2009-09-06 22:59:39 +08:00
|
|
|
apimod = type(sys)('api')
|
|
|
|
def xyz():
|
|
|
|
pass
|
|
|
|
apimod.xyz = xyz
|
|
|
|
rec.start_recording(apimod)
|
|
|
|
rec.hook.xyz()
|
|
|
|
call = rec.popcall("xyz")
|
|
|
|
assert call._name == "xyz"
|
|
|
|
|
|
|
|
def test_functional(testdir, linecomp):
|
|
|
|
reprec = testdir.inline_runsource("""
|
|
|
|
import py
|
2010-01-14 00:15:54 +08:00
|
|
|
from py._test.pluginmanager import HookRelay, Registry
|
2009-09-06 22:59:39 +08:00
|
|
|
pytest_plugins="_pytest"
|
|
|
|
def test_func(_pytest):
|
|
|
|
class ApiClass:
|
|
|
|
def xyz(self, arg): pass
|
2009-12-29 19:36:17 +08:00
|
|
|
hook = HookRelay(ApiClass, Registry())
|
|
|
|
rec = _pytest.gethookrecorder(hook)
|
2009-09-06 22:59:39 +08:00
|
|
|
class Plugin:
|
|
|
|
def xyz(self, arg):
|
|
|
|
return arg + 1
|
2009-12-29 19:36:17 +08:00
|
|
|
rec._registry.register(Plugin())
|
2009-09-06 22:59:39 +08:00
|
|
|
res = rec.hook.xyz(arg=41)
|
|
|
|
assert res == [42]
|
|
|
|
""")
|
|
|
|
reprec.assertoutcome(passed=1)
|