summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorKevin Van Brunt <kmvanbrunt@gmail.com>2019-07-07 22:44:33 -0400
committerKevin Van Brunt <kmvanbrunt@gmail.com>2019-07-07 22:44:33 -0400
commit8d3d59801ca690f718ed9814c9e124e27040c141 (patch)
tree667512e1efa069beaadbd4bdc757df8eacff33c3 /tests
parentbb2dd69bd04f5dccff9474c018eb6b6eea74c6af (diff)
downloadcmd2-git-8d3d59801ca690f718ed9814c9e124e27040c141.tar.gz
More argparse completer unit tests
Diffstat (limited to 'tests')
-rw-r--r--tests/test_argparse_completer.py89
1 files changed, 65 insertions, 24 deletions
diff --git a/tests/test_argparse_completer.py b/tests/test_argparse_completer.py
index 6e092619..fd43c91c 100644
--- a/tests/test_argparse_completer.py
+++ b/tests/test_argparse_completer.py
@@ -33,13 +33,60 @@ def completer_function(text: str, line: str, begidx: int, endidx: int) -> List[s
return basic_complete(text, line, begidx, endidx, completions_from_function)
-# noinspection PyMethodMayBeStatic
+# noinspection PyMethodMayBeStatic,PyUnusedLocal
class AutoCompleteTester(cmd2.Cmd):
"""Cmd2 app that exercises AutoCompleter class"""
def __init__(self):
super().__init__()
############################################################################################################
+ # Begin code related to help and command name completion
+ ############################################################################################################
+ def _music_create(self, args: argparse.Namespace) -> None:
+ """Implements the 'music create' command"""
+ self.poutput('music create')
+
+ def _music_create_jazz(self, args: argparse.Namespace) -> None:
+ """Implements the 'music create jazz' command"""
+ self.poutput('music create jazz')
+
+ def _music_create_rock(self, args: argparse.Namespace) -> None:
+ """Implements the 'music create rock' command"""
+ self.poutput('music create rock')
+
+ # Top level parser for music command
+ music_parser = Cmd2ArgParser(description='Manage music', prog='music')
+
+ # Add sub-commands to music
+ music_subparsers = music_parser.add_subparsers()
+
+ # music -> create
+ music_create_parser = music_subparsers.add_parser('create', help='Create music')
+ music_create_parser.set_defaults(func=_music_create)
+
+ # Add sub-commands to music -> create
+ music_create_subparsers = music_create_parser.add_subparsers()
+
+ # music -> create -> jazz
+ music_create_jazz_parser = music_create_subparsers.add_parser('jazz', help='Create jazz')
+ music_create_jazz_parser.set_defaults(func=_music_create_jazz)
+
+ # music -> create -> rock
+ music_create_rock_parser = music_create_subparsers.add_parser('rock', help='Create rocks')
+ music_create_rock_parser.set_defaults(func=_music_create_rock)
+
+ @with_argparser(music_parser)
+ def do_music(self, args: argparse.Namespace) -> None:
+ """Music command"""
+ func = getattr(args, 'func', None)
+ if func is not None:
+ # Call whatever sub-command function was selected
+ func(self, args)
+ else:
+ # No sub-command was provided, so call help
+ self.do_help('music')
+
+ ############################################################################################################
# Begin code related to testing choices, choices_function, and choices_method parameters
############################################################################################################
def choices_method(self) -> List[str]:
@@ -126,36 +173,30 @@ def ac_app():
return app
-def test_help(ac_app):
- out1, err1 = run_cmd(ac_app, 'alias -h')
- out2, err2 = run_cmd(ac_app, 'help alias')
- assert out1 == out2
-
-
-def test_help_subcommand(ac_app):
- out1, err1 = run_cmd(ac_app, 'alias create -h')
- out2, err2 = run_cmd(ac_app, 'help alias create')
+@pytest.mark.parametrize('command', [
+ 'music',
+ 'music create',
+ 'music create rock',
+ 'music create jazz'
+])
+def test_help(ac_app, command):
+ out1, err1 = run_cmd(ac_app, '{} -h'.format(command))
+ out2, err2 = run_cmd(ac_app, 'help {}'.format(command))
assert out1 == out2
-def test_complete_help(ac_app):
- text = 'al'
- line = 'help {}'.format(text)
- endidx = len(line)
- begidx = endidx - len(text)
-
- first_match = complete_tester(text, line, begidx, endidx, ac_app)
- assert first_match is not None and ac_app.completion_matches == ['alias ']
-
-
-def test_complete_help_subcommand(ac_app):
- text = 'cre'
- line = 'help alias {}'.format(text)
+@pytest.mark.parametrize('command, text, completions', [
+ ('', 'mu', ['music ']),
+ ('music', 'cre', ['create ']),
+ ('music create', '', ['jazz', 'rock'])
+])
+def test_complete_help(ac_app, command, text, completions):
+ line = 'help {} {}'.format(command, text)
endidx = len(line)
begidx = endidx - len(text)
first_match = complete_tester(text, line, begidx, endidx, ac_app)
- assert first_match is not None and ac_app.completion_matches == ['create ']
+ assert first_match is not None and ac_app.completion_matches == completions
@pytest.mark.parametrize('text, completions', [