@@ -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 = '\n File %s, line %d\n Command was:\n %s\n Expected:\n %s\n Got:\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 = '\n File {}, line {}\n Command was:\n {}\n Expected:\n {}\n Got:\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
0 commit comments