Skip to content

Commit 88fe14c

Browse files
authored
Merge pull request #179 from python-cmd2/unit_tests
Added a few more unit tests
2 parents a140ad1 + a425e19 commit 88fe14c

File tree

3 files changed

+51
-3
lines changed

3 files changed

+51
-3
lines changed

cmd2.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -878,20 +878,23 @@ def _surround_ansi_escapes(prompt, start="\x01", end="\x02"):
878878
def pseudo_raw_input(self, prompt):
879879
"""copied from cmd's cmdloop; like raw_input, but accounts for changed stdin, stdout"""
880880

881+
# Deal with the vagaries of readline and ANSI escape codes
882+
safe_prompt = self._surround_ansi_escapes(prompt)
883+
881884
if self.use_rawinput:
882-
safe_prompt = self._surround_ansi_escapes(prompt)
883885
try:
884886
line = sm.input(safe_prompt)
885887
except EOFError:
886888
line = 'EOF'
887889
else:
888-
self.stdout.write(prompt)
890+
self.stdout.write(safe_prompt)
889891
self.stdout.flush()
890892
line = self.stdin.readline()
891893
if not len(line):
892894
line = 'EOF'
893895
else:
894896
line = line.rstrip('\r\n')
897+
895898
return line.strip()
896899

897900
def _cmdloop(self):
@@ -942,7 +945,7 @@ def _cmdloop(self):
942945
# Need to set empty list this way because Python 2 doesn't support the clear() method on lists
943946
self.cmdqueue = []
944947
self._script_dir = []
945-
948+
946949
return stop
947950

948951
# noinspection PyUnusedLocal

tests/test_cmd2.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1282,3 +1282,39 @@ def test_echo(capsys):
12821282
assert app.cmdqueue == []
12831283
assert app._current_script_dir is None
12841284
assert out.startswith('help history\n' + 'history [arg]: lists past commands issued')
1285+
1286+
1287+
def test_raw_input(base_app):
1288+
base_app.use_raw_input = True
1289+
fake_input = 'quit'
1290+
1291+
# Mock out the input call so we don't actually wait for a user's response on stdin
1292+
m = mock.Mock(name='input', return_value=fake_input)
1293+
sm.input = m
1294+
1295+
line = base_app.pseudo_raw_input('(cmd2)')
1296+
assert line == fake_input
1297+
1298+
def test_stdin_input():
1299+
app = cmd2.Cmd()
1300+
app.use_rawinput = False
1301+
fake_input = 'quit'
1302+
1303+
# Mock out the readline call so we don't actually read from stdin
1304+
m = mock.Mock(name='readline', return_value=fake_input)
1305+
app.stdin.readline = m
1306+
1307+
line = app.pseudo_raw_input('(cmd2)')
1308+
assert line == fake_input
1309+
1310+
def test_empty_stdin_input():
1311+
app = cmd2.Cmd()
1312+
app.use_rawinput = False
1313+
fake_input = ''
1314+
1315+
# Mock out the readline call so we don't actually read from stdin
1316+
m = mock.Mock(name='readline', return_value=fake_input)
1317+
app.stdin.readline = m
1318+
1319+
line = app.pseudo_raw_input('(cmd2)')
1320+
assert line == 'EOF'

tests/test_parsing.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,3 +329,12 @@ def test_option_parser_exit_with_msg(option_parser, capsys):
329329
out, err = capsys.readouterr()
330330
assert out == msg + '\n'
331331
assert err == ''
332+
333+
334+
def test_empty_statement_raises_exception():
335+
app = cmd2.Cmd()
336+
with pytest.raises(cmd2.EmptyStatement):
337+
app._complete_statement('')
338+
339+
with pytest.raises(cmd2.EmptyStatement):
340+
app._complete_statement(' ')

0 commit comments

Comments
 (0)