Skip to content

Commit 68e7528

Browse files
committed
Updated arg_tokens unit tests
1 parent 89fa7fb commit 68e7528

File tree

1 file changed

+15
-50
lines changed

1 file changed

+15
-50
lines changed

tests/test_argparse_completer.py

Lines changed: 15 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,6 @@
3030
completions_from_function = ['completions', 'function', 'fairly', 'complete']
3131
completions_from_method = ['completions', 'method', 'missed', 'spot']
3232

33-
set_one_choices = ['this', 'is', 'set', 'one']
34-
set_two_choices = ['this', 'is', 'set', 'two']
35-
3633

3734
def choices_function() -> List[str]:
3835
"""Function that provides choices"""
@@ -46,19 +43,13 @@ def completer_function(text: str, line: str, begidx: int, endidx: int) -> List[s
4643

4744
def choices_takes_arg_tokens(arg_tokens: argparse.Namespace) -> List[str]:
4845
"""Choices function that receives arg_tokens from AutoCompleter"""
49-
if arg_tokens['set_pos'][0] == 'set1':
50-
return set_one_choices
51-
else:
52-
return set_two_choices
46+
return [arg_tokens['parent_arg'][0], arg_tokens['subcommand'][0]]
5347

5448

5549
def completer_takes_arg_tokens(text: str, line: str, begidx: int, endidx: int,
5650
arg_tokens: argparse.Namespace) -> List[str]:
5751
"""Completer function that receives arg_tokens from AutoCompleter"""
58-
if arg_tokens['set_pos'][0] == 'set1':
59-
match_against = set_one_choices
60-
else:
61-
match_against = set_two_choices
52+
match_against = [arg_tokens['parent_arg'][0], arg_tokens['subcommand'][0]]
6253
return basic_complete(text, line, begidx, endidx, match_against)
6354

6455

@@ -71,50 +62,21 @@ def __init__(self, *args, **kwargs):
7162
############################################################################################################
7263
# Begin code related to help and command name completion
7364
############################################################################################################
74-
def _music_create(self, args: argparse.Namespace) -> None:
75-
"""Implements the 'music create' command"""
76-
self.poutput('music create')
77-
78-
def _music_create_jazz(self, args: argparse.Namespace) -> None:
79-
"""Implements the 'music create jazz' command"""
80-
self.poutput('music create jazz')
81-
82-
def _music_create_rock(self, args: argparse.Namespace) -> None:
83-
"""Implements the 'music create rock' command"""
84-
self.poutput('music create rock')
85-
8665
# Top level parser for music command
8766
music_parser = Cmd2ArgumentParser(description='Manage music', prog='music')
8867

8968
# Add subcommands to music
90-
music_subparsers = music_parser.add_subparsers()
91-
92-
# music -> create
69+
music_subparsers = music_parser.add_subparsers(required=True, dest='subcommand')
9370
music_create_parser = music_subparsers.add_parser('create', help='Create music')
94-
music_create_parser.set_defaults(func=_music_create)
9571

9672
# Add subcommands to music -> create
97-
music_create_subparsers = music_create_parser.add_subparsers()
98-
99-
# music -> create -> jazz
73+
music_create_subparsers = music_create_parser.add_subparsers(required=True, dest='subcommand')
10074
music_create_jazz_parser = music_create_subparsers.add_parser('jazz', help='Create jazz')
101-
music_create_jazz_parser.set_defaults(func=_music_create_jazz)
102-
103-
# music -> create -> rock
10475
music_create_rock_parser = music_create_subparsers.add_parser('rock', help='Create rocks')
105-
music_create_rock_parser.set_defaults(func=_music_create_rock)
10676

10777
@with_argparser(music_parser)
10878
def do_music(self, args: argparse.Namespace) -> None:
109-
"""Music command"""
110-
func = getattr(args, 'func', None)
111-
if func is not None:
112-
# Call whatever subcommand function was selected
113-
func(self, args)
114-
else:
115-
# No subcommand was provided, so call help
116-
# noinspection PyTypeChecker
117-
self.do_help('music')
79+
pass
11880

11981
############################################################################################################
12082
# Begin code related to flag completion
@@ -252,9 +214,14 @@ def do_hint(self, args: argparse.Namespace) -> None:
252214
# Begin code related to receiving arg_tokens
253215
############################################################################################################
254216
arg_tokens_parser = Cmd2ArgumentParser()
255-
arg_tokens_parser.add_argument('set_pos', help='determines what will be tab completed')
256-
arg_tokens_parser.add_argument('choices_pos', choices_function=choices_takes_arg_tokens)
257-
arg_tokens_parser.add_argument('completer_pos', completer_function=completer_takes_arg_tokens)
217+
arg_tokens_parser.add_argument('parent_arg', help='arg from a parent parser')
218+
219+
# Create a subcommand for to exercise receiving parent_tokens and subcommand name in arg_tokens
220+
arg_tokens_subparser = arg_tokens_parser.add_subparsers(required=True, dest='subcommand')
221+
arg_tokens_subcmd_parser = arg_tokens_subparser.add_parser('subcmd')
222+
223+
arg_tokens_subcmd_parser.add_argument('choices_pos', choices_function=choices_takes_arg_tokens)
224+
arg_tokens_subcmd_parser.add_argument('completer_pos', completer_function=completer_takes_arg_tokens)
258225

259226
@with_argparser(arg_tokens_parser)
260227
def do_arg_tokens(self, args: argparse.Namespace) -> None:
@@ -755,12 +722,10 @@ def test_autocomp_hint_no_help_text(ac_app, capsys):
755722

756723
@pytest.mark.parametrize('command_and_args, completions', [
757724
# Exercise a choices function that receives arg_tokens dictionary
758-
('arg_tokens set1', set_one_choices),
759-
('arg_tokens set2', set_two_choices),
725+
('arg_tokens choice subcmd', ['choice', 'subcmd']),
760726
761727
# Exercise a completer that receives arg_tokens dictionary
762-
('arg_tokens set1 fake', set_one_choices),
763-
('arg_tokens set2 fake', set_two_choices),
728+
('arg_tokens completer subcmd fake', ['completer', 'subcmd']),
764729
])
765730
def test_arg_tokens(ac_app, command_and_args, completions):
766731
text = ''

0 commit comments

Comments
 (0)