[Zope3-checkins] CVS: Zope3/lib/python/Zope/ContextWrapper/tests - testSimpleMethodWrapper.py:1.3
Steve Alexander
steve@cat-box.net
Thu, 31 Oct 2002 07:30:50 -0500
Update of /cvs-repository/Zope3/lib/python/Zope/ContextWrapper/tests
In directory cvs.zope.org:/tmp/cvs-serv1415/lib/python/Zope/ContextWrapper/tests
Modified Files:
testSimpleMethodWrapper.py
Log Message:
Refactored and improved quality and coverage of tests for ContextMethods.
Implemented ContextProperties in three flavours:
ContextProperty: gives both get and set methods a wrapped_self.
ContextGetProperty: gives get method a wrapped_self,
leaves set method with a regular self.
ContextSetProperty: leaves get method with a regular self,
gives set method a wrapped_self.
=== Zope3/lib/python/Zope/ContextWrapper/tests/testSimpleMethodWrapper.py 1.2 => 1.3 ===
--- Zope3/lib/python/Zope/ContextWrapper/tests/testSimpleMethodWrapper.py:1.2 Mon Jun 10 19:29:25 2002
+++ Zope3/lib/python/Zope/ContextWrapper/tests/testSimpleMethodWrapper.py Thu Oct 31 07:30:49 2002
@@ -22,66 +22,216 @@
# However, this test suite can form the basis of a test for the improved C
# implementation, when that lands.
#
-from Zope.ContextWrapper import Wrapper, ContextMethod, wrapperTypes
+from Zope.ContextWrapper import \
+ Wrapper, wrapperTypes, ContextMethod, \
+ ContextProperty, ContextGetProperty, ContextSetProperty
class NewStyleClass(object):
- def thisIsAContextMethod(wrapped_self):
- if type(wrapped_self) not in wrapperTypes:
- raise AssertionError
+
+ result = None
+
+ def thisIsAContextMethod(wrapped_self, val=None):
+ wrapped_self.result = wrapped_self
+ return val
thisIsAContextMethod = ContextMethod(thisIsAContextMethod)
- def __call__(wrapped_self):
- if type(wrapped_self) not in wrapperTypes:
- raise AssertionError
+ def __call__(wrapped_self, a, b=None):
+ wrapped_self.result = wrapped_self, a, b
+ return a
__call__ = ContextMethod(__call__)
- def thisIsNotAContextMethod(self):
- if type(self) in wrapperTypes:
- raise AssertionError
+ def thisIsNotAContextMethod(self, val=None):
+ self.result = self
+ return val
+
+ def _getter(wrapped_self):
+ wrapped_self.result = wrapped_self
+ return True
+
+ def _setter(wrapped_self, value):
+ wrapped_self.result = wrapped_self, value
+ thisIsAContextProperty = ContextProperty(_getter, _setter)
+ thisIsAContextGetProperty = ContextGetProperty(_getter, _setter)
+ thisIsAContextSetProperty = ContextSetProperty(_getter, _setter)
+
+class NewStyleClassWithSlots(object):
+ __slots__ = ['result']
+
+ def __init__(self):
+ self.result = None
+
+ def thisIsAContextMethod(wrapped_self, val=None):
+ wrapped_self.result = wrapped_self
+ return val
+ thisIsAContextMethod = ContextMethod(thisIsAContextMethod)
-
+ def __call__(wrapped_self, a, b=None):
+ wrapped_self.result = wrapped_self, a, b
+ return a
+ __call__ = ContextMethod(__call__)
+
+ def thisIsNotAContextMethod(self, val=None):
+ self.result = self
+ return val
+
+ def _getter(wrapped_self):
+ wrapped_self.result = wrapped_self
+ return True
+
+ def _setter(wrapped_self, value):
+ wrapped_self.result = wrapped_self, value
+
+ thisIsAContextProperty = ContextProperty(_getter, _setter)
+ thisIsAContextGetProperty = ContextGetProperty(_getter, _setter)
+ thisIsAContextSetProperty = ContextSetProperty(_getter, _setter)
+
+
class ClassicClass:
- def thisIsAContextMethod(wrapped_self):
- if type(wrapped_self) not in wrapperTypes:
- raise AssertionError
+
+ result = None
+
+ def thisIsAContextMethod(wrapped_self, val=None):
+ wrapped_self.result = wrapped_self
+ return val
thisIsAContextMethod = ContextMethod(thisIsAContextMethod)
def __call__(wrapped_self, a, b=None):
- if type(wrapped_self) not in wrapperTypes:
- raise AssertionError
- if a != 1:
- raise AssertionError
- if b != 2:
- raise AssertionError
+ wrapped_self.result = wrapped_self, a, b
+ return a
__call__ = ContextMethod(__call__)
- def thisIsNotAContextMethod(self):
- if type(self) in wrapperTypes:
- raise AssertionError
-
-
-class SimpleMethodWrapperTestCase(unittest.TestCase):
- def testNewStyleClass(self):
- newstyle = NewStyleClass()
- wrapped = Wrapper(newstyle)
- wrapped.thisIsAContextMethod()
- wrapped.thisIsNotAContextMethod()
- self.assertRaises(AssertionError, newstyle.thisIsAContextMethod)
- wrapped()
-
- def testClassicClass(self):
- classic = ClassicClass()
- wrapped = Wrapper(classic)
- wrapped.thisIsAContextMethod()
- wrapped.thisIsNotAContextMethod()
- self.assertRaises(AssertionError, classic.thisIsAContextMethod)
- wrapped(1,2)
- wrapped(a=1, b=2)
- wrapped(1, b=2)
-
+ def thisIsNotAContextMethod(self, val=None):
+ self.result = self
+ return val
+
+ def _getter(wrapped_self):
+ wrapped_self.result = wrapped_self
+ return True
+
+ # not using a setter, as this is a classic class, and so
+ # setting an attribute that is a property results in overwriting
+ # that property.
+ thisIsAContextProperty = ContextProperty(_getter)
+ thisIsAContextGetProperty = ContextGetProperty(_getter)
+
+
+class TestNewStyleClass(unittest.TestCase):
+
+ def createObject(self):
+ # to be overridden in tests that subclass this one
+ return NewStyleClass()
+
+ def setUp(self):
+ super(TestNewStyleClass, self).setUp()
+ self.obj = self.createObject()
+ self.wrapped = Wrapper(self.obj)
+ self.assertEqual(self.obj.result, None)
+
+ def testContextMethod(self):
+ value = object()
+ self.assertEqual(self.wrapped.thisIsAContextMethod(value), value)
+ self.assert_(self.obj.result is self.wrapped)
+
+ self.assertEqual(self.obj.thisIsAContextMethod(value), value)
+ self.assert_(self.obj.result is self.obj)
+
+ def testNotContextMethod(self):
+ value = object()
+ self.assertEqual(self.wrapped.thisIsNotAContextMethod(value), value)
+ self.assert_(self.obj.result is self.obj)
+
+ def testCall(self):
+ value_a = object()
+ value_b = object()
+
+ self.assertRaises(TypeError, self.wrapped)
+
+ self.assertEqual(self.wrapped(value_a), value_a)
+ result_obj, result_a, result_b = self.obj.result
+ self.assert_(result_obj is self.wrapped)
+ self.assert_(result_a is value_a)
+ self.assert_(result_b is None)
+
+ self.assertEqual(self.wrapped(value_a, value_b), value_a)
+ result_obj, result_a, result_b = self.obj.result
+ self.assert_(result_obj is self.wrapped)
+ self.assert_(result_a is value_a)
+ self.assert_(result_b is value_b)
+
+ self.assertEqual(self.wrapped(value_a, b=value_b), value_a)
+ result_obj, result_a, result_b = self.obj.result
+ self.assert_(result_obj is self.wrapped)
+ self.assert_(result_a is value_a)
+ self.assert_(result_b is value_b)
+
+ self.assertEqual(self.wrapped(a=value_a, b=value_b), value_a)
+ result_obj, result_a, result_b = self.obj.result
+ self.assert_(result_obj is self.wrapped)
+ self.assert_(result_a is value_a)
+ self.assert_(result_b is value_b)
+
+ def testGetContextProperty(self):
+ self.assertEqual(self.wrapped.thisIsAContextProperty, True)
+ self.assert_(self.obj.result is self.wrapped)
+
+ def testSetContextProperty(self):
+ value = 23
+ self.wrapped.thisIsAContextProperty = value
+ result_obj, result_value = self.obj.result
+ self.assert_(result_obj is self.wrapped)
+ self.assert_(result_value is value)
+
+ def testGetContextGetProperty(self):
+ self.assertEqual(self.wrapped.thisIsAContextGetProperty, True)
+ self.assert_(self.obj.result is self.wrapped)
+
+ def testSetContextGetProperty(self):
+ value = 23
+ self.wrapped.thisIsAContextGetProperty = value
+ result_obj, result_value = self.obj.result
+ self.assert_(result_obj is self.obj)
+ self.assert_(result_value is value)
+
+ def testGetContextSetProperty(self):
+ self.assertEqual(self.wrapped.thisIsAContextSetProperty, True)
+ self.assert_(self.obj.result is self.obj)
+
+ def testSetContextSetProperty(self):
+ value = 23
+ self.wrapped.thisIsAContextSetProperty = value
+ result_obj, result_value = self.obj.result
+ self.assert_(result_obj is self.wrapped)
+ self.assert_(result_value is value)
+
+ def testNotFound(self):
+ self.assertRaises(AttributeError,
+ getattr, self.wrapped, 'noSuchAttribute')
+
+
+class TestNewStyleClassWithSlots(TestNewStyleClass):
+
+ def createObject(self):
+ return NewStyleClassWithSlots()
+
+class TestClassicClass(TestNewStyleClass):
+
+ def createObject(self):
+ return ClassicClass()
+
+ # setting properties doesn't work with classic classes
+ # so, disable those tests
+ def testSetContextProperty(self):
+ pass
+ testSetContextGetProperty = testSetContextProperty
+ testGetContextSetProperty = testSetContextProperty
+ testSetContextSetProperty = testSetContextProperty
def test_suite():
- return unittest.makeSuite(SimpleMethodWrapperTestCase)
+ return unittest.TestSuite((
+ unittest.makeSuite(TestNewStyleClass),
+ unittest.makeSuite(TestNewStyleClassWithSlots),
+ unittest.makeSuite(TestClassicClass)
+ ))
if __name__ == "__main__":
@@ -89,3 +239,4 @@
result = runner.run(test_suite())
newerrs = len(result.errors) + len(result.failures)
sys.exit(newerrs and 1 or 0)
+