diff options
author | Eric Lin <anselor@gmail.com> | 2018-04-21 22:29:18 -0400 |
---|---|---|
committer | Eric Lin <anselor@gmail.com> | 2018-04-21 22:29:18 -0400 |
commit | 965fa83804fec8ba3df8209b253e11acfb015d37 (patch) | |
tree | 5e4c3bb9f48f72a24dcddb35fdb83c07428c6d5b /tests | |
parent | 85c2c6bba46900af6012b54c31e650095194b1aa (diff) | |
download | cmd2-git-965fa83804fec8ba3df8209b253e11acfb015d37.tar.gz |
Switched the default behavior in cmd2 for argparse commands to use the AutoCompleter by default.
Diffstat (limited to 'tests')
-rw-r--r-- | tests/test_completion.py | 89 |
1 files changed, 9 insertions, 80 deletions
diff --git a/tests/test_completion.py b/tests/test_completion.py index a01d1166..cf45f281 100644 --- a/tests/test_completion.py +++ b/tests/test_completion.py @@ -14,7 +14,8 @@ import sys import cmd2 import pytest -from .conftest import complete_tester +from .conftest import complete_tester, StdOut +from examples.subcommands import SubcommandsExample # List of strings used with completion functions food_item_strs = ['Pizza', 'Ham', 'Ham Sandwich', 'Potato'] @@ -726,76 +727,13 @@ 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 -class SubcommandsExample(cmd2.Cmd): - """ - Example cmd2 application where we a base command which has a couple subcommands - and the "sport" subcommand has tab completion enabled. - """ - - def __init__(self): - cmd2.Cmd.__init__(self) - - # subcommand functions for the base command - def base_foo(self, args): - """foo subcommand of base command""" - self.poutput(args.x * args.y) - - def base_bar(self, args): - """bar subcommand of base command""" - self.poutput('((%s))' % args.z) - - def base_sport(self, args): - """sport subcommand of base command""" - self.poutput('Sport is {}'.format(args.sport)) - - # noinspection PyUnusedLocal - def complete_base_sport(self, text, line, begidx, endidx): - """ Adds tab completion to base sport subcommand """ - index_dict = {1: sport_item_strs} - return self.index_based_complete(text, line, begidx, endidx, index_dict) - - # create the top-level parser for the base command - base_parser = argparse.ArgumentParser(prog='base') - base_subparsers = base_parser.add_subparsers(title='subcommands', help='subcommand help') - - # create the parser for the "foo" subcommand - parser_foo = base_subparsers.add_parser('foo', help='foo help') - parser_foo.add_argument('-x', type=int, default=1, help='integer') - parser_foo.add_argument('y', type=float, help='float') - parser_foo.set_defaults(func=base_foo) - - # create the parser for the "bar" subcommand - parser_bar = base_subparsers.add_parser('bar', help='bar help') - parser_bar.add_argument('z', help='string') - parser_bar.set_defaults(func=base_bar) - - # create the parser for the "sport" subcommand - parser_sport = base_subparsers.add_parser('sport', help='sport help') - parser_sport.add_argument('sport', help='Enter name of a sport') - - # Set both a function and tab completer for the "sport" subcommand - parser_sport.set_defaults(func=base_sport, completer=complete_base_sport) - - @cmd2.with_argparser(base_parser) - def do_base(self, args): - """Base command help""" - func = getattr(args, 'func', None) - if func is not None: - # Call whatever subcommand function was selected - func(self, args) - else: - # No subcommand was provided, so call help - self.do_help('base') - - # Enable tab completion of base to make sure the subcommands' completers get called. - complete_base = cmd2.Cmd.cmd_with_subs_completer - @pytest.fixture def sc_app(): - app = SubcommandsExample() - return app + c = SubcommandsExample() + c.stdout = StdOut() + return c def test_cmd2_subcommand_completion_single_end(sc_app): text = 'f' @@ -913,12 +851,6 @@ class SubcommandsWithUnknownExample(cmd2.Cmd): """sport subcommand of base command""" self.poutput('Sport is {}'.format(args.sport)) - # noinspection PyUnusedLocal - def complete_base_sport(self, text, line, begidx, endidx): - """ Adds tab completion to base sport subcommand """ - index_dict = {1: sport_item_strs} - return self.index_based_complete(text, line, begidx, endidx, index_dict) - # create the top-level parser for the base command base_parser = argparse.ArgumentParser(prog='base') base_subparsers = base_parser.add_subparsers(title='subcommands', help='subcommand help') @@ -936,10 +868,8 @@ class SubcommandsWithUnknownExample(cmd2.Cmd): # create the parser for the "sport" subcommand parser_sport = base_subparsers.add_parser('sport', help='sport help') - parser_sport.add_argument('sport', help='Enter name of a sport') - - # Set both a function and tab completer for the "sport" subcommand - parser_sport.set_defaults(func=base_sport, completer=complete_base_sport) + sport_arg = parser_sport.add_argument('sport', help='Enter name of a sport') + setattr(sport_arg, 'arg_choices', sport_item_strs) @cmd2.with_argparser_and_unknown_args(base_parser) def do_base(self, args): @@ -952,9 +882,6 @@ class SubcommandsWithUnknownExample(cmd2.Cmd): # No subcommand was provided, so call help self.do_help('base') - # Enable tab completion of base to make sure the subcommands' completers get called. - complete_base = cmd2.Cmd.cmd_with_subs_completer - @pytest.fixture def scu_app(): @@ -971,6 +898,8 @@ def test_cmd2_subcmd_with_unknown_completion_single_end(scu_app): first_match = complete_tester(text, line, begidx, endidx, scu_app) + print('first_match: {}'.format(first_match)) + # It is at end of line, so extra space is present assert first_match is not None and scu_app.completion_matches == ['foo '] |