summaryrefslogtreecommitdiff
path: root/plugins/template/tests
diff options
context:
space:
mode:
authorEric Lin <anselor@gmail.com>2020-07-13 15:28:40 -0400
committeranselor <anselor@gmail.com>2020-07-14 19:26:30 -0400
commite38684d219847f636562ab2720b82aae4a6fd408 (patch)
treeec20d7a46b8cba1a662d46e79d5e004590bee93b /plugins/template/tests
parent683d049299c0cf7a2821b639a95ad0911bab1bc7 (diff)
downloadcmd2-git-e38684d219847f636562ab2720b82aae4a6fd408.tar.gz
Brought in cmd2 plugin template as a first-class member of cmd2 proper
Diffstat (limited to 'plugins/template/tests')
-rw-r--r--plugins/template/tests/__init__.py2
-rw-r--r--plugins/template/tests/pylintrc19
-rw-r--r--plugins/template/tests/test_myplugin.py67
3 files changed, 88 insertions, 0 deletions
diff --git a/plugins/template/tests/__init__.py b/plugins/template/tests/__init__.py
new file mode 100644
index 00000000..eb198dc0
--- /dev/null
+++ b/plugins/template/tests/__init__.py
@@ -0,0 +1,2 @@
+#
+# empty file to create a package
diff --git a/plugins/template/tests/pylintrc b/plugins/template/tests/pylintrc
new file mode 100644
index 00000000..1dd17c1c
--- /dev/null
+++ b/plugins/template/tests/pylintrc
@@ -0,0 +1,19 @@
+#
+# pylint configuration for tests package
+#
+# $ pylint --rcfile=tests/pylintrc tests
+#
+
+[basic]
+# allow for longer method and function names
+method-rgx=(([a-z][a-z0-9_]{2,50})|(_[a-z0-9_]*))$
+function-rgx=(([a-z][a-z0-9_]{2,50})|(_[a-z0-9_]*))$
+
+[messages control]
+# too-many-public-methods -> test classes can have lots of methods, so let's ignore those
+# missing-docstring -> prefer method names instead of docstrings
+# no-self-use -> test methods part of a class hardly ever use self
+# unused-variable -> sometimes we are expecting exceptions
+# redefined-outer-name -> pylint fixtures cause these
+# protected-access -> we want to test private methods
+disable=too-many-public-methods,missing-docstring,no-self-use,unused-variable,redefined-outer-name,protected-access
diff --git a/plugins/template/tests/test_myplugin.py b/plugins/template/tests/test_myplugin.py
new file mode 100644
index 00000000..4149f7df
--- /dev/null
+++ b/plugins/template/tests/test_myplugin.py
@@ -0,0 +1,67 @@
+#
+# coding=utf-8
+
+from cmd2 import cmd2
+import cmd2_myplugin
+
+######
+#
+# define a class which uses our plugin and some convenience functions
+#
+######
+
+
+class MyApp(cmd2_myplugin.MyPluginMixin, cmd2.Cmd):
+ """Simple subclass of cmd2.Cmd with our SayMixin plugin included."""
+ def __init__(self, *args, **kwargs):
+ super().__init__(*args, **kwargs)
+
+ @cmd2_myplugin.empty_decorator
+ def do_empty(self, args):
+ self.poutput("running the empty command")
+
+#
+# You can't use a fixture to instantiate your app if you want to use
+# to use the capsys fixture to capture the output. cmd2.Cmd sets
+# internal variables to sys.stdout and sys.stderr on initialization
+# and then uses those internal variables instead of sys.stdout. It does
+# this so you can redirect output from within the app. The capsys fixture
+# can't capture the output properly in this scenario.
+#
+# If you have extensive initialization needs, create a function
+# to initialize your cmd2 application.
+
+
+def init_app():
+ app = MyApp()
+ return app
+
+
+#####
+#
+# unit tests
+#
+#####
+
+def test_say(capsys):
+ # call our initialization function instead of using a fixture
+ app = init_app()
+ # run our mixed in command
+ app.onecmd_plus_hooks('say hello')
+ # use the capsys fixture to retrieve the output on stdout and stderr
+ out, err = capsys.readouterr()
+ # make our assertions
+ assert out == 'in postparsing hook\nhello\n'
+ assert not err
+
+
+def test_decorator(capsys):
+ # call our initialization function instead of using a fixture
+ app = init_app()
+ # run one command in the app
+ app.onecmd_plus_hooks('empty')
+ # use the capsys fixture to retrieve the output on stdout and stderr
+ out, err = capsys.readouterr()
+ # make our assertions
+ assert out == 'in postparsing hook\nin the empty decorator\nrunning the empty command\n'
+ assert not err