summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/pyscript/foo4.py8
-rw-r--r--tests/scripts/recursive.py2
-rw-r--r--tests/test_pyscript.py52
3 files changed, 60 insertions, 2 deletions
diff --git a/tests/pyscript/foo4.py b/tests/pyscript/foo4.py
new file mode 100644
index 00000000..88fd3ce8
--- /dev/null
+++ b/tests/pyscript/foo4.py
@@ -0,0 +1,8 @@
+result = app.foo('aaa', 'bbb', counter=3)
+out_text = 'Fail'
+if result:
+ data = result.data
+ if 'aaa' in data.variable and 'bbb' in data.variable and data.counter == 3:
+ out_text = 'Success'
+
+print(out_text)
diff --git a/tests/scripts/recursive.py b/tests/scripts/recursive.py
index 84f445bb..32c981b6 100644
--- a/tests/scripts/recursive.py
+++ b/tests/scripts/recursive.py
@@ -3,4 +3,4 @@
"""
Example demonstrating that running a Python script recursively inside another Python script isn't allowed
"""
-cmd('pyscript ../script.py')
+app('pyscript ../script.py')
diff --git a/tests/test_pyscript.py b/tests/test_pyscript.py
index 5d685c41..8d0cefd8 100644
--- a/tests/test_pyscript.py
+++ b/tests/test_pyscript.py
@@ -8,7 +8,8 @@ import os
import pytest
from cmd2.cmd2 import Cmd, with_argparser
from cmd2 import argparse_completer
-from .conftest import run_cmd, normalize, StdOut, complete_tester
+from .conftest import run_cmd, StdOut
+from cmd2.utils import namedtuple_with_defaults
class PyscriptExample(Cmd):
ratings_types = ['G', 'PG', 'PG-13', 'R', 'NC-17']
@@ -82,6 +83,16 @@ class PyscriptExample(Cmd):
@with_argparser(foo_parser)
def do_foo(self, args):
print('foo ' + str(args.__dict__))
+ if self._in_py:
+ FooResult = namedtuple_with_defaults('FooResult',
+ ['counter', 'trueval', 'constval',
+ 'variable', 'optional', 'zeroormore'])
+ self._last_result = FooResult(**{'counter': args.counter,
+ 'trueval': args.trueval,
+ 'constval': args.constval,
+ 'variable': args.variable,
+ 'optional': args.optional,
+ 'zeroormore': args.zeroormore})
bar_parser = argparse_completer.ACArgumentParser(prog='bar')
bar_parser.add_argument('first')
@@ -101,6 +112,23 @@ def ps_app():
return c
+class PyscriptCustomNameExample(Cmd):
+ def __init__(self):
+ super().__init__()
+ self.pyscript_name = 'custom'
+
+ def do_echo(self, out):
+ print(out)
+
+
+@pytest.fixture
+def ps_echo():
+ c = PyscriptCustomNameExample()
+ c.stdout = StdOut()
+
+ return c
+
+
@pytest.mark.parametrize('command, pyscript_file', [
('help', 'help.py'),
('help media', 'help_media.py'),
@@ -162,3 +190,25 @@ def test_pyscript_errors(ps_app, capsys, command, error):
assert 'Traceback' in err
assert error in err
+
+@pytest.mark.parametrize('pyscript_file, exp_out', [
+ ('foo4.py', 'Success'),
+])
+def test_pyscript_results(ps_app, capsys, request, pyscript_file, exp_out):
+ test_dir = os.path.dirname(request.module.__file__)
+ python_script = os.path.join(test_dir, 'pyscript', pyscript_file)
+
+ run_cmd(ps_app, 'pyscript {}'.format(python_script))
+ expected, _ = capsys.readouterr()
+ assert len(expected) > 0
+ assert exp_out in expected
+
+
+def test_pyscript_custom_name(ps_echo, capsys):
+ message = 'blah!'
+ run_cmd(ps_echo, 'py custom.echo("{}")'.format(message))
+ expected, _ = capsys.readouterr()
+ assert len(expected) > 0
+ expected = expected.splitlines()
+ assert message == expected[0]
+