# HG changeset patch # User Catherine Devlin # Date 1238760082 14400 # Node ID 2b69c4d72cd8c57b53242d4b85d086d53e590625 # Parent 5147fa4166b0bb7ed907b697a107370b5c3dec11 unfinished experiments with testing for regular expressions diff -r 5147fa4166b0 -r 2b69c4d72cd8 cmd2.py --- a/cmd2.py Thu Apr 02 18:07:04 2009 -0400 +++ b/cmd2.py Fri Apr 03 08:01:22 2009 -0400 @@ -1107,6 +1107,7 @@ def tearDown(self): sys.stdout = self.old_stdout + class Cmd2TestCase(unittest.TestCase): '''Subclass this, setting CmdApp, to make a unittest.TestCase class that will execute the commands in a transcript file and expect the results shown. @@ -1114,13 +1115,13 @@ CmdApp = None def fetchTranscripts(self): self.transcripts = {} - if not self.CmdApp.testfiles: - raise optparse.OptionError, "No test files named - nothing to test." for fileset in self.CmdApp.testfiles: for fname in glob.glob(fileset): tfile = open(fname) self.transcripts[fname] = iter(tfile.readlines()) tfile.close() + if not len(self.transcripts): + raise StandardError, "No test files found - nothing to test." def setUp(self): if self.CmdApp: @@ -1154,9 +1155,9 @@ line = transcript.next() command = ''.join(command) self.cmdapp.onecmd(command) - result = self.outputTrap.read() + result = self.outputTrap.read().strip() if line.startswith(self.cmdapp.prompt): - self.assertEqualEnough(result.strip(), '', + self.assertEqualEnough(result, '', '\nFile %s, line %d\nCommand was:\n%s\nExpected: (nothing) \nGot:\n%s\n' % (fname, lineNum, command, result)) continue @@ -1164,10 +1165,14 @@ while not line.startswith(self.cmdapp.prompt): expected.append(line) line = transcript.next() - expected = ''.join(expected) - self.assertEqualEnough(expected.strip(), result.strip(), - '\nFile %s, line %d\nCommand was:\n%s\nExpected:\n%s\nGot:\n%s\n' % - (fname, lineNum, command, expected, result)) + expected = ''.join(expected).strip() + message = '\nFile %s, line %d\nCommand was:\n%s\nExpected:\n%s\nGot:\n%s\n'%\ + (fname, lineNum, command, expected, result) + if False and ((expected[0] == '/' == expected[-1]) and not expected.startswith('/*')): + self.assert_(re.match(expected[1:-1], result, re.MULTILINE | re.DOTALL), + message) + else: + self.assertEqualEnough(expected, result, message) # this needs to account for a line-by-line strip()ping except StopIteration: pass