Skip to content

Commit 06a84fd

Browse files
committed
Added unit tests for index based completion
1 parent 1a02457 commit 06a84fd

1 file changed

Lines changed: 48 additions & 4 deletions

File tree

tests/test_completion.py

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -389,13 +389,12 @@ def test_path_completion_directories_only(request):
389389
assert path_complete(text, line, begidx, endidx, dir_only=True) == ['scripts' + os.path.sep]
390390

391391

392-
# List of strings used with flag and index based complete functions
392+
# List of strings used with flag and index based completion functions
393393
food_item_strs = ['Pizza', 'Hamburger', 'Ham', 'Potato']
394-
sports_equipment_strs = ['Soccer', 'Socks', 'Basketball', 'Football']
394+
sports_equipment_strs = ['Bat', 'Basket', 'Basketball', 'Football']
395395

396-
# Dictionaries used with flag and index based complete functions
396+
# Dictionary used with flag based completion functions
397397
flag_dict = {'-f': food_item_strs, '-s': sports_equipment_strs}
398-
position_dict = {1: food_item_strs, 2: sports_equipment_strs}
399398

400399
def test_flag_based_completion_single_end():
401400
text = 'Pi'
@@ -439,6 +438,51 @@ def test_flag_based_default_completer(request):
439438

440439
assert flag_based_complete(text, line, begidx, endidx, flag_dict, path_complete) == ['conftest.py ']
441440

441+
# Dictionary used with index based completion functions
442+
index_dict = {1: food_item_strs, 2: sports_equipment_strs}
443+
444+
def test_index_based_completion_single_end():
445+
text = 'Foo'
446+
line = 'command Pizza Foo'
447+
endidx = len(line)
448+
begidx = endidx - len(text)
449+
450+
assert index_based_complete(text, line, begidx, endidx, index_dict) == ['Football ']
451+
452+
def test_index_based_completion_single_mid():
453+
text = 'Foo'
454+
line = 'command Pizza Foo'
455+
begidx = len(line) - len(text)
456+
endidx = begidx + 1
457+
458+
assert index_based_complete(text, line, begidx, endidx, index_dict) == ['Football']
459+
460+
def test_index_based_completion_multiple():
461+
text = ''
462+
line = 'command Pizza '
463+
endidx = len(line)
464+
begidx = endidx - len(text)
465+
assert index_based_complete(text, line, begidx, endidx, index_dict) == sorted(sports_equipment_strs)
466+
467+
def test_index_based_completion_nomatch():
468+
text = 'q'
469+
line = 'command q'
470+
endidx = len(line)
471+
begidx = endidx - len(text)
472+
assert index_based_complete(text, line, begidx, endidx, index_dict) == []
473+
474+
def test_index_based_default_completer(request):
475+
test_dir = os.path.dirname(request.module.__file__)
476+
477+
text = 'c'
478+
path = os.path.join(test_dir, text)
479+
line = 'command Pizza Bat {}'.format(path)
480+
481+
endidx = len(line)
482+
begidx = endidx - len(text)
483+
484+
assert flag_based_complete(text, line, begidx, endidx, flag_dict, path_complete) == ['conftest.py ']
485+
442486

443487
def test_parseline_command_and_args(cmd2_app):
444488
line = 'help history'

0 commit comments

Comments
 (0)