Skip to content

Commit 9d1bab0

Browse files
author
Tyler Goodlet
committed
Add baseline test to verify iterable hooks
1 parent 14e84a5 commit 9d1bab0

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
@@ -393,3 +393,60 @@ def example_hook():
393393
assert getattr(pm.hook, 'example_hook', None) # conftest.example_hook should be collected
394394
assert pm.parse_hookimpl_opts(conftest, 'example_blah') is None
395395
assert pm.parse_hookimpl_opts(conftest, 'example_hook') == {}
396+
397+
398+
def test_iterable_hooks(pm):
399+
class Hooks(object):
400+
@hookspec
401+
def he_method1(self, arg):
402+
pass
403+
404+
pm.add_hookspecs(Hooks)
405+
406+
l = []
407+
408+
class Plugin1(object):
409+
@hookimpl
410+
def he_method1(self, arg):
411+
l.append(1)
412+
return 1
413+
414+
class Plugin2(object):
415+
@hookimpl
416+
def he_method1(self, arg):
417+
l.append(2)
418+
return 2
419+
420+
class Plugin3(object):
421+
@hookimpl
422+
def he_method1(self, arg):
423+
l.append(3)
424+
return 3
425+
426+
class Plugin4(object):
427+
@hookimpl
428+
def he_method1(self, arg):
429+
l.append(4)
430+
return 4
431+
432+
class PluginWrapper(object):
433+
@hookimpl(hookwrapper=True)
434+
def he_method1(self, arg):
435+
assert not l
436+
outcome = yield
437+
res = outcome.get_result()
438+
assert res
439+
assert res == [1, 2, 3] == l
440+
441+
pm.register(Plugin1())
442+
pm.register(Plugin2())
443+
pm.register(Plugin3())
444+
pm.register(Plugin4())
445+
pm.register(PluginWrapper())
446+
447+
for result, i in zip(pm.ihook.he_method1(arg=None), reversed(range(1, 5))):
448+
assert result == i
449+
if result == 2: # stop before the final iteration
450+
break
451+
452+
assert l == [4, 3, 2]

0 commit comments

Comments
 (0)