comparison cmd2.py @ 261:57070e181cf7

line end-stripping working in transcript testing
author Catherine Devlin <catherine.devlin@gmail.com>
date Fri, 03 Apr 2009 12:47:13 -0400
parents 2b69c4d72cd8
children e81378f82c7c
comparison
equal deleted inserted replaced
260:2b69c4d72cd8 261:57070e181cf7
1105 self.trap.truncate(0) 1105 self.trap.truncate(0)
1106 return result.strip('\x00') 1106 return result.strip('\x00')
1107 def tearDown(self): 1107 def tearDown(self):
1108 sys.stdout = self.old_stdout 1108 sys.stdout = self.old_stdout
1109 1109
1110 1110
1111 class Cmd2TestCase(unittest.TestCase): 1111 class Cmd2TestCase(unittest.TestCase):
1112 '''Subclass this, setting CmdApp, to make a unittest.TestCase class 1112 '''Subclass this, setting CmdApp, to make a unittest.TestCase class
1113 that will execute the commands in a transcript file and expect the results shown. 1113 that will execute the commands in a transcript file and expect the results shown.
1114 See example.py''' 1114 See example.py'''
1115 CmdApp = None 1115 CmdApp = None
1139 def testall(self): 1139 def testall(self):
1140 if self.CmdApp: 1140 if self.CmdApp:
1141 its = sorted(self.transcripts.items()) 1141 its = sorted(self.transcripts.items())
1142 for (fname, transcript) in its: 1142 for (fname, transcript) in its:
1143 self._test_transcript(fname, transcript) 1143 self._test_transcript(fname, transcript)
1144 regexPattern = pyparsing.QuotedString(quoteChar=r'/', escChar='\\', multiline=True, unquoteResults=True)
1145 regexPattern.ignore(pyparsing.cStyleComment)
1146 notRegexPattern = pyparsing.Word(pyparsing.printables)
1147 notRegexPattern.setParseAction(lambda t: re.escape(t[0]))
1148 expectationParser = regexPattern | notRegexPattern
1149 endStrippingRegex = re.compile(r'[ \t]*\n')
1144 def _test_transcript(self, fname, transcript): 1150 def _test_transcript(self, fname, transcript):
1145 lineNum = 0 1151 lineNum = 0
1146 try: 1152 try:
1147 line = transcript.next() 1153 line = transcript.next()
1148 while True: 1154 while True:
1165 while not line.startswith(self.cmdapp.prompt): 1171 while not line.startswith(self.cmdapp.prompt):
1166 expected.append(line) 1172 expected.append(line)
1167 line = transcript.next() 1173 line = transcript.next()
1168 expected = ''.join(expected).strip() 1174 expected = ''.join(expected).strip()
1169 message = '\nFile %s, line %d\nCommand was:\n%s\nExpected:\n%s\nGot:\n%s\n'%\ 1175 message = '\nFile %s, line %d\nCommand was:\n%s\nExpected:\n%s\nGot:\n%s\n'%\
1170 (fname, lineNum, command, expected, result) 1176 (fname, lineNum, command, expected, result)
1171 if False and ((expected[0] == '/' == expected[-1]) and not expected.startswith('/*')): 1177 expected = self.expectationParser.transformString(expected)
1172 self.assert_(re.match(expected[1:-1], result, re.MULTILINE | re.DOTALL), 1178 expected = self.endStrippingRegex.sub('\s*\n', expected)
1173 message) 1179 self.assert_(re.match(expected, result, re.MULTILINE | re.DOTALL), message)
1174 else:
1175 self.assertEqualEnough(expected, result, message)
1176 # this needs to account for a line-by-line strip()ping 1180 # this needs to account for a line-by-line strip()ping
1177 except StopIteration: 1181 except StopIteration:
1178 pass 1182 pass
1179 def tearDown(self): 1183 def tearDown(self):
1180 if self.CmdApp: 1184 if self.CmdApp: