Skip to content

Commit 560133d

Browse files
committed
Simple but working version of new regex transcript
Escapes of slashes don’t work yet.
1 parent 1f2fea6 commit 560133d

2 files changed

Lines changed: 55 additions & 14 deletions

File tree

cmd2.py

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2231,12 +2231,6 @@ class Cmd2TestCase(unittest.TestCase):
22312231
that will execute the commands in a transcript file and expect the results shown.
22322232
See example.py"""
22332233
cmdapp = None
2234-
regexPattern = pyparsing.QuotedString(quoteChar=r'/', escChar='\\', multiline=True, unquoteResults=True)
2235-
regexPattern.ignore(pyparsing.cStyleComment)
2236-
notRegexPattern = pyparsing.Word(pyparsing.printables)
2237-
notRegexPattern.setParseAction(lambda t: re.escape(t[0]))
2238-
expectationParser = regexPattern | notRegexPattern
2239-
anyWhitespace = re.compile(r'\s', re.DOTALL | re.MULTILINE)
22402234

22412235
def fetchTranscripts(self):
22422236
self.transcripts = {}
@@ -2310,15 +2304,42 @@ def _test_transcript(self, fname, transcript):
23102304
break
23112305
line_num += 1
23122306
expected = ''.join(expected)
2313-
# Compare actual result to expected
2314-
message = '\nFile %s, line %d\nCommand was:\n%s\nExpected:\n%s\nGot:\n%s\n' % \
2315-
(fname, line_num, command, expected, result)
2316-
expected = self.expectationParser.transformString(expected)
2317-
# checking whitespace is a pain - let's skip it
2318-
expected = self.anyWhitespace.sub('', expected)
2319-
result = self.anyWhitespace.sub('', result)
2307+
2308+
# transform the expected text into a valid regular expression
2309+
expected = self._transform_transcript_expected(expected)
2310+
message = '\nFile {}, line {}\nCommand was:\n{}\nExpected:\n{}\nGot:\n{}\n'.format(
2311+
fname, line_num, command, expected, result)
23202312
self.assertTrue(re.match(expected, result, re.MULTILINE | re.DOTALL), message)
23212313

2314+
def _transform_transcript_expected(self, expected):
2315+
"""parse the expected text from the transcript into a valid regex"""
2316+
slash = '/'
2317+
backslash = '\\'
2318+
regex = ''
2319+
start = 0
2320+
while True:
2321+
first_slash_pos = expected.find(slash, start)
2322+
if first_slash_pos == -1:
2323+
# no more slashes, add the rest of the string and bail
2324+
regex += re.escape(expected[start:])
2325+
break
2326+
else:
2327+
# there is a slash, go find the next one
2328+
second_slash_pos = expected.find(slash, first_slash_pos+1)
2329+
if second_slash_pos > 0:
2330+
# add everything before the first slash as plain text
2331+
regex += re.escape(expected[start:first_slash_pos])
2332+
# add everything between the slashes (but not the slashes)
2333+
# as a regular expression
2334+
regex += expected[first_slash_pos+1:second_slash_pos]
2335+
# and change where we start looking for slashed on the
2336+
# turn through the loop
2337+
start = second_slash_pos + 1
2338+
else:
2339+
# no closing slash, treat it all as plain text
2340+
regex += re.escape(expected[start:])
2341+
return regex
2342+
23222343
def tearDown(self):
23232344
if self.cmdapp:
23242345
# Restore stdout

tests/test_transcript.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
# Used for sm.input: raw_input() for Python 2 or input() for Python 3
1616
import six.moves as sm
1717

18-
from cmd2 import Cmd, make_option, options, Cmd2TestCase, set_use_arg_list, set_posix_shlex, set_strip_quotes
18+
from cmd2 import (Cmd, make_option, options, Cmd2TestCase, set_use_arg_list,
19+
set_posix_shlex, set_strip_quotes)
1920
from conftest import run_cmd, StdOut, normalize
2021

2122

@@ -279,3 +280,22 @@ def test_transcript(request, capsys, filename, feedback_to_output):
279280
else:
280281
assert err == ''
281282
assert out == ''
283+
284+
285+
@pytest.mark.parametrize('expected, transformed', [
286+
( 'text with no slashes', 'text\ with\ no\ slashes'),
287+
( 'specials .*', 'specials\ \.\*'),
288+
( '/.*/', '.*'),
289+
( 'specials ^ and + /[0-9]+/', 'specials\ \^\ and\ \+\ [0-9]+'),
290+
( '/a{6}/ but not a{6} with /.*?/ more', 'a{6}\ but\ not\ a\{6\}\ with\ .*?\ more'),
291+
])
292+
def test_parse_transcript_expected(expected, transformed):
293+
app = CmdLineApp()
294+
295+
class TestMyAppCase(Cmd2TestCase):
296+
cmdapp = app
297+
298+
testcase = TestMyAppCase()
299+
300+
assert testcase._transform_transcript_expected(expected) == transformed
301+

0 commit comments

Comments
 (0)