diff options
author | Kevin Van Brunt <kmvanbrunt@gmail.com> | 2019-07-07 22:44:33 -0400 |
---|---|---|
committer | Kevin Van Brunt <kmvanbrunt@gmail.com> | 2019-07-07 22:44:33 -0400 |
commit | 8d3d59801ca690f718ed9814c9e124e27040c141 (patch) | |
tree | 667512e1efa069beaadbd4bdc757df8eacff33c3 | |
parent | bb2dd69bd04f5dccff9474c018eb6b6eea74c6af (diff) | |
download | cmd2-git-8d3d59801ca690f718ed9814c9e124e27040c141.tar.gz |
More argparse completer unit tests
-rwxr-xr-x | examples/tab_autocompletion.py | 12 | ||||
-rw-r--r-- | tests/test_argparse_completer.py | 89 |
2 files changed, 71 insertions, 30 deletions
diff --git a/examples/tab_autocompletion.py b/examples/tab_autocompletion.py index a6d5487d..c4fc6218 100755 --- a/examples/tab_autocompletion.py +++ b/examples/tab_autocompletion.py @@ -177,9 +177,9 @@ class TabCompleteExample(cmd2.Cmd): if not args.type: self.do_help('orig_suggest') - def _do_vid_media_movies(self, args) -> None: + def _do_vid_movies(self, args) -> None: if not args.command: - self.do_help('media movies') + self.do_help('video movies') elif args.command == 'list': for movie_id in TabCompleteExample.MOVIE_DATABASE: movie = TabCompleteExample.MOVIE_DATABASE[movie_id] @@ -188,9 +188,9 @@ class TabCompleteExample(cmd2.Cmd): ', '.join(movie['director']), '\n '.join(movie['actor']))) - def _do_vid_media_shows(self, args) -> None: + def _do_vid_shows(self, args) -> None: if not args.command: - self.do_help('media shows') + self.do_help('video shows') elif args.command == 'list': for show_id in TabCompleteExample.SHOW_DATABASE: @@ -209,7 +209,7 @@ class TabCompleteExample(cmd2.Cmd): video_types_subparsers = video_parser.add_subparsers(title='Media Types', dest='type') vid_movies_parser = video_types_subparsers.add_parser('movies') - vid_movies_parser.set_defaults(func=_do_vid_media_movies) + vid_movies_parser.set_defaults(func=_do_vid_movies) vid_movies_commands_subparsers = vid_movies_parser.add_subparsers(title='Commands', dest='command') @@ -243,7 +243,7 @@ class TabCompleteExample(cmd2.Cmd): descriptive_header='Title') vid_shows_parser = video_types_subparsers.add_parser('shows') - vid_shows_parser.set_defaults(func=_do_vid_media_shows) + vid_shows_parser.set_defaults(func=_do_vid_shows) vid_shows_commands_subparsers = vid_shows_parser.add_subparsers(title='Commands', dest='command') 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', [ |