summaryrefslogtreecommitdiff
path: root/tests/test_argparse.py
diff options
context:
space:
mode:
authorTodd Leonhardt <todd.leonhardt@gmail.com>2018-01-20 16:48:52 -0500
committerTodd Leonhardt <todd.leonhardt@gmail.com>2018-01-20 16:48:52 -0500
commit5550ab73a91d2834e6bda72eb3889998ad59be51 (patch)
tree12b387896ea91720416639daf8b10f8d66406dda /tests/test_argparse.py
parent29ef26875664dfa3d70273b321092a83a68d9966 (diff)
downloadcmd2-git-5550ab73a91d2834e6bda72eb3889998ad59be51.tar.gz
Added unit tests for sub-commands
Diffstat (limited to 'tests/test_argparse.py')
-rw-r--r--tests/test_argparse.py92
1 files changed, 92 insertions, 0 deletions
diff --git a/tests/test_argparse.py b/tests/test_argparse.py
index 733e741b..bb494d49 100644
--- a/tests/test_argparse.py
+++ b/tests/test_argparse.py
@@ -8,6 +8,7 @@ import pytest
import cmd2
from conftest import run_cmd, StdOut
+
class ArgparseApp(cmd2.Cmd):
def __init__(self):
self.maxrepeats = 3
@@ -168,3 +169,94 @@ def test_arglist(argparse_app):
def test_arglist_decorator_twice(argparse_app):
out = run_cmd(argparse_app, 'arglisttwice "we should" get these')
assert out[0] == 'we should get these'
+
+
+class SubcommandApp(cmd2.Cmd):
+ """ Example cmd2 application where we a base command which has a couple subcommands."""
+
+ def __init__(self):
+ cmd2.Cmd.__init__(self)
+
+ # sub-command functions for the base command
+ def foo(self, args):
+ """foo subcommand of base command"""
+ self.poutput(args.x * args.y)
+
+ def bar(self, args):
+ """bar sucommand of base command"""
+ self.poutput('((%s))' % args.z)
+
+ # create the top-level parser for the base command
+ base_parser = argparse.ArgumentParser(prog='base')
+ base_subparsers = base_parser.add_subparsers(title='subcommands', help='subcommand help')
+
+ # create the parser for the "foo" sub-command
+ parser_foo = base_subparsers.add_parser('foo', help='foo help')
+ parser_foo.add_argument('-x', type=int, default=1, help='integer')
+ parser_foo.add_argument('y', type=float, help='float')
+ parser_foo.set_defaults(func=foo)
+
+ # create the parser for the "bar" sub-command
+ parser_bar = base_subparsers.add_parser('bar', help='bar help')
+ parser_bar.add_argument('z', help='string')
+ parser_bar.set_defaults(func=bar)
+
+ # Create a list of subcommand names, which is used to enable tab-completion of sub-commands
+ subcommands = ['foo', 'bar']
+
+ @cmd2.with_argparser_and_unknown_args(base_parser, subcommands)
+ def do_base(self, args, arglist):
+ """Base command help"""
+ try:
+ # Call whatever sub-command function was selected
+ args.func(self, args)
+ except AttributeError:
+ # No sub-command was provided, so as called
+ self.do_help('base')
+
+@pytest.fixture
+def subcommand_app():
+ app = SubcommandApp()
+ app.stdout = StdOut()
+ return app
+
+
+def test_subcommand_foo(subcommand_app):
+ out = run_cmd(subcommand_app, 'base foo -x2 5.0')
+ assert out == ['10.0']
+
+
+def test_subcommand_bar(subcommand_app):
+ out = run_cmd(subcommand_app, 'base bar baz')
+ assert out == ['((baz))']
+
+def test_subcommand_invalid(subcommand_app, capsys):
+ run_cmd(subcommand_app, 'base baz')
+ out, err = capsys.readouterr()
+ err = err.splitlines()
+ assert err[0].startswith('usage: base')
+ assert err[1].startswith("base: error: invalid choice: 'baz'")
+
+def test_subcommand_base_help(subcommand_app, capsys):
+ run_cmd(subcommand_app, 'help base')
+ out, err = capsys.readouterr()
+ out = out.splitlines()
+ assert out[0].startswith('usage: base')
+ assert out[1] == ''
+ assert out[2] == 'Base command help'
+
+def test_subcommand_help(subcommand_app, capsys):
+ run_cmd(subcommand_app, 'help base foo')
+ out, err = capsys.readouterr()
+ out = out.splitlines()
+ assert out[0].startswith('usage: base foo')
+ assert out[1] == ''
+ assert out[2] == 'positional arguments:'
+
+
+def test_subcommand_invalid_help(subcommand_app, capsys):
+ run_cmd(subcommand_app, 'help base baz')
+ out, err = capsys.readouterr()
+ err = err.splitlines()
+ assert err[0].startswith('usage: base')
+ assert err[1].startswith("base: error: invalid choice: 'baz'")