[Zope-Checkins] CVS: Zope3/lib/python/Zope/ContextWrapper/tests - test_wrapper.py:1.1.2.2.6.1
Fred L. Drake, Jr.
fdrake@acm.org
Thu, 25 Apr 2002 15:50:22 -0400
Update of /cvs-repository/Zope3/lib/python/Zope/ContextWrapper/tests
In directory cvs.zope.org:/tmp/cvs-serv14776/tests
Modified Files:
Tag: SecurityProxy-branch
test_wrapper.py
Log Message:
Modified the context wrapper to use the base proxy type.
=== Zope3/lib/python/Zope/ContextWrapper/tests/test_wrapper.py 1.1.2.2 => 1.1.2.2.6.1 ===
##############################################################################
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)