diff options
author | Todd Leonhardt <todd.leonhardt@gmail.com> | 2017-07-01 14:11:38 -0400 |
---|---|---|
committer | Todd Leonhardt <todd.leonhardt@gmail.com> | 2017-07-01 14:11:38 -0400 |
commit | f2f91230b23cbed8f636e663e29e6bb4b1dbe9ee (patch) | |
tree | e3c0c9a3a596b1144daf31500ecbdb2f2623e847 | |
parent | 0b6edb1663ba20ece40f8dbba491f6e28911b183 (diff) | |
download | cmd2-git-f2f91230b23cbed8f636e663e29e6bb4b1dbe9ee.tar.gz |
Added more unit tests for the cmd2.Cmd.select() method
Also
- Moved the existing tests for select to a more appropriate location
- Minor tweak to working in README for readability
-rwxr-xr-x | README.md | 2 | ||||
-rw-r--r-- | tests/test_cmd2.py | 136 | ||||
-rw-r--r-- | tests/test_transcript.py | 28 |
3 files changed, 137 insertions, 29 deletions
@@ -7,7 +7,7 @@ cmd2 [](https://codecov.io/gh/python-cmd2/cmd2) [](https://pypi.python.org/pypi/cmd2/) -cmd2 is a tool for writing command-line interactive applications for Python 2.7 and Python 3.3+. It is based on the +cmd2 is a tool for writing interactive command-line applications for Python 2.7 and Python 3.3+. It is based on the Python Standard Library's [cmd](https://docs.python.org/3/library/cmd.html) module, and can be used any place cmd is used simply by importing cmd2 instead. It is pure Python code with the only 3rd-party dependencies being on [six](https://pypi.python.org/pypi/six), [pyparsing](http://pyparsing.wikispaces.com), and [pyperclip](https://github.com/asweigart/pyperclip). diff --git a/tests/test_cmd2.py b/tests/test_cmd2.py index f719721a..03cab912 100644 --- a/tests/test_cmd2.py +++ b/tests/test_cmd2.py @@ -813,3 +813,139 @@ def test_help_overridden_method(help_app): out = run_cmd(help_app, 'help pause') expected = normalize('This overrides the pause command and does nothing.') assert out == expected + + +class SelectApp(cmd2.Cmd): + def do_eat(self, arg): + """Eat something, with a selection of sauces to choose from.""" + # Pass in a single string of space-separated selections + sauce = self.select('sweet salty', 'Sauce? ') + result = '{food} with {sauce} sauce, yum!' + result = result.format(food=arg, sauce=sauce) + self.stdout.write(result + '\n') + + def do_study(self, arg): + """Learn something, with a selection of subjects to choose from.""" + # Pass in a list of strings for selections + subject = self.select(['math', 'science'], 'Subject? ') + result = 'Good luck learning {}!\n'.format(subject) + self.stdout.write(result) + + def do_procrastinate(self, arg): + """Waste time in your manner of choice.""" + # Pass in a list of tuples for selections + leisure_activity = self.select([('Netflix and chill', 'Netflix'), ('Porn', 'WebSurfing')], + 'How would you like to procrastinate? ') + result = 'Have fun procrasinating with {}!\n'.format(leisure_activity) + self.stdout.write(result) + + def do_play(self, arg): + """Play your favorite musical instrument.""" + # Pass in an uneven list of tuples for selections + instrument = self.select([('Guitar', 'Electric Guitar'), ('Drums',)], 'Instrument? ') + result = 'Charm us with the {}...\n'.format(instrument) + self.stdout.write(result) + +@pytest.fixture +def select_app(): + app = SelectApp() + app.stdout = StdOut() + return app + +def test_select_options(select_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(select_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 + +def test_select_invalid_option(select_app): + # Mock out the input call so we don't actually wait for a user's response on stdin + m = mock.MagicMock(name='input') + # If side_effect is an iterable then each call to the mock will return the next value from the iterable. + m.side_effect = ['3', '1'] # First pass and invalid selection, then pass a valid one + sm.input = m + + food = 'fish' + out = run_cmd(select_app, "eat {}".format(food)) + expected = normalize(""" + 1. sweet + 2. salty +3 isn't a valid choice. Pick a number between 1 and 2: +{} with sweet sauce, yum! +""".format(food)) + + # Make sure our mock was called exactly twice with the expected arguments + arg = 'Sauce? ' + calls = [mock.call(arg), mock.call(arg)] + m.assert_has_calls(calls) + + # And verify the expected output to stdout + assert out == expected + +def test_select_list_of_strings(select_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 + + out = run_cmd(select_app, "study") + expected = normalize(""" + 1. math + 2. science +Good luck learning {}! +""".format('science')) + + # Make sure our mock was called with the expected arguments + m.assert_called_once_with('Subject? ') + + # And verify the expected output to stdout + assert out == expected + +def test_select_list_of_tuples(select_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 + + out = run_cmd(select_app, "procrastinate") + expected = normalize(""" + 1. Netflix + 2. WebSurfing +Have fun procrasinating with {}! +""".format('Porn')) + + # Make sure our mock was called with the expected arguments + m.assert_called_once_with('How would you like to procrastinate? ') + + # And verify the expected output to stdout + assert out == expected + + +def test_select_uneven_list_of_tuples(select_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 + + out = run_cmd(select_app, "play") + expected = normalize(""" + 1. Electric Guitar + 2. Drums +Charm us with the {}... +""".format('Drums')) + + # Make sure our mock was called with the expected arguments + m.assert_called_once_with('Instrument? ') + + # And verify the expected output to stdout + assert out == expected diff --git a/tests/test_transcript.py b/tests/test_transcript.py index a31ebd17..d1363f8e 100644 --- a/tests/test_transcript.py +++ b/tests/test_transcript.py @@ -68,13 +68,6 @@ 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(): @@ -240,27 +233,6 @@ def test_commands_at_invocation(): 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' - 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 - - def test_transcript_from_cmdloop(request, capsys): # Create a cmd2.Cmd() instance and make sure basic settings are like we want for test app = CmdLineApp() |