summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTodd Leonhardt <todd.leonhardt@gmail.com>2017-07-08 14:05:26 -0400
committerTodd Leonhardt <todd.leonhardt@gmail.com>2017-07-08 14:05:26 -0400
commita425e19c6a93dc7e6b0a5f13f335de9f6a5e348e (patch)
tree45a5f052b45286d2e14b7074843c4e9124f49b6c
parenta140ad1cdb531d2bc60237502cf8601f46d60692 (diff)
downloadcmd2-git-a425e19c6a93dc7e6b0a5f13f335de9f6a5e348e.tar.gz
Added a few more unit tests
-rwxr-xr-xcmd2.py9
-rw-r--r--tests/test_cmd2.py36
-rw-r--r--tests/test_parsing.py9
3 files changed, 51 insertions, 3 deletions
diff --git a/cmd2.py b/cmd2.py
index dba8689c..59d7cbc1 100755
--- a/cmd2.py
+++ b/cmd2.py
@@ -878,20 +878,23 @@ class Cmd(cmd.Cmd):
def pseudo_raw_input(self, prompt):
"""copied from cmd's cmdloop; like raw_input, but accounts for changed stdin, stdout"""
+ # Deal with the vagaries of readline and ANSI escape codes
+ safe_prompt = self._surround_ansi_escapes(prompt)
+
if self.use_rawinput:
- safe_prompt = self._surround_ansi_escapes(prompt)
try:
line = sm.input(safe_prompt)
except EOFError:
line = 'EOF'
else:
- self.stdout.write(prompt)
+ self.stdout.write(safe_prompt)
self.stdout.flush()
line = self.stdin.readline()
if not len(line):
line = 'EOF'
else:
line = line.rstrip('\r\n')
+
return line.strip()
def _cmdloop(self):
@@ -942,7 +945,7 @@ class Cmd(cmd.Cmd):
# Need to set empty list this way because Python 2 doesn't support the clear() method on lists
self.cmdqueue = []
self._script_dir = []
-
+
return stop
# noinspection PyUnusedLocal
diff --git a/tests/test_cmd2.py b/tests/test_cmd2.py
index 7e393b7b..8d91c8ef 100644
--- a/tests/test_cmd2.py
+++ b/tests/test_cmd2.py
@@ -1282,3 +1282,39 @@ def test_echo(capsys):
assert app.cmdqueue == []
assert app._current_script_dir is None
assert out.startswith('help history\n' + 'history [arg]: lists past commands issued')
+
+
+def test_raw_input(base_app):
+ base_app.use_raw_input = True
+ fake_input = 'quit'
+
+ # Mock out the input call so we don't actually wait for a user's response on stdin
+ m = mock.Mock(name='input', return_value=fake_input)
+ sm.input = m
+
+ line = base_app.pseudo_raw_input('(cmd2)')
+ assert line == fake_input
+
+def test_stdin_input():
+ app = cmd2.Cmd()
+ app.use_rawinput = False
+ fake_input = 'quit'
+
+ # Mock out the readline call so we don't actually read from stdin
+ m = mock.Mock(name='readline', return_value=fake_input)
+ app.stdin.readline = m
+
+ line = app.pseudo_raw_input('(cmd2)')
+ assert line == fake_input
+
+def test_empty_stdin_input():
+ app = cmd2.Cmd()
+ app.use_rawinput = False
+ fake_input = ''
+
+ # Mock out the readline call so we don't actually read from stdin
+ m = mock.Mock(name='readline', return_value=fake_input)
+ app.stdin.readline = m
+
+ line = app.pseudo_raw_input('(cmd2)')
+ assert line == 'EOF'
diff --git a/tests/test_parsing.py b/tests/test_parsing.py
index 254aef15..bcb19f10 100644
--- a/tests/test_parsing.py
+++ b/tests/test_parsing.py
@@ -329,3 +329,12 @@ def test_option_parser_exit_with_msg(option_parser, capsys):
out, err = capsys.readouterr()
assert out == msg + '\n'
assert err == ''
+
+
+def test_empty_statement_raises_exception():
+ app = cmd2.Cmd()
+ with pytest.raises(cmd2.EmptyStatement):
+ app._complete_statement('')
+
+ with pytest.raises(cmd2.EmptyStatement):
+ app._complete_statement(' ')