diff 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
line wrap: on
line diff
--- 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