Skip to content

Commit 6982e81

Browse files
author
Tyler Goodlet
committed
Add baseline test to verify iterable hooks
1 parent b4bb48d commit 6982e81

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

testing/test_pluginmanager.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,3 +372,60 @@ def example_hook():
372372
assert getattr(pm.hook, 'example_hook', None) # conftest.example_hook should be collected
373373
assert pm.parse_hookimpl_opts(conftest, 'example_blah') is None
374374
assert pm.parse_hookimpl_opts(conftest, 'example_hook') == {}
375+
376+
377+
def test_iterable_hooks(pm):
378+
class Hooks(object):
379+
@hookspec
380+
def he_method1(self, arg):
381+
pass
382+
383+
pm.add_hookspecs(Hooks)
384+
385+
l = []
386+
387+
class Plugin1(object):
388+
@hookimpl
389+
def he_method1(self, arg):
390+
l.append(1)
391+
return 1
392+
393+
class Plugin2(object):
394+
@hookimpl
395+
def he_method1(self, arg):
396+
l.append(2)
397+
return 2
398+
399+
class Plugin3(object):
400+
@hookimpl
401+
def he_method1(self, arg):
402+
l.append(3)
403+
return 3
404+
405+
class Plugin4(object):
406+
@hookimpl
407+
def he_method1(self, arg):
408+
l.append(4)
409+
return 4
410+
411+
class PluginWrapper(object):
412+
@hookimpl(hookwrapper=True)
413+
def he_method1(self, arg):
414+
assert not l
415+
outcome = yield
416+
res = outcome.get_result()
417+
assert res
418+
assert res == [1, 2, 3] == l
419+
420+
pm.register(Plugin1())
421+
pm.register(Plugin2())
422+
pm.register(Plugin3())
423+
pm.register(Plugin4())
424+
pm.register(PluginWrapper())
425+
426+
for result, i in zip(pm.ihook.he_method1(arg=None), reversed(range(1, 5))):
427+
assert result == i
428+
if result == 2: # stop before the final iteration
429+
break
430+
431+
assert l == [4, 3, 2]

0 commit comments

Comments
 (0)