diff options
author | Todd Leonhardt <todd.leonhardt@gmail.com> | 2017-07-02 17:45:12 -0400 |
---|---|---|
committer | Todd Leonhardt <todd.leonhardt@gmail.com> | 2017-07-02 17:45:12 -0400 |
commit | b0c3fa4dd7d762fb812a9cf74e6879dbfba04a50 (patch) | |
tree | 7324bef4db334308b257388f758e259b85d09da7 /tests/test_cmd2.py | |
parent | 482e9ce1f21677667eca52326d966a594ef6cc1a (diff) | |
download | cmd2-git-b0c3fa4dd7d762fb812a9cf74e6879dbfba04a50.tar.gz |
Fixed bugs, removed dead code, and added unit tests
Fixed a bug where CmdResult named tuple Truthiness wasn't working in Python 2
Removed unused cmd2.History.search() method.
Added unit tests for:
- History class
- cast() function
- CmdResult namedtuple
Diffstat (limited to 'tests/test_cmd2.py')
-rw-r--r-- | tests/test_cmd2.py | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/tests/test_cmd2.py b/tests/test_cmd2.py index 544b758e..02a26af9 100644 --- a/tests/test_cmd2.py +++ b/tests/test_cmd2.py @@ -1154,3 +1154,44 @@ def test_clipboard_failure(capsys): out, err = capsys.readouterr() assert out == '' assert 'Cannot redirect to paste buffer; install ``xclip`` and re-run to enable' in err + + +def test_run_command_with_empty_arg(base_app): + command = 'help' + run_cmd(base_app, command) + out = run_cmd(base_app, 'run') + expected = normalize('{}\n\n'.format(command) + BASE_HELP) + assert out == expected + +def test_run_command_with_empty_history(base_app): + out = run_cmd(base_app, 'run') + assert out == [] + + +class CmdResultApp(cmd2.Cmd): + def __init__(self, *args, **kwargs): + # Need to use this older form of invoking super class constructor to support Python 2.x and Python 3.x + cmd2.Cmd.__init__(self, *args, **kwargs) + + def do_affirmative(self, arg): + self._last_result = cmd2.CmdResult(arg) + + def do_negative(self, arg): + self._last_result = cmd2.CmdResult('', arg) + +@pytest.fixture +def cmdresult_app(): + app = CmdResultApp() + app.stdout = StdOut() + return app + +def test_cmdresult(cmdresult_app): + arg = 'foo' + run_cmd(cmdresult_app, 'affirmative {}'.format(arg)) + assert cmdresult_app._last_result + assert cmdresult_app._last_result == cmd2.CmdResult(arg) + + arg = 'bar' + run_cmd(cmdresult_app, 'negative {}'.format(arg)) + assert not cmdresult_app._last_result + assert cmdresult_app._last_result == cmd2.CmdResult('', arg) |