[pypy-svn] rev 841 - in pypy/trunk/src/pypy: interpreter tool

mwh at codespeak.net mwh at codespeak.net
Fri Jun 20 13:41:38 CEST 2003


Author: mwh
Date: Fri Jun 20 13:41:38 2003
New Revision: 841

Modified:
   pypy/trunk/src/pypy/interpreter/unittest_w.py
   pypy/trunk/src/pypy/tool/test.py
Log:
rejig appspace tests in aid of getting better error messages.


Modified: pypy/trunk/src/pypy/interpreter/unittest_w.py
==============================================================================
--- pypy/trunk/src/pypy/interpreter/unittest_w.py	(original)
+++ pypy/trunk/src/pypy/interpreter/unittest_w.py	Fri Jun 20 13:41:38 2003
@@ -18,12 +18,13 @@
     from pypy.interpreter.extmodule import make_builtin_func
     w = space.wrap
     d = space.newdict([])
+    space.setitem(d, w('failureException'), space.w_AssertionError)
+
     for name in dir(AppTestCase):
-        if name.startswith('assert') or name.startswith('fail'):
-            if hasattr(tc_w, 'app_' + name):
-                builtin_func = make_builtin_func(space, getattr(tc_w, "app_" + name),
-                                                 boundmethod=True)
-                space.setitem(d, w(name), builtin_func)
+        if ( name.startswith('assert') or name.startswith('fail')
+             and name != 'failureException'):
+            w_func = wrap_func(space, getattr(tc_w, name).im_func)
+            space.setitem(d, w(name), w_func)
     w_tc = space.call_function(space.w_type,
                                w('TestCase'),
                                space.newtuple([]),
@@ -49,19 +50,7 @@
             setattr(s, w_tc_attr, w_tc)
 
         w_f = wrap_func(s, self.testMethod.im_func)
-        try:
-            s.call_function(w_f, w_tc)
-        except executioncontext.OperationError, oe:
-            oe.print_application_traceback(s)
-            import __builtin__
-            w_res = s.gethelper(pyframe.appfile).call(
-                "normalize_exception", [oe.w_type, oe.w_value])
-            w_value = s.getitem(w_res, s.wrap(1))
-            exc_name = s.getattr(s.getattr(w_value, w('__class__')),
-                                 w('__name__'))
-            exc_type = getattr(__builtin__, s.unwrap(exc_name))
-            # it's a tad annoying we can't fake the traceback
-            raise exc_type(*s.unwrap(s.getattr(w_value, w('args'))))
+        s.call_function(w_f, w_tc)
 
 
 class IntTestCase(unittest.TestCase):
@@ -118,40 +107,7 @@
             setattr(self, self.methodName,
                 WrappedFunc(self, getattr(self, self.methodName)))
         return unittest.TestCase.__call__(self, result)
-
+        
     def setUp(self):
         from pypy.tool import test
         self.space = test.objspace()
-
-    def app_fail(self, w_self, w_msg=None):
-        msg = self.space.unwrap(w_msg)
-        self.fail(msg)
-
-    def app_failIf(self, w_self, w_expr, w_msg=None):
-        msg = self.space.unwrap(w_msg)
-        self.failIf_w(w_expr)
-
-    def app_failUnless(self, w_self, w_expr, w_msg=None):
-        msg = self.space.unwrap(w_msg)
-        self.failUnless_w(w_expr)
-
-    def app_failUnlessRaises(self, w_self, w_exc_class,
-                             w_callable, *args_w, **kw_w):
-        self.assertWRaises_w(w_exc_class, w_callable, *args_w, **kw_w)
-
-    def app_failUnlessEqual(self, w_self, w_first, w_second, w_msg=None):
-        msg = self.space.unwrap(w_msg)
-        self.assertEqual_w(w_first, w_second, msg)
-
-    def app_failIfEqual(self, w_self, w_first, w_second, w_msg=None):
-        msg = self.space.unwrap(w_msg)
-        self.assertNotEqual_w(w_first, w_second, msg)
-
-    app_assertEqual = app_assertEquals = app_failUnlessEqual
-
-    app_assertNotEqual = app_assertNotEquals = app_failIfEqual
-
-    app_assertRaises = app_failUnlessRaises
-
-    app_assert_ = app_failUnless
-                            

Modified: pypy/trunk/src/pypy/tool/test.py
==============================================================================
--- pypy/trunk/src/pypy/tool/test.py	(original)
+++ pypy/trunk/src/pypy/tool/test.py	Fri Jun 20 13:41:38 2003
@@ -1,5 +1,5 @@
 import autopath
-import os, sys, unittest, re, warnings, unittest
+import os, sys, unittest, re, warnings, unittest, traceback
 from unittest import TestCase, TestLoader
 
 import pypy.interpreter.unittest_w
@@ -46,19 +46,61 @@
     def __init__(self):
         unittest.TestResult.__init__(self)
         self.successes = []
+    def addError(self, test, err):
+        # XXX not nice:
+        from pypy.interpreter.baseobjspace import OperationError
+        if isinstance(err[1], OperationError):
+            if err[1].match(test.space, test.space.w_AssertionError):
+                self.addFailure(test, err)
+                return
+        unittest.TestResult.addError(self, test, err)
     def addSuccess(self, test):
         self.successes.append(test)
 
+class MyTextTestResult(unittest._TextTestResult):
+    def addFailure(self, test, err):
+        unittest._TextTestResult.addFailure(self, test, err)
+    def munge(self, list, test, err):
+        import StringIO
+        from pypy.interpreter.baseobjspace import OperationError
+        text1 = list.pop()[1]
+        if isinstance(err[1], OperationError):
+            sio = StringIO.StringIO()
+            err[1].print_application_traceback(test.space, sio)
+            text2 = sio.getvalue()
+            
+            list.append((test, text1 + "\nand at app-level:\n\n" + text2))
+        else:
+            list.append((test, text1))
+        
+    def addError(self, test, err):
+        from pypy.interpreter.baseobjspace import OperationError
+        if isinstance(err[1], OperationError):
+            if err[1].match(test.space, test.space.w_AssertionError):
+                self.addFailure(test, err)
+                return
+        unittest._TextTestResult.addError(self, test, err)
+        self.munge(self.errors, test, err)
+        
+    def addFailure(self, test, err):
+        unittest._TextTestResult.addFailure(self, test, err)
+        self.munge(self.failures, test, err)
+
 class CtsTestRunner:
     def run(self, test):
         import pickle
 
+        output = sys.stdout
         result = MyTestResult()
-        sys.stdout = open('/dev/null', 'w')
-        sys.stderr = open('/dev/null', 'w')
-        test(result)
-        sys.stdout = sys.__stdout__
-        sys.stderr = sys.__stderr__
+        sso = sys.stdout
+        sse = sys.stderr
+        try:
+            sys.stdout = open('/dev/null', 'w')
+            sys.stderr = open('/dev/null', 'w')
+            test(result)
+        finally:
+            sys.stdout = sso
+            sys.stderr = sse
 
         ostatus = {}
         if os.path.exists('testcts.pickle'):
@@ -67,10 +109,12 @@
         status = {}
 
         for e in result.errors:
-            name = e[0].__class__.__name__ + '.' + e[0]._TestCase__testMethodName
+            name = e[0].__class__.__name__ + '.' + \
+                   e[0]._TestCase__testMethodName
             status[name] = 'ERROR'
         for f in result.failures:
-            name = f[0].__class__.__name__ + '.' + f[0]._TestCase__testMethodName
+            name = f[0].__class__.__name__ + '.' + \
+                   f[0]._TestCase__testMethodName
             status[name] = 'FAILURE'
         for s in result.successes:
             name = s.__class__.__name__ + '.' + s._TestCase__testMethodName
@@ -85,18 +129,23 @@
                 del ostatus[k]
             new = status[k]
             if old != new:
-                print k, 'has transitioned from', old, 'to', new
+                print >>output, k, 'has transitioned from', old, 'to', new
             elif new != 'success':
-                print k, "is still a", new
+                print >>output, k, "is still a", new
 
         for k in ostatus:
-            print k, 'was a', ostatus[k], 'was not run this time'
+            print >>output, k, 'was a', ostatus[k], 'was not run this time'
             status[k] = ostatus[k]
 
         pickle.dump(status, open('testcts.pickle','w'))
 
         return result
 
+class MyTextTestRunner(unittest.TextTestRunner):
+    def _makeResult(self):
+        return MyTextTestResult(self.stream, self.descriptions, self.verbosity)
+
+
 def testsuite_from_main():
     """ return test modules from __main__
 
@@ -197,6 +246,7 @@
 %s [-chrvST] [regex1] [regex2] [...]
 
   -c  run CtsTestRunner (catches stdout and prints report after testing)
+      [unix only, for now]
   -h  this help message
   -r  gather only tests relative to current dir
   -v  increase verbosity level (including unittest-verbosity)
@@ -256,7 +306,7 @@
     if Options.runcts:
         runner = CtsTestRunner() # verbosity=Options.verbose+1)
     else:
-        runner = unittest.TextTestRunner(verbosity=Options.verbose+1)
+        runner = MyTextTestRunner(verbosity=Options.verbose+1)
 
     if spacename:
         Options.spacename = spacename


More information about the Pypy-commit mailing list