comparison cmd2.py @ 253:24289178b367

midway through allowing multiple testing files
author Catherine Devlin <catherine.devlin@gmail.com>
date Tue, 31 Mar 2009 17:47:04 -0400
parents bc6dec08275f
children 07dff0ba981e
comparison
equal deleted inserted replaced
252:b1b37ea258af 253:24289178b367
24 CHANGES: 24 CHANGES:
25 As of 0.3.0, options should be specified as `optparse` options. See README.txt. 25 As of 0.3.0, options should be specified as `optparse` options. See README.txt.
26 flagReader.py options are still supported for backward compatibility 26 flagReader.py options are still supported for backward compatibility
27 """ 27 """
28 import cmd, re, os, sys, optparse, subprocess, tempfile, pyparsing, doctest 28 import cmd, re, os, sys, optparse, subprocess, tempfile, pyparsing, doctest
29 import unittest, string, datetime, urllib 29 import unittest, string, datetime, urllib, glob
30 from code import InteractiveConsole, InteractiveInterpreter, softspace 30 from code import InteractiveConsole, InteractiveInterpreter, softspace
31 from optparse import make_option 31 from optparse import make_option
32 __version__ = '0.5.1' 32 __version__ = '0.5.1'
33 33
34 class OptionParser(optparse.OptionParser): 34 class OptionParser(optparse.OptionParser):
1111 '''Subclass this, setting CmdApp and transcriptFileName, to make a unittest.TestCase class 1111 '''Subclass this, setting CmdApp and transcriptFileName, to make a unittest.TestCase class
1112 that will execute the commands in transcriptFileName and expect the results shown. 1112 that will execute the commands in transcriptFileName and expect the results shown.
1113 See example.py''' 1113 See example.py'''
1114 CmdApp = None 1114 CmdApp = None
1115 transcriptFileName = '' 1115 transcriptFileName = ''
1116 transcriptExtension = ''
1117 def fetchTranscripts(self):
1118 self.transcripts = {}
1119 if self.transcriptExtension:
1120 for fname in glob.glob('*.' + self.transcriptExtension):
1121 tfile = open(fname)
1122 self.transcripts[fname] = iter(tfile.readlines())
1123 tfile.close()
1124 elif self.transcriptFileName:
1125 tfile = open(os.path.expanduser(self.transcriptFileName))
1126 self.transcripts = {'transcript': iter(tfile.readlines())}
1127 tfile.close()
1128 else:
1129 raise AttributeError, 'Cmd2TestCase must define transcriptFileName or transcriptExtension'
1130
1116 def setUp(self): 1131 def setUp(self):
1117 if self.CmdApp: 1132 if self.CmdApp:
1118 self.outputTrap = OutputTrap() 1133 self.outputTrap = OutputTrap()
1119 self.cmdapp = self.CmdApp() 1134 self.cmdapp = self.CmdApp()
1120 try: 1135 self.fetchTranscripts()
1121 tfile = open(os.path.expanduser(self.transcriptFileName))
1122 self.transcript = iter(tfile.readlines())
1123 tfile.close()
1124 except IOError:
1125 self.transcript = []
1126 def assertEqualEnough(self, got, expected, message): 1136 def assertEqualEnough(self, got, expected, message):
1127 got = got.strip().splitlines() 1137 got = got.strip().splitlines()
1128 expected = expected.strip().splitlines() 1138 expected = expected.strip().splitlines()
1129 self.assertEqual(len(got), len(expected), message) 1139 self.assertEqual(len(got), len(expected), message)
1130 for (linegot, lineexpected) in zip(got, expected): 1140 for (linegot, lineexpected) in zip(got, expected):
1131 matchme = re.escape(lineexpected.strip()).replace('\\*', '.*'). \ 1141 matchme = re.escape(lineexpected.strip()).replace('\\*', '.*'). \
1132 replace('\\ ', ' ') 1142 replace('\\ ', ' ')
1133 self.assert_(re.match(matchme, linegot.strip()), message) 1143 self.assert_(re.match(matchme, linegot.strip()), message)
1134 def testall(self): 1144 def testall(self):
1135 if self.CmdApp: 1145 if self.CmdApp:
1136 lineNum = 0 1146 setup = 'setup.' + self.transcriptExtension
1137 try: 1147 teardown = 'teardown.' + self.transcriptExtension
1138 line = self.transcript.next() 1148 transcripts = self.transcripts.copy()
1139 while True: 1149 transcriptfiles = []
1140 while not line.startswith(self.cmdapp.prompt): 1150 if setup in transcripts:
1141 line = self.transcript.next() 1151 transcriptfiles.append((setup, transcripts.pop(setup)))
1142 command = [line[len(self.cmdapp.prompt):]] 1152 transcriptfiles.extend(sorted((fname, transcripts[fname]) for fname in transcripts if fname != teardown))
1143 line = self.transcript.next() 1153 if teardown in transcripts:
1144 while line.startswith(self.cmdapp.continuation_prompt): 1154 transcriptfiles.append((teardown, transcripts[teardown]))
1145 command.append(line[len(self.cmdapp.continuation_prompt):]) 1155 for transcript in transcriptfiles:
1146 line = self.transcript.next() 1156 self.testone(transcript)
1147 command = ''.join(command) 1157 def testone(self, transcript):
1148 self.cmdapp.onecmd(command) 1158 lineNum = 0
1149 result = self.outputTrap.read() 1159 try:
1150 if line.startswith(self.cmdapp.prompt): 1160 line = transcript.next()
1151 self.assertEqualEnough(result.strip(), '', 1161 while True:
1152 '\nFile %s, line %d\nCommand was:\n%s\nExpected: (nothing) \nGot:\n%s\n' % 1162 while not line.startswith(self.cmdapp.prompt):
1153 (self.transcriptFileName, lineNum, command, result)) 1163 line = transcript.next()
1154 continue 1164 command = [line[len(self.cmdapp.prompt):]]
1155 expected = [] 1165 line = transcript.next()
1156 while not line.startswith(self.cmdapp.prompt): 1166 while line.startswith(self.cmdapp.continuation_prompt):
1157 expected.append(line) 1167 command.append(line[len(self.cmdapp.continuation_prompt):])
1158 line = self.transcript.next() 1168 line = transcript.next()
1159 expected = ''.join(expected) 1169 command = ''.join(command)
1160 self.assertEqualEnough(expected.strip(), result.strip(), 1170 self.cmdapp.onecmd(command)
1161 '\nFile %s, line %d\nCommand was:\n%s\nExpected:\n%s\nGot:\n%s\n' % 1171 result = self.outputTrap.read()
1162 (self.transcriptFileName, lineNum, command, expected, result)) 1172 if line.startswith(self.cmdapp.prompt):
1163 # this needs to account for a line-by-line strip()ping 1173 self.assertEqualEnough(result.strip(), '',
1164 except StopIteration: 1174 '\nFile %s, line %d\nCommand was:\n%s\nExpected: (nothing) \nGot:\n%s\n' %
1165 pass 1175 (self.transcriptFileName, lineNum, command, result))
1166 # catch the final output? 1176 continue
1177 expected = []
1178 while not line.startswith(self.cmdapp.prompt):
1179 expected.append(line)
1180 line = transcript.next()
1181 expected = ''.join(expected)
1182 self.assertEqualEnough(expected.strip(), result.strip(),
1183 '\nFile %s, line %d\nCommand was:\n%s\nExpected:\n%s\nGot:\n%s\n' %
1184 (self.transcriptFileName, lineNum, command, expected, result))
1185 # this needs to account for a line-by-line strip()ping
1186 except StopIteration:
1187 pass
1167 def tearDown(self): 1188 def tearDown(self):
1168 if self.CmdApp: 1189 if self.CmdApp:
1169 self.outputTrap.tearDown() 1190 self.outputTrap.tearDown()
1170 1191
1171 if __name__ == '__main__': 1192 if __name__ == '__main__':