summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKevin Van Brunt <kmvanbrunt@gmail.com>2019-07-12 17:47:30 -0400
committerKevin Van Brunt <kmvanbrunt@gmail.com>2019-07-12 17:47:30 -0400
commit0b9794ab065c7f981fe8841e53c8cb7805026dcc (patch)
tree2694269caede9e21e2528a858a0ef9dbdbc9e5a9
parent5d1fcdba6f1674b46629e92bb3075c12d706af5f (diff)
downloadcmd2-git-0b9794ab065c7f981fe8841e53c8cb7805026dcc.tar.gz
Added more hint unit tests
-rw-r--r--cmd2/argparse_completer.py9
-rw-r--r--tests/test_argparse_completer.py75
2 files changed, 53 insertions, 31 deletions
diff --git a/cmd2/argparse_completer.py b/cmd2/argparse_completer.py
index 3a5cb64c..ae82a4a7 100644
--- a/cmd2/argparse_completer.py
+++ b/cmd2/argparse_completer.py
@@ -187,7 +187,9 @@ class AutoCompleter(object):
consumed_arg_values.setdefault(arg_state.action.dest, [])
consumed_arg_values[arg_state.action.dest].append(token)
- # Enumerate over the sliced list up to the token being completed
+ #############################################################################################
+ # Parse all but the last token
+ #############################################################################################
for loop_index, token in enumerate(tokens[self._token_start_index:-1]):
# If we're in a positional REMAINDER arg, force all future tokens to go to that
@@ -304,7 +306,10 @@ class AutoCompleter(object):
self._positional_actions[next_pos_arg_index].nargs == argparse.REMAINDER:
skip_remaining_flags = True
- # We have now parsed all tokens up to the one being completed and have enough information to do so.
+ #############################################################################################
+ # We have parsed all but the last token and have enough information to complete it
+ #############################################################################################
+
# Check if we are completing a flag name. This check ignores strings with a length of one, like '-'.
# This is because that could be the start of a negative number which may be a valid completion for
# the current argument. We will handle the completion of flags that start with only one prefix
diff --git a/tests/test_argparse_completer.py b/tests/test_argparse_completer.py
index 72efdc20..c477f92f 100644
--- a/tests/test_argparse_completer.py
+++ b/tests/test_argparse_completer.py
@@ -534,6 +534,7 @@ def test_completion_items(ac_app, num_aliases, show_description):
# REMAINDER positional. Flags don't work in REMAINDER
('the positional --set_value', remainder_choices),
+ ('the positional remainder --set_value', ['choices '])
])
def test_autcomp_nargs(ac_app, args, completions):
text = ''
@@ -614,45 +615,50 @@ def test_completion_items_default_header(ac_app):
assert DEFAULT_DESCRIPTIVE_HEADER in ac_app.completion_header
-def test_autocomp_hint_flag(ac_app, capsys):
- text = ''
- line = 'hint --flag {}'.format(text)
- endidx = len(line)
- begidx = endidx - len(text)
-
- first_match = complete_tester(text, line, begidx, endidx, ac_app)
- out, err = capsys.readouterr()
+@pytest.mark.parametrize('command_and_args, text, has_hint', [
+ # Normal cases
+ ('hint', '', True),
+ ('hint --flag', '', True),
+ ('hint --suppressed_help', '', False),
+ ('hint --suppressed_hint', '--', False),
- assert first_match is None and "Hint" in out
+ # Hint because flag does not have enough values to be considered finished
+ ('nargs --one_or_more', '-', True),
+ # This flag has reached its minimum value count and therefore a new flag could start.
+ # However the flag can still consume values and the text is not a single prefix character.
+ # Therefor a hint will be shown.
+ ('nargs --one_or_more choices', 'bad_completion', True),
-def test_autocomp_hint_suppressed_help(ac_app, capsys):
- text = ''
- line = 'hint --suppressed_help {}'.format(text)
- endidx = len(line)
- begidx = endidx - len(text)
-
- first_match = complete_tester(text, line, begidx, endidx, ac_app)
- out, err = capsys.readouterr()
+ # Like the previous case, but this time text is a single prefix character which will cause flag
+ # name completion to occur instead of a hint for the current flag.
+ ('nargs --one_or_more choices', '-', False),
- assert first_match is None
- assert not out
+ # Hint because this is a REMAINDER flag and therefore no more flag name completions occur.
+ ('nargs --remainder', '-', True),
+ # No hint for the positional because text is a single prefix character which results in flag name completion
+ ('hint', '-', False),
-def test_autocomp_hint_suppressed_hint(ac_app, capsys):
- text = ''
- line = 'hint --suppressed_hint {}'.format(text)
+ # Hint because this is a REMAINDER positional and therefore no more flag name completions occur.
+ ('nargs the choices', '-', True),
+ ('nargs the choices remainder', '-', True),
+])
+def test_autocomp_hint(ac_app, command_and_args, text, has_hint, capsys):
+ line = '{} {}'.format(command_and_args, text)
endidx = len(line)
begidx = endidx - len(text)
- first_match = complete_tester(text, line, begidx, endidx, ac_app)
+ complete_tester(text, line, begidx, endidx, ac_app)
out, err = capsys.readouterr()
- assert first_match is None
- assert not out
+ if has_hint:
+ assert "Hint" in out
+ else:
+ assert "Hint" not in out
-def test_autocomp_hint_pos(ac_app, capsys):
+def test_autocomp_hint_multiple_lines(ac_app, capsys):
text = ''
line = 'hint {}'.format(text)
endidx = len(line)
@@ -661,10 +667,16 @@ def test_autocomp_hint_pos(ac_app, capsys):
first_match = complete_tester(text, line, begidx, endidx, ac_app)
out, err = capsys.readouterr()
- assert first_match is None and "Hint" in out
+ assert first_match is None
+ assert out == '''
+Hint:
+ HINT_POS here is a hint
+ with new lines
+
+'''
-def test_autocomp_hint_no_help(ac_app, capsys):
+def test_autocomp_hint_no_help_text(ac_app, capsys):
text = ''
line = 'hint foo {}'.format(text)
endidx = len(line)
@@ -673,7 +685,12 @@ def test_autocomp_hint_no_help(ac_app, capsys):
first_match = complete_tester(text, line, begidx, endidx, ac_app)
out, err = capsys.readouterr()
- assert first_match is None and "Hint" in out
+ assert first_match is None
+ assert not out == '''
+Hint:
+ NO_HELP_POS
+
+'''
def test_single_prefix_char():