summaryrefslogtreecommitdiff
path: root/tests/test_plugin.py
diff options
context:
space:
mode:
authorkotfu <kotfu@kotfu.net>2018-06-03 15:27:47 -0600
committerkotfu <kotfu@kotfu.net>2018-06-03 15:27:47 -0600
commit242742b94ea2c11db3929aa1de8b21deadb6fe5c (patch)
treee4a6798237f0a173c89ac9e9ffb9be8e6c36ed58 /tests/test_plugin.py
parente5a0f306a16bb06406ba3c6f88821bfe6c8ca738 (diff)
downloadcmd2-git-242742b94ea2c11db3929aa1de8b21deadb6fe5c.tar.gz
Precommand hooks now check typing of passed callables
Diffstat (limited to 'tests/test_plugin.py')
-rw-r--r--tests/test_plugin.py59
1 files changed, 57 insertions, 2 deletions
diff --git a/tests/test_plugin.py b/tests/test_plugin.py
index 133284a0..26eb88bb 100644
--- a/tests/test_plugin.py
+++ b/tests/test_plugin.py
@@ -53,6 +53,11 @@ class Plugin:
self.called_postparsing += 1
raise ValueError
+ ###
+ #
+ # precommand hooks, some valid, some invalid
+ #
+ ###
def precmd(self, statement: cmd2.Statement) -> cmd2.Statement:
"Override cmd.Cmd method"
self.called_precmd += 1
@@ -63,16 +68,39 @@ class Plugin:
self.called_precmd += 1
return data
- def precmd_hook_emptystatement(self, statement: cmd2.Statement) -> cmd2.Statement:
+ def precmd_hook_emptystatement(self, data: plugin.PrecommandData) -> plugin.PrecommandData:
"A precommand hook which raises an EmptyStatement exception"
self.called_precmd += 1
raise cmd2.EmptyStatement
- def precmd_hook_exception(self, statement: cmd2.Statement) -> cmd2.Statement:
+ def precmd_hook_exception(self, data: plugin.PrecommandData) -> plugin.PrecommandData:
"A precommand hook which raises an exception"
self.called_precmd += 1
raise ValueError
+ def precmd_hook_not_enough_parameters(self) -> plugin.PrecommandData:
+ "A precommand hook with no parameters"
+ pass
+
+ def precmd_hook_too_many_parameters(self, one: plugin.PrecommandData, two: str) -> plugin.PrecommandData:
+ "A precommand hook with too many parameters"
+ return one
+
+ def precmd_hook_no_parameter_annotation(self, mydat) -> plugin.PrecommandData:
+ "A precommand hook with no type annotation on the parameter"
+ return mydat
+
+ def precmd_hook_wrong_parameter_annotation(self, mydat: str) -> plugin.PrecommandData:
+ "A precommand hook with the incorrect type annotation on the parameter"
+ return mydat
+
+ def precmd_hook_no_return_annotation(self, mydat: plugin.PrecommandData):
+ "A precommand hook with no type annotation on the return value"
+ return mydat
+
+ def precmd_hook_wrong_return_annotation(self, mydat: plugin.PrecommandData) -> cmd2.Statement:
+ return self.statement_parser.parse('hi there')
+
class PluggedApp(Plugin, cmd2.Cmd):
"A sample app with a plugin mixed in"
@@ -270,6 +298,33 @@ def test_postparsing_hook_exception(capsys):
# test precmd hooks
#
#####
+def test_register_precmd_hook_parameter_count():
+ app = PluggedApp()
+ with pytest.raises(TypeError):
+ app.register_precmd_hook(app.precmd_hook_not_enough_parameters)
+ with pytest.raises(TypeError):
+ app.register_precmd_hook(app.precmd_hook_too_many_parameters)
+
+def test_register_precmd_hook_no_parameter_annotation():
+ app = PluggedApp()
+ with pytest.raises(TypeError):
+ app.register_precmd_hook(app.precmd_hook_no_parameter_annotation)
+
+def test_register_precmd_hook_wrong_parameter_annotation():
+ app = PluggedApp()
+ with pytest.raises(TypeError):
+ app.register_precmd_hook(app.precmd_hook_wrong_parameter_annotation)
+
+def test_register_precmd_hook_no_return_annotation():
+ app = PluggedApp()
+ with pytest.raises(TypeError):
+ app.register_precmd_hook(app.precmd_hook_no_return_annotation)
+
+def test_register_precmd_hook_wrong_return_annotation():
+ app = PluggedApp()
+ with pytest.raises(TypeError):
+ app.register_precmd_hook(app.precmd_hook_wrong_return_annotation)
+
def test_precmd_hook(capsys):
app = PluggedApp()
app.onecmd_plus_hooks('say hello')