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

Guido van Rossum guido@python.org
Thu, 25 Apr 2002 22:54:43 -0400


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

Modified Files:
      Tag: SecurityProxy-branch
	test_Proxy.py 
Log Message:
- Improve shouldFail() -- reset c.ok to 1 afterwards.

- Use shouldFail() in a few more places.

- Use failUnless() in a few more places.

- Rearrange binops/unops test so unops come first.

- Add tests for **, some inplace operators, and coerce.
  XXX test_coerce *fails* unless you are using the latest Python from CVS!!!


=== Zope3/lib/python/Zope/Security/tests/test_Proxy.py 1.1.2.15 => 1.1.2.16 ===
         self.c.ok = 0
         self.assertRaises(RuntimeError, *args)
+        self.c.ok = 1
 
     def testStr(self):
         self.assertEqual(str(self.p), str(self.x))
@@ -106,7 +107,7 @@
         self.shouldFail(self.p, None)
 
     def testRichCompareOK(self):
-        self.assertEqual(self.p == self.x, 1)
+        self.failUnless(self.p == self.x)
 
     def testRichCompareFail(self):
         self.shouldFail(lambda: self.p == self.x)
@@ -161,7 +162,7 @@
         self.shouldFail(doit)
 
     def testContainsOK(self):
-        self.assertEqual(42 in self.p, 1)
+        self.failUnless(42 in self.p)
 
     def testContainsFail(self):
         self.shouldFail(lambda: 42 in self.p)
@@ -186,9 +187,30 @@
         pC = ProxyFactory(C, self.c)
         self.assertEqual(d[pC], d[C])
 
+    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:
+            x = 1
+            y = eval(expr)
+            x = P(1)
+            z = eval(expr)
+            self.assertEqual(getObject(z), y, "x=%r; expr=%r" % (x, expr))
+            self.shouldFail(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.assertEqual(func(P(100)), func(100))
+            self.shouldFail(func, P(100))
+
     binops = [
-        "x+y", "x-y", "x*y", "x/y", "divmod(x, y)",
-        # "x**y",
+        "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",
         ]
 
@@ -202,37 +224,87 @@
                         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)
+                        self.shouldFail(lambda x, y: eval(expr), x, y)
 
-    unops = [
-        "-x", "+x", "abs(x)", "~x",
-        "int(x)", "long(x)", "float(x)",
-        ]
-
-    def test_unops(self):
+    def test_inplace(self):
+        # XXX should test all inplace operators...
         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
+        pa = P(1)
+        pa += 2
+        self.assertEqual(getObject(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])
+
+        def doit():
+            pa = P(1)
+            pa += 2
+        self.shouldFail(doit)
+
+        pa = P(2)
+        pa **= 2
+        self.assertEqual(getObject(pa), 4)
+
+        def doit():
+            pa = P(2)
+            pa **= 2
+        self.shouldFail(doit)
+
+    def test_coerce(self):
         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))
+
+        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(type(getObject(a)) is float and b is y)
+
+        x = P(1.1)
+        y = P(2)
+        a, b = coerce(x, y)
+        self.failUnless(a is x and type(getObject(b)) is float)
+
+        x = P(1)
+        y = 2
+        a, b = coerce(x, y)
+        self.failUnless(a is x and b is y)
+
+        x = P(1)
+        y = 2.1
+        a, b = coerce(x, y)
+        self.failUnless(type(getObject(a)) is float and b is y)
+
+        x = P(1.1)
+        y = 2
+        a, b = coerce(x, y)
+        self.failUnless(a is x and type(getObject(b)) is float)
+
+        x = 1
+        y = P(2)
+        a, b = coerce(x, y)
+        self.failUnless(a is x and b is y)
+
+        x = 1.1
+        y = P(2)
+        a, b = coerce(x, y)
+        self.failUnless(a is x and type(getObject(b)) is float)
+
+        x = 1
+        y = P(2.1)
+        a, b = coerce(x, y)
+        self.failUnless(type(getObject(a)) is float and b is y)
 
 def test_suite():
     return unittest.makeSuite(ProxyTests)