diff options
author | Eric Lin <anselor@gmail.com> | 2020-07-21 17:18:38 -0400 |
---|---|---|
committer | anselor <anselor@gmail.com> | 2020-08-04 13:38:08 -0400 |
commit | c05d2b0480f284a83dfe868b1aad3d780174c289 (patch) | |
tree | ac3cb591b694718c7865e602181e870435f2ecf8 /isolated_tests | |
parent | 372676d479dff8bb07f9361deddd63b17ce7c9d3 (diff) | |
download | cmd2-git-c05d2b0480f284a83dfe868b1aad3d780174c289.tar.gz |
Removed support for functions outside of CommandSets
Diffstat (limited to 'isolated_tests')
-rw-r--r-- | isolated_tests/test_commandset/test_commandset.py | 209 |
1 files changed, 1 insertions, 208 deletions
diff --git a/isolated_tests/test_commandset/test_commandset.py b/isolated_tests/test_commandset/test_commandset.py index eedf51de..8de2d3b0 100644 --- a/isolated_tests/test_commandset/test_commandset.py +++ b/isolated_tests/test_commandset/test_commandset.py @@ -14,51 +14,6 @@ from cmd2 import utils from .conftest import complete_tester, normalize, run_cmd -@cmd2.register_command -@cmd2.with_category("AAA") -def do_unbound(cmd: cmd2.Cmd, statement: cmd2.Statement): - """ - This is an example of registering an unbound function - - :param cmd: - :param statement: - :return: - """ - cmd.poutput('Unbound Command: {}'.format(statement.args)) - - -@cmd2.register_command -@cmd2.with_category("AAA") -def do_command_with_support(cmd: cmd2.Cmd, statement: cmd2.Statement): - """ - This is an example of registering an unbound function - - :param cmd: - :param statement: - :return: - """ - cmd.poutput('Command with support functions: {}'.format(statement.args)) - - -def help_command_with_support(cmd: cmd2.Cmd): - cmd.poutput('Help for command_with_support') - - -def complete_command_with_support(cmd: cmd2.Cmd, text: str, line: str, begidx: int, endidx: int) -> List[str]: - """Completion function for do_index_based""" - food_item_strs = ['Pizza', 'Ham', 'Ham Sandwich', 'Potato'] - sport_item_strs = ['Bat', 'Basket', 'Basketball', 'Football', 'Space Ball'] - - index_dict = \ - { - 1: food_item_strs, # Tab complete food items at index 1 in command line - 2: sport_item_strs, # Tab complete sport items at index 2 in command line - 3: cmd.path_complete, # Tab complete using path_complete function at index 3 in command line - } - - return cmd.index_based_complete(text, line, begidx, endidx, index_dict=index_dict) - - @cmd2.with_default_category('Command Set') class CommandSetA(cmd2.CommandSet): def do_apple(self, cmd: cmd2.Cmd, statement: cmd2.Statement): @@ -126,9 +81,6 @@ def test_autoload_commands(command_sets_app): cmds_cats, cmds_doc, cmds_undoc, help_topics = command_sets_app._build_command_info() - assert 'AAA' in cmds_cats - assert 'unbound' in cmds_cats['AAA'] - assert 'Alone' in cmds_cats assert 'elderberry' in cmds_cats['Alone'] @@ -150,41 +102,6 @@ def test_custom_construct_commandsets(): def test_load_commands(command_sets_manual): - cmds_cats, cmds_doc, cmds_undoc, help_topics = command_sets_manual._build_command_info() - - # start by verifying none of the installable commands are present - assert 'AAA' not in cmds_cats - assert 'Alone' not in cmds_cats - assert 'Command Set' not in cmds_cats - - # install the `unbound` command - command_sets_manual.install_registered_command('unbound') - - # verify that the same registered command can't be installed twice - with pytest.raises(ValueError): - assert command_sets_manual.install_registered_command('unbound') - - # verifies detection of unregistered commands - with pytest.raises(KeyError): - assert command_sets_manual.install_registered_command('nonexistent_command') - - # verifies that a duplicate function name is detected - def do_unbound(cmd: cmd2.Cmd, statement: cmd2.Statement): - """ - This function duplicates an existing command - """ - cmd.poutput('Unbound Command: {}'.format(statement.args)) - - with pytest.raises(KeyError): - assert cmd2.register_command(do_unbound) - - # verify only the `unbound` command was installed - cmds_cats, cmds_doc, cmds_undoc, help_topics = command_sets_manual._build_command_info() - - assert 'AAA' in cmds_cats - assert 'unbound' in cmds_cats['AAA'] - assert 'Alone' not in cmds_cats - assert 'Command Set' not in cmds_cats # now install a command set and verify the commands are now present cmd_set = CommandSetA() @@ -192,22 +109,6 @@ def test_load_commands(command_sets_manual): cmds_cats, cmds_doc, cmds_undoc, help_topics = command_sets_manual._build_command_info() - assert 'AAA' in cmds_cats - assert 'unbound' in cmds_cats['AAA'] - - assert 'Alone' in cmds_cats - assert 'elderberry' in cmds_cats['Alone'] - - assert 'Command Set' in cmds_cats - assert 'cranberry' in cmds_cats['Command Set'] - - # uninstall the `unbound` command and verify only it was uninstalled - command_sets_manual.uninstall_command('unbound') - - cmds_cats, cmds_doc, cmds_undoc, help_topics = command_sets_manual._build_command_info() - - assert 'AAA' not in cmds_cats - assert 'Alone' in cmds_cats assert 'elderberry' in cmds_cats['Alone'] @@ -219,17 +120,14 @@ def test_load_commands(command_sets_manual): cmds_cats, cmds_doc, cmds_undoc, help_topics = command_sets_manual._build_command_info() - assert 'AAA' not in cmds_cats assert 'Alone' not in cmds_cats assert 'Command Set' not in cmds_cats - # reinstall the command set and verifyt is accessible but the `unbound` command isn't + # reinstall the command set and verify it is accessible command_sets_manual.install_command_set(cmd_set) cmds_cats, cmds_doc, cmds_undoc, help_topics = command_sets_manual._build_command_info() - assert 'AAA' not in cmds_cats - assert 'Alone' in cmds_cats assert 'elderberry' in cmds_cats['Alone'] @@ -237,111 +135,6 @@ def test_load_commands(command_sets_manual): assert 'cranberry' in cmds_cats['Command Set'] -def test_command_functions(command_sets_manual): - cmds_cats, cmds_doc, cmds_undoc, help_topics = command_sets_manual._build_command_info() - assert 'AAA' not in cmds_cats - - out, err = run_cmd(command_sets_manual, 'command_with_support') - assert 'is not a recognized command, alias, or macro' in err[0] - - out, err = run_cmd(command_sets_manual, 'help command_with_support') - assert 'No help on command_with_support' in err[0] - - text = '' - line = 'command_with_support' - endidx = len(line) - begidx = endidx - len(text) - - first_match = complete_tester(text, line, begidx, endidx, command_sets_manual) - assert first_match is None - - # A bad command name gets rejected with an exception - with pytest.raises(ValueError): - assert command_sets_manual.install_command_function('>"', - do_command_with_support, - complete_command_with_support, - help_command_with_support) - - # create an alias to verify that it gets removed when the command is created - out, err = run_cmd(command_sets_manual, 'alias create command_with_support run_pyscript') - assert out == normalize("Alias 'command_with_support' created") - - command_sets_manual.install_registered_command('command_with_support') - - cmds_cats, cmds_doc, cmds_undoc, help_topics = command_sets_manual._build_command_info() - assert 'AAA' in cmds_cats - assert 'command_with_support' in cmds_cats['AAA'] - - out, err = run_cmd(command_sets_manual, 'command_with_support') - assert 'Command with support functions' in out[0] - - out, err = run_cmd(command_sets_manual, 'help command_with_support') - assert 'Help for command_with_support' in out[0] - - first_match = complete_tester(text, line, begidx, endidx, command_sets_manual) - assert first_match == 'Ham' - - text = '' - line = 'command_with_support Ham' - endidx = len(line) - begidx = endidx - len(text) - - first_match = complete_tester(text, line, begidx, endidx, command_sets_manual) - - assert first_match == 'Basket' - - command_sets_manual.uninstall_command('command_with_support') - - cmds_cats, cmds_doc, cmds_undoc, help_topics = command_sets_manual._build_command_info() - assert 'AAA' not in cmds_cats - - out, err = run_cmd(command_sets_manual, 'command_with_support') - assert 'is not a recognized command, alias, or macro' in err[0] - - out, err = run_cmd(command_sets_manual, 'help command_with_support') - assert 'No help on command_with_support' in err[0] - - text = '' - line = 'command_with_support' - endidx = len(line) - begidx = endidx - len(text) - - first_match = complete_tester(text, line, begidx, endidx, command_sets_manual) - assert first_match is None - - # create an alias to verify that it gets removed when the command is created - out, err = run_cmd(command_sets_manual, 'macro create command_with_support run_pyscript') - assert out == normalize("Macro 'command_with_support' created") - - command_sets_manual.install_command_function('command_with_support', - do_command_with_support, - complete_command_with_support, - help_command_with_support) - - cmds_cats, cmds_doc, cmds_undoc, help_topics = command_sets_manual._build_command_info() - assert 'AAA' in cmds_cats - assert 'command_with_support' in cmds_cats['AAA'] - - out, err = run_cmd(command_sets_manual, 'command_with_support') - assert 'Command with support functions' in out[0] - - out, err = run_cmd(command_sets_manual, 'help command_with_support') - assert 'Help for command_with_support' in out[0] - - first_match = complete_tester(text, line, begidx, endidx, command_sets_manual) - assert first_match == 'Ham' - - text = '' - line = 'command_with_support Ham' - endidx = len(line) - begidx = endidx - len(text) - - first_match = complete_tester(text, line, begidx, endidx, command_sets_manual) - - assert first_match == 'Basket' - - - def test_partial_with_passthru(): def test_func(arg1, arg2): |