Skip to content

Commit 2a474dc

Browse files
authored
Merge pull request #245 from python-cmd2/issue244
Fix for #244
2 parents 5f97663 + e8481e1 commit 2a474dc

File tree

3 files changed

+34
-13
lines changed

3 files changed

+34
-13
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ cmd2.egg-info
66
.cache
77
*.pyc
88
.coverage
9+
.tox
910
htmlcov

cmd2.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2398,7 +2398,22 @@ def _test_transcript(self, fname, transcript):
23982398
self.assertTrue(re.match(expected, result, re.MULTILINE | re.DOTALL), message)
23992399

24002400
def _transform_transcript_expected(self, s):
2401-
"""parse the string with slashed regexes into a valid regex"""
2401+
"""parse the string with slashed regexes into a valid regex
2402+
2403+
Given a string like:
2404+
2405+
Match a 10 digit phone number: /\d{3}-\d{3}-\d{4}/
2406+
2407+
Turn it into a valid regular expression which matches the literal text
2408+
of the string and the regular expression. We have to remove the slashes
2409+
because they differentiate between plain text and a regular expression.
2410+
Unless the slashes are escaped, in which case they are interpreted as
2411+
plain text, or there is only one slash, which is treated as plain text
2412+
also.
2413+
2414+
Check the tests in tests/test_transcript.py to see all the edge
2415+
cases.
2416+
"""
24022417
regex = ''
24032418
start = 0
24042419

tests/test_transcript.py

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

1213
import mock
@@ -309,21 +310,25 @@ def test_transcript(request, capsys, filename, feedback_to_output):
309310

310311

311312
@pytest.mark.parametrize('expected, transformed', [
312-
( 'text with no slashes', 'text\ with\ no\ slashes' ),
313-
# stuff with just one slash
314-
( 'use 2/3 cup', 'use\ 2\/3\ cup' ),
315-
( '/tmp is nice', '\/tmp\ is\ nice'),
316-
( 'slash at end/', 'slash\ at\ end\/'),
313+
# strings with zero or one slash or with escaped slashes means no regular
314+
# expression present, so the result should just be what re.escape returns.
315+
# we don't use static strings in these tests because re.escape behaves
316+
# differently in python 3.7 than in prior versions
317+
( 'text with no slashes', re.escape('text with no slashes') ),
318+
( 'specials .*', re.escape('specials .*') ),
319+
( 'use 2/3 cup', re.escape('use 2/3 cup') ),
320+
( '/tmp is nice', re.escape('/tmp is nice') ),
321+
( 'slash at end/', re.escape('slash at end/') ),
322+
# escaped slashes
323+
( 'not this slash\/ or this one\/', re.escape('not this slash/ or this one/' ) ),
317324
# regexes
318-
( 'specials .*', 'specials\ \.\*' ),
319325
( '/.*/', '.*' ),
320-
( 'specials ^ and + /[0-9]+/', 'specials\ \^\ and\ \+\ [0-9]+' ),
321-
( '/a{6}/ but not \/a{6} with /.*?/ more', 'a{6}\ but\ not\ \/a\{6\}\ with\ .*?\ more' ),
322-
( 'not this slash\/ or this one\/', 'not\ this\ slash\\/\ or\ this\ one\\/' ),
323-
( 'not \/, use /\|?/, not \/', 'not\ \\/\,\ use\ \|?\,\ not\ \\/' ),
326+
( 'specials ^ and + /[0-9]+/', re.escape('specials ^ and + ') + '[0-9]+' ),
327+
( '/a{6}/ but not \/a{6} with /.*?/ more', 'a{6}' + re.escape(' but not /a{6} with ') + '.*?' + re.escape(' more') ),
328+
( 'not \/, use /\|?/, not \/', re.escape('not /, use ') + '\|?' + re.escape(', not /') ),
324329
# inception: slashes in our regex. backslashed on input, bare on output
325-
( 'not \/, use /\/?/, not \/', 'not\ \\/\,\ use\ /?\,\ not\ \\/' ),
326-
( 'the /\/?/ more /.*/ stuff', 'the\ /?\ more\ .*\ stuff' ),
330+
( 'not \/, use /\/?/, not \/', re.escape('not /, use ') + '/?' + re.escape(', not /') ),
331+
( 'lots /\/?/ more /.*/ stuff', re.escape('lots ') + '/?' + re.escape(' more ') + '.*' + re.escape(' stuff') ),
327332
])
328333
def test_parse_transcript_expected(expected, transformed):
329334
app = CmdLineApp()

0 commit comments

Comments
 (0)