[Zope-CVS] CVS: Packages/ContextWrapper - test_wrapper.py:1.2

Fred Drake Jr fdrake@acm.org
Mon, 12 Nov 2001 23:32:17 -0500


Update of /cvs-repository/Packages/ContextWrapper
In directory cvs.zope.org:/tmp/cvs-serv26904

Modified Files:
	test_wrapper.py 
Log Message:
Migrate to PyUnit, add the Zopish test harness.
For both callable & non-callable objects, test the results of both the
generic factory and the specific wrapper types.


=== Packages/ContextWrapper/test_wrapper.py 1.1 => 1.2 ===
 import distutils.util
+import sys
+import unittest
+
 sys.path.append('build/lib.%s-%s'
                 % (distutils.util.get_platform(), sys.version[:3]))
 
 import wrapper
 
-def testit(o, o2, type):
-    w = type(o)
-    print w
-    print wrapper.getobject(w)
-    print "match:", o is wrapper.getobject(w)
-    print wrapper.getdict(w)
-    d = wrapper.getdictcreate(w)
-    print d
-    print wrapper.getdictcreate(w) is d
-    print "context:", wrapper.getcontext(w)
-    c = 'context'
-    wrapper.setcontext(w, c)
-    print `wrapper.getcontext(w)`
-    print wrapper.getcontext(w) is c
-    wrapper.setcontext(w, None)
-    print `wrapper.getcontext(w)`
-    print wrapper.getcontext(w) is None
-
-    wrapper.setobject(w, o2)
-    print wrapper.getobject(w)
-
-
-testit(1, 12, wrapper.WrapperType)
-testit(2, 24, wrapper.new)
-testit(lambda: 3, lambda: 36, wrapper.CallableWrapperType)
+
+class WrapperTestCase(unittest.TestCase):
+    def check_wrapper_basics(self, o1, o2, factory):
+        w = factory(o1)
+        self.assert_(o1 is wrapper.getobject(w))
+        self.assert_(wrapper.getdict(w) is None)
+        d = wrapper.getdictcreate(w)
+        self.assert_(wrapper.getdictcreate(w) is d)
+
+        c = 'context'
+        wrapper.setcontext(w, c)
+        self.assert_(wrapper.getcontext(w) is c)
+        wrapper.setcontext(w, None)
+        self.assert_(wrapper.getcontext(w) is None)
+
+        wrapper.setobject(w, o2)
+        self.assert_(wrapper.getobject(w) is o2)
+
+    def test_basic_wrapper(self):
+        self.check_wrapper_basics(1, 12,
+                                  wrapper.WrapperType)
+        self.check_wrapper_basics(1, 12,
+                                  wrapper.new)
+        w = wrapper.new(1)
+        self.assert_(not callable(w))
+
+    def test_callable_wrapper(self):
+        self.check_wrapper_basics(lambda:3, lambda:36,
+                                  wrapper.CallableWrapperType)
+        self.check_wrapper_basics(lambda:3, lambda:36,
+                                  wrapper.new)
+        w = wrapper.new(lambda:3)
+        # not yet implemented
+        #self.assert_(callable(w))
+
+
+def test_suite():
+    suite = unittest.TestSuite()
+    suite.addTest(unittest.makeSuite(WrapperTestCase))
+    return suite
+
+
+if __name__ == "__main__":
+    runner = unittest.TextTestRunner(sys.stdout)
+    result = runner.run(test_suite())
+    newerrs = len(result.errors) + len(result.failures)
+    sys.exit(newerrs and 1 or 0)