Skip to content

Commit 9fdd7f3

Browse files
author
Tyler Goodlet
committed
Add a defaults precedence test
Verify that defaults declared in hook impls and specs adhere to the lookup order: call provided value, hook spec default, and finally falling back to the spec's default value.
1 parent e143650 commit 9fdd7f3

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

testing/test_hookrelay.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,32 @@ def hello(self, arg):
7676
pm.register(Plugin())
7777
res = pm.hook.hello(arg=3)
7878
assert res == 4
79+
80+
81+
def test_defaults(pm):
82+
"""Verify that default keyword arguments can be declared on both specs
83+
and impls. The default value look up precedence is up as follows:
84+
- caller provided value
85+
- hookspec default
86+
- hookimpl default
87+
"""
88+
class Api:
89+
@hookspec
90+
def myhook(self, arg, kwarg="default"):
91+
"A spec with a default"
92+
93+
class Plugin:
94+
@hookimpl
95+
def myhook(self, arg, kwarg="my default"):
96+
return kwarg
97+
98+
pm.register(Plugin())
99+
100+
# with no spec registered
101+
assert pm.hook.myhook(arg='yeah!')[0] == "my default"
102+
assert pm.hook.myhook(arg='yeah!', kwarg='doggy')[0] == "doggy"
103+
104+
# with spec registered
105+
pm.add_hookspecs(Api)
106+
assert pm.hook.myhook(arg='yeah!')[0] == "default"
107+
assert pm.hook.myhook(arg='yeah!', kwarg='doggy')[0] == "doggy"

0 commit comments

Comments
 (0)