comparison cmd2.py @ 262:e81378f82c7c 0.5.2

transcript tests with regex now work smoothly
author Catherine Devlin <catherine.devlin@gmail.com>
date Fri, 03 Apr 2009 13:02:36 -0400
parents 57070e181cf7
children ba45c2df855b
comparison
equal deleted inserted replaced
261:57070e181cf7 262:e81378f82c7c
1120 tfile = open(fname) 1120 tfile = open(fname)
1121 self.transcripts[fname] = iter(tfile.readlines()) 1121 self.transcripts[fname] = iter(tfile.readlines())
1122 tfile.close() 1122 tfile.close()
1123 if not len(self.transcripts): 1123 if not len(self.transcripts):
1124 raise StandardError, "No test files found - nothing to test." 1124 raise StandardError, "No test files found - nothing to test."
1125
1126 def setUp(self): 1125 def setUp(self):
1127 if self.CmdApp: 1126 if self.CmdApp:
1128 self.outputTrap = OutputTrap() 1127 self.outputTrap = OutputTrap()
1129 self.cmdapp = self.CmdApp() 1128 self.cmdapp = self.CmdApp()
1130 self.fetchTranscripts() 1129 self.fetchTranscripts()
1131 def assertEqualEnough(self, got, expected, message):
1132 got = got.strip().splitlines()
1133 expected = expected.strip().splitlines()
1134 self.assertEqual(len(got), len(expected), message)
1135 for (linegot, lineexpected) in zip(got, expected):
1136 matchme = re.escape(lineexpected.strip()).replace('\\*', '.*'). \
1137 replace('\\ ', ' ')
1138 self.assert_(re.match(matchme, linegot.strip()), message)
1139 def testall(self): 1130 def testall(self):
1140 if self.CmdApp: 1131 if self.CmdApp:
1141 its = sorted(self.transcripts.items()) 1132 its = sorted(self.transcripts.items())
1142 for (fname, transcript) in its: 1133 for (fname, transcript) in its:
1143 self._test_transcript(fname, transcript) 1134 self._test_transcript(fname, transcript)
1161 line = transcript.next() 1152 line = transcript.next()
1162 command = ''.join(command) 1153 command = ''.join(command)
1163 self.cmdapp.onecmd(command) 1154 self.cmdapp.onecmd(command)
1164 result = self.outputTrap.read().strip() 1155 result = self.outputTrap.read().strip()
1165 if line.startswith(self.cmdapp.prompt): 1156 if line.startswith(self.cmdapp.prompt):
1166 self.assertEqualEnough(result, '', 1157 message = '\nFile %s, line %d\nCommand was:\n%s\nExpected: (nothing)\nGot:\n%s\n'%\
1167 '\nFile %s, line %d\nCommand was:\n%s\nExpected: (nothing) \nGot:\n%s\n' % 1158 (fname, lineNum, command, result)
1168 (fname, lineNum, command, result)) 1159 self.assert_(not(result.strip()), message)
1169 continue 1160 continue
1170 expected = [] 1161 expected = []
1171 while not line.startswith(self.cmdapp.prompt): 1162 while not line.startswith(self.cmdapp.prompt):
1172 expected.append(line) 1163 expected.append(line)
1173 line = transcript.next() 1164 line = transcript.next()
1175 message = '\nFile %s, line %d\nCommand was:\n%s\nExpected:\n%s\nGot:\n%s\n'%\ 1166 message = '\nFile %s, line %d\nCommand was:\n%s\nExpected:\n%s\nGot:\n%s\n'%\
1176 (fname, lineNum, command, expected, result) 1167 (fname, lineNum, command, expected, result)
1177 expected = self.expectationParser.transformString(expected) 1168 expected = self.expectationParser.transformString(expected)
1178 expected = self.endStrippingRegex.sub('\s*\n', expected) 1169 expected = self.endStrippingRegex.sub('\s*\n', expected)
1179 self.assert_(re.match(expected, result, re.MULTILINE | re.DOTALL), message) 1170 self.assert_(re.match(expected, result, re.MULTILINE | re.DOTALL), message)
1180 # this needs to account for a line-by-line strip()ping
1181 except StopIteration: 1171 except StopIteration:
1182 pass 1172 pass
1183 def tearDown(self): 1173 def tearDown(self):
1184 if self.CmdApp: 1174 if self.CmdApp:
1185 self.outputTrap.tearDown() 1175 self.outputTrap.tearDown()