summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorEric Lin <anselor@gmail.com>2020-06-12 22:22:11 -0400
committeranselor <anselor@gmail.com>2020-08-04 13:38:08 -0400
commite32cccc4e599c924c3fd5f8376f7efd085f88019 (patch)
tree14ad4d8d4438d74652a2a7620084a1601752dc9e /tests
parent6da2cf30311f97d23a7121f8c02f9123674194b4 (diff)
downloadcmd2-git-e32cccc4e599c924c3fd5f8376f7efd085f88019.tar.gz
Added new constructor parameter to flag whether commands should autoload. Added unit tests. Moved installing commands into separate functions that can be called
Issue #943
Diffstat (limited to 'tests')
-rw-r--r--tests/test_commandset.py86
1 files changed, 86 insertions, 0 deletions
diff --git a/tests/test_commandset.py b/tests/test_commandset.py
new file mode 100644
index 00000000..acdb58b3
--- /dev/null
+++ b/tests/test_commandset.py
@@ -0,0 +1,86 @@
+# coding=utf-8
+# flake8: noqa E302
+"""
+Test CommandSet
+"""
+
+from typing import List
+import pytest
+
+import cmd2
+from cmd2 import utils
+
+
+# Python 3.5 had some regressions in the unitest.mock module, so use 3rd party mock if available
+try:
+ import mock
+except ImportError:
+ from unittest import mock
+
+
+@cmd2.register_command
+@cmd2.with_category("AAA")
+def do_unbound(cmd: cmd2.Cmd, statement: cmd2.Statement):
+ """
+ This is an example of registering an unbound function
+
+ :param cmd:
+ :param statement:
+ :return:
+ """
+ cmd.poutput('Unbound Command: {}'.format(statement.args))
+
+
+@cmd2.with_default_category('Command Set')
+class TestCommandSet(cmd2.CommandSet):
+ def do_apple(self, cmd: cmd2.Cmd, statement: cmd2.Statement):
+ cmd.poutput('Apple!')
+
+ def do_banana(self, cmd: cmd2.Cmd, statement: cmd2.Statement):
+ """Banana Command"""
+ cmd.poutput('Banana!!')
+
+ def do_cranberry(self, cmd: cmd2.Cmd, statement: cmd2.Statement):
+ cmd.poutput('Cranberry!!')
+
+ def help_cranberry(self, cmd: cmd2.Cmd):
+ cmd.stdout.write('This command does diddly squat...\n')
+
+ def do_durian(self, cmd: cmd2.Cmd, statement: cmd2.Statement):
+ """Durian Command"""
+ cmd.poutput('Durian!!')
+
+ def complete_durian(self, cmd: cmd2.Cmd, text: str, line: str, begidx: int, endidx: int) -> List[str]:
+ return utils.basic_complete(text, line, begidx, endidx, ['stinks', 'smells', 'disgusting'])
+
+ @cmd2.with_category('Alone')
+ def do_elderberry(self, cmd: cmd2.Cmd, statement: cmd2.Statement):
+ cmd.poutput('Elderberry!!')
+
+
+class WithCommandSets(cmd2.Cmd):
+ """Class for testing custom help_* methods which override docstring help."""
+ def __init__(self, *args, **kwargs):
+ super().__init__(*args, **kwargs)
+
+
+@pytest.fixture
+def command_sets_app():
+ app = WithCommandSets()
+ return app
+
+
+def test_autoload_commands(command_sets_app):
+ cmds_cats, cmds_doc, cmds_undoc, help_topics = command_sets_app._build_command_info()
+
+ assert 'AAA' in cmds_cats
+ assert 'unbound' in cmds_cats['AAA']
+
+ assert 'Alone' in cmds_cats
+ assert 'elderberry' in cmds_cats['Alone']
+
+ assert 'Command Set' in cmds_cats
+ assert 'cranberry' in cmds_cats['Command Set']
+
+
+