summaryrefslogtreecommitdiff
path: root/tests/test_transcript.py
diff options
context:
space:
mode:
authorEric Lin <anselor@gmail.com>2018-05-16 10:39:49 -0400
committerEric Lin <anselor@gmail.com>2018-05-16 10:39:49 -0400
commit371284d20370a8e85dd8527d9bbcc6267b335cde (patch)
tree002ac709eca15522a0b1abaa235aae04975b08e6 /tests/test_transcript.py
parentab8194e92b9c3728d8f86cb9c81de180b6884eee (diff)
parenta9b712108e5af49937b0af3aa51db2ebe5c159e4 (diff)
downloadcmd2-git-371284d20370a8e85dd8527d9bbcc6267b335cde.tar.gz
Merge branch 'master' into pyscript
Diffstat (limited to 'tests/test_transcript.py')
-rw-r--r--tests/test_transcript.py175
1 files changed, 32 insertions, 143 deletions
diff --git a/tests/test_transcript.py b/tests/test_transcript.py
index c0fb49c1..70658161 100644
--- a/tests/test_transcript.py
+++ b/tests/test_transcript.py
@@ -10,12 +10,13 @@ import os
import sys
import re
import random
+import tempfile
from unittest import mock
import pytest
from cmd2 import cmd2
-from .conftest import run_cmd, StdOut, normalize
+from .conftest import run_cmd, StdOut
class CmdLineApp(cmd2.Cmd):
@@ -26,7 +27,6 @@ class CmdLineApp(cmd2.Cmd):
def __init__(self, *args, **kwargs):
self.multiline_commands = ['orate']
self.maxrepeats = 3
- self.redirector = '->'
# Add stuff to settable and/or shortcuts before calling base class initializer
self.settable['maxrepeats'] = 'Max number of `--repeat`s allowed'
@@ -63,7 +63,7 @@ class CmdLineApp(cmd2.Cmd):
def do_mumble(self, opts, arg):
"""Mumbles what you tell me to."""
repetitions = opts.repeat or 1
- arg = arg.split()
+ #arg = arg.split()
for i in range(min(repetitions, self.maxrepeats)):
output = []
if random.random() < .33:
@@ -77,136 +77,6 @@ class CmdLineApp(cmd2.Cmd):
self.poutput(' '.join(output))
-class DemoApp(cmd2.Cmd):
- hello_parser = argparse.ArgumentParser()
- hello_parser.add_argument('-n', '--name', help="your name")
- @cmd2.with_argparser_and_unknown_args(hello_parser)
- def do_hello(self, opts, arg):
- """Says hello."""
- if opts.name:
- self.stdout.write('Hello {}\n'.format(opts.name))
- else:
- self.stdout.write('Hello Nobody\n')
-
-
-@pytest.fixture
-def _cmdline_app():
- c = CmdLineApp()
- c.stdout = StdOut()
- return c
-
-
-@pytest.fixture
-def _demo_app():
- c = DemoApp()
- c.stdout = StdOut()
- return c
-
-
-def _get_transcript_blocks(transcript):
- cmd = None
- expected = ''
- for line in transcript.splitlines():
- if line.startswith('(Cmd) '):
- if cmd is not None:
- yield cmd, normalize(expected)
-
- cmd = line[6:]
- expected = ''
- else:
- expected += line + '\n'
- yield cmd, normalize(expected)
-
-
-def test_base_with_transcript(_cmdline_app):
- app = _cmdline_app
- transcript = """
-(Cmd) help
-
-Documented commands (type help <topic>):
-========================================
-alias help load orate pyscript say shell speak
-edit history mumble py quit set shortcuts unalias
-
-(Cmd) help say
-usage: speak [-h] [-p] [-s] [-r REPEAT]
-
-Repeats what you tell me to.
-
-optional arguments:
- -h, --help show this help message and exit
- -p, --piglatin atinLay
- -s, --shout N00B EMULATION MODE
- -r REPEAT, --repeat REPEAT
- output [n] times
-
-(Cmd) say goodnight, Gracie
-goodnight, Gracie
-(Cmd) say -ps --repeat=5 goodnight, Gracie
-OODNIGHT, GRACIEGAY
-OODNIGHT, GRACIEGAY
-OODNIGHT, GRACIEGAY
-(Cmd) set maxrepeats 5
-maxrepeats - was: 3
-now: 5
-(Cmd) say -ps --repeat=5 goodnight, Gracie
-OODNIGHT, GRACIEGAY
-OODNIGHT, GRACIEGAY
-OODNIGHT, GRACIEGAY
-OODNIGHT, GRACIEGAY
-OODNIGHT, GRACIEGAY
-(Cmd) history
--------------------------[1]
-help
--------------------------[2]
-help say
--------------------------[3]
-say goodnight, Gracie
--------------------------[4]
-say -ps --repeat=5 goodnight, Gracie
--------------------------[5]
-set maxrepeats 5
--------------------------[6]
-say -ps --repeat=5 goodnight, Gracie
-(Cmd) history -r 4
-OODNIGHT, GRACIEGAY
-OODNIGHT, GRACIEGAY
-OODNIGHT, GRACIEGAY
-OODNIGHT, GRACIEGAY
-OODNIGHT, GRACIEGAY
-(Cmd) set prompt "---> "
-prompt - was: (Cmd)
-now: --->
-"""
-
- for cmd, expected in _get_transcript_blocks(transcript):
- out = run_cmd(app, cmd)
- assert out == expected
-
-
-class TestMyAppCase(cmd2.Cmd2TestCase):
- CmdApp = CmdLineApp
- CmdApp.testfiles = ['tests/transcript.txt']
-
-
-def test_comment_stripping(_cmdline_app):
- out = run_cmd(_cmdline_app, 'speak it was /* not */ delicious! # Yuck!')
- expected = normalize("""it was delicious!""")
- assert out == expected
-
-
-def test_argparser_correct_args_with_quotes_and_midline_options(_cmdline_app):
- out = run_cmd(_cmdline_app, "speak 'This is a' -s test of the emergency broadcast system!")
- expected = normalize("""THIS IS A TEST OF THE EMERGENCY BROADCAST SYSTEM!""")
- assert out == expected
-
-
-def test_argparser_options_with_spaces_in_quotes(_demo_app):
- out = run_cmd(_demo_app, "hello foo -n 'Bugs Bunny' bar baz")
- expected = normalize("""Hello Bugs Bunny""")
- assert out == expected
-
-
def test_commands_at_invocation():
testargs = ["prog", "say hello", "say Gracie", "quit"]
expected = "This is an intro banner ...\nhello\nGracie\n"
@@ -217,19 +87,12 @@ def test_commands_at_invocation():
out = app.stdout.buffer
assert out == expected
-def test_invalid_syntax(_cmdline_app, capsys):
- run_cmd(_cmdline_app, 'speak "')
- out, err = capsys.readouterr()
- expected = normalize("""ERROR: Invalid syntax: No closing quotation""")
- assert normalize(str(err)) == expected
-
-
-@pytest.mark.parametrize('filename, feedback_to_output', [
+@pytest.mark.parametrize('filename,feedback_to_output', [
('bol_eol.txt', False),
('characterclass.txt', False),
('dotstar.txt', False),
('extension_notation.txt', False),
- # ('from_cmdloop.txt', True),
+ ('from_cmdloop.txt', True),
('multiline_no_regex.txt', False),
('multiline_regex.txt', False),
('regex_set.txt', False),
@@ -237,7 +100,7 @@ def test_invalid_syntax(_cmdline_app, capsys):
('slashes_escaped.txt', False),
('slashslash.txt', False),
('spaces.txt', False),
- # ('word_boundaries.txt', False),
+ ('word_boundaries.txt', False),
])
def test_transcript(request, capsys, filename, feedback_to_output):
# Create a cmd2.Cmd() instance and make sure basic settings are
@@ -263,6 +126,32 @@ def test_transcript(request, capsys, filename, feedback_to_output):
assert err.startswith(expected_start)
assert err.endswith(expected_end)
+def test_history_transcript(request, capsys):
+ app = CmdLineApp()
+ app.stdout = StdOut()
+ run_cmd(app, 'orate this is\na /multiline/\ncommand;\n')
+ run_cmd(app, 'speak /tmp/file.txt is not a regex')
+
+ expected = r"""(Cmd) orate this is
+> a /multiline/
+> command;
+this is a \/multiline\/ command
+(Cmd) speak /tmp/file.txt is not a regex
+\/tmp\/file.txt is not a regex
+"""
+
+ # make a tmp file
+ fd, history_fname = tempfile.mkstemp(prefix='', suffix='.txt')
+ os.close(fd)
+
+ # tell the history command to create a transcript
+ run_cmd(app, 'history -t "{}"'.format(history_fname))
+
+ # read in the transcript created by the history command
+ with open(history_fname) as f:
+ transcript = f.read()
+
+ assert transcript == expected
@pytest.mark.parametrize('expected, transformed', [
# strings with zero or one slash or with escaped slashes means no regular