Skip to content

Commit

Permalink
Add a defaults precedence test
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Tyler Goodlet committed Nov 19, 2016
1 parent d743939 commit 178ffdf
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions testing/test_hookrelay.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,32 @@ def hello(self, arg):
pm.register(Plugin())
res = pm.hook.hello(arg=3)
assert res == 4


def test_defaults(pm):
"""Verify that default keyword arguments can be declared on both specs
and impls. The default value look up precedence is up as follows:
- caller provided value
- hookspec default
- hookimpl default
"""
class Api:
@hookspec
def myhook(self, arg, kwarg="default"):
"A spec with a default"

class Plugin:
@hookimpl
def myhook(self, arg, kwarg="my default"):
return kwarg

pm.register(Plugin())

# with no spec registered
assert pm.hook.myhook(arg='yeah!')[0] == "my default"
assert pm.hook.myhook(arg='yeah!', kwarg='doggy')[0] == "doggy"

# with spec registered
pm.add_hookspecs(Api)
assert pm.hook.myhook(arg='yeah!')[0] == "default"
assert pm.hook.myhook(arg='yeah!', kwarg='doggy')[0] == "doggy"

0 comments on commit 178ffdf

Please sign in to comment.