summaryrefslogtreecommitdiff
path: root/tests/test_autocompletion.py
diff options
context:
space:
mode:
authorkmvanbrunt <kmvanbrunt@gmail.com>2018-10-11 14:07:50 -0400
committerGitHub <noreply@github.com>2018-10-11 14:07:50 -0400
commit473cb08237b402658e857d786d5294defe721ec7 (patch)
tree322051b16a75531aa8015c494b3f17c5caafee60 /tests/test_autocompletion.py
parentf38e100fd77f4a136a4883d23b2f4f8b3cd934b7 (diff)
parent8bed2448ede91fc5cb7d8ff1044a75650350810e (diff)
downloadcmd2-git-473cb08237b402658e857d786d5294defe721ec7.tar.gz
Merge pull request #573 from python-cmd2/double_dash
Double dash
Diffstat (limited to 'tests/test_autocompletion.py')
-rw-r--r--tests/test_autocompletion.py63
1 files changed, 63 insertions, 0 deletions
diff --git a/tests/test_autocompletion.py b/tests/test_autocompletion.py
index 3473ab38..7285af5c 100644
--- a/tests/test_autocompletion.py
+++ b/tests/test_autocompletion.py
@@ -279,3 +279,66 @@ def test_autcomp_custom_func_list_and_dict_arg(cmd2_app):
cmd2_app.completion_matches == ['S01E02', 'S01E03', 'S02E01', 'S02E03']
+def test_argparse_remainder_flag_completion(cmd2_app):
+ import cmd2
+ import argparse
+
+ # Test flag completion as first arg of positional with nargs=argparse.REMAINDER
+ text = '--h'
+ line = 'help command {}'.format(text)
+ endidx = len(line)
+ begidx = endidx - len(text)
+
+ # --h should not complete into --help because we are in the argparse.REMAINDER section
+ assert complete_tester(text, line, begidx, endidx, cmd2_app) is None
+
+ # Test flag completion within an already started positional with nargs=argparse.REMAINDER
+ text = '--h'
+ line = 'help command subcommand {}'.format(text)
+ endidx = len(line)
+ begidx = endidx - len(text)
+
+ # --h should not complete into --help because we are in the argparse.REMAINDER section
+ assert complete_tester(text, line, begidx, endidx, cmd2_app) is None
+
+ # Test a flag with nargs=argparse.REMAINDER
+ parser = argparse.ArgumentParser()
+ parser.add_argument('-f', nargs=argparse.REMAINDER)
+
+ # Overwrite eof's parser for this test
+ cmd2.Cmd.do_eof.argparser = parser
+
+ text = '--h'
+ line = 'eof -f {}'.format(text)
+ endidx = len(line)
+ begidx = endidx - len(text)
+
+ # --h should not complete into --help because we are in the argparse.REMAINDER section
+ assert complete_tester(text, line, begidx, endidx, cmd2_app) is None
+
+
+def test_completion_after_double_dash(cmd2_app):
+ """
+ Test completion after --, which argparse says (all args after -- are non-options)
+ All of these tests occur outside of an argparse.REMAINDER section since those tests
+ are handled in test_argparse_remainder_flag_completion
+ """
+
+ # Test -- as the last token
+ text = '--'
+ line = 'help {}'.format(text)
+ endidx = len(line)
+ begidx = endidx - len(text)
+
+ # Since -- is the last token, then it should show flag choices
+ first_match = complete_tester(text, line, begidx, endidx, cmd2_app)
+ assert first_match is not None and '--help' in cmd2_app.completion_matches
+
+ # Test -- to end all flag completion
+ text = '--'
+ line = 'help -- {}'.format(text)
+ endidx = len(line)
+ begidx = endidx - len(text)
+
+ # Since -- appeared before the -- being completed, nothing should be completed
+ assert complete_tester(text, line, begidx, endidx, cmd2_app) is None