[Zope-Checkins] CVS: Zope3/lib/python/Zope/ContextWrapper/tests - test_wrapper.py:1.1.2.1
Fred L. Drake, Jr.
fdrake@acm.org
Fri, 22 Mar 2002 10:11:09 -0500
Update of /cvs-repository/Zope3/lib/python/Zope/ContextWrapper/tests
In directory cvs.zope.org:/tmp/cvs-serv19530/tests
Added Files:
Tag: Zope-3x-branch
test_wrapper.py
Log Message:
Move test_wrapper.py to the tests/ directory so both test.py and the
unittestgui.py script find it. (test.py requires that tests be in
subpackages named "tests"; unittestgui.py does not.)
=== Added File Zope3/lib/python/Zope/ContextWrapper/tests/test_wrapper.py ===
import pickle
import sys
import unittest
from Zope.ContextWrapper import wrapper
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 WrapperTestCase(unittest.TestCase):
def test_wrapper_basics(self):
o1 = 1
o2 = 12
w = wrapper.Wrapper(o1)
self.assert_(o1 is wrapper.getobject(w))
self.assert_(wrapper.getdict(w) is None)
d = wrapper.getdictcreate(w)
self.assert_(wrapper.getdictcreate(w) is d)
c = 'context'
wrapper.setcontext(w, c)
self.assert_(wrapper.getcontext(w) is c)
wrapper.setcontext(w, None)
self.assert_(wrapper.getcontext(w) is None)
wrapper.setobject(w, o2)
self.assert_(wrapper.getobject(w) is o2)
# test 2-argument version of constructor
o = object()
w = wrapper.Wrapper(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):
super(MyWrapper, self).__init__(ob)
self.foo = 1
o = Thing()
o.foo = 'not 1'
o.bar = 2
w = MyWrapper(o)
self.assert_(w.foo == 1)
self.assert_(w.bar == 2)
def test_getobject(self):
obj1 = object()
obj2 = object()
w = wrapper.Wrapper(obj1)
self.assert_(wrapper.getobject(w) is obj1)
wrapper.setobject(w, obj2)
self.assert_(wrapper.getobject(w) is obj2)
self.assert_(wrapper.getobject(None) is None)
self.assert_(wrapper.getobject(obj1) is obj1)
def test_getbaseobject(self):
obj = object()
self.assert_(wrapper.getbaseobject(obj) is obj)
w1 = wrapper.Wrapper(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)
self.assert_(wrapper.getbaseobject(w) is obj)
wrapper.setobject(w1, None)
self.assert_(wrapper.getbaseobject(w) is None)
obj = object()
wrapper.setobject(w1, obj)
self.assert_(wrapper.getbaseobject(w) is obj)
def test_getcontext(self):
context = object()
w = wrapper.Wrapper(None, context)
self.assert_(wrapper.getcontext(w) is context)
self.assert_(wrapper.getcontext(wrapper.Wrapper(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)
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())
self.assert_(wrapper.getinnercontext(w) is context)
wrapper.setcontext(w1, None)
self.assert_(wrapper.getinnercontext(w) is None)
context = object()
wrapper.setcontext(w1, context)
self.assert_(wrapper.getinnercontext(w) is context)
def test_getinnercontext(self):
self.check_getinnercontext(None)
self.check_getinnercontext(object())
def test_getinnerwrapper(self):
context = object()
o = object()
w1 = wrapper.Wrapper(o)
w2 = wrapper.Wrapper(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)
self.assert_(wrapper.getdict(w) is None)
d = wrapper.getdictcreate(w)
self.assert_(wrapper.getdict(w) is d)
self.assert_(wrapper.getdictcreate(w) is d)
self.assert_(wrapper.getdict(w) is d)
w = wrapper.Wrapper(None, name="myobject")
d = wrapper.getdict(w)
self.assert_(d is not None)
self.assert_(wrapper.getdictcreate(w) is d)
self.assert_(wrapper.getdictcreate(w) is d)
self.assert_(len(d) == 1)
def test_setobject(self):
obj1 = object()
obj2 = object()
w = wrapper.Wrapper(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)
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())
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
if __name__ == "__main__":
runner = unittest.TextTestRunner(sys.stdout)
result = runner.run(test_suite())
newerrs = len(result.errors) + len(result.failures)
sys.exit(newerrs and 1 or 0)