[Zope-Checkins] CVS: Zope3/lib/python/Zope/ContextWrapper/tests - test_proxy.py:1.1.4.1 test_wrapper.py:1.1.2.3
Jim Fulton
jim@zope.com
Sun, 28 Apr 2002 13:17:16 -0400
Update of /cvs-repository/Zope3/lib/python/Zope/ContextWrapper/tests
In directory cvs.zope.org:/tmp/cvs-serv17050/lib/python/Zope/ContextWrapper/tests
Modified Files:
Tag: Zope-3x-branch
test_wrapper.py
Added Files:
Tag: Zope-3x-branch
test_proxy.py
Log Message:
HOTYB: Merged SecurityProxy-branch into main branch.
All tests pass and folders can be listed and added through the web.
It is likely that most other things don't work and will need to be
fixed. The reason is that many accesses that should have been checked
before are now being checked and additional checks and thinking about
permissions and security settings are needed.
I'm in the process of drafting a paper for the wiki that describes the
changes in more detail.
=== Added File Zope3/lib/python/Zope/ContextWrapper/tests/test_proxy.py ===
import pickle
import unittest
from Zope.ContextWrapper import proxy
class Thing:
pass
class Comparable:
def __init__(self, value):
self.value = value
def __eq__(self, other):
if hasattr(other, "value"):
other = other.value
return self.value == other
def __ne__(self, other):
return not self.__eq__(other)
def __lt__(self, other):
if hasattr(other, "value"):
other = other.value
return self.value < other
def __ge__(self, other):
return not self.__lt__(other)
def __le__(self, other):
if hasattr(other, "value"):
other = other.value
return self.value <= other
def __gt__(self, other):
return not self.__le__(other)
def __repr__(self):
return "<Comparable: %r>" % self.value
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)
def test_proxy_attributes(self):
o = Thing()
o.foo = 1
w = self.new_proxy(o)
self.assert_(w.foo == 1)
def test_getobject(self):
obj1 = object()
w = self.new_proxy(obj1)
self.assert_(proxy.getobject(w) is obj1)
def test___class__(self):
o = object()
w = self.new_proxy(o)
self.assert_(w.__class__ is o.__class__)
def test_pickle_prevention(self):
w = self.new_proxy(Thing())
self.assertRaises(pickle.PicklingError,
pickle.dumps, w)
def test_proxy_equality(self):
w = self.new_proxy('foo')
self.assertEquals(w, 'foo')
o1 = Comparable(1)
o2 = Comparable(1.0)
o3 = Comparable("splat!")
w1 = self.new_proxy(o1)
w2 = self.new_proxy(o2)
w3 = self.new_proxy(o3)
self.assertEquals(o1, w1)
self.assertEquals(o1, w2)
self.assertEquals(o2, w1)
self.assertEquals(w1, o2)
self.assertEquals(w2, o1)
self.assertNotEquals(o3, w1)
self.assertNotEquals(w1, o3)
self.assertNotEquals(w3, o1)
self.assertNotEquals(o1, w3)
def test_proxy_ordering_lt(self):
o1 = Comparable(1)
o2 = Comparable(2.0)
w1 = self.new_proxy(o1)
w2 = self.new_proxy(o2)
self.assert_(w1 < w2)
self.assert_(w1 <= w2)
self.assert_(o1 < w2)
self.assert_(o1 <= w2)
self.assert_(w1 < o2)
self.assert_(w2 <= o2)
def test_proxy_callable(self):
w = self.new_proxy({}.get)
self.assert_(callable(w))
def test_wrapped_iterable(self):
a = [1, 2, 3]
b = []
for x in self.new_proxy(a):
b.append(x)
self.assertEquals(a, b)
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
# Before 2.3, coerce() of two proxies returns them unchanged
import sys
fixed_coerce = sys.version_info >= (2, 3, 0)
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)
self.failUnless(a == 1.0)
self.failUnless(b is y)
if fixed_coerce:
self.failUnless(a.__class__ is float, a.__class__)
x = P(1.1)
y = P(2)
a, b = coerce(x, y)
self.failUnless(a is x)
self.failUnless(b == 2.0)
if fixed_coerce:
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():
return unittest.makeSuite(ProxyTestCase)
if __name__ == "__main__":
import sys
runner = unittest.TextTestRunner(sys.stdout)
result = runner.run(test_suite())
newerrs = len(result.errors) + len(result.failures)
sys.exit(newerrs and 1 or 0)
=== Zope3/lib/python/Zope/ContextWrapper/tests/test_wrapper.py 1.1.2.2 => 1.1.2.3 ===
##############################################################################
import pickle
-import sys
import unittest
from Zope.ContextWrapper import wrapper
+from Zope.ContextWrapper.tests.test_proxy \
+ import Comparable, Thing, ProxyTestCase
-class Thing:
- pass
+class WrapperTestCase(ProxyTestCase):
+ def new_proxy(self, o, c=None):
+ return wrapper.Wrapper(o, c)
-class Comparable:
- def __init__(self, value):
- self.value = value
-
- def __eq__(self, other):
- if hasattr(other, "value"):
- other = other.value
- return self.value == other
-
- def __ne__(self, other):
- return not self.__eq__(other)
-
- def __lt__(self, other):
- if hasattr(other, "value"):
- other = other.value
- return self.value < other
-
- def __ge__(self, other):
- return not self.__lt__(other)
-
- def __le__(self, other):
- if hasattr(other, "value"):
- other = other.value
- return self.value <= other
-
- def __gt__(self, other):
- return not self.__le__(other)
-
- def __repr__(self):
- return "<Comparable: %r>" % self.value
-
-
-class WrapperTestCase(unittest.TestCase):
def test_wrapper_basics(self):
o1 = 1
o2 = 12
- w = wrapper.Wrapper(o1)
+ w = self.new_proxy(o1)
self.assert_(o1 is wrapper.getobject(w))
self.assert_(wrapper.getdict(w) is None)
d = wrapper.getdictcreate(w)
@@ -74,16 +43,10 @@
# test 2-argument version of constructor
o = object()
- w = wrapper.Wrapper(o, c)
+ w = self.new_proxy(o, c)
self.assert_(wrapper.getobject(w) is o)
self.assert_(wrapper.getcontext(w) is c)
- def test_wrapper_attributes(self):
- o = Thing()
- o.foo = 1
- w = wrapper.Wrapper(o)
- self.assert_(w.foo == 1)
-
def test_wrapper_subclass_attributes(self):
class MyWrapper(wrapper.Wrapper):
def __init__(self, ob):
@@ -100,7 +63,7 @@
def test_getobject(self):
obj1 = object()
obj2 = object()
- w = wrapper.Wrapper(obj1)
+ w = self.new_proxy(obj1)
self.assert_(wrapper.getobject(w) is obj1)
wrapper.setobject(w, obj2)
self.assert_(wrapper.getobject(w) is obj2)
@@ -110,13 +73,13 @@
def test_getbaseobject(self):
obj = object()
self.assert_(wrapper.getbaseobject(obj) is obj)
- w1 = wrapper.Wrapper(obj)
+ w1 = self.new_proxy(obj)
self.assert_(wrapper.getbaseobject(w1) is obj)
- w = wrapper.Wrapper(w1)
- w = wrapper.Wrapper(w)
- w = wrapper.Wrapper(w)
- w = wrapper.Wrapper(w)
- w = wrapper.Wrapper(w)
+ w = self.new_proxy(w1)
+ w = self.new_proxy(w)
+ w = self.new_proxy(w)
+ w = self.new_proxy(w)
+ w = self.new_proxy(w)
self.assert_(wrapper.getbaseobject(w) is obj)
wrapper.setobject(w1, None)
self.assert_(wrapper.getbaseobject(w) is None)
@@ -126,21 +89,21 @@
def test_getcontext(self):
context = object()
- w = wrapper.Wrapper(None, context)
+ w = self.new_proxy(None, context)
self.assert_(wrapper.getcontext(w) is context)
- self.assert_(wrapper.getcontext(wrapper.Wrapper(None)) is None)
+ self.assert_(wrapper.getcontext(self.new_proxy(None)) is None)
self.assert_(wrapper.getcontext(object()) is None)
def check_getinnercontext(self, context):
obj = object()
self.assert_(wrapper.getinnercontext(obj) is None)
- w1 = wrapper.Wrapper(obj, context)
+ w1 = self.new_proxy(obj, context)
self.assert_(wrapper.getinnercontext(w1) is context)
- w = wrapper.Wrapper(w1, object())
- w = wrapper.Wrapper(w)
- w = wrapper.Wrapper(w, object())
- w = wrapper.Wrapper(w)
- w = wrapper.Wrapper(w, object())
+ w = self.new_proxy(w1, object())
+ w = self.new_proxy(w)
+ w = self.new_proxy(w, object())
+ w = self.new_proxy(w)
+ w = self.new_proxy(w, object())
self.assert_(wrapper.getinnercontext(w) is context)
wrapper.setcontext(w1, None)
self.assert_(wrapper.getinnercontext(w) is None)
@@ -155,14 +118,14 @@
def test_getinnerwrapper(self):
context = object()
o = object()
- w1 = wrapper.Wrapper(o)
- w2 = wrapper.Wrapper(w1, context)
+ w1 = self.new_proxy(o)
+ w2 = self.new_proxy(w1, context)
x = wrapper.getinnerwrapper(w2)
self.assert_(x is w1)
self.assert_(wrapper.getinnerwrapper(o) is o)
def test_getdict(self):
- w = wrapper.Wrapper(None)
+ w = self.new_proxy(None)
self.assert_(wrapper.getdict(w) is None)
d = wrapper.getdictcreate(w)
self.assert_(wrapper.getdict(w) is d)
@@ -179,87 +142,29 @@
def test_setobject(self):
obj1 = object()
obj2 = object()
- w = wrapper.Wrapper(obj1)
+ w = self.new_proxy(obj1)
self.assert_(wrapper.getobject(w) is obj1)
wrapper.setobject(w, obj2)
self.assert_(wrapper.getobject(w) is obj2)
def test_setcontext(self):
- w = wrapper.Wrapper(None)
+ w = self.new_proxy(None)
context = object()
wrapper.setcontext(w, context)
self.assert_(wrapper.getcontext(w) is context)
- def test___class__(self):
- o = object()
- w = wrapper.Wrapper(o)
- self.assert_(w.__class__ is o.__class__)
-
def test_pickle_prevention(self):
- w = wrapper.Wrapper(Thing())
+ w = self.new_proxy(Thing())
self.assertRaises(pickle.PicklingError,
pickle.dumps, w)
- def test_proxy_equality(self):
- w = wrapper.Wrapper('foo')
- self.assertEquals(w, 'foo')
-
- o1 = Comparable(1)
- o2 = Comparable(1.0)
- o3 = Comparable("splat!")
-
- w1 = wrapper.Wrapper(o1)
- w2 = wrapper.Wrapper(o2)
- w3 = wrapper.Wrapper(o3)
-
- self.assertEquals(o1, w1)
- self.assertEquals(o1, w2)
- self.assertEquals(o2, w1)
- self.assertEquals(w1, o2)
- self.assertEquals(w2, o1)
-
- self.assertNotEquals(o3, w1)
- self.assertNotEquals(w1, o3)
- self.assertNotEquals(w3, o1)
- self.assertNotEquals(o1, w3)
-
- def test_proxy_ordering_lt(self):
- o1 = Comparable(1)
- o2 = Comparable(2.0)
-
- w1 = wrapper.Wrapper(o1)
- w2 = wrapper.Wrapper(o2)
-
- self.assert_(w1 < w2)
- self.assert_(w1 <= w2)
- self.assert_(o1 < w2)
- self.assert_(o1 <= w2)
- self.assert_(w1 < o2)
- self.assert_(w2 <= o2)
-
- def test_proxy_callable(self):
- w = wrapper.Wrapper({}.get)
- self.assert_(callable(w))
-
- def test_wrapped_iterable(self):
- a = [1, 2, 3]
- b = []
- for x in wrapper.Wrapper(a):
- b.append(x)
- self.assertEquals(a, b)
-
- def test_bool_wrapped_None(self):
- w = wrapper.Wrapper(None)
- self.assertEquals(not w, 1)
-
def test_suite():
- suite = unittest.TestSuite()
- suite.addTest(unittest.makeSuite(WrapperTestCase))
- return suite
+ return unittest.makeSuite(WrapperTestCase)
if __name__ == "__main__":
+ import sys
runner = unittest.TextTestRunner(sys.stdout)
result = runner.run(test_suite())
newerrs = len(result.errors) + len(result.failures)