comparison cmd2.py @ 404:e4bc5174aad1

don't let falling-off-the-edge equal test success
author catherine@dellzilla
date Thu, 25 Mar 2010 18:40:03 -0400
parents 2594f69698b2
children
comparison
equal deleted inserted replaced
403:c4a821f40050 404:e4bc5174aad1
1470 notRegexPattern = pyparsing.Word(pyparsing.printables) 1470 notRegexPattern = pyparsing.Word(pyparsing.printables)
1471 notRegexPattern.setParseAction(lambda t: re.escape(t[0])) 1471 notRegexPattern.setParseAction(lambda t: re.escape(t[0]))
1472 expectationParser = regexPattern | notRegexPattern 1472 expectationParser = regexPattern | notRegexPattern
1473 endStrippingRegex = re.compile(r'[ \t]*\n') 1473 endStrippingRegex = re.compile(r'[ \t]*\n')
1474 def _test_transcript(self, fname, transcript): 1474 def _test_transcript(self, fname, transcript):
1475 def assert_matching():
1476 _expected = ''.join(expected)
1477 _expected = self.expectationParser.transformString(_expected)
1478 _expected = self.endStrippingRegex.sub('\s*', _expected) # was \s*\n
1479 _message = '\nFile %s, line %d\nCommand was:\n%s\nExpected:\n%s\nGot:\n%s\n'%\
1480 (fname, lineNum, command, _expected, result)
1481 self.assert_(re.match(_expected, result.strip(), re.MULTILINE | re.DOTALL), _message)
1475 lineNum = 0 1482 lineNum = 0
1476 try: 1483 try:
1477 line = transcript.next() 1484 line = transcript.next()
1478 while True: 1485 while True:
1479 while not line.startswith(self.cmdapp.prompt): 1486 while not line.startswith(self.cmdapp.prompt):
1481 command = [line[len(self.cmdapp.prompt):]] 1488 command = [line[len(self.cmdapp.prompt):]]
1482 line = transcript.next() 1489 line = transcript.next()
1483 while line.startswith(self.cmdapp.continuation_prompt): 1490 while line.startswith(self.cmdapp.continuation_prompt):
1484 command.append(line[len(self.cmdapp.continuation_prompt):]) 1491 command.append(line[len(self.cmdapp.continuation_prompt):])
1485 line = transcript.next() 1492 line = transcript.next()
1486 command = ''.join(command) 1493 command = ''.join(command)
1487 stop = self.cmdapp.onecmd_plus_hooks(command) 1494 stop = self.cmdapp.onecmd_plus_hooks(command)
1488 #TODO: should act on ``stop`` 1495 #TODO: should act on ``stop``
1489 result = self.outputTrap.read() 1496 result = self.outputTrap.read()
1497 expected = []
1490 if line.startswith(self.cmdapp.prompt): 1498 if line.startswith(self.cmdapp.prompt):
1491 message = '\nFile %s, line %d\nCommand was:\n%s\nExpected: (nothing)\nGot:\n%s\n'%\ 1499 assert_matching()
1492 (fname, lineNum, command, result) 1500 else:
1493 self.assert_(not(result.strip()), message) 1501 while not line.startswith(self.cmdapp.prompt):
1494 continue 1502 expected.append(line)
1495 expected = [] 1503 line = transcript.next()
1496 while not line.startswith(self.cmdapp.prompt): 1504 assert_matching()
1497 expected.append(line)
1498 line = transcript.next()
1499 expected = ''.join(expected)
1500 message = '\nFile %s, line %d\nCommand was:\n%s\nExpected:\n%s\nGot:\n%s\n'%\
1501 (fname, lineNum, command, expected, result)
1502 expected = self.expectationParser.transformString(expected)
1503 expected = self.endStrippingRegex.sub('\s*\n', expected)
1504 self.assert_(re.match(expected, result, re.MULTILINE | re.DOTALL), message)
1505 except StopIteration: 1505 except StopIteration:
1506 pass 1506 assert_matching()
1507 def tearDown(self): 1507 def tearDown(self):
1508 if self.CmdApp: 1508 if self.CmdApp:
1509 self.outputTrap.tearDown() 1509 self.outputTrap.tearDown()
1510 1510
1511 if __name__ == '__main__': 1511 if __name__ == '__main__':