Skip to content

Commit 1a02457

Browse files
committed
Added unit tests for flag based completion
1 parent 1e3facc commit 1a02457

1 file changed

Lines changed: 61 additions & 9 deletions

File tree

tests/test_completion.py

Lines changed: 61 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import mock
1818
import pytest
1919

20-
from cmd2 import path_complete
20+
from cmd2 import path_complete, flag_based_complete, index_based_complete
2121

2222
@pytest.fixture
2323
def cmd2_app():
@@ -272,7 +272,7 @@ def test_shell_command_completion_does_path_completion_when_after_command(cmd2_a
272272
assert cmd2_app.complete_shell(text, line, begidx, endidx) == ['conftest.py ']
273273

274274

275-
def test_path_completion_single_end(cmd2_app, request):
275+
def test_path_completion_single_end(request):
276276
test_dir = os.path.dirname(request.module.__file__)
277277

278278
text = 'c'
@@ -284,7 +284,7 @@ def test_path_completion_single_end(cmd2_app, request):
284284

285285
assert path_complete(text, line, begidx, endidx) == ['conftest.py ']
286286

287-
def test_path_completion_single_mid(cmd2_app, request):
287+
def test_path_completion_single_mid(request):
288288
test_dir = os.path.dirname(request.module.__file__)
289289

290290
text = 'tes'
@@ -296,7 +296,7 @@ def test_path_completion_single_mid(cmd2_app, request):
296296

297297
assert path_complete(text, line, begidx, endidx) == ['tests' + os.path.sep]
298298

299-
def test_path_completion_multiple(cmd2_app, request):
299+
def test_path_completion_multiple(request):
300300
test_dir = os.path.dirname(request.module.__file__)
301301

302302
text = 's'
@@ -308,7 +308,7 @@ def test_path_completion_multiple(cmd2_app, request):
308308

309309
assert path_complete(text, line, begidx, endidx) == ['script.py', 'script.txt', 'scripts' + os.path.sep]
310310

311-
def test_path_completion_nomatch(cmd2_app, request):
311+
def test_path_completion_nomatch(request):
312312
test_dir = os.path.dirname(request.module.__file__)
313313

314314
text = 'z'
@@ -320,7 +320,7 @@ def test_path_completion_nomatch(cmd2_app, request):
320320

321321
assert path_complete(text, line, begidx, endidx) == []
322322

323-
def test_path_completion_cwd(cmd2_app):
323+
def test_path_completion_cwd():
324324
# Run path complete with no path and no search text
325325
text = ''
326326
line = 'shell ls {}'.format(text)
@@ -339,7 +339,7 @@ def test_path_completion_cwd(cmd2_app):
339339
assert completions_empty == completions_cwd
340340
assert completions_cwd
341341

342-
def test_path_completion_doesnt_match_wildcards(cmd2_app, request):
342+
def test_path_completion_doesnt_match_wildcards(request):
343343
test_dir = os.path.dirname(request.module.__file__)
344344

345345
text = 'c*'
@@ -352,7 +352,7 @@ def test_path_completion_doesnt_match_wildcards(cmd2_app, request):
352352
# Currently path completion doesn't accept wildcards, so will always return empty results
353353
assert path_complete(text, line, begidx, endidx) == []
354354

355-
def test_path_completion_user_expansion(cmd2_app):
355+
def test_path_completion_user_expansion():
356356
# Run path with just a tilde
357357
text = ''
358358
if sys.platform.startswith('win'):
@@ -376,7 +376,7 @@ def test_path_completion_user_expansion(cmd2_app):
376376
# Verify that the results are the same in both cases
377377
assert completions_tilde == completions_home
378378

379-
def test_path_completion_directories_only(cmd2_app, request):
379+
def test_path_completion_directories_only(request):
380380
test_dir = os.path.dirname(request.module.__file__)
381381

382382
text = 's'
@@ -389,6 +389,57 @@ def test_path_completion_directories_only(cmd2_app, 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
393+
food_item_strs = ['Pizza', 'Hamburger', 'Ham', 'Potato']
394+
sports_equipment_strs = ['Soccer', 'Socks', 'Basketball', 'Football']
395+
396+
# Dictionaries used with flag and index based complete functions
397+
flag_dict = {'-f': food_item_strs, '-s': sports_equipment_strs}
398+
position_dict = {1: food_item_strs, 2: sports_equipment_strs}
399+
400+
def test_flag_based_completion_single_end():
401+
text = 'Pi'
402+
line = 'list_food -f Pi'
403+
endidx = len(line)
404+
begidx = endidx - len(text)
405+
406+
assert flag_based_complete(text, line, begidx, endidx, flag_dict) == ['Pizza ']
407+
408+
def test_flag_based_completion_single_mid():
409+
text = 'Pi'
410+
line = 'list_food -f Pi'
411+
begidx = len(line) - len(text)
412+
endidx = begidx + 1
413+
414+
assert flag_based_complete(text, line, begidx, endidx, flag_dict) == ['Pizza']
415+
416+
def test_flag_based_completion_multiple():
417+
text = ''
418+
line = 'list_food -f '
419+
endidx = len(line)
420+
begidx = endidx - len(text)
421+
assert flag_based_complete(text, line, begidx, endidx, flag_dict) == sorted(food_item_strs)
422+
423+
def test_flag_based_completion_nomatch():
424+
text = 'q'
425+
line = 'list_food -f q'
426+
endidx = len(line)
427+
begidx = endidx - len(text)
428+
assert flag_based_complete(text, line, begidx, endidx, flag_dict) == []
429+
430+
def test_flag_based_default_completer(request):
431+
test_dir = os.path.dirname(request.module.__file__)
432+
433+
text = 'c'
434+
path = os.path.join(test_dir, text)
435+
line = 'list_food {}'.format(path)
436+
437+
endidx = len(line)
438+
begidx = endidx - len(text)
439+
440+
assert flag_based_complete(text, line, begidx, endidx, flag_dict, path_complete) == ['conftest.py ']
441+
442+
392443
def test_parseline_command_and_args(cmd2_app):
393444
line = 'help history'
394445
command, args, out_line = cmd2_app.parseline(line)
@@ -613,6 +664,7 @@ def get_endidx():
613664

614665
assert first_match is not None and sc_app.completion_matches == ['foo ']
615666

667+
616668
def test_cmd2_help_subcommand_completion_single_end(sc_app):
617669
text = 'base'
618670
line = 'help base'

0 commit comments

Comments
 (0)