Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ cmd2.egg-info
.cache
*.pyc
.coverage
.tox
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, that should have always been there ;-)

htmlcov
17 changes: 16 additions & 1 deletion cmd2.py
Original file line number Diff line number Diff line change
Expand Up @@ -2398,7 +2398,22 @@ def _test_transcript(self, fname, transcript):
self.assertTrue(re.match(expected, result, re.MULTILINE | re.DOTALL), message)

def _transform_transcript_expected(self, s):
"""parse the string with slashed regexes into a valid regex"""
"""parse the string with slashed regexes into a valid regex
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice description


Given a string like:

Match a 10 digit phone number: /\d{3}-\d{3}-\d{4}/

Turn it into a valid regular expression which matches the literal text
of the string and the regular expression. We have to remove the slashes
because they differentiate between plain text and a regular expression.
Unless the slashes are escaped, in which case they are interpreted as
plain text, or there is only one slash, which is treated as plain text
also.

Check the tests in tests/test_transcript.py to see all the edge
cases.
"""
regex = ''
start = 0

Expand Down
29 changes: 17 additions & 12 deletions tests/test_transcript.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"""
import os
import sys
import re
import random

import mock
Expand Down Expand Up @@ -309,21 +310,25 @@ def test_transcript(request, capsys, filename, feedback_to_output):


@pytest.mark.parametrize('expected, transformed', [
( 'text with no slashes', 'text\ with\ no\ slashes' ),
# stuff with just one slash
( 'use 2/3 cup', 'use\ 2\/3\ cup' ),
( '/tmp is nice', '\/tmp\ is\ nice'),
( 'slash at end/', 'slash\ at\ end\/'),
# strings with zero or one slash or with escaped slashes means no regular
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good.

# expression present, so the result should just be what re.escape returns.
# we don't use static strings in these tests because re.escape behaves
# differently in python 3.7 than in prior versions
( 'text with no slashes', re.escape('text with no slashes') ),
( 'specials .*', re.escape('specials .*') ),
( 'use 2/3 cup', re.escape('use 2/3 cup') ),
( '/tmp is nice', re.escape('/tmp is nice') ),
( 'slash at end/', re.escape('slash at end/') ),
# escaped slashes
( 'not this slash\/ or this one\/', re.escape('not this slash/ or this one/' ) ),
# regexes
( 'specials .*', 'specials\ \.\*' ),
( '/.*/', '.*' ),
( 'specials ^ and + /[0-9]+/', 'specials\ \^\ and\ \+\ [0-9]+' ),
( '/a{6}/ but not \/a{6} with /.*?/ more', 'a{6}\ but\ not\ \/a\{6\}\ with\ .*?\ more' ),
( 'not this slash\/ or this one\/', 'not\ this\ slash\\/\ or\ this\ one\\/' ),
( 'not \/, use /\|?/, not \/', 'not\ \\/\,\ use\ \|?\,\ not\ \\/' ),
( 'specials ^ and + /[0-9]+/', re.escape('specials ^ and + ') + '[0-9]+' ),
( '/a{6}/ but not \/a{6} with /.*?/ more', 'a{6}' + re.escape(' but not /a{6} with ') + '.*?' + re.escape(' more') ),
( 'not \/, use /\|?/, not \/', re.escape('not /, use ') + '\|?' + re.escape(', not /') ),
# inception: slashes in our regex. backslashed on input, bare on output
( 'not \/, use /\/?/, not \/', 'not\ \\/\,\ use\ /?\,\ not\ \\/' ),
( 'the /\/?/ more /.*/ stuff', 'the\ /?\ more\ .*\ stuff' ),
( 'not \/, use /\/?/, not \/', re.escape('not /, use ') + '/?' + re.escape(', not /') ),
( 'lots /\/?/ more /.*/ stuff', re.escape('lots ') + '/?' + re.escape(' more ') + '.*' + re.escape(' stuff') ),
])
def test_parse_transcript_expected(expected, transformed):
app = CmdLineApp()
Expand Down