|
1 | 1 | import pytest
|
2 | 2 |
|
3 |
| -from pluggy import HookimplMarker, HookspecMarker |
| 3 | +from pluggy import HookimplMarker, HookspecMarker, PluginValidationError |
4 | 4 | from pluggy.hooks import HookImpl
|
5 | 5 |
|
6 | 6 | hookspec = HookspecMarker("example")
|
@@ -213,3 +213,60 @@ def hello(self, arg):
|
213 | 213 | assert not hasattr(hook, "world")
|
214 | 214 | pm.unregister(plugin)
|
215 | 215 | assert hook.hello(arg=3) == []
|
| 216 | + |
| 217 | + |
| 218 | +def test_hookrelay_registration_by_specname(pm): |
| 219 | + """Verify hook caller instances may also be registered by specifying a |
| 220 | + specname option to the hookimpl""" |
| 221 | + |
| 222 | + class Api(object): |
| 223 | + @hookspec |
| 224 | + def hello(self, arg): |
| 225 | + "api hook 1" |
| 226 | + |
| 227 | + pm.add_hookspecs(Api) |
| 228 | + hook = pm.hook |
| 229 | + assert hasattr(hook, "hello") |
| 230 | + assert len(pm.hook.hello.get_hookimpls()) == 0 |
| 231 | + |
| 232 | + class Plugin(object): |
| 233 | + @hookimpl(specname="hello") |
| 234 | + def foo(self, arg): |
| 235 | + return arg + 1 |
| 236 | + |
| 237 | + plugin = Plugin() |
| 238 | + pm.register(plugin) |
| 239 | + out = hook.hello(arg=3) |
| 240 | + assert out == [4] |
| 241 | + |
| 242 | + |
| 243 | +def test_hookrelay_registration_by_specname_raises(pm): |
| 244 | + """Verify using specname still raises the types of errors during registration as it |
| 245 | + would have without using specname.""" |
| 246 | + |
| 247 | + class Api(object): |
| 248 | + @hookspec |
| 249 | + def hello(self, arg): |
| 250 | + "api hook 1" |
| 251 | + |
| 252 | + pm.add_hookspecs(Api) |
| 253 | + |
| 254 | + # make sure a bad signature still raises an error when using specname |
| 255 | + class Plugin(object): |
| 256 | + @hookimpl(specname="hello") |
| 257 | + def foo(self, arg, too, many, args): |
| 258 | + return arg + 1 |
| 259 | + |
| 260 | + with pytest.raises(PluginValidationError): |
| 261 | + pm.register(Plugin()) |
| 262 | + |
| 263 | + # make sure check_pending still fails if specname doesn't have a |
| 264 | + # corresponding spec. EVEN if the function name matches one. |
| 265 | + class Plugin2(object): |
| 266 | + @hookimpl(specname="bar") |
| 267 | + def hello(self, arg): |
| 268 | + return arg + 1 |
| 269 | + |
| 270 | + pm.register(Plugin2()) |
| 271 | + with pytest.raises(PluginValidationError): |
| 272 | + pm.check_pending() |
0 commit comments