Skip to content

Commit 0f1a69f

Browse files
committed
More opening quote unit tests
1 parent 6fe8fd6 commit 0f1a69f

File tree

1 file changed

+63
-3
lines changed

1 file changed

+63
-3
lines changed

tests/test_completion.py

Lines changed: 63 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,14 @@
3232
# List of strings used with completion functions
3333
food_item_strs = ['Pizza', 'Ham', 'Ham Sandwich', 'Potato']
3434
sport_item_strs = ['Bat', 'Basket', 'Basketball', 'Football', 'Space Ball']
35-
delimited_strs = ['/home/user/file.txt', '/home/user/prog.c', '/home/otheruser/maps']
35+
delimited_strs = \
36+
[
37+
'/home/user/file.txt',
38+
'/home/user/file space.txt',
39+
'/home/user/prog.c',
40+
'/home/other user/maps',
41+
'/home/other user/tests'
42+
]
3643

3744
# Dictionary used with flag based completion functions
3845
flag_dict = \
@@ -684,7 +691,7 @@ def test_add_opening_quote_basic_quote_added(cmd2_app):
684691
endidx = len(line)
685692
begidx = endidx - len(text)
686693

687-
expected = ['"Ham', '"Ham Sandwich']
694+
expected = sorted(['"Ham', '"Ham Sandwich'])
688695
first_match = complete_tester(text, line, begidx, endidx, cmd2_app)
689696
assert first_match is not None and cmd2_app.completion_matches == expected
690697

@@ -695,10 +702,63 @@ def test_add_opening_quote_basic_text_is_common_prefix(cmd2_app):
695702
endidx = len(line)
696703
begidx = endidx - len(text)
697704

698-
expected = ['"Ham', '"Ham Sandwich']
705+
expected = sorted(['"Ham', '"Ham Sandwich'])
699706
first_match = complete_tester(text, line, begidx, endidx, cmd2_app)
700707
assert first_match is not None and cmd2_app.completion_matches == expected
701708

709+
def test_add_opening_quote_delimited_no_text(cmd2_app):
710+
text = ''
711+
line = 'test_delimited {}'.format(text)
712+
endidx = len(line)
713+
begidx = endidx - len(text)
714+
715+
# The whole list will be returned with no opening quotes added
716+
first_match = complete_tester(text, line, begidx, endidx, cmd2_app)
717+
assert first_match is not None and cmd2_app.completion_matches == sorted(delimited_strs)
718+
719+
def test_add_opening_quote_delimited_nothing_added(cmd2_app):
720+
text = '/ho'
721+
line = 'test_delimited {}'.format(text)
722+
endidx = len(line)
723+
begidx = endidx - len(text)
724+
725+
expected_matches = sorted(delimited_strs)
726+
expected_display = sorted(['other user', 'user'])
727+
728+
first_match = complete_tester(text, line, begidx, endidx, cmd2_app)
729+
assert first_match is not None and \
730+
cmd2_app.completion_matches == expected_matches and \
731+
cmd2_app.display_matches == expected_display
732+
733+
def test_add_opening_quote_delimited_quote_added(cmd2_app):
734+
text = '/home/oth'
735+
line = 'test_delimited {}'.format(text)
736+
endidx = len(line)
737+
begidx = endidx - len(text)
738+
739+
expected_prefix = '"/home/other user/'
740+
expected_display = ['maps', 'tests']
741+
742+
first_match = complete_tester(text, line, begidx, endidx, cmd2_app)
743+
assert first_match is not None and \
744+
os.path.commonprefix(cmd2_app.completion_matches) == expected_prefix and \
745+
cmd2_app.display_matches == expected_display
746+
747+
def test_add_opening_quote_delimited_text_is_common_prefix(cmd2_app):
748+
# This tests when the text entered is the same as the common prefix of the matches
749+
text = '/home/user/file'
750+
line = 'test_delimited {}'.format(text)
751+
endidx = len(line)
752+
begidx = endidx - len(text)
753+
754+
expected_prefix = '"/home/user/file'
755+
expected_display = sorted(['file.txt', 'file space.txt'])
756+
757+
first_match = complete_tester(text, line, begidx, endidx, cmd2_app)
758+
assert first_match is not None and \
759+
os.path.commonprefix(cmd2_app.completion_matches) == expected_prefix and \
760+
cmd2_app.display_matches == expected_display
761+
702762
class SubcommandsExample(cmd2.Cmd):
703763
"""
704764
Example cmd2 application where we a base command which has a couple subcommands

0 commit comments

Comments
 (0)