From 9fdd7f376607a4c67a2a9550333419b50bae331e Mon Sep 17 00:00:00 2001 From: Tyler Goodlet Date: Sat, 19 Nov 2016 12:46:47 -0500 Subject: [PATCH] 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. --- testing/test_hookrelay.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/testing/test_hookrelay.py b/testing/test_hookrelay.py index 38c5e2ad..c713caa5 100644 --- a/testing/test_hookrelay.py +++ b/testing/test_hookrelay.py @@ -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"