[Zope3-checkins] CVS: Zope3/lib/python/Zope/ContextWrapper/tests - testSimpleMethodWrapper.py:1.8

Steve Alexander steve@cat-box.net
Tue, 3 Dec 2002 16:54:09 -0500


Update of /cvs-repository/Zope3/lib/python/Zope/ContextWrapper/tests
In directory cvs.zope.org:/tmp/cvs-serv5452/tests

Modified Files:
	testSimpleMethodWrapper.py 
Log Message:
fixed stuff - now __getitem__ can be a ContextMethod without unwanted
side-effects.


=== Zope3/lib/python/Zope/ContextWrapper/tests/testSimpleMethodWrapper.py 1.7 => 1.8 ===
--- Zope3/lib/python/Zope/ContextWrapper/tests/testSimpleMethodWrapper.py:1.7	Tue Nov 12 10:55:58 2002
+++ Zope3/lib/python/Zope/ContextWrapper/tests/testSimpleMethodWrapper.py	Tue Dec  3 16:54:09 2002
@@ -39,6 +39,11 @@
         return a
     __call__ = ContextMethod(__call__)
 
+    def __getitem__(wrapped_self, a, b=None):
+        wrapped_self.result = wrapped_self, a, b
+        return a
+    __getitem__ = ContextMethod(__getitem__)
+
     def thisIsNotAContextMethod(self, val=None):
         self.result = self
         return val
@@ -69,6 +74,11 @@
         return a
     __call__ = ContextMethod(__call__)
 
+    def __getitem__(wrapped_self, a, b=None):
+        wrapped_self.result = wrapped_self, a, b
+        return a
+    __getitem__ = ContextMethod(__getitem__)
+
     def thisIsNotAContextMethod(self, val=None):
         self.result = self
         return val
@@ -99,6 +109,11 @@
         return a
     __call__ = ContextMethod(__call__)
 
+    def __getitem__(wrapped_self, a, b=None):
+        wrapped_self.result = wrapped_self, a, b
+        return a
+    __getitem__ = ContextMethod(__getitem__)
+
     def thisIsNotAContextMethod(self, val=None):
         self.result = self
         return val
@@ -169,6 +184,37 @@
         self.assert_(result_a is value_a)
         self.assert_(result_b is value_b)
 
+    def testGetitem(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.__getitem__(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.__getitem__(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.__getitem__(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)
@@ -240,13 +286,133 @@
         result_obj, result_value = self.wrapped.result
         self.assert_(result_obj is self.wrapped)
         self.assert_(result_value is value)
+       
+
+class SimpleClass(object):
+    pass
+
+class CallableClass(object):
+    called = False
+    def __call__(self):
+        self.called = True
+    __call__ = ContextMethod(__call__)
+
+class ClassWithGetitem(object):
+    gotitem = False
+    def __getitem__(self, key):
+        self.gotitem = True
+        return key*2
+    __getitem__ = ContextMethod(__getitem__)
+
+class CallableClassWithGetitem(CallableClass, ClassWithGetitem):
+    pass
+
+class CallableGetitemNoContextMethods:
+    def __call__(self):
+        pass
+    def __getitem__(self, key):
+        pass
+
+class TestWrapperTypeSelection(unittest.TestCase):
+
+    def testSelection(self):
         
+        from Zope.ContextWrapper.SimpleMethodWrapper \
+            import SimpleMethodWrapper, SimpleCallableMethodWrapper, \
+            SimpleGetitemMethodWrapper, SimpleCallableGetitemMethodWrapper
+
+        self.assert_(type(Wrapper({})) is SimpleMethodWrapper)
+        self.assert_(type(Wrapper(SimpleClass())) is SimpleMethodWrapper)
+        self.assert_(type(Wrapper(CallableClass())) is
+                     SimpleCallableMethodWrapper)
+        self.assert_(type(Wrapper(ClassWithGetitem())) is
+                     SimpleGetitemMethodWrapper)
+        self.assert_(type(Wrapper(CallableClassWithGetitem())) is
+                     SimpleCallableGetitemMethodWrapper)
+        self.assert_(type(Wrapper(CallableGetitemNoContextMethods())) is
+                     SimpleMethodWrapper)
+
+    def testSimpleClass(self):
+        obj = SimpleClass()
+        wrapped = Wrapper(obj)
+        self.assertRaises(TypeError, obj)  # call as obj()
+        self.assertRaises(TypeError, wrapped) # call as wrapped()
+        self.assertRaises(TypeError, lambda: wrapped[23])
+        self.assert_(not callable(obj))
+        # This fails, because the base proxy c class in Zope/Proxy/proxy.c
+        # fills the tp_call slot.
+        #
+        # self.assert_(not callable(wrapped))
+        #
+        # let's just check that the (slightly broken) converse is true
+        self.assert_(callable(wrapped))
+
+    def testCallableClass(self):
+        obj = CallableClass()
+        wrapped = Wrapper(obj)
+        self.assert_(callable(obj))
+        self.assert_(callable(wrapped))
+        wrapped()
+        self.assertEqual(obj.called, True)
+
+        self.assertRaises(TypeError, lambda: wrapped[23])
+        self.assertRaises(AttributeError, getattr, wrapped, '__getitem__')
+
+    def testGetitemClass(self):
+        obj = ClassWithGetitem()
+        wrapped = Wrapper(obj)
+        self.assert_(not callable(obj))
+        # This fails, because the base proxy c class in Zope/Proxy/proxy.c
+        # fills the tp_call slot.
+        #
+        # self.assert_(not callable(wrapped))
+        #
+        # let's just check that the (slightly broken) converse is true
+        self.assert_(callable(wrapped))
+        self.assertRaises(TypeError, wrapped)
+
+        self.assert_(hasattr(obj, '__getitem__'))
+        self.assert_(hasattr(wrapped, '__getitem__'))
+        self.assertEqual(wrapped[23], 46)
+        self.assertEqual(wrapped.__getitem__(23), 46)
+
+    def testCallableGetitemClass(self):
+        obj = CallableClassWithGetitem()
+        wrapped = Wrapper(obj)
+        self.assert_(callable(obj))
+        self.assert_(callable(wrapped))
+        wrapped()
+        self.assertEqual(obj.called, True)
+
+        self.assert_(hasattr(obj, '__getitem__'))
+        self.assert_(hasattr(wrapped, '__getitem__'))
+        self.assertEqual(wrapped[23], 46)
+        self.assertEqual(wrapped.__getitem__(23), 46)
+
+    def testBuiltinMethod(self):
+        obj = {0:23}
+        wrapped = Wrapper(obj)
+        self.assert_(not callable(obj))
+        # This fails, because the base proxy c class in Zope/Proxy/proxy.c
+        # fills the tp_call slot.
+        #
+        # self.assert_(not callable(wrapped))
+        #
+        # let's just check that the (slightly broken) converse is true
+        self.assert_(callable(wrapped))
+        self.assertRaises(TypeError, wrapped)
+
+        self.assert_(hasattr(obj, '__getitem__'))
+        self.assert_(hasattr(wrapped, '__getitem__'))
+        self.assertEqual(wrapped[0], 23)
+        self.assertEqual(wrapped.__getitem__(0), 23)
 
 def test_suite():
     return unittest.TestSuite((
         unittest.makeSuite(TestNewStyleClass),
         unittest.makeSuite(TestNewStyleClassWithSlots),
-        unittest.makeSuite(TestClassicClass)
+        unittest.makeSuite(TestClassicClass),
+        unittest.makeSuite(TestWrapperTypeSelection),
         ))