[Zope-CVS] CVS: Packages/ContextWrapper - test_wrapper.py:1.5
Fred Drake Jr
fdrake@acm.org
Wed, 14 Nov 2001 14:38:02 -0500
Update of /cvs-repository/Packages/ContextWrapper
In directory cvs.zope.org:/tmp/cvs-serv9185
Modified Files:
test_wrapper.py
Log Message:
Remove tests for callable wrappers.
Add thorough tests of all the functions in the module API.
=== Packages/ContextWrapper/test_wrapper.py 1.4 => 1.5 ===
pass
-class CallableThing:
- def __call__(self):
- return 'splat!'
-
class WrapperTestCase(unittest.TestCase):
- def check_wrapper_basics(self, o1, o2, factory):
- w = factory(o1)
+ 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)
@@ -33,51 +31,14 @@
wrapper.setobject(w, o2)
self.assert_(wrapper.getobject(w) is o2)
- def test_basic_wrapper(self):
- self.check_wrapper_basics(1, 12,
- wrapper.WrapperType)
- self.check_wrapper_basics(1, 12,
- wrapper.new)
- w = wrapper.new(1)
- self.assert_(not callable(w))
-
- def test_callable_wrapper(self):
- self.check_wrapper_basics(lambda:3, lambda:36,
- wrapper.CallableWrapperType)
- self.check_wrapper_basics(lambda:3, lambda:36,
- wrapper.new)
- w = wrapper.new(lambda:3)
- self.assert_(callable(w))
-
- def test_callable_wrapper_calls(self):
- # test wrappers with various combinations of positional,
- # extra, and keyword args
- w = wrapper.new(lambda:3)
- self.assert_(w() == 3)
-
- w = wrapper.new(lambda a:a*3)
- self.assert_(w(3) == 9)
-
- w = wrapper.new(lambda **kw:kw)
- self.assert_(w(foo="bar") == {"foo": "bar"})
-
- w = wrapper.new(lambda a,*args,**kw:(a*3,args,kw))
- self.assert_(w(4,5,6,frob="nitz") == (12, (5, 6), {"frob": "nitz"}))
-
def test_wrapper_attributes(self):
o = Thing()
o.foo = 1
- w = wrapper.new(o)
+ w = wrapper.Wrapper(o)
self.assert_(w.foo == 1)
- o = CallableThing()
- o.foo = 2
- w = wrapper.new(o)
- self.assert_(callable(w))
- self.assert_(w.foo == 2)
-
def test_wrapper_subclass_attributes(self):
- class MyWrapper(wrapper.WrapperType):
+ class MyWrapper(wrapper.Wrapper):
def __init__(self, ob):
self.foo = 1
super(MyWrapper, self).__init__(ob)
@@ -88,6 +49,82 @@
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)
+
+ def test_getinnerobject(self):
+ obj = object()
+ self.assert_(wrapper.getinnerobject(obj) is obj)
+ w1 = wrapper.Wrapper(obj)
+ self.assert_(wrapper.getinnerobject(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.getinnerobject(w) is obj)
+ wrapper.setobject(w1, None)
+ self.assert_(wrapper.getinnerobject(w) is None)
+ obj = object()
+ wrapper.setobject(w1, obj)
+ self.assert_(wrapper.getinnerobject(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_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)
+
+ 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_suite():