diff options
Diffstat (limited to 'tests/test_autocompletion.py')
-rw-r--r-- | tests/test_autocompletion.py | 63 |
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 |