diff options
author | Kevin Van Brunt <kmvanbrunt@gmail.com> | 2019-07-10 16:22:47 -0400 |
---|---|---|
committer | Kevin Van Brunt <kmvanbrunt@gmail.com> | 2019-07-10 16:22:47 -0400 |
commit | 782bab855b0ec1d1b9728a322b932f99e6fb3849 (patch) | |
tree | 7f02393cd0e0b3352badfc1b376c88e299e64e3d /tests/test_argparse_completer.py | |
parent | 60e294ee645e092b8713d5cf60f8797d3685416a (diff) | |
download | cmd2-git-782bab855b0ec1d1b9728a322b932f99e6fb3849.tar.gz |
Fixed some double-dash handling logic added unit tests
Diffstat (limited to 'tests/test_argparse_completer.py')
-rw-r--r-- | tests/test_argparse_completer.py | 102 |
1 files changed, 57 insertions, 45 deletions
diff --git a/tests/test_argparse_completer.py b/tests/test_argparse_completer.py index 404ba10b..cf4ac7b3 100644 --- a/tests/test_argparse_completer.py +++ b/tests/test_argparse_completer.py @@ -19,6 +19,11 @@ static_choices_list = ['static', 'choices', 'stop', 'here'] choices_from_function = ['choices', 'function', 'chatty', 'smith'] choices_from_method = ['choices', 'method', 'most', 'improved'] +set_value_choices = ['set', 'value', 'choices'] +one_or_more_choices = ['one', 'or', 'more', 'choices'] +optional_choices = ['optional', 'choices'] +remainder_choices = ['remainder', 'choices'] + completions_from_function = ['completions', 'function', 'fairly', 'complete'] completions_from_method = ['completions', 'method', 'missed', 'spot'] @@ -185,11 +190,13 @@ class AutoCompleteTester(cmd2.Cmd): # Flag args for nargs command nargs_parser.add_argument("--set_value", help="a flag with a set value for nargs", nargs=2, - choices=static_choices_list) + choices=set_value_choices) nargs_parser.add_argument("--one_or_more", help="a flag wanting one or more args", nargs=argparse.ONE_OR_MORE, - choices=static_choices_list) + choices=one_or_more_choices) + nargs_parser.add_argument("--optional", help="a flag with an optional value", nargs=argparse.OPTIONAL, + choices=optional_choices) nargs_parser.add_argument("--remainder", help="a flag wanting remaining", nargs=argparse.REMAINDER, - choices=static_choices_list) + choices=remainder_choices) @with_argparser(nargs_parser) def do_nargs(self, args: argparse.Namespace) -> None: @@ -218,48 +225,6 @@ def ac_app(): return app -@pytest.mark.parametrize('args, completions', [ - # Flag with nargs = 2 - ('--set_value', static_choices_list), - ('--set_value static', ['choices', 'stop', 'here']), - ('--set_value static choices', []), - - # Using the flag again will reset the choices available - ('--set_value static choices --set_value', static_choices_list), - - # Flag with nargs = ONE_OR_MORE - ('--one_or_more', static_choices_list), - ('--one_or_more static', ['choices', 'stop', 'here']), - ('--one_or_more static choices', ['stop', 'here']), - - # No more flags after a double dash - ('-- --one_or_more static choices', []), - - # Flag with nargs = REMAINDER - ('--remainder', static_choices_list), - ('--remainder static ', ['choices', 'stop', 'here']), - - # No more flags can appear after a REMAINDER flag) - ('--remainder static --set_value', ['choices', 'stop', 'here']), - - # Double dash ends a remainder flag - ('--remainder static --', []) -]) -def test_autcomp_nargs(ac_app, args, completions): - text = '' - line = 'nargs {} {}'.format(args, text) - endidx = len(line) - begidx = endidx - len(text) - - first_match = complete_tester(text, line, begidx, endidx, ac_app) - if completions: - assert first_match is not None - else: - assert first_match is None - - assert ac_app.completion_matches == sorted(completions, key=ac_app.matches_sort_key) - - @pytest.mark.parametrize('command', [ 'music', 'music create', @@ -465,6 +430,53 @@ def test_completion_items(ac_app, num_aliases, show_description): assert ('help' in ac_app.display_matches[0]) == show_description +@pytest.mark.parametrize('args, completions', [ + # Flag with nargs = 2 + ('--set_value', set_value_choices), + ('--set_value set', ['value', 'choices']), + ('--set_value set value choices', []), + + # Another flag can't start until all expected args are filled out + ('--set_value --one_or_more', set_value_choices), + + # Using the flag again will reset the choices available + ('--set_value set value --set_value', set_value_choices), + + # Flag with nargs = ONE_OR_MORE + ('--one_or_more', one_or_more_choices), + ('--one_or_more one', ['or', 'more', 'choices']), + + # Flag with nargs = REMAINDER + ('--remainder', remainder_choices), + ('--remainder remainder ', ['choices ']), + + # No more flags can appear after a REMAINDER flag) + ('--remainder choices --set_value', ['remainder ']), + + # Double dash ends the current flag (even if all expected args aren't entered) + ('--set_value --', []), + + # Double dash ends a REMAINDER flag + ('--remainder remainder --', []), + + # No more flags after a double dash + ('-- --one_or_more ', []), +]) +def test_autcomp_nargs(ac_app, args, completions): + text = '' + line = 'nargs {} {}'.format(args, text) + endidx = len(line) + begidx = endidx - len(text) + + first_match = complete_tester(text, line, begidx, endidx, ac_app) + if completions: + assert first_match is not None + else: + assert first_match is None + + assert ac_app.completion_matches == sorted(completions, key=ac_app.matches_sort_key) + + def test_completion_items_default_header(ac_app): from cmd2.argparse_completer import DEFAULT_DESCRIPTIVE_HEADER |