changeset 181:24eff658997b

accepts wildcards in tests, maybe?
author catherine@Elli.myhome.westell.com
date Wed, 24 Dec 2008 16:03:46 -0500
parents 951c124b0404
children 1c21db096f49
files cmd2.py
diffstat 1 files changed, 10 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/cmd2.py	Wed Dec 24 11:15:03 2008 -0500
+++ b/cmd2.py	Wed Dec 24 16:03:46 2008 -0500
@@ -890,14 +890,14 @@
                 tfile.close()
             except IOError:
                 self.transcript = []
-    def assertEqualWithWildcards(self, got, expected, message):
-        got = got.strip()
-        expected = expected.strip()
-        try:
-            self.assertEqual(got, expected, message)
-        except AssertionError:
-            print 'ooh, this is bad'
-            raise
+    def assertEqualEnough(self, got, expected, message):
+        got = got.strip().splitlines()
+        expected = expected.strip().splitlines()
+        self.assertEqual(len(got), len(expected), message)
+        for (linegot, lineexpected) in zip(got, expected):
+            matchme = re.escape(lineexpected.strip()).replace('\\*', '.*').  \
+                      replace('\\ ', ' ')
+            self.assert_(re.match(matchme, linegot.strip()), message)
     def testall(self):
         if self.CmdApp:
             lineNum = 0
@@ -915,7 +915,7 @@
                     self.cmdapp.onecmd(command)
                     result = self.outputTrap.read()
                     if line.startswith(self.cmdapp.prompt):
-                        self.assertEqual(result.strip(), '', 
+                        self.assertEqualEnough(result.strip(), '', 
                             '\nFile %s, line %d\nCommand was:\n%s\nExpected: (nothing) \nGot:\n%s\n' % 
                             (self.transcriptFileName, lineNum, command, result))    
                         continue
@@ -924,7 +924,7 @@
                         expected.append(line)
                         line = self.transcript.next()
                     expected = ''.join(expected)
-                    self.assertEqual(expected.strip(), result.strip(), 
+                    self.assertEqualEnough(expected.strip(), result.strip(), 
                         '\nFile %s, line %d\nCommand was:\n%s\nExpected:\n%s\nGot:\n%s\n' % 
                         (self.transcriptFileName, lineNum, command, expected, result))    
                     # this needs to account for a line-by-line strip()ping
@@ -933,7 +933,6 @@
                 # catch the final output?
     def tearDown(self):
         if self.CmdApp:
-            self.tfile.close()
             self.outputTrap.tearDown()
         
 if __name__ == '__main__':