comparison cmd2.py @ 396:e60e2c15f026

ignore whitespace during comparisons
author Catherine Devlin <catherine.devlin@gmail.com>
date Thu, 16 Sep 2010 07:38:26 -0400
parents 9f9c69fbb78f
children 50acba85cf9e 3bef4253cf1b
comparison
equal deleted inserted replaced
395:9f9c69fbb78f 396:e60e2c15f026
1467 regexPattern = pyparsing.QuotedString(quoteChar=r'/', escChar='\\', multiline=True, unquoteResults=True) 1467 regexPattern = pyparsing.QuotedString(quoteChar=r'/', escChar='\\', multiline=True, unquoteResults=True)
1468 regexPattern.ignore(pyparsing.cStyleComment) 1468 regexPattern.ignore(pyparsing.cStyleComment)
1469 notRegexPattern = pyparsing.Word(pyparsing.printables) 1469 notRegexPattern = pyparsing.Word(pyparsing.printables)
1470 notRegexPattern.setParseAction(lambda t: re.escape(t[0])) 1470 notRegexPattern.setParseAction(lambda t: re.escape(t[0]))
1471 expectationParser = regexPattern | notRegexPattern 1471 expectationParser = regexPattern | notRegexPattern
1472 endStrippingRegex = re.compile(r'[ \t]*\n') 1472 anyWhitespace = re.compile(r'\s', re.DOTALL | re.MULTILINE)
1473 def _test_transcript(self, fname, transcript): 1473 def _test_transcript(self, fname, transcript):
1474 lineNum = 0 1474 lineNum = 0
1475 try: 1475 try:
1476 line = transcript.next() 1476 line = transcript.next()
1477 while True: 1477 while True:
1497 line = transcript.next() 1497 line = transcript.next()
1498 expected = ''.join(expected) 1498 expected = ''.join(expected)
1499 message = '\nFile %s, line %d\nCommand was:\n%s\nExpected:\n%s\nGot:\n%s\n'%\ 1499 message = '\nFile %s, line %d\nCommand was:\n%s\nExpected:\n%s\nGot:\n%s\n'%\
1500 (fname, lineNum, command, expected, result) 1500 (fname, lineNum, command, expected, result)
1501 expected = self.expectationParser.transformString(expected) 1501 expected = self.expectationParser.transformString(expected)
1502 expected = self.endStrippingRegex.sub('\s*\n', expected) 1502 # checking whitespace is a pain - let's skip it
1503 expected = self.anyWhitespace.sub('', expected)
1504 result = self.anyWhitespace.sub('', result)
1503 self.assert_(re.match(expected, result, re.MULTILINE | re.DOTALL), message) 1505 self.assert_(re.match(expected, result, re.MULTILINE | re.DOTALL), message)
1504 except StopIteration: 1506 except StopIteration:
1505 pass 1507 pass
1506 def tearDown(self): 1508 def tearDown(self):
1507 if self.CmdApp: 1509 if self.CmdApp: