Skip to content

Commit 8706f55

Browse files
wahunekeRonnyPfannschmidt
authored andcommitted
test hookcaller: targeted testing for multi impl scenario
1 parent 5d7bd2c commit 8706f55

1 file changed

Lines changed: 53 additions & 0 deletions

File tree

testing/test_hookcaller.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,3 +511,56 @@ def extra2() -> str:
511511
"2",
512512
"3",
513513
]
514+
515+
516+
def test_hook_multi_impl(pm: PluginManager) -> None:
517+
"""A plugin can implement the same spec multiple times
518+
via specname= on hookimpl."""
519+
520+
class Api:
521+
@hookspec
522+
def hello(self, arg: object) -> None:
523+
"api hook 1"
524+
525+
pm.add_hookspecs(Api)
526+
hook = pm.hook
527+
test_hc = hook.hello
528+
529+
class Plugin:
530+
@hookimpl
531+
def hello(self, arg):
532+
return arg + 1
533+
534+
@hookimpl(specname="hello")
535+
def hello_again(self, arg):
536+
return arg + 100
537+
538+
plugin = Plugin()
539+
540+
pm.register(plugin)
541+
out = test_hc(arg=3)
542+
assert out == [103, 4]
543+
544+
assert len(pm.get_plugins()) == 1
545+
546+
hookimpls = test_hc.get_hookimpls()
547+
hook_plugins = [item.plugin for item in hookimpls]
548+
assert hook_plugins == [plugin, plugin]
549+
for impl in hookimpls:
550+
pm._verify_hook(test_hc, impl)
551+
552+
hook_callers = pm.get_hookcallers(plugin)
553+
assert hook_callers is not None
554+
assert len(hook_callers) == 1
555+
assert hook_callers[0] is test_hc
556+
557+
subset_caller = pm.subset_hook_caller("hello", [plugin])
558+
out = subset_caller(arg=3)
559+
assert out == []
560+
561+
pm.unregister(plugin)
562+
assert test_hc(arg=3) == []
563+
hookimpls = test_hc.get_hookimpls()
564+
assert hookimpls == []
565+
hook_callers = pm.get_hookcallers(plugin)
566+
assert hook_callers is None

0 commit comments

Comments
 (0)