[Zope-Checkins] CVS: Zope3/lib/python/Zope/ContextWrapper/tests - test_proxy.py:1.1.2.2

Fred L. Drake, Jr. fdrake@acm.org
Fri, 26 Apr 2002 14:52:24 -0400


Update of /cvs-repository/Zope3/lib/python/Zope/ContextWrapper/tests
In directory cvs.zope.org:/tmp/cvs-serv3457/tests

Modified Files:
      Tag: SecurityProxy-branch
	test_proxy.py 
Log Message:
Port the math ops from Security.Proxy to the base proxy.

=== Zope3/lib/python/Zope/ContextWrapper/tests/test_proxy.py 1.1.2.1 => 1.1.2.2 ===
 
 class ProxyTestCase(unittest.TestCase):
+    def setUp(self):
+        self.x = Thing()
+        self.p = self.new_proxy(self.x)
+
     def new_proxy(self, o):
         return proxy.proxy(o)
 
@@ -115,6 +119,123 @@
     def test_bool_wrapped_None(self):
         w = self.new_proxy(None)
         self.assertEquals(not w, 1)
+
+    # Numeric ops.
+
+    unops = [
+        "-x", "+x", "abs(x)", "~x",
+        "int(x)", "long(x)", "float(x)",
+        ]
+
+    def test_unops(self):
+        P = self.new_proxy
+        for expr in self.unops:
+            x = 1
+            y = eval(expr)
+            x = P(1)
+            z = eval(expr)
+            self.assertEqual(z, y,
+                             "x=%r; expr=%r" % (x, expr))
+
+    def test_odd_unops(self):
+        # unops that don't return a proxy
+        P = self.new_proxy
+        for func in hex, oct, lambda x: not x:
+            self.assertEqual(func(P(100)), func(100))
+
+    binops = [
+        "x+y", "x-y", "x*y", "x/y", "divmod(x, y)", "x**y", "x//y",
+        "x<<y", "x>>y", "x&y", "x|y", "x^y",
+        ]
+
+    def test_binops(self):
+        P = self.new_proxy
+        for expr in self.binops:
+            first = 1
+            for x in [1, P(1)]:
+                for y in [2, P(2)]:
+                    if first:
+                        z = eval(expr)
+                        first = 0
+                    else:
+                        self.assertEqual(eval(expr), z,
+                                         "x=%r; y=%r; expr=%r" % (x, y, expr))
+
+    def test_inplace(self):
+        # XXX should test all inplace operators...
+        P = self.new_proxy
+
+        pa = P(1)
+        pa += 2
+        self.assertEqual(pa, 3)
+
+        a = [1, 2, 3]
+        pa = qa = P(a)
+        pa += [4, 5, 6]
+        self.failUnless(pa is qa)
+        self.assertEqual(a, [1, 2, 3, 4, 5, 6])
+
+        pa = P(2)
+        pa **= 2
+        self.assertEqual(pa, 4)
+
+    def test_coerce(self):
+        P = self.new_proxy
+
+        x = P(1)
+        y = P(2)
+        a, b = coerce(x, y)
+        self.failUnless(a is x and b is y)
+
+        x = P(1)
+        y = P(2.1)
+        a, b = coerce(x, y)
+        # XXX This fails unless your Python is the latest from CVS!!!
+        # XXX (PyNumber_Coerce[Ex] needs a change)
+        self.failUnless(a.__class__ is float, a.__class__)
+        self.failUnless(b is y)
+
+        x = P(1.1)
+        y = P(2)
+        a, b = coerce(x, y)
+        self.failUnless(a is x)
+        self.failUnless(b.__class__ is float, b.__class__)
+
+        x = P(1)
+        y = 2
+        a, b = coerce(x, y)
+        self.failUnless(a is x)
+        self.failUnless(b is y)
+
+        x = P(1)
+        y = 2.1
+        a, b = coerce(x, y)
+        self.failUnless(a.__class__ is float, a.__class__)
+        self.failUnless(b is y)
+
+        x = P(1.1)
+        y = 2
+        a, b = coerce(x, y)
+        self.failUnless(a is x)
+        self.failUnless(b.__class__ is float, b.__class__)
+
+        x = 1
+        y = P(2)
+        a, b = coerce(x, y)
+        self.failUnless(a is x)
+        self.failUnless(b is y)
+
+        x = 1.1
+        y = P(2)
+        a, b = coerce(x, y)
+        self.failUnless(a is x)
+        self.failUnless(b.__class__ is float, b.__class__)
+
+        x = 1
+        y = P(2.1)
+        a, b = coerce(x, y)
+        self.failUnless(a.__class__ is float, a.__class__)
+        self.failUnless(b is y)
 
 
 def test_suite():