Skip to content

Commit ce76120

Browse files
committed
Reorganized some unit tests
1 parent a16cbaf commit ce76120

2 files changed

Lines changed: 43 additions & 75 deletions

File tree

tests/test_argparse.py

Lines changed: 0 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -194,17 +194,6 @@ def base_bar(self, args):
194194
"""bar subcommand of base command"""
195195
self.poutput('((%s))' % args.z)
196196

197-
def base_sport(self, args):
198-
"""sport subcommand of base command"""
199-
self.poutput('Sport is {}'.format(args.sport))
200-
201-
# noinspection PyUnusedLocal
202-
def complete_base_sport(self, text, line, begidx, endidx):
203-
""" Adds tab completion to base sport subcommand """
204-
sports = ['Football', 'Hockey', 'Soccer', 'Baseball']
205-
index_dict = {1: sports}
206-
return self.index_based_complete(text, line, begidx, endidx, index_dict)
207-
208197
# create the top-level parser for the base command
209198
base_parser = argparse.ArgumentParser(prog='base')
210199
base_subparsers = base_parser.add_subparsers(title='subcommands', help='subcommand help')
@@ -220,11 +209,6 @@ def complete_base_sport(self, text, line, begidx, endidx):
220209
parser_bar.add_argument('z', help='string')
221210
parser_bar.set_defaults(func=base_bar)
222211

223-
# create the parser for the "sport" subcommand
224-
parser_sport = base_subparsers.add_parser('sport', help='sport help')
225-
parser_sport.add_argument('sport', help='Enter name of a sport')
226-
parser_sport.set_defaults(func=base_sport)
227-
228212
@cmd2.with_argparser(base_parser)
229213
def do_base(self, args):
230214
"""Base command help"""
@@ -236,9 +220,6 @@ def do_base(self, args):
236220
# No subcommand was provided, so call help
237221
self.do_help('base')
238222

239-
def complete_base(self, text, line, begidx, endidx):
240-
return self.cmd_with_subs_completer(text, line, begidx, endidx, base='base')
241-
242223
@pytest.fixture
243224
def subcommand_app():
244225
app = SubcommandApp()
@@ -279,55 +260,3 @@ def test_subcommand_invalid_help(subcommand_app):
279260
out = run_cmd(subcommand_app, 'help base baz')
280261
assert out[0].startswith('usage: base')
281262
assert out[1].startswith("base: error: invalid choice: 'baz'")
282-
283-
def test_subcommand_tab_completion(subcommand_app):
284-
# This makes sure the correct completer for the sport subcommand is called
285-
text = 'Foot'
286-
line = 'base sport Foot'
287-
endidx = len(line)
288-
begidx = endidx - len(text)
289-
state = 0
290-
291-
def get_line():
292-
return line
293-
294-
def get_begidx():
295-
return begidx
296-
297-
def get_endidx():
298-
return endidx
299-
300-
with mock.patch.object(readline, 'get_line_buffer', get_line):
301-
with mock.patch.object(readline, 'get_begidx', get_begidx):
302-
with mock.patch.object(readline, 'get_endidx', get_endidx):
303-
# Run the readline tab-completion function with readline mocks in place
304-
first_match = subcommand_app.complete(text, state)
305-
306-
# It is at end of line, so extra space is present
307-
assert first_match is not None and subcommand_app.completion_matches == ['Football ']
308-
309-
def test_subcommand_tab_completion_with_no_completer(subcommand_app):
310-
# This tests what happens when a subcommand has no completer
311-
# In this case, the foo subcommand has no completer defined
312-
text = 'Foot'
313-
line = 'base foo Foot'
314-
endidx = len(line)
315-
begidx = endidx - len(text)
316-
state = 0
317-
318-
def get_line():
319-
return line
320-
321-
def get_begidx():
322-
return begidx
323-
324-
def get_endidx():
325-
return endidx
326-
327-
with mock.patch.object(readline, 'get_line_buffer', get_line):
328-
with mock.patch.object(readline, 'get_begidx', get_begidx):
329-
with mock.patch.object(readline, 'get_endidx', get_endidx):
330-
# Run the readline tab-completion function with readline mocks in place
331-
first_match = subcommand_app.complete(text, state)
332-
333-
assert first_match is None

tests/test_completion.py

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ def cmd2_app():
3636

3737

3838
# List of strings used with completion functions
39-
food_item_strs = ['Pizza', 'Hamburger', 'Ham', 'Potato', 'Space Food']
40-
sport_item_strs = ['Bat', 'Basket', 'Basketball', 'Football']
39+
food_item_strs = ['Pizza', 'Hamburger', 'Ham', 'Potato']
40+
sport_item_strs = ['Bat', 'Basket', 'Basketball', 'Football', 'Space Ball']
4141
delimited_strs = ['/home/user/file.txt', '/home/user/prog.c', '/home/otheruser/maps']
4242

4343
# Dictionary used with flag based completion functions
@@ -636,6 +636,15 @@ def base_bar(self, args):
636636
"""bar subcommand of base command"""
637637
self.poutput('((%s))' % args.z)
638638

639+
def base_sport(self, args):
640+
"""sport subcommand of base command"""
641+
self.poutput('Sport is {}'.format(args.sport))
642+
643+
def complete_base_sport(self, text, line, begidx, endidx):
644+
""" Adds tab completion to base sport subcommand """
645+
index_dict = {1: sport_item_strs}
646+
return self.index_based_complete(text, line, begidx, endidx, index_dict)
647+
639648
# create the top-level parser for the base command
640649
base_parser = argparse.ArgumentParser(prog='base')
641650
base_subparsers = base_parser.add_subparsers(title='subcommands', help='subcommand help')
@@ -651,6 +660,11 @@ def base_bar(self, args):
651660
parser_bar.add_argument('z', help='string')
652661
parser_bar.set_defaults(func=base_bar)
653662

663+
# create the parser for the "sport" subcommand
664+
parser_sport = base_subparsers.add_parser('sport', help='sport help')
665+
parser_sport.add_argument('sport', help='Enter name of a sport')
666+
parser_sport.set_defaults(func=base_sport)
667+
654668
@cmd2.with_argparser(base_parser)
655669
def do_base(self, args):
656670
"""Base command help"""
@@ -662,6 +676,9 @@ def do_base(self, args):
662676
# No sub-command was provided, so as called
663677
self.do_help('base')
664678

679+
def complete_base(self, text, line, begidx, endidx):
680+
return self.cmd_with_subs_completer(text, line, begidx, endidx, base='base')
681+
665682

666683
@pytest.fixture
667684
def sc_app():
@@ -687,7 +704,7 @@ def test_cmd2_subcommand_completion_multiple(sc_app):
687704
begidx = endidx - len(text)
688705

689706
first_match = complete_tester(text, line, begidx, endidx, sc_app)
690-
assert first_match is not None and sc_app.completion_matches == ['bar', 'foo']
707+
assert first_match is not None and sc_app.completion_matches == ['bar', 'foo', 'sport']
691708

692709
def test_cmd2_subcommand_completion_nomatch(sc_app):
693710
text = 'z'
@@ -712,7 +729,7 @@ def test_cmd2_help_subcommand_completion_multiple(sc_app):
712729
endidx = len(line)
713730
begidx = endidx - len(text)
714731

715-
assert sc_app.complete_help(text, line, begidx, endidx) == ['bar', 'foo']
732+
assert sc_app.complete_help(text, line, begidx, endidx) == ['bar', 'foo', 'sport']
716733

717734

718735
def test_cmd2_help_subcommand_completion_nomatch(sc_app):
@@ -722,6 +739,28 @@ def test_cmd2_help_subcommand_completion_nomatch(sc_app):
722739
begidx = endidx - len(text)
723740
assert sc_app.complete_help(text, line, begidx, endidx) == []
724741

742+
def test_subcommand_tab_completion(sc_app):
743+
# This makes sure the correct completer for the sport subcommand is called
744+
text = 'Foot'
745+
line = 'base sport Foot'
746+
endidx = len(line)
747+
begidx = endidx - len(text)
748+
749+
first_match = complete_tester(text, line, begidx, endidx, sc_app)
750+
751+
# It is at end of line, so extra space is present
752+
assert first_match is not None and sc_app.completion_matches == ['Football ']
753+
754+
def test_subcommand_tab_completion_with_no_completer(sc_app):
755+
# This tests what happens when a subcommand has no completer
756+
# In this case, the foo subcommand has no completer defined
757+
text = 'Foot'
758+
line = 'base foo Foot'
759+
endidx = len(line)
760+
begidx = endidx - len(text)
761+
762+
first_match = complete_tester(text, line, begidx, endidx, sc_app)
763+
assert first_match is None
725764

726765
class SecondLevel(cmd2.Cmd):
727766
"""To be used as a second level command class. """

0 commit comments

Comments
 (0)