comparison cmd2.py @ 410:48ebbdfe9dd4

broke down StopIteration catches
author Catherine Devlin <catherine.devlin@gmail.com>
date Mon, 08 Nov 2010 08:49:32 -0500
parents 130889170679
children 9d5ff2ddfdea
comparison
equal deleted inserted replaced
409:130889170679 410:48ebbdfe9dd4
1478 notRegexPattern.setParseAction(lambda t: re.escape(t[0])) 1478 notRegexPattern.setParseAction(lambda t: re.escape(t[0]))
1479 expectationParser = regexPattern | notRegexPattern 1479 expectationParser = regexPattern | notRegexPattern
1480 anyWhitespace = re.compile(r'\s', re.DOTALL | re.MULTILINE) 1480 anyWhitespace = re.compile(r'\s', re.DOTALL | re.MULTILINE)
1481 def _test_transcript(self, fname, transcript): 1481 def _test_transcript(self, fname, transcript):
1482 lineNum = 0 1482 lineNum = 0
1483 try: 1483 finished = False
1484 line = transcript.next()
1485 lineNum += 1
1486 tests_run = 0
1487 while not finished:
1488 # Scroll forward to where actual commands begin
1489 while not line.startswith(self.cmdapp.prompt):
1490 try:
1491 line = transcript.next()
1492 except StopIteration:
1493 finished = True
1494 break
1495 lineNum += 1
1496 command = [line[len(self.cmdapp.prompt):]]
1484 line = transcript.next() 1497 line = transcript.next()
1485 lineNum += 1 1498 # Read the entirety of a multi-line command
1486 while True: 1499 while line.startswith(self.cmdapp.continuation_prompt):
1487 # Scroll forward to where actual commands begin 1500 command.append(line[len(self.cmdapp.continuation_prompt):])
1488 while not line.startswith(self.cmdapp.prompt): 1501 try:
1489 line = transcript.next() 1502 line = transcript.next()
1490 lineNum += 1 1503 except StopIteration:
1491 command = [line[len(self.cmdapp.prompt):]] 1504 raise (StopIteration,
1492 line = transcript.next() 1505 'Transcript broke off while reading command beginning at line %d with\n%s'
1493 # Read the entirety of a multi-line command 1506 % (command[0]))
1494 while line.startswith(self.cmdapp.continuation_prompt): 1507 lineNum += 1
1495 command.append(line[len(self.cmdapp.continuation_prompt):]) 1508 command = ''.join(command)
1509 # Send the command into the application and capture the resulting output
1510 stop = self.cmdapp.onecmd_plus_hooks(command)
1511 #TODO: should act on ``stop``
1512 result = self.outputTrap.read()
1513 # Read the expected result from transcript
1514 if line.startswith(self.cmdapp.prompt):
1515 message = '\nFile %s, line %d\nCommand was:\n%s\nExpected: (nothing)\nGot:\n%s\n'%\
1516 (fname, lineNum, command, result)
1517 self.assert_(not(result.strip()), message)
1518 continue
1519 expected = []
1520 while not line.startswith(self.cmdapp.prompt):
1521 expected.append(line)
1522 try:
1496 line = transcript.next() 1523 line = transcript.next()
1497 lineNum += 1 1524 except StopIteration:
1498 command = ''.join(command) 1525 finished = True
1499 # Send the command into the application and capture the resulting output 1526 break
1500 stop = self.cmdapp.onecmd_plus_hooks(command) 1527 lineNum += 1
1501 #TODO: should act on ``stop`` 1528 expected = ''.join(expected)
1502 result = self.outputTrap.read() 1529 # Compare actual result to expected
1503 # Read the expected result from transcript 1530 message = '\nFile %s, line %d\nCommand was:\n%s\nExpected:\n%s\nGot:\n%s\n'%\
1504 if line.startswith(self.cmdapp.prompt): 1531 (fname, lineNum, command, expected, result)
1505 message = '\nFile %s, line %d\nCommand was:\n%s\nExpected: (nothing)\nGot:\n%s\n'%\ 1532 expected = self.expectationParser.transformString(expected)
1506 (fname, lineNum, command, result) 1533 # checking whitespace is a pain - let's skip it
1507 self.assert_(not(result.strip()), message) 1534 expected = self.anyWhitespace.sub('', expected)
1508 continue 1535 result = self.anyWhitespace.sub('', result)
1509 expected = [] 1536 self.assert_(re.match(expected, result, re.MULTILINE | re.DOTALL), message)
1510 while not line.startswith(self.cmdapp.prompt): 1537
1511 expected.append(line)
1512 line = transcript.next()
1513 lineNum += 1
1514 expected = ''.join(expected)
1515 # Compare actual result to expected
1516 message = '\nFile %s, line %d\nCommand was:\n%s\nExpected:\n%s\nGot:\n%s\n'%\
1517 (fname, lineNum, command, expected, result)
1518 expected = self.expectationParser.transformString(expected)
1519 # checking whitespace is a pain - let's skip it
1520 expected = self.anyWhitespace.sub('', expected)
1521 result = self.anyWhitespace.sub('', result)
1522 self.assert_(re.match(expected, result, re.MULTILINE | re.DOTALL), message)
1523 except StopIteration:
1524 message = 'Final portion of test not returned, beginning at line %d' % (lineNum)
1525 self.assert_(len(expected) < 3, message)
1526 def tearDown(self): 1538 def tearDown(self):
1527 if self.CmdApp: 1539 if self.CmdApp:
1528 self.outputTrap.tearDown() 1540 self.outputTrap.tearDown()
1529 1541
1530 if __name__ == '__main__': 1542 if __name__ == '__main__':