summaryrefslogtreecommitdiff
path: root/tests/test_transcript.py
diff options
context:
space:
mode:
authorTodd Leonhardt <todd.leonhardt@gmail.com>2017-02-05 11:55:39 -0500
committerTodd Leonhardt <todd.leonhardt@gmail.com>2017-02-05 11:55:39 -0500
commitce71e89c1d01e03df7abba3396d9445657ad04d9 (patch)
treee969ca29823c4eaf9d301b8c1518d8b9e87d99aa /tests/test_transcript.py
parent59bd32f5d4d2b04522b54b0a042d2d69fc7ed710 (diff)
downloadcmd2-git-ce71e89c1d01e03df7abba3396d9445657ad04d9.tar.gz
Fixed how the six.moves.input function is imported and used.
Also added a unit test for the cmd2.Cmd.select() method.
Diffstat (limited to 'tests/test_transcript.py')
-rw-r--r--tests/test_transcript.py33
1 files changed, 31 insertions, 2 deletions
diff --git a/tests/test_transcript.py b/tests/test_transcript.py
index a4102bc8..d11eb02a 100644
--- a/tests/test_transcript.py
+++ b/tests/test_transcript.py
@@ -7,8 +7,11 @@ Released under MIT license, see LICENSE file
"""
import sys
+import mock
import pytest
-from mock import patch
+
+# Used for sm.input: raw_input() for Python 2 or input() for Python 3
+import six.moves as sm
from cmd2 import Cmd, make_option, options, Cmd2TestCase
from conftest import run_cmd, StdOut, normalize
@@ -55,6 +58,13 @@ class DemoApp(Cmd):
else:
self.stdout.write('Hello Nobody\n')
+ def do_eat(self, arg):
+ """Eat something, with a selection of sauces to choose from."""
+ sauce = self.select('sweet salty', 'Sauce? ')
+ result = '{food} with {sauce} sauce, yum!'
+ result = result.format(food=arg, sauce=sauce)
+ self.stdout.write(result + '\n')
+
@pytest.fixture
def _cmdline_app():
@@ -217,7 +227,7 @@ def test_optarser_options_with_spaces_in_quotes(_demo_app):
def test_commands_at_invocation(_cmdline_app):
testargs = ["prog", "say hello", "say Gracie", "quit"]
expected = "hello\nGracie\n"
- with patch.object(sys, 'argv', testargs):
+ with mock.patch.object(sys, 'argv', testargs):
app = CmdLineApp()
app.stdout = StdOut()
app.cmdloop()
@@ -225,3 +235,22 @@ def test_commands_at_invocation(_cmdline_app):
assert out == expected
+def test_select_options(_demo_app):
+ # Mock out the input call so we don't actually wait for a user's `response on stdin
+ m = mock.MagicMock(name='input', return_value='2')
+ sm.input = m
+
+ food = 'bacon'
+ out = run_cmd(_demo_app, "set debug true")
+ out = run_cmd(_demo_app, "eat {}".format(food))
+ expected = normalize("""
+ 1. sweet
+ 2. salty
+{} with salty sauce, yum!
+""".format(food))
+
+ # Make sure our mock was called with the expected arguments
+ m.assert_called_once_with('Sauce? ')
+
+ # And verify the expected output to stdout
+ assert out == expected