Skip to content

Commit 6fe8fd6

Browse files
committed
Unit tests for adding opening quote
1 parent fa5c7ad commit 6fe8fd6

File tree

1 file changed

+68
-18
lines changed

1 file changed

+68
-18
lines changed

tests/test_completion.py

Lines changed: 68 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,8 @@
2929
pass
3030

3131

32-
@pytest.fixture
33-
def cmd2_app():
34-
c = cmd2.Cmd()
35-
return c
36-
37-
3832
# List of strings used with completion functions
39-
food_item_strs = ['Pizza', 'Hamburger', 'Ham', 'Potato']
33+
food_item_strs = ['Pizza', 'Ham', 'Ham Sandwich', 'Potato']
4034
sport_item_strs = ['Bat', 'Basket', 'Basketball', 'Football', 'Space Ball']
4135
delimited_strs = ['/home/user/file.txt', '/home/user/prog.c', '/home/otheruser/maps']
4236

@@ -59,6 +53,33 @@ def cmd2_app():
5953
2: sport_item_strs, # Tab-complete sport items at index 2 in command line
6054
}
6155

56+
57+
class CompletionsExample(cmd2.Cmd):
58+
"""
59+
Example cmd2 application used to exercise tab-completion tests
60+
"""
61+
def __init__(self):
62+
cmd2.Cmd.__init__(self)
63+
64+
def do_test_basic(self, args):
65+
pass
66+
67+
def complete_test_basic(self, text, line, begidx, endidx):
68+
return self.basic_complete(text, line, begidx, endidx, food_item_strs)
69+
70+
def do_test_delimited(self, args):
71+
pass
72+
73+
def complete_test_delimited(self, text, line, begidx, endidx):
74+
return self.delimiter_complete(text, line, begidx, endidx, delimited_strs, '/')
75+
76+
77+
@pytest.fixture
78+
def cmd2_app():
79+
c = CompletionsExample()
80+
return c
81+
82+
6283
def complete_tester(text, line, begidx, endidx, app):
6384
"""
6485
This is a convenience function to test cmd2.complete() since
@@ -425,7 +446,7 @@ def test_delimiter_completion(cmd2_app):
425446

426447
cmd2_app.delimiter_complete(text, line, begidx, endidx, delimited_strs, '/')
427448

428-
# Remove duplicates from display_matches and sort it. This is typically done in the display function.
449+
# Remove duplicates from display_matches and sort it. This is typically done in complete().
429450
display_set = set(cmd2_app.display_matches)
430451
display_list = list(display_set)
431452
display_list.sort()
@@ -638,6 +659,45 @@ def test_parseline_expands_shortcuts(cmd2_app):
638659
assert args == 'cat foobar.txt'
639660
assert line.replace('!', 'shell ') == out_line
640661

662+
def test_add_opening_quote_basic_no_text(cmd2_app):
663+
text = ''
664+
line = 'test_basic {}'.format(text)
665+
endidx = len(line)
666+
begidx = endidx - len(text)
667+
668+
# The whole list will be returned with no opening quotes added
669+
first_match = complete_tester(text, line, begidx, endidx, cmd2_app)
670+
assert first_match is not None and cmd2_app.completion_matches == sorted(food_item_strs)
671+
672+
def test_add_opening_quote_basic_nothing_added(cmd2_app):
673+
text = 'P'
674+
line = 'test_basic {}'.format(text)
675+
endidx = len(line)
676+
begidx = endidx - len(text)
677+
678+
first_match = complete_tester(text, line, begidx, endidx, cmd2_app)
679+
assert first_match is not None and cmd2_app.completion_matches == ['Pizza', 'Potato']
680+
681+
def test_add_opening_quote_basic_quote_added(cmd2_app):
682+
text = 'Ha'
683+
line = 'test_basic {}'.format(text)
684+
endidx = len(line)
685+
begidx = endidx - len(text)
686+
687+
expected = ['"Ham', '"Ham Sandwich']
688+
first_match = complete_tester(text, line, begidx, endidx, cmd2_app)
689+
assert first_match is not None and cmd2_app.completion_matches == expected
690+
691+
def test_add_opening_quote_basic_text_is_common_prefix(cmd2_app):
692+
# This tests when the text entered is the same as the common prefix of the matches
693+
text = 'Ham'
694+
line = 'test_basic {}'.format(text)
695+
endidx = len(line)
696+
begidx = endidx - len(text)
697+
698+
expected = ['"Ham', '"Ham Sandwich']
699+
first_match = complete_tester(text, line, begidx, endidx, cmd2_app)
700+
assert first_match is not None and cmd2_app.completion_matches == expected
641701

642702
class SubcommandsExample(cmd2.Cmd):
643703
"""
@@ -787,16 +847,6 @@ def test_subcommand_tab_completion_with_no_completer(sc_app):
787847
first_match = complete_tester(text, line, begidx, endidx, sc_app)
788848
assert first_match is None
789849

790-
def test_subcommand_tab_completion_add_quote(sc_app):
791-
# This makes sure an opening quote is added to the readline line buffer
792-
text = 'Space'
793-
line = 'base sport {}'.format(text)
794-
endidx = len(line)
795-
begidx = endidx - len(text)
796-
797-
first_match = complete_tester(text, line, begidx, endidx, sc_app)
798-
assert first_match is not None and sc_app.completion_matches == ['"Space Ball" ']
799-
800850
def test_subcommand_tab_completion_space_in_text(sc_app):
801851
text = 'B'
802852
line = 'base sport "Space {}'.format(text)

0 commit comments

Comments
 (0)