diff options
| author | Todd Leonhardt <todd.leonhardt@gmail.com> | 2021-01-31 22:29:57 -0500 |
|---|---|---|
| committer | Todd Leonhardt <todd.leonhardt@gmail.com> | 2021-01-31 22:29:57 -0500 |
| commit | f456b802754c3d1095b488d670bebba21018d823 (patch) | |
| tree | bf7011f585a8a35ab4cc7ff98aaeebed4de87f84 /tests/test_plugin.py | |
| parent | 918200c02d392c17862fff81bbf58820ed15c725 (diff) | |
| download | cmd2-git-f456b802754c3d1095b488d670bebba21018d823.tar.gz | |
Add black for automatic code format
Diffstat (limited to 'tests/test_plugin.py')
| -rw-r--r-- | tests/test_plugin.py | 67 |
1 files changed, 60 insertions, 7 deletions
diff --git a/tests/test_plugin.py b/tests/test_plugin.py index 4a019b15..831b4cef 100644 --- a/tests/test_plugin.py +++ b/tests/test_plugin.py @@ -23,9 +23,9 @@ except ImportError: from unittest import mock - class Plugin: """A mixin class for testing hook registration and calling""" + def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.reset_counters() @@ -227,14 +227,16 @@ class Plugin: self.called_cmdfinalization += 1 raise ValueError - def cmdfinalization_hook_system_exit(self, data: cmd2.plugin.CommandFinalizationData) -> \ - cmd2.plugin.CommandFinalizationData: + def cmdfinalization_hook_system_exit( + self, data: cmd2.plugin.CommandFinalizationData + ) -> cmd2.plugin.CommandFinalizationData: """A command finalization hook which raises a SystemExit""" self.called_cmdfinalization += 1 raise SystemExit - def cmdfinalization_hook_keyboard_interrupt(self, data: cmd2.plugin.CommandFinalizationData) -> \ - cmd2.plugin.CommandFinalizationData: + def cmdfinalization_hook_keyboard_interrupt( + self, data: cmd2.plugin.CommandFinalizationData + ) -> cmd2.plugin.CommandFinalizationData: """A command finalization hook which raises a KeyboardInterrupt""" self.called_cmdfinalization += 1 raise KeyboardInterrupt @@ -243,8 +245,9 @@ class Plugin: """A command finalization hook with no parameters.""" pass - def cmdfinalization_hook_too_many_parameters(self, one: plugin.CommandFinalizationData, two: str) -> \ - plugin.CommandFinalizationData: + def cmdfinalization_hook_too_many_parameters( + self, one: plugin.CommandFinalizationData, two: str + ) -> plugin.CommandFinalizationData: """A command finalization hook with too many parameters.""" return one @@ -267,6 +270,7 @@ class Plugin: class PluggedApp(Plugin, cmd2.Cmd): """A sample app with a plugin mixed in""" + def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) @@ -286,6 +290,7 @@ class PluggedApp(Plugin, cmd2.Cmd): """Repeat back the arguments""" self.poutput(namespace.cmd2_statement.get()) + ### # # test pre and postloop hooks @@ -296,11 +301,13 @@ 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_annotation(): app = PluggedApp() with pytest.raises(TypeError): app.register_preloop_hook(app.prepost_hook_with_wrong_return_annotation) + def test_preloop_hook(capsys): # Need to patch sys.argv so cmd2 doesn't think it was called with arguments equal to the py.test args testargs = ["prog", "say hello", 'quit'] @@ -314,6 +321,7 @@ def test_preloop_hook(capsys): assert out == 'one\nhello\n' assert not err + def test_preloop_hooks(capsys): # Need to patch sys.argv so cmd2 doesn't think it was called with arguments equal to the py.test args testargs = ["prog", "say hello", 'quit'] @@ -328,16 +336,19 @@ def test_preloop_hooks(capsys): assert out == 'one\ntwo\nhello\n' assert not err + def test_register_postloop_hook_too_many_parameters(): app = PluggedApp() with pytest.raises(TypeError): app.register_postloop_hook(app.prepost_hook_too_many_parameters) + 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_annotation) + def test_postloop_hook(capsys): # Need to patch sys.argv so cmd2 doesn't think it was called with arguments equal to the py.test args testargs = ["prog", "say hello", 'quit'] @@ -351,6 +362,7 @@ def test_postloop_hook(capsys): assert out == 'hello\none\n' assert not err + def test_postloop_hooks(capsys): # Need to patch sys.argv so cmd2 doesn't think it was called with arguments equal to the py.test args testargs = ["prog", "say hello", 'quit'] @@ -365,6 +377,7 @@ def test_postloop_hooks(capsys): assert out == 'hello\none\ntwo\n' assert not err + ### # # test preparse hook @@ -379,6 +392,7 @@ def test_preparse(capsys): assert not err assert app.called_preparse == 1 + ### # # test postparsing hooks @@ -389,26 +403,31 @@ 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_annotation(): app = PluggedApp() with pytest.raises(TypeError): app.register_postparsing_hook(app.postparse_hook_undeclared_parameter_annotation) + def test_postparsing_hook_wrong_parameter_annotation(): app = PluggedApp() with pytest.raises(TypeError): app.register_postparsing_hook(app.postparse_hook_wrong_parameter_annotation) + def test_postparsing_hook_undeclared_return_annotation(): app = PluggedApp() with pytest.raises(TypeError): app.register_postparsing_hook(app.postparse_hook_undeclared_return_annotation) + def test_postparsing_hook_wrong_return_annotation(): app = PluggedApp() with pytest.raises(TypeError): app.register_postparsing_hook(app.postparse_hook_wrong_return_annotation) + def test_postparsing_hook(capsys): app = PluggedApp() app.onecmd_plus_hooks('say hello') @@ -434,6 +453,7 @@ def test_postparsing_hook(capsys): assert not err assert app.called_postparsing == 2 + def test_postparsing_hook_stop_first(capsys): app = PluggedApp() app.register_postparsing_hook(app.postparse_hook_stop) @@ -448,6 +468,7 @@ def test_postparsing_hook_stop_first(capsys): assert app.called_postparsing == 1 assert stop + def test_postparsing_hook_stop_second(capsys): app = PluggedApp() app.register_postparsing_hook(app.postparse_hook) @@ -469,6 +490,7 @@ def test_postparsing_hook_stop_second(capsys): assert app.called_postparsing == 2 assert stop + def test_postparsing_hook_emptystatement_first(capsys): app = PluggedApp() app.register_postparsing_hook(app.postparse_hook_emptystatement) @@ -489,6 +511,7 @@ def test_postparsing_hook_emptystatement_first(capsys): assert not err assert app.called_postparsing == 1 + def test_postparsing_hook_emptystatement_second(capsys): app = PluggedApp() app.register_postparsing_hook(app.postparse_hook) @@ -519,6 +542,7 @@ def test_postparsing_hook_emptystatement_second(capsys): assert not err assert app.called_postparsing == 2 + def test_postparsing_hook_exception(capsys): app = PluggedApp() app.register_postparsing_hook(app.postparse_hook_exception) @@ -539,6 +563,7 @@ def test_postparsing_hook_exception(capsys): assert err assert app.called_postparsing == 1 + ### # # test precmd hooks @@ -551,26 +576,31 @@ def test_register_precmd_hook_parameter_count(): 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') @@ -599,6 +629,7 @@ def test_precmd_hook(capsys): # with two hooks registered, we should get precmd() and both hooks assert app.called_precmd == 3 + def test_precmd_hook_emptystatement_first(capsys): app = PluggedApp() app.register_precmd_hook(app.precmd_hook_emptystatement) @@ -624,6 +655,7 @@ def test_precmd_hook_emptystatement_first(capsys): # called assert app.called_precmd == 1 + def test_precmd_hook_emptystatement_second(capsys): app = PluggedApp() app.register_precmd_hook(app.precmd_hook) @@ -660,6 +692,7 @@ def test_precmd_hook_emptystatement_second(capsys): # if a registered hook throws an exception, precmd() is never called assert app.called_precmd == 2 + ### # # test postcmd hooks @@ -672,26 +705,31 @@ def test_register_postcmd_hook_parameter_count(): 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): app = PluggedApp() app.onecmd_plus_hooks('say hello') @@ -720,6 +758,7 @@ def test_postcmd(capsys): # 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) @@ -746,6 +785,7 @@ def test_postcmd_exception_first(capsys): # called assert app.called_postcmd == 1 + def test_postcmd_exception_second(capsys): app = PluggedApp() app.register_postcmd_hook(app.postcmd_hook) @@ -771,6 +811,7 @@ def test_postcmd_exception_second(capsys): # the exception assert app.called_postcmd == 2 + ## # # command finalization @@ -783,26 +824,31 @@ def test_register_cmdfinalization_hook_parameter_count(): with pytest.raises(TypeError): app.register_cmdfinalization_hook(app.cmdfinalization_hook_too_many_parameters) + def test_register_cmdfinalization_hook_no_parameter_annotation(): app = PluggedApp() with pytest.raises(TypeError): app.register_cmdfinalization_hook(app.cmdfinalization_hook_no_parameter_annotation) + def test_register_cmdfinalization_hook_wrong_parameter_annotation(): app = PluggedApp() with pytest.raises(TypeError): app.register_cmdfinalization_hook(app.cmdfinalization_hook_wrong_parameter_annotation) + def test_register_cmdfinalization_hook_no_return_annotation(): app = PluggedApp() with pytest.raises(TypeError): app.register_cmdfinalization_hook(app.cmdfinalization_hook_no_return_annotation) + def test_register_cmdfinalization_hook_wrong_return_annotation(): app = PluggedApp() with pytest.raises(TypeError): app.register_cmdfinalization_hook(app.cmdfinalization_hook_wrong_return_annotation) + def test_cmdfinalization(capsys): app = PluggedApp() app.onecmd_plus_hooks('say hello') @@ -827,6 +873,7 @@ def test_cmdfinalization(capsys): assert not err assert app.called_cmdfinalization == 2 + def test_cmdfinalization_stop_first(capsys): app = PluggedApp() app.register_cmdfinalization_hook(app.cmdfinalization_hook_stop) @@ -838,6 +885,7 @@ def test_cmdfinalization_stop_first(capsys): assert app.called_cmdfinalization == 2 assert stop + def test_cmdfinalization_stop_second(capsys): app = PluggedApp() app.register_cmdfinalization_hook(app.cmdfinalization_hook) @@ -849,6 +897,7 @@ def test_cmdfinalization_stop_second(capsys): assert app.called_cmdfinalization == 2 assert stop + def test_cmdfinalization_hook_exception(capsys): app = PluggedApp() app.register_cmdfinalization_hook(app.cmdfinalization_hook_exception) @@ -869,6 +918,7 @@ def test_cmdfinalization_hook_exception(capsys): assert err assert app.called_cmdfinalization == 1 + def test_cmdfinalization_hook_system_exit(capsys): app = PluggedApp() app.register_cmdfinalization_hook(app.cmdfinalization_hook_system_exit) @@ -876,6 +926,7 @@ def test_cmdfinalization_hook_system_exit(capsys): assert stop assert app.called_cmdfinalization == 1 + def test_cmdfinalization_hook_keyboard_interrupt(capsys): app = PluggedApp() app.register_cmdfinalization_hook(app.cmdfinalization_hook_keyboard_interrupt) @@ -898,6 +949,7 @@ def test_cmdfinalization_hook_keyboard_interrupt(capsys): assert stop assert app.called_cmdfinalization == 1 + def test_skip_postcmd_hooks(capsys): app = PluggedApp() app.register_postcmd_hook(app.postcmd_hook) @@ -910,6 +962,7 @@ def test_skip_postcmd_hooks(capsys): assert app.called_postcmd == 0 assert app.called_cmdfinalization == 1 + def test_cmd2_argparse_exception(capsys): """ Verify Cmd2ArgparseErrors raised after calling a command prevent postcmd events from |
