Skip to content

Commit 3893119

Browse files
authored
Merge pull request #131 from python-cmd2/completer_tests
Started to add some unit tests for tab completion
2 parents 571ff7d + 59df117 commit 3893119

File tree

1 file changed

+97
-0
lines changed

1 file changed

+97
-0
lines changed

tests/test_completion.py

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
# coding=utf-8
2+
"""
3+
Unit/functional testing for readline tab-completion functions in the cmd2.py module.
4+
5+
These are primarily tests related to readline completer functions which handle tab-completion of cmd2/cmd commands,
6+
file system paths, and shell commands.
7+
8+
Copyright 2017 Todd Leonhardt <[email protected]>
9+
Released under MIT license, see LICENSE file
10+
"""
11+
import sys
12+
13+
import cmd2
14+
import pytest
15+
16+
17+
@pytest.fixture
18+
def cmd2_app():
19+
c = cmd2.Cmd()
20+
return c
21+
22+
23+
def test_cmd2_command_completion_single_end(cmd2_app):
24+
text = 'he'
25+
line = 'he'
26+
begidx = 0
27+
endidx = 2
28+
# It is at end of line, so extra space is present
29+
assert cmd2_app.completenames(text, line, begidx, endidx) == ['help ']
30+
31+
def test_cmd2_command_completion_single_mid(cmd2_app):
32+
text = 'he'
33+
line = 'he'
34+
begidx = 0
35+
endidx = 1
36+
# It is not at end of line, so no extra space
37+
assert cmd2_app.completenames(text, line, begidx, endidx) == ['help']
38+
39+
def test_cmd2_command_completion_multiple(cmd2_app):
40+
text = 'h'
41+
line = 'h'
42+
begidx = 0
43+
endidx = 1
44+
# It is not at end of line, so no extra space
45+
assert cmd2_app.completenames(text, line, begidx, endidx) == ['help', 'history']
46+
47+
def test_cmd2_help_completion_single_end(cmd2_app):
48+
text = 'he'
49+
line = 'help he'
50+
begidx = 5
51+
endidx = 7
52+
# Even though it is at end of line, no extra space is present when tab completing a command name to get help on
53+
assert cmd2_app.completenames(text, line, begidx, endidx) == ['help']
54+
55+
def test_cmd2_help_completion_single_mid(cmd2_app):
56+
text = 'he'
57+
line = 'help he'
58+
begidx = 5
59+
endidx = 6
60+
assert cmd2_app.completenames(text, line, begidx, endidx) == ['help']
61+
62+
def test_cmd2_help_completion_multiple(cmd2_app):
63+
text = 'h'
64+
line = 'help h'
65+
begidx = 5
66+
endidx = 6
67+
assert cmd2_app.completenames(text, line, begidx, endidx) == ['help', 'history']
68+
69+
def test_shell_command_completion(cmd2_app):
70+
if sys.platform == "win32":
71+
text = 'calc'
72+
line = 'shell calc'
73+
begidx = 6
74+
endidx = 10
75+
assert cmd2_app.complete_shell(text, line, begidx, endidx) == ['calc.exe ']
76+
else:
77+
text = 'eg'
78+
line = '!eg'
79+
begidx = 1
80+
endidx = 3
81+
assert cmd2_app.complete_shell(text, line, begidx, endidx) == ['egrep ']
82+
83+
def test_shell_command_completion_multiple(cmd2_app):
84+
if sys.platform == "win32":
85+
text = 'c'
86+
line = 'shell c'
87+
begidx = 6
88+
endidx = 7
89+
assert 'calc.exe' in cmd2_app.complete_shell(text, line, begidx, endidx)
90+
else:
91+
text = 'l'
92+
line = '!l'
93+
begidx = 1
94+
endidx = 2
95+
assert 'ls' in cmd2_app.complete_shell(text, line, begidx, endidx)
96+
97+
# TODO: Add tests for path completion

0 commit comments

Comments
 (0)