Skip to content

Commit ba1d8dd

Browse files
committed
Increased code coverage of _complete_worker() to 100%
1 parent 0bbbe92 commit ba1d8dd

File tree

1 file changed

+43
-2
lines changed

1 file changed

+43
-2
lines changed

tests/test_completion.py

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
from examples.subcommands import SubcommandsExample
2121

2222
# List of strings used with completion functions
23-
food_item_strs = ['Pizza', 'Ham', 'Ham Sandwich', 'Potato']
23+
food_item_strs = ['Pizza', 'Ham', 'Ham Sandwich', 'Potato', 'Cheese "Pizza"']
2424
sport_item_strs = ['Bat', 'Basket', 'Basketball', 'Football', 'Space Ball']
2525
delimited_strs = \
2626
[
@@ -83,6 +83,17 @@ def do_test_raise_exception(self, args):
8383
def complete_test_raise_exception(self, text, line, begidx, endidx):
8484
raise IndexError("You are out of bounds!!")
8585

86+
def do_test_no_completer(self, args):
87+
"""Completing this should result in completedefault() being called"""
88+
pass
89+
90+
def completedefault(self, *ignored):
91+
"""Method called to complete an input line when no command-specific
92+
complete_*() method is available.
93+
94+
"""
95+
return ['default']
96+
8697

8798
@pytest.fixture
8899
def cmd2_app():
@@ -123,8 +134,9 @@ def test_complete_bogus_command(cmd2_app):
123134
endidx = len(line)
124135
begidx = endidx - len(text)
125136

137+
expected = ['default ']
126138
first_match = complete_tester(text, line, begidx, endidx, cmd2_app)
127-
assert first_match is None
139+
assert first_match is not None and cmd2_app.completion_matches == expected
128140

129141
def test_complete_exception(cmd2_app, capsys):
130142
text = ''
@@ -737,6 +749,16 @@ def test_add_opening_quote_basic_quote_added(cmd2_app):
737749
first_match = complete_tester(text, line, begidx, endidx, cmd2_app)
738750
assert first_match is not None and cmd2_app.completion_matches == expected
739751

752+
def test_add_opening_quote_basic_single_quote_added(cmd2_app):
753+
text = 'Ch'
754+
line = 'test_basic {}'.format(text)
755+
endidx = len(line)
756+
begidx = endidx - len(text)
757+
758+
expected = ["'Cheese \"Pizza\"' "]
759+
first_match = complete_tester(text, line, begidx, endidx, cmd2_app)
760+
assert first_match is not None and cmd2_app.completion_matches == expected
761+
740762
def test_add_opening_quote_basic_text_is_common_prefix(cmd2_app):
741763
# This tests when the text entered is the same as the common prefix of the matches
742764
text = 'Ham'
@@ -816,6 +838,25 @@ def test_add_opening_quote_delimited_space_in_prefix(cmd2_app):
816838
os.path.commonprefix(cmd2_app.completion_matches) == expected_common_prefix and \
817839
cmd2_app.display_matches == expected_display
818840

841+
def test_no_completer(cmd2_app):
842+
text = ''
843+
line = 'test_no_completer {}'.format(text)
844+
endidx = len(line)
845+
begidx = endidx - len(text)
846+
847+
expected = ['default ']
848+
first_match = complete_tester(text, line, begidx, endidx, cmd2_app)
849+
assert first_match is not None and cmd2_app.completion_matches == expected
850+
851+
def test_quote_as_command(cmd2_app):
852+
text = ''
853+
line = '" {}'.format(text)
854+
endidx = len(line)
855+
begidx = endidx - len(text)
856+
857+
first_match = complete_tester(text, line, begidx, endidx, cmd2_app)
858+
assert first_match is None and not cmd2_app.completion_matches
859+
819860
@pytest.fixture
820861
def sc_app():
821862
c = SubcommandsExample()

0 commit comments

Comments
 (0)