diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/test_acargparse.py | 18 | ||||
-rw-r--r-- | tests/test_autocompletion.py | 48 | ||||
-rw-r--r-- | tests/test_completion.py | 25 |
3 files changed, 57 insertions, 34 deletions
diff --git a/tests/test_acargparse.py b/tests/test_acargparse.py index 617afd4f..b6abc444 100644 --- a/tests/test_acargparse.py +++ b/tests/test_acargparse.py @@ -5,7 +5,7 @@ Copyright 2018 Eric Lin <anselor@gmail.com> Released under MIT license, see LICENSE file """ import pytest -from cmd2.argparse_completer import ACArgumentParser, token_resembles_flag +from cmd2.argparse_completer import ACArgumentParser, is_potential_flag def test_acarg_narg_empty_tuple(): @@ -53,16 +53,16 @@ def test_acarg_narg_tuple_zero_to_one(): parser.add_argument('tuple', nargs=(0, 1)) -def test_token_resembles_flag(): +def test_is_potential_flag(): parser = ACArgumentParser() # Not valid flags - assert not token_resembles_flag('', parser) - assert not token_resembles_flag('non-flag', parser) - assert not token_resembles_flag('-', parser) - assert not token_resembles_flag('--has space', parser) - assert not token_resembles_flag('-2', parser) + assert not is_potential_flag('', parser) + assert not is_potential_flag('non-flag', parser) + assert not is_potential_flag('-', parser) + assert not is_potential_flag('--has space', parser) + assert not is_potential_flag('-2', parser) # Valid flags - assert token_resembles_flag('-flag', parser) - assert token_resembles_flag('--flag', parser) + assert is_potential_flag('-flag', parser) + assert is_potential_flag('--flag', parser) diff --git a/tests/test_autocompletion.py b/tests/test_autocompletion.py index 3473ab38..34155d88 100644 --- a/tests/test_autocompletion.py +++ b/tests/test_autocompletion.py @@ -279,3 +279,51 @@ def test_autcomp_custom_func_list_and_dict_arg(cmd2_app): cmd2_app.completion_matches == ['S01E02', 'S01E03', 'S02E01', 'S02E03'] +def test_argparse_remainder_completion(cmd2_app): + import cmd2 + import argparse + + # First test a 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 sections + assert complete_tester(text, line, begidx, endidx, cmd2_app) is None + + # Now 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 sections + assert complete_tester(text, line, begidx, endidx, cmd2_app) is None + + +def test_completion_after_double_dash(cmd2_app): + # Test -- as the last token before an argparse.REMAINDER sections + text = '--' + line = 'help {}'.format(text) + endidx = len(line) + begidx = endidx - len(text) + + # Since -- is the last token in a non-remainder section, 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, no more flags should be completed + assert complete_tester(text, line, begidx, endidx, cmd2_app) is None diff --git a/tests/test_completion.py b/tests/test_completion.py index ed36eb01..0df06423 100644 --- a/tests/test_completion.py +++ b/tests/test_completion.py @@ -716,31 +716,6 @@ def test_add_opening_quote_delimited_space_in_prefix(cmd2_app): os.path.commonprefix(cmd2_app.completion_matches) == expected_common_prefix and \ cmd2_app.display_matches == expected_display -def test_argparse_remainder_completion(cmd2_app): - # First test a 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 sections - assert complete_tester(text, line, begidx, endidx, cmd2_app) is None - - # Now 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 sections - assert complete_tester(text, line, begidx, endidx, cmd2_app) is None - @pytest.fixture def sc_app(): c = SubcommandsExample() |