From a33da043985257e1ed0ac7b3524e27d62611b1ac Mon Sep 17 00:00:00 2001 From: Kevin Van Brunt Date: Tue, 1 Sep 2020 17:23:03 -0400 Subject: Added unit tests for CommandSet callbacks --- tests_isolated/test_commandset/test_commandset.py | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) (limited to 'tests_isolated') diff --git a/tests_isolated/test_commandset/test_commandset.py b/tests_isolated/test_commandset/test_commandset.py index 5b670601..407c386f 100644 --- a/tests_isolated/test_commandset/test_commandset.py +++ b/tests_isolated/test_commandset/test_commandset.py @@ -21,6 +21,18 @@ class CommandSetBase(cmd2.CommandSet): @cmd2.with_default_category('Fruits') class CommandSetA(CommandSetBase): + def on_register(self, cmd) -> None: + super().on_register(cmd) + print("in on_register now") + + def on_registered(self) -> None: + super().on_registered() + print("in on_registered now") + + def on_unregister(self) -> None: + print("in on_unregister now") + super().on_unregister() + def do_apple(self, statement: cmd2.Statement): self._cmd.poutput('Apple!') @@ -158,7 +170,7 @@ def test_custom_construct_commandsets(): assert command_set_2 not in matches -def test_load_commands(command_sets_manual): +def test_load_commands(command_sets_manual, capsys): # now install a command set and verify the commands are now present cmd_set = CommandSetA() @@ -171,6 +183,11 @@ def test_load_commands(command_sets_manual): assert command_sets_manual.find_commandsets(CommandSetA)[0] is cmd_set assert command_sets_manual.find_commandset_for_command('elderberry') is cmd_set + # Make sure registration callbacks ran + out, err = capsys.readouterr() + assert "in on_register now" in out + assert "in on_registered now" in out + cmds_cats, cmds_doc, cmds_undoc, help_topics = command_sets_manual._build_command_info() assert 'Alone' in cmds_cats @@ -192,6 +209,10 @@ def test_load_commands(command_sets_manual): assert 'Alone' not in cmds_cats assert 'Fruits' not in cmds_cats + # Make sure unregistration callback ran + out, err = capsys.readouterr() + assert "in on_unregister now" in out + # uninstall a second time and verify no errors happen command_sets_manual.unregister_command_set(cmd_set) -- cgit v1.2.1 From 72fc6bf105f4b712d5a6f991746fd2f9084893fc Mon Sep 17 00:00:00 2001 From: Kevin Van Brunt Date: Tue, 1 Sep 2020 20:20:34 -0400 Subject: Updated docs --- tests_isolated/test_commandset/test_commandset.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) (limited to 'tests_isolated') diff --git a/tests_isolated/test_commandset/test_commandset.py b/tests_isolated/test_commandset/test_commandset.py index 407c386f..cbac7654 100644 --- a/tests_isolated/test_commandset/test_commandset.py +++ b/tests_isolated/test_commandset/test_commandset.py @@ -40,7 +40,7 @@ class CommandSetA(CommandSetBase): """Banana Command""" self._cmd.poutput('Banana!!') - cranberry_parser = cmd2.Cmd2ArgumentParser('cranberry') + cranberry_parser = cmd2.Cmd2ArgumentParser() cranberry_parser.add_argument('arg1', choices=['lemonade', 'juice', 'sauce']) @cmd2.with_argparser(cranberry_parser, with_unknown_args=True) @@ -65,7 +65,7 @@ class CommandSetA(CommandSetBase): def complete_durian(self, text: str, line: str, begidx: int, endidx: int) -> List[str]: return utils.basic_complete(text, line, begidx, endidx, ['stinks', 'smells', 'disgusting']) - elderberry_parser = cmd2.Cmd2ArgumentParser('elderberry') + elderberry_parser = cmd2.Cmd2ArgumentParser() elderberry_parser.add_argument('arg1') @cmd2.with_category('Alone') @@ -319,7 +319,7 @@ class LoadableBase(cmd2.CommandSet): self._dummy = dummy # prevents autoload self._cut_called = False - cut_parser = cmd2.Cmd2ArgumentParser('cut') + cut_parser = cmd2.Cmd2ArgumentParser() cut_subparsers = cut_parser.add_subparsers(title='item', help='item to cut') def namespace_provider(self) -> argparse.Namespace: @@ -340,8 +340,7 @@ class LoadableBase(cmd2.CommandSet): self._cmd.pwarning('This command does nothing without sub-parsers registered') self._cmd.do_help('cut') - - stir_parser = cmd2.Cmd2ArgumentParser('stir') + stir_parser = cmd2.Cmd2ArgumentParser() stir_subparsers = stir_parser.add_subparsers(title='item', help='what to stir') @cmd2.with_argparser(stir_parser, ns_provider=namespace_provider) @@ -613,7 +612,7 @@ class AppWithSubCommands(cmd2.Cmd): def __init__(self, *args, **kwargs): super(AppWithSubCommands, self).__init__(*args, **kwargs) - cut_parser = cmd2.Cmd2ArgumentParser('cut') + cut_parser = cmd2.Cmd2ArgumentParser() cut_subparsers = cut_parser.add_subparsers(title='item', help='item to cut') @cmd2.with_argparser(cut_parser) @@ -874,7 +873,7 @@ def test_bad_subcommand(): def __init__(self, *args, **kwargs): super(BadSubcommandApp, self).__init__(*args, **kwargs) - cut_parser = cmd2.Cmd2ArgumentParser('cut') + cut_parser = cmd2.Cmd2ArgumentParser() cut_subparsers = cut_parser.add_subparsers(title='item', help='item to cut') @cmd2.with_argparser(cut_parser) -- cgit v1.2.1 From 68c7750765ba9a4035775a5f46f8b37339935135 Mon Sep 17 00:00:00 2001 From: Kevin Van Brunt Date: Thu, 3 Sep 2020 11:14:15 -0400 Subject: Added CommandSet.on_unregistered() --- tests_isolated/test_commandset/test_commandset.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'tests_isolated') diff --git a/tests_isolated/test_commandset/test_commandset.py b/tests_isolated/test_commandset/test_commandset.py index cbac7654..1685accf 100644 --- a/tests_isolated/test_commandset/test_commandset.py +++ b/tests_isolated/test_commandset/test_commandset.py @@ -30,8 +30,12 @@ class CommandSetA(CommandSetBase): print("in on_registered now") def on_unregister(self) -> None: - print("in on_unregister now") super().on_unregister() + print("in on_unregister now") + + def on_unregistered(self) -> None: + super().on_unregistered() + print("in on_unregistered now") def do_apple(self, statement: cmd2.Statement): self._cmd.poutput('Apple!') @@ -209,9 +213,10 @@ def test_load_commands(command_sets_manual, capsys): assert 'Alone' not in cmds_cats assert 'Fruits' not in cmds_cats - # Make sure unregistration callback ran + # Make sure unregistration callbacks ran out, err = capsys.readouterr() assert "in on_unregister now" in out + assert "in on_unregistered now" in out # uninstall a second time and verify no errors happen command_sets_manual.unregister_command_set(cmd_set) -- cgit v1.2.1