summaryrefslogtreecommitdiff
path: root/tests/test_plugin.py
diff options
context:
space:
mode:
authorkotfu <kotfu@kotfu.net>2018-06-22 16:26:59 -0600
committerkotfu <kotfu@kotfu.net>2018-06-22 16:26:59 -0600
commita73c061b82ac1824db8e1747698c90c0ae7766b1 (patch)
tree1ea8bd58ddd885776e91ed79dec41fb69b5a169a /tests/test_plugin.py
parent1cd46fd53c698f062995c2c45db57c07285a6f46 (diff)
downloadcmd2-git-a73c061b82ac1824db8e1747698c90c0ae7766b1.tar.gz
Postcommand hooks implemented
Diffstat (limited to 'tests/test_plugin.py')
-rw-r--r--tests/test_plugin.py198
1 files changed, 172 insertions, 26 deletions
diff --git a/tests/test_plugin.py b/tests/test_plugin.py
index 1f07f177..cc60ce2a 100644
--- a/tests/test_plugin.py
+++ b/tests/test_plugin.py
@@ -24,6 +24,7 @@ class Plugin:
def reset_counters(self):
self.called_postparsing = 0
self.called_precmd = 0
+ self.called_postcmd = 0
###
#
@@ -43,7 +44,7 @@ class Plugin:
"A preloop or postloop hook with too many parameters"
pass
- def prepost_hook_with_wrong_return_type(self) -> bool:
+ def prepost_hook_with_wrong_return_annotation(self) -> bool:
"A preloop or postloop hook with incorrect return type"
pass
@@ -77,19 +78,19 @@ class Plugin:
"A postparsing hook with too many parameters"
pass
- def postparse_hook_undeclared_parameter_type(self, data) -> cmd2.plugin.PostparsingData:
+ def postparse_hook_undeclared_parameter_annotation(self, data) -> cmd2.plugin.PostparsingData:
"A postparsing hook with an undeclared parameter type"
pass
- def postparse_hook_wrong_parameter_type(self, data: str) -> cmd2.plugin.PostparsingData:
+ def postparse_hook_wrong_parameter_annotation(self, data: str) -> cmd2.plugin.PostparsingData:
"A postparsing hook with the wrong parameter type"
pass
- def postparse_hook_undeclared_return_type(self, data: cmd2.plugin.PostparsingData):
+ def postparse_hook_undeclared_return_annotation(self, data: cmd2.plugin.PostparsingData):
"A postparsing hook with an undeclared return type"
pass
- def postparse_hook_wrong_return_type(self, data: cmd2.plugin.PostparsingData) -> str:
+ def postparse_hook_wrong_return_annotation(self, data: cmd2.plugin.PostparsingData) -> str:
"A postparsing hook with the wrong return type"
pass
@@ -126,21 +127,63 @@ class Plugin:
"A precommand hook with too many parameters"
return one
- def precmd_hook_no_parameter_type(self, data) -> plugin.PrecommandData:
+ def precmd_hook_no_parameter_annotation(self, data) -> plugin.PrecommandData:
"A precommand hook with no type annotation on the parameter"
return data
- def precmd_hook_wrong_parameter_type(self, data: str) -> plugin.PrecommandData:
+ def precmd_hook_wrong_parameter_annotation(self, data: str) -> plugin.PrecommandData:
"A precommand hook with the incorrect type annotation on the parameter"
return data
- def precmd_hook_no_return_type(self, data: plugin.PrecommandData):
+ def precmd_hook_no_return_annotation(self, data: plugin.PrecommandData):
"A precommand hook with no type annotation on the return value"
return data
- def precmd_hook_wrong_return_type(self, data: plugin.PrecommandData) -> cmd2.Statement:
+ def precmd_hook_wrong_return_annotation(self, data: plugin.PrecommandData) -> cmd2.Statement:
return self.statement_parser.parse('hi there')
+ ###
+ #
+ # postcommand hooks, some valid, some invalid
+ #
+ ###
+ def postcmd(self, stop: bool, statement: cmd2.Statement) -> bool:
+ "Override cmd.Cmd method"
+ self.called_postcmd += 1
+ return stop
+
+ def postcmd_hook(self, data: plugin.PostcommandData) -> plugin.PostcommandData:
+ "A postcommand hook"
+ self.called_postcmd += 1
+ return data
+
+ def postcmd_hook_exception(self, data: plugin.PostcommandData) -> plugin.PostcommandData:
+ "A postcommand hook with raises an exception"
+ self.called_postcmd += 1
+ raise ZeroDivisionError
+
+ def postcmd_hook_not_enough_parameters(self) -> plugin.PostcommandData:
+ "A precommand hook with no parameters"
+ pass
+
+ def postcmd_hook_too_many_parameters(self, one: plugin.PostcommandData, two: str) -> plugin.PostcommandData:
+ "A precommand hook with too many parameters"
+ return one
+
+ def postcmd_hook_no_parameter_annotation(self, data) -> plugin.PostcommandData:
+ "A precommand hook with no type annotation on the parameter"
+ return data
+
+ def postcmd_hook_wrong_parameter_annotation(self, data: str) -> plugin.PostcommandData:
+ "A precommand hook with the incorrect type annotation on the parameter"
+ return data
+
+ def postcmd_hook_no_return_annotation(self, data: plugin.PostcommandData):
+ "A precommand hook with no type annotation on the return value"
+ return data
+
+ def postcmd_hook_wrong_return_annotation(self, data: plugin.PostcommandData) -> cmd2.Statement:
+ return self.statement_parser.parse('hi there')
class PluggedApp(Plugin, cmd2.Cmd):
"A sample app with a plugin mixed in"
@@ -161,10 +204,10 @@ def test_register_preloop_hook_too_many_parameters():
with pytest.raises(TypeError):
app.register_preloop_hook(app.prepost_hook_too_many_parameters)
-def test_register_preloop_hook_with_return_type():
+def test_register_preloop_hook_with_return_annotation():
app = PluggedApp()
with pytest.raises(TypeError):
- app.register_preloop_hook(app.prepost_hook_with_wrong_return_type)
+ app.register_preloop_hook(app.prepost_hook_with_wrong_return_annotation)
def test_preloop_hook(capsys):
app = PluggedApp()
@@ -192,10 +235,10 @@ def test_register_postloop_hook_too_many_parameters():
with pytest.raises(TypeError):
app.register_postloop_hook(app.prepost_hook_too_many_parameters)
-def test_register_postloop_hook_with_wrong_return_type():
+def test_register_postloop_hook_with_wrong_return_annotation():
app = PluggedApp()
with pytest.raises(TypeError):
- app.register_postloop_hook(app.prepost_hook_with_wrong_return_type)
+ app.register_postloop_hook(app.prepost_hook_with_wrong_return_annotation)
def test_postloop_hook(capsys):
app = PluggedApp()
@@ -228,25 +271,25 @@ def test_postparsing_hook_too_many_parameters():
with pytest.raises(TypeError):
app.register_postparsing_hook(app.postparse_hook_too_many_parameters)
-def test_postparsing_hook_undeclared_parameter_type():
+def test_postparsing_hook_undeclared_parameter_annotation():
app = PluggedApp()
with pytest.raises(TypeError):
- app.register_postparsing_hook(app.postparse_hook_undeclared_parameter_type)
+ app.register_postparsing_hook(app.postparse_hook_undeclared_parameter_annotation)
-def test_postparsing_hook_wrong_parameter_type():
+def test_postparsing_hook_wrong_parameter_annotation():
app = PluggedApp()
with pytest.raises(TypeError):
- app.register_postparsing_hook(app.postparse_hook_wrong_parameter_type)
+ app.register_postparsing_hook(app.postparse_hook_wrong_parameter_annotation)
-def test_postparsing_hook_undeclared_return_type():
+def test_postparsing_hook_undeclared_return_annotation():
app = PluggedApp()
with pytest.raises(TypeError):
- app.register_postparsing_hook(app.postparse_hook_undeclared_return_type)
+ app.register_postparsing_hook(app.postparse_hook_undeclared_return_annotation)
-def test_postparsing_hook_wrong_return_type():
+def test_postparsing_hook_wrong_return_annotation():
app = PluggedApp()
with pytest.raises(TypeError):
- app.register_postparsing_hook(app.postparse_hook_wrong_return_type)
+ app.register_postparsing_hook(app.postparse_hook_wrong_return_annotation)
def test_postparsing_hook(capsys):
app = PluggedApp()
@@ -393,22 +436,22 @@ def test_register_precmd_hook_parameter_count():
def test_register_precmd_hook_no_parameter_annotation():
app = PluggedApp()
with pytest.raises(TypeError):
- app.register_precmd_hook(app.precmd_hook_no_parameter_type)
+ 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_type)
+ 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_type)
+ 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_type)
+ app.register_precmd_hook(app.precmd_hook_wrong_return_annotation)
def test_precmd_hook(capsys):
app = PluggedApp()
@@ -504,8 +547,111 @@ def test_precmd_hook_emptystatement_second(capsys):
# test postcmd hooks
#
####
+def test_register_postcmd_hook_parameter_count():
+ app = PluggedApp()
+ with pytest.raises(TypeError):
+ app.register_postcmd_hook(app.postcmd_hook_not_enough_parameters)
+ with pytest.raises(TypeError):
+ app.register_postcmd_hook(app.postcmd_hook_too_many_parameters)
+
+def test_register_postcmd_hook_no_parameter_annotation():
+ app = PluggedApp()
+ with pytest.raises(TypeError):
+ app.register_postcmd_hook(app.postcmd_hook_no_parameter_annotation)
+
+def test_register_postcmd_hook_wrong_parameter_annotation():
+ app = PluggedApp()
+ with pytest.raises(TypeError):
+ app.register_postcmd_hook(app.postcmd_hook_wrong_parameter_annotation)
+
+def test_register_postcmd_hook_no_return_annotation():
+ app = PluggedApp()
+ with pytest.raises(TypeError):
+ app.register_postcmd_hook(app.postcmd_hook_no_return_annotation)
+
+def test_register_postcmd_hook_wrong_return_annotation():
+ app = PluggedApp()
+ with pytest.raises(TypeError):
+ app.register_postcmd_hook(app.postcmd_hook_wrong_return_annotation)
+
def test_postcmd(capsys):
- pass
+ app = PluggedApp()
+ app.onecmd_plus_hooks('say hello')
+ out, err = capsys.readouterr()
+ assert out == 'hello\n'
+ assert not err
+ # without registering any hooks, postcmd() should be called
+ assert app.called_postcmd == 1
+
+ app.reset_counters()
+ app.register_postcmd_hook(app.postcmd_hook)
+ app.onecmd_plus_hooks('say hello')
+ out, err = capsys.readouterr()
+ assert out == 'hello\n'
+ assert not err
+ # with one hook registered, we should get precmd() and the hook
+ assert app.called_postcmd == 2
+
+ # register the function again, so it should be called twice
+ app.reset_counters()
+ app.register_postcmd_hook(app.postcmd_hook)
+ app.onecmd_plus_hooks('say hello')
+ out, err = capsys.readouterr()
+ assert out == 'hello\n'
+ assert not err
+ # with two hooks registered, we should get precmd() and both hooks
+ assert app.called_postcmd == 3
+
+def test_postcmd_exception_first(capsys):
+ app = PluggedApp()
+ app.register_postcmd_hook(app.postcmd_hook_exception)
+ stop = app.onecmd_plus_hooks('say hello')
+ out, err = capsys.readouterr()
+ assert not stop
+ assert out == 'hello\n'
+ assert err
+ # since the registered hooks are called before postcmd(), if a registered
+ # hook throws an exception, postcmd() is never called. So we should have
+ # a count of one because we called the hook that raised the exception
+ assert app.called_postcmd == 1
+
+ # register another function but it shouldn't be called
+ app.reset_counters()
+ stop = app.register_postcmd_hook(app.postcmd_hook)
+ app.onecmd_plus_hooks('say hello')
+ out, err = capsys.readouterr()
+ assert not stop
+ assert out == 'hello\n'
+ assert err
+ # the exception raised by the first hook should prevent the second
+ # hook from being called, and it also prevents postcmd() from being
+ # called
+ assert app.called_postcmd == 1
+
+def test_postcmd_exception_second(capsys):
+ app = PluggedApp()
+ app.register_postcmd_hook(app.postcmd_hook)
+ stop = app.onecmd_plus_hooks('say hello')
+ out, err = capsys.readouterr()
+ assert not stop
+ assert out == 'hello\n'
+ assert not err
+ # with one hook registered, we should get the hook and postcmd()
+ assert app.called_postcmd == 2
+
+ # register another function which should be called
+ app.reset_counters()
+ stop = app.register_postcmd_hook(app.postcmd_hook_exception)
+ app.onecmd_plus_hooks('say hello')
+ out, err = capsys.readouterr()
+ assert not stop
+ assert out == 'hello\n'
+ assert err
+ # the exception raised by the first hook should prevent the second
+ # hook from being called, and it also prevents postcmd() from being
+ # called. So we have the first hook, and the second hook, which raised
+ # the exception
+ assert app.called_postcmd == 2
##
#