summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorKevin Van Brunt <kmvanbrunt@gmail.com>2020-08-25 16:11:49 -0400
committerKevin Van Brunt <kmvanbrunt@gmail.com>2020-08-26 17:52:34 -0400
commit478ea83336a4d1659ad06ef365db3dc5e051e46d (patch)
treecbaff855e2d73f1d86ae1230f32770e31c62ad6b /tests
parent97c348c599d8fa963553593e5c19fb100b85e313 (diff)
downloadcmd2-git-478ea83336a4d1659ad06ef365db3dc5e051e46d.tar.gz
The functions cmd2 adds to Namespaces (get_statement() and get_handler()) are now
Cmd2AttributeWrapper objects named cmd2_statement and cmd2_handler. This makes it easy to filter out which attributes in an argparse.Namespace were added by cmd2.
Diffstat (limited to 'tests')
-rw-r--r--tests/test_argparse.py5
-rw-r--r--tests/test_argparse_custom.py10
-rw-r--r--tests/test_plugin.py2
3 files changed, 13 insertions, 4 deletions
diff --git a/tests/test_argparse.py b/tests/test_argparse.py
index 9806c9b5..20d05bed 100644
--- a/tests/test_argparse.py
+++ b/tests/test_argparse.py
@@ -289,7 +289,6 @@ class SubcommandApp(cmd2.Cmd):
func = getattr(args, 'func')
func(self, args)
-
# Add a subcommand using as_subcommand_to decorator
has_subcmd_parser = cmd2.Cmd2ArgumentParser(description="Tests as_subcmd_to decorator")
has_subcmd_subparsers = has_subcmd_parser.add_subparsers(dest='subcommand', metavar='SUBCOMMAND')
@@ -297,7 +296,7 @@ class SubcommandApp(cmd2.Cmd):
@cmd2.with_argparser(has_subcmd_parser)
def do_test_subcmd_decorator(self, args: argparse.Namespace):
- handler = args.get_handler()
+ handler = args.cmd2_handler.get()
handler(args)
subcmd_parser = cmd2.Cmd2ArgumentParser(add_help=False, description="The subcommand")
@@ -306,7 +305,7 @@ class SubcommandApp(cmd2.Cmd):
def subcmd_func(self, args: argparse.Namespace):
# Make sure printing the Namespace works. The way we originally added get_hander()
# to it resulted in a RecursionError when printing.
- print(args)
+ self.poutput(args)
@pytest.fixture
def subcommand_app():
diff --git a/tests/test_argparse_custom.py b/tests/test_argparse_custom.py
index 3ce90118..e2b3bb97 100644
--- a/tests/test_argparse_custom.py
+++ b/tests/test_argparse_custom.py
@@ -267,3 +267,13 @@ def test_apcustom_metavar_tuple():
parser = Cmd2ArgumentParser()
parser.add_argument('--aflag', nargs=2, metavar=('foo', 'bar'), help='This is a test')
assert '[--aflag foo bar]' in parser.format_help()
+
+
+def test_cmd2_attribute_wrapper():
+ initial_val = 5
+ wrapper = cmd2.Cmd2AttributeWrapper(initial_val)
+ assert wrapper.get() == initial_val
+
+ new_val = 22
+ wrapper.set(new_val)
+ assert wrapper.get() == new_val
diff --git a/tests/test_plugin.py b/tests/test_plugin.py
index d40a9b61..279f2f79 100644
--- a/tests/test_plugin.py
+++ b/tests/test_plugin.py
@@ -279,7 +279,7 @@ class PluggedApp(Plugin, cmd2.Cmd):
@with_argparser(parser)
def do_argparse_cmd(self, namespace: argparse.Namespace):
"""Repeat back the arguments"""
- self.poutput(namespace.get_statement())
+ self.poutput(namespace.cmd2_statement.get())
###
#