Skip to content

Commit 4233570

Browse files
committed
More unit tests and comments
1 parent 0f1a69f commit 4233570

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

cmd2.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2097,6 +2097,9 @@ def complete(self, text, state):
20972097

20982098
if matches_delimited:
20992099
display_prefix = os.path.commonprefix(self.display_matches)
2100+
2101+
# For delimited matches, we check what appears before the display
2102+
# matches (common_prefix) as well as the display matches themselves.
21002103
if (' ' in common_prefix) or (display_prefix and ' ' in ''.join(self.display_matches)):
21012104
add_quote = True
21022105

tests/test_completion.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -731,13 +731,13 @@ def test_add_opening_quote_delimited_nothing_added(cmd2_app):
731731
cmd2_app.display_matches == expected_display
732732

733733
def test_add_opening_quote_delimited_quote_added(cmd2_app):
734-
text = '/home/oth'
734+
text = '/home/user/fi'
735735
line = 'test_delimited {}'.format(text)
736736
endidx = len(line)
737737
begidx = endidx - len(text)
738738

739-
expected_prefix = '"/home/other user/'
740-
expected_display = ['maps', 'tests']
739+
expected_prefix = '"/home/user/file'
740+
expected_display = sorted(['file.txt', 'file space.txt'])
741741

742742
first_match = complete_tester(text, line, begidx, endidx, cmd2_app)
743743
assert first_match is not None and \
@@ -759,6 +759,21 @@ def test_add_opening_quote_delimited_text_is_common_prefix(cmd2_app):
759759
os.path.commonprefix(cmd2_app.completion_matches) == expected_prefix and \
760760
cmd2_app.display_matches == expected_display
761761

762+
def test_add_opening_quote_delimited_space_in_prefix(cmd2_app):
763+
# This test when a space appears before the part of the string that is the display match
764+
text = '/home/oth'
765+
line = 'test_delimited {}'.format(text)
766+
endidx = len(line)
767+
begidx = endidx - len(text)
768+
769+
expected_prefix = '"/home/other user/'
770+
expected_display = ['maps', 'tests']
771+
772+
first_match = complete_tester(text, line, begidx, endidx, cmd2_app)
773+
assert first_match is not None and \
774+
os.path.commonprefix(cmd2_app.completion_matches) == expected_prefix and \
775+
cmd2_app.display_matches == expected_display
776+
762777
class SubcommandsExample(cmd2.Cmd):
763778
"""
764779
Example cmd2 application where we a base command which has a couple subcommands

0 commit comments

Comments
 (0)