[Zope-Checkins] CVS: Zope3/lib/python/Zope/Security/tests - test_Proxy.py:1.1.2.15

Guido van Rossum guido@python.org
Wed, 24 Apr 2002 17:28:10 -0400


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

Modified Files:
      Tag: SecurityProxy-branch
	test_Proxy.py 
Log Message:
Add tests for binary and unary functions.

Add __nonzero__ to class Something.


=== Zope3/lib/python/Zope/Security/tests/test_Proxy.py 1.1.2.14 => 1.1.2.15 ===
     def __len__(self):
         return 42
+    def __nonzero__(self):
+        return 1
     def __getslice__(self, i, j):
         return [42]
     def __setslice__(self, i, j, value):
@@ -51,6 +53,7 @@
     def __contains__(self, x):
         return x == 42
 
+
 class ProxyTests(unittest.TestCase):
 
     def setUp(self):
@@ -182,6 +185,54 @@
         d = {C: C()}
         pC = ProxyFactory(C, self.c)
         self.assertEqual(d[pC], d[C])
+
+    binops = [
+        "x+y", "x-y", "x*y", "x/y", "divmod(x, y)",
+        # "x**y",
+        "x<<y", "x>>y", "x&y", "x|y", "x^y",
+        ]
+
+    def test_binops(self):
+        P = self.c.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.c.ok = 1
+                        self.assertEqual(getObject(eval(expr)), z,
+                                         "x=%r; y=%r; expr=%r" % (x, y, expr))
+                        self.c.ok = 0
+                        self.assertRaises(RuntimeError, lambda x, y: eval(expr), x, y)
+
+    unops = [
+        "-x", "+x", "abs(x)", "~x",
+        "int(x)", "long(x)", "float(x)",
+        ]
+
+    def test_unops(self):
+        P = self.c.proxy
+        for expr in self.unops:
+            self.c.ok = 1
+            x = 1
+            y = eval(expr)
+            x = P(1)
+            z = eval(expr)
+            self.assertEqual(getObject(z), y, "x=%r; expr=%r" % (x, expr))
+            self.c.ok = 0
+            self.assertRaises(RuntimeError, lambda x: eval(expr), x)
+
+    def test_odd_unops(self):
+        # unops that don't return a proxy
+        P = self.c.proxy
+        for func in hex, oct, lambda x: not x:
+            self.c.ok = 1
+            self.assertEqual(func(P(100)), func(100), func.__name__)
+            self.c.ok = 0
+            self.assertRaises(RuntimeError, func, P(100))
 
 def test_suite():
     return unittest.makeSuite(ProxyTests)