Mercurial > python-cmd2
comparison cmd2.py @ 260:2b69c4d72cd8
unfinished experiments with testing for regular expressions
author | Catherine Devlin <catherine.devlin@gmail.com> |
---|---|
date | Fri, 03 Apr 2009 08:01:22 -0400 |
parents | 5147fa4166b0 |
children | 57070e181cf7 |
comparison
equal
deleted
inserted
replaced
259:5147fa4166b0 | 260:2b69c4d72cd8 |
---|---|
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 class Cmd2TestCase(unittest.TestCase): | 1111 class Cmd2TestCase(unittest.TestCase): |
1111 '''Subclass this, setting CmdApp, to make a unittest.TestCase class | 1112 '''Subclass this, setting CmdApp, to make a unittest.TestCase class |
1112 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. |
1113 See example.py''' | 1114 See example.py''' |
1114 CmdApp = None | 1115 CmdApp = None |
1115 def fetchTranscripts(self): | 1116 def fetchTranscripts(self): |
1116 self.transcripts = {} | 1117 self.transcripts = {} |
1117 if not self.CmdApp.testfiles: | |
1118 raise optparse.OptionError, "No test files named - nothing to test." | |
1119 for fileset in self.CmdApp.testfiles: | 1118 for fileset in self.CmdApp.testfiles: |
1120 for fname in glob.glob(fileset): | 1119 for fname in glob.glob(fileset): |
1121 tfile = open(fname) | 1120 tfile = open(fname) |
1122 self.transcripts[fname] = iter(tfile.readlines()) | 1121 self.transcripts[fname] = iter(tfile.readlines()) |
1123 tfile.close() | 1122 tfile.close() |
1123 if not len(self.transcripts): | |
1124 raise StandardError, "No test files found - nothing to test." | |
1124 | 1125 |
1125 def setUp(self): | 1126 def setUp(self): |
1126 if self.CmdApp: | 1127 if self.CmdApp: |
1127 self.outputTrap = OutputTrap() | 1128 self.outputTrap = OutputTrap() |
1128 self.cmdapp = self.CmdApp() | 1129 self.cmdapp = self.CmdApp() |
1152 while line.startswith(self.cmdapp.continuation_prompt): | 1153 while line.startswith(self.cmdapp.continuation_prompt): |
1153 command.append(line[len(self.cmdapp.continuation_prompt):]) | 1154 command.append(line[len(self.cmdapp.continuation_prompt):]) |
1154 line = transcript.next() | 1155 line = transcript.next() |
1155 command = ''.join(command) | 1156 command = ''.join(command) |
1156 self.cmdapp.onecmd(command) | 1157 self.cmdapp.onecmd(command) |
1157 result = self.outputTrap.read() | 1158 result = self.outputTrap.read().strip() |
1158 if line.startswith(self.cmdapp.prompt): | 1159 if line.startswith(self.cmdapp.prompt): |
1159 self.assertEqualEnough(result.strip(), '', | 1160 self.assertEqualEnough(result, '', |
1160 '\nFile %s, line %d\nCommand was:\n%s\nExpected: (nothing) \nGot:\n%s\n' % | 1161 '\nFile %s, line %d\nCommand was:\n%s\nExpected: (nothing) \nGot:\n%s\n' % |
1161 (fname, lineNum, command, result)) | 1162 (fname, lineNum, command, result)) |
1162 continue | 1163 continue |
1163 expected = [] | 1164 expected = [] |
1164 while not line.startswith(self.cmdapp.prompt): | 1165 while not line.startswith(self.cmdapp.prompt): |
1165 expected.append(line) | 1166 expected.append(line) |
1166 line = transcript.next() | 1167 line = transcript.next() |
1167 expected = ''.join(expected) | 1168 expected = ''.join(expected).strip() |
1168 self.assertEqualEnough(expected.strip(), result.strip(), | 1169 message = '\nFile %s, line %d\nCommand was:\n%s\nExpected:\n%s\nGot:\n%s\n'%\ |
1169 '\nFile %s, line %d\nCommand was:\n%s\nExpected:\n%s\nGot:\n%s\n' % | 1170 (fname, lineNum, command, expected, result) |
1170 (fname, lineNum, command, expected, result)) | 1171 if False and ((expected[0] == '/' == expected[-1]) and not expected.startswith('/*')): |
1172 self.assert_(re.match(expected[1:-1], result, re.MULTILINE | re.DOTALL), | |
1173 message) | |
1174 else: | |
1175 self.assertEqualEnough(expected, result, message) | |
1171 # this needs to account for a line-by-line strip()ping | 1176 # this needs to account for a line-by-line strip()ping |
1172 except StopIteration: | 1177 except StopIteration: |
1173 pass | 1178 pass |
1174 def tearDown(self): | 1179 def tearDown(self): |
1175 if self.CmdApp: | 1180 if self.CmdApp: |