Skip to content

Commit 208fd69

Browse files
committed
regex implementation complete. Still a few bugs.
1 parent d3766eb commit 208fd69

12 files changed

Lines changed: 67 additions & 34 deletions

docs/transcript.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,14 @@ match the path instead of specifying it verbatim, or we can escape the slashes::
9494
(Cmd) say cd /usr/local/lib/python3.6/site-packages
9595
\/usr\/local\/lib\/python3.6\/site-packages
9696

97+
.. note::
98+
99+
Be careful with trailing spaces and newlines. Some terminal emulators strip
100+
trailing space when you copy text from them. This could make the actual data
101+
generated by your app different than the text you pasted into the
102+
transcript, and it might not be readily obvious why the transcript is not
103+
passing.
104+
97105

98106
Running a transcript
99107
====================

examples/transcript_regex.txt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
# Run this transcript with "python example.py -t transcript_regex.txt"
22
# The regex for colors is because no color on Windows.
33
# The regex for editor will match whatever program you use.
4+
# regexes on prompts just make the trailing space obvious
45
(Cmd) set
56
abbrev: True
67
autorun_on_edit: False
78
colors: /(True|False)/
8-
continuation_prompt: >
9+
continuation_prompt: >/ /
910
debug: False
1011
echo: False
11-
editor: /.*/
12+
editor: /.*?/
1213
feedback_to_output: False
1314
locals_in_py: True
1415
maxrepeats: 3
15-
prompt: (Cmd)
16+
prompt: (Cmd)/ /
1617
quiet: False
1718
timing: False

tests/test_transcript.py

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"""
88
import os
99
import sys
10+
import random
1011

1112
import mock
1213
import pytest
@@ -21,6 +22,11 @@
2122

2223

2324
class CmdLineApp(Cmd):
25+
26+
MUMBLES = ['like', '...', 'um', 'er', 'hmmm', 'ahh']
27+
MUMBLE_FIRST = ['so', 'like', 'well']
28+
MUMBLE_LAST = ['right?']
29+
2430
def __init__(self, *args, **kwargs):
2531
self.abbrev = True
2632
self.multilineCommands = ['orate']
@@ -61,6 +67,23 @@ def do_speak(self, arg, opts=None):
6167
do_say = do_speak # now "say" is a synonym for "speak"
6268
do_orate = do_speak # another synonym, but this one takes multi-line input
6369

70+
@options([ make_option('-r', '--repeat', type="int", help="output [n] times") ])
71+
def do_mumble(self, arg, opts=None):
72+
"""Mumbles what you tell me to."""
73+
repetitions = opts.repeat or 1
74+
arg = arg.split()
75+
for i in range(min(repetitions, self.maxrepeats)):
76+
output = []
77+
if (random.random() < .33):
78+
output.append(random.choice(self.MUMBLE_FIRST))
79+
for word in arg:
80+
if (random.random() < .40):
81+
output.append(random.choice(self.MUMBLES))
82+
output.append(word)
83+
if (random.random() < .25):
84+
output.append(random.choice(self.MUMBLE_LAST))
85+
self.poutput(' '.join(output))
86+
6487

6588
class DemoApp(Cmd):
6689
@options(make_option('-n', '--name', action="store", help="your name"))
@@ -108,8 +131,9 @@ def test_base_with_transcript(_cmdline_app):
108131
109132
Documented commands (type help <topic>):
110133
========================================
111-
_relative_load edit history orate pyscript run say shell show
112-
cmdenvironment help load py quit save set shortcuts speak
134+
_relative_load help mumble pyscript save shell speak
135+
cmdenvironment history orate quit say shortcuts
136+
edit load py run set show
113137
114138
(Cmd) help say
115139
Repeats what you tell me to.
@@ -251,9 +275,10 @@ def test_invalid_syntax(_cmdline_app, capsys):
251275
('regex_set.txt', False),
252276
('singleslash.txt', False),
253277
('slashdot.txt', False),
254-
('slashslash_escaped.txt', False),
278+
('slashes_escaped.txt', False),
255279
('slashslash.txt', False),
256280
('spaces.txt', False),
281+
('word_boundaries.txt', False),
257282
])
258283
def test_transcript(request, capsys, filename, feedback_to_output):
259284
# Create a cmd2.Cmd() instance and make sure basic settings are
@@ -285,16 +310,17 @@ def test_transcript(request, capsys, filename, feedback_to_output):
285310

286311

287312
@pytest.mark.parametrize('expected, transformed', [
288-
( 'text with no slashes', 'text\ with\ no\ slashes'),
289-
( 'specials .*', 'specials\ \.\*'),
290-
( '/.*/', '.*'),
291-
( 'specials ^ and + /[0-9]+/', 'specials\ \^\ and\ \+\ [0-9]+'),
292-
( '/a{6}/ but not a{6} with /.*?/ more', 'a{6}\ but\ not\ a\{6\}\ with\ .*?\ more'),
293-
( 'not this slash\/ or this one\/', 'not\ this\ slash\\/\ or\ this\ one\\/'),
294-
( 'not \/, use /\|?/, not \/', 'not\ \\/\,\ use\ \|?\,\ not\ \\/'),
313+
( 'text with no slashes', 'text\ with\ no\ slashes' ),
314+
( 'specials .*', 'specials\ \.\*' ),
315+
( '/.*/', '.*' ),
316+
( 'use 2\/3 cup', 'use\ 2\/3\ cup' ),
317+
( 'specials ^ and + /[0-9]+/', 'specials\ \^\ and\ \+\ [0-9]+' ),
318+
( '/a{6}/ but not a{6} with /.*?/ more', 'a{6}\ but\ not\ a\{6\}\ with\ .*?\ more' ),
319+
( 'not this slash\/ or this one\/', 'not\ this\ slash\\/\ or\ this\ one\\/' ),
320+
( 'not \/, use /\|?/, not \/', 'not\ \\/\,\ use\ \|?\,\ not\ \\/' ),
295321
# inception: we have a slashes in our regex: backslashed on input, bare on output
296-
( 'not \/, use /\/?/, not \/', 'not\ \\/\,\ use\ /?\,\ not\ \\/'),
297-
( 'the /\/?/ more /.*/ stuff', 'the\ /?\ more\ .*\ stuff')
322+
( 'not \/, use /\/?/, not \/', 'not\ \\/\,\ use\ /?\,\ not\ \\/' ),
323+
( 'the /\/?/ more /.*/ stuff', 'the\ /?\ more\ .*\ stuff' ),
298324
])
299325
def test_parse_transcript_expected(expected, transformed):
300326
app = CmdLineApp()

tests/transcripts/from_cmdloop.txt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
Documented commands (type help <topic>):
44
========================================
5-
_relative_load edit history orate pyscript run say shell show
6-
cmdenvironment help load py quit save set shortcuts speak
5+
_relative_load help mumble pyscript save shell speak
6+
cmdenvironment history orate quit say shortcuts
7+
edit load py run set show
78

89
(Cmd) help say
910
Repeats what you tell me to.
@@ -46,12 +47,11 @@ set maxrepeats 5
4647
say -ps --repeat=5 goodnight, Gracie
4748
(Cmd) run 4
4849
say -ps --repeat=5 goodnight, Gracie
49-
5050
OODNIGHT, GRACIEGAY
5151
OODNIGHT, GRACIEGAY
5252
OODNIGHT, GRACIEGAY
5353
OODNIGHT, GRACIEGAY
5454
OODNIGHT, GRACIEGAY
5555
(Cmd) set prompt "---> "
56-
prompt - was: (Cmd)
57-
now: --->
56+
prompt - was: (Cmd)/ /
57+
now: --->/ /

tests/transcripts/multiline_no_regex.txt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,4 @@
55
(Cmd) orate This is a test
66
> of the
77
> emergency broadcast system
8-
This is a test
9-
of the
10-
emergency broadcast system
8+
This is a test of the emergency broadcast system
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
(Cmd) say -r 3 -s yabba dabba do
22
/\A(YA.*?DO\n?){3}/
33
(Cmd) say -r 5 -s yabba dabba do
4-
YABBA DABBA DO
5-
YABBA /[A-Z ]*?/
6-
DO
4+
/\A([A-Z\s]*$){3}/

tests/transcripts/regex_set.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
# Run this transcript with "python example.py -t transcript_regex.txt"
22
# The regex for colors is because no color on Windows.
33
# The regex for editor will match whatever program you use.
4+
# regexes on prompts just make the trailing space obvious
45
(Cmd) set
56
abbrev: True
67
autorun_on_edit: False
78
colors: /(True|False)/
8-
continuation_prompt: >
9+
continuation_prompt: >/ /
910
debug: False
1011
echo: False
1112
editor: /.*/
1213
feedback_to_output: False
1314
locals_in_py: True
1415
maxrepeats: 3
15-
prompt: (Cmd)
16+
prompt: (Cmd)/ /
1617
quiet: False
1718
timing: False

tests/transcripts/singleslash.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
(Cmd) say use 2/3 cup of sugar
2-
use 2/3 cup of sugar
2+
use 2\/3 cup of sugar

tests/transcripts/slashdot.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
(Cmd) say mix 2/3 c. sugar, 1/2 c. butter, and 1/2 tsp. salt
2-
mix 2/3 c. sugar, 1/2 c. butter, and 1/2 tsp. salt
2+
mix 2\/3 c. sugar, 1\/2 c. butter, and 1\/2 tsp. salt
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,2 @@
1-
(Cmd) say //
2-
\/\/
31
(Cmd) say /some/unix/path
42
\/some\/unix\/path
5-

0 commit comments

Comments
 (0)