[Zope3-checkins] CVS: Zope3/src/zope/proxy/context/tests - test_wrapperinteraction.py:1.1.2.1 test_ContextAware.py:1.1.10.1 test_wrapper.py:1.2.12.1 test_simplemethodwrapper.py:NONE

Steve Alexander steve@cat-box.net
Tue, 1 Apr 2003 14:53:07 -0500


Update of /cvs-repository/Zope3/src/zope/proxy/context/tests
In directory cvs.zope.org:/tmp/cvs-serv9941/src/zope/proxy/context/tests

Modified Files:
      Tag: stevea-contextaware_in_c-branch
	test_ContextAware.py test_wrapper.py 
Added Files:
      Tag: stevea-contextaware_in_c-branch
	test_wrapperinteraction.py 
Removed Files:
      Tag: stevea-contextaware_in_c-branch
	test_simplemethodwrapper.py 
Log Message:
Initial checkin of context wrappers that are context aware, written in C.


=== Added File Zope3/src/zope/proxy/context/tests/test_wrapperinteraction.py === (410/510 lines abridged)
##############################################################################
#
# Copyright (c) 2003 Zope Corporation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""Testing the interaction of Wrapper, ContextWrapper, ContextMethod etc.

Testing the Wrapper type's interaction with ContextDescriptors such as
ContextMethod and ContextProperty, and the ContextWrapper factory function
that creates a wrapper object, and checks for misuse of ContextDescriptors
as members of classic classes. (Descriptors generally don't work properly
as members of classic classes.)

$Id: test_wrapperinteraction.py,v 1.1.2.1 2003/04/01 19:53:06 stevea Exp $
"""
import sys
import unittest

from zope.proxy.context import Wrapper, ContextMethod, ContextProperty
from zope.proxy.context import ContextSuper, ContextWrapper, ContextAware

class NewStyleClass(object):

    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 __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

[-=- -=- -=- 410 lines omitted -=- -=- -=-]

        # 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 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)

class TestContextWrapperFactory(unittest.TestCase):

    def testClassicClassWarning(self):
        from types import ClassType
        class Classic:
            __metaclass__ = ClassType
            def foo(self):
                pass

        class BadClassic:
            __metaclass__ = ClassType
            def foo(self):
                pass
            foo = ContextMethod(foo)

        # ok if there are no ContextDescriptors 
        w = ContextWrapper(Classic(), None)

        # raises if there is a ContextDescriptor
        self.assertRaises(TypeError, ContextWrapper, BadClassic(), None)

def test_suite():
    return unittest.TestSuite((
        unittest.makeSuite(TestNewStyleClass),
        unittest.makeSuite(TestNewStyleClassWithSlots),
        unittest.makeSuite(TestClassicClass),
        unittest.makeSuite(TestWrapperOnObjectsWithDifferentSlots),
        unittest.makeSuite(TestContextWrapperFactory),
        ))


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)


=== Zope3/src/zope/proxy/context/tests/test_ContextAware.py 1.1 => 1.1.10.1 ===
--- Zope3/src/zope/proxy/context/tests/test_ContextAware.py:1.1	Sat Jan 25 10:32:52 2003
+++ Zope3/src/zope/proxy/context/tests/test_ContextAware.py	Tue Apr  1 14:53:06 2003
@@ -44,7 +44,6 @@
 
     def fC(self):
         return getWrapperContainer(self)
-        
 
 class Test(TestCase):
 
@@ -71,7 +70,7 @@
         self.assertEqual(b.fC(), None)
         b.p2C = 1
         self.assertEqual(b.v, (None, 1))
-        
+
         # Check wrapper case
         b = ContextWrapper(b, 42)
         self.assertEqual(b.p1B, 42)


=== Zope3/src/zope/proxy/context/tests/test_wrapper.py 1.2 => 1.2.12.1 ===
--- Zope3/src/zope/proxy/context/tests/test_wrapper.py:1.2	Wed Dec 25 09:15:17 2002
+++ Zope3/src/zope/proxy/context/tests/test_wrapper.py	Tue Apr  1 14:53:06 2003
@@ -14,10 +14,12 @@
 import pickle
 import unittest
 
-from zope.proxy.context import wrapper
+from zope.proxy.context import wrapper, getcontext, ContextAware, ContextMethod
 from zope.proxy.tests.test_proxy import Comparable, Thing, ProxyTestCase
 
 
+_marker = object()
+
 class WrapperTestCase(ProxyTestCase):
     def new_proxy(self, o, c=None):
         return wrapper.Wrapper(o, c)
@@ -79,6 +81,133 @@
         w = MyWrapper(o)
         self.assert_(w.foo == 1)
         self.assert_(w.bar == 2)
+
+    def make_proxies(self, slot, fixed_retval=_marker):
+        context = object()
+
+        #
+        # XXX: redo it using type as a constructor and setting just the
+        # appropriate slot
+        #
+
+        # context-unaware object
+        class ContextUnawareObj(object):
+            def doit(self, *args):
+                self.retval = getcontext(self), args
+                if fixed_retval is _marker:
+                    return self.retval
+                else:
+                    return fixed_retval
+        setattr(ContextUnawareObj, slot, ContextUnawareObj.doit)
+        proxy1 = self.new_proxy(ContextUnawareObj(), context)
+
+        # context-aware object
+        class ContextAwareObj(ContextAware):
+            def doit(self, *args):
+                self.retval = getcontext(self), args
+                if fixed_retval is _marker:
+                    return self.retval
+                else:
+                    return fixed_retval
+        setattr(ContextAwareObj, slot, ContextAwareObj.doit)
+        proxy2 = self.new_proxy(ContextAwareObj(), context)
+
+        # object with context method
+        class ContextMethodObj(object):
+            def doit(self, *args):
+                self.retval = getcontext(self), args
+                if fixed_retval is _marker:
+                    return self.retval
+                else:
+                    return fixed_retval
+            doit = ContextMethod(doit)
+        setattr(ContextMethodObj, slot, ContextMethodObj.doit)
+        proxy3 = self.new_proxy(ContextMethodObj(), context)
+
+        return proxy1, proxy2, proxy3, context
+
+    def test_getitem(self):
+        p1, p2, p3, context = self.make_proxies('__getitem__')
+        self.assertEquals(p1[42], (None, (42, )))
+        self.assertEquals(p2[42], (context, (42, )))
+        self.assertEquals(p3[42], (context, (42, )))
+        # builtin
+        p4 = self.new_proxy((1, 2), context)
+        self.assertEquals(p4[0], 1)
+        self.assertEquals(p4[1], 2)
+        self.assertRaises(IndexError, p4.__getitem__, 2)
+
+    def test_setitem(self):
+        p1, p2, p3, context = self.make_proxies('__setitem__')
+        p1[24] = 42
+        p2[24] = 42
+        p3[24] = 42
+        self.assertEquals(p1.retval, (None, (24, 42)))
+        self.assertEquals(p2.retval, (context, (24, 42)))
+        self.assertEquals(p3.retval, (context, (24, 42)))
+        # builtin
+        p4 = self.new_proxy([1, 2], context)
+        p4[1] = 3
+        self.assertEquals(p4[1], 3)
+        self.assertRaises(IndexError, p4.__setitem__, 2, 4)
+
+    def test_delitem(self):
+        p1, p2, p3, context = self.make_proxies('__delitem__')
+        del p1[42]
+        del p2[42]
+        del p3[42]
+        self.assertEquals(p1.retval, (None, (42, )))
+        self.assertEquals(p2.retval, (context, (42, )))
+        self.assertEquals(p3.retval, (context, (42, )))
+        # builtin
+        p4 = self.new_proxy([1, 2], context)
+        del p4[1]
+        self.assertEquals(p4, [1])
+        self.assertRaises(IndexError, p4.__delitem__, 2)
+
+    def test_iter(self):
+        p1, p2, p3, context = self.make_proxies('__iter__', iter(()))
+        iter(p1)
+        iter(p2)
+        iter(p3)
+        self.assertEquals(p1.retval, (None, ()))
+        self.assertEquals(p2.retval, (context, ()))
+        self.assertEquals(p3.retval, (context, ()))
+
+    def test_call(self):
+        p1, p2, p3, context = self.make_proxies('__call__')
+        self.assertEquals(p1('foo', 'bar'), (None, ('foo', 'bar')))
+        self.assertEquals(p2('foo', 'bar'), (context, ('foo', 'bar')))
+        self.assertEquals(p3('foo', 'bar'), (context, ('foo', 'bar')))
+
+    def test_str(self):
+        p1, p2, p3, context = self.make_proxies('__str__', 'foo')
+        self.assertEquals(str(p1), 'foo')
+        self.assertEquals(str(p2), 'foo')
+        self.assertEquals(str(p3), 'foo')
+        self.assertEquals(p1.retval, (None, ()))
+        self.assertEquals(p2.retval, (context, ()))
+        self.assertEquals(p3.retval, (context, ()))
+
+    def test_contains(self):
+        p1, p2, p3, context = self.make_proxies('__contains__', 1)
+        self.assert_(42 in p1)
+        self.assert_(42 in p2)
+        self.assert_(42 in p3)
+        self.assertEquals(p1.retval, (None, (42, )))
+        self.assertEquals(p2.retval, (context, (42, )))
+        self.assertEquals(p3.retval, (context, (42, )))
+
+    def test_len(self):
+        p1, p2, p3, context = self.make_proxies('__len__', 5)
+        self.assertEquals(len(p1), 5)
+        self.assertEquals(len(p2), 5)
+        self.assertEquals(len(p3), 5)
+        self.assertEquals(p1.retval, (None, ()))
+        self.assertEquals(p2.retval, (context, ()))
+        self.assertEquals(p3.retval, (context, ()))
+
+    # Tests for wrapper module globals
 
     def test_getobject(self):
         obj1 = object()

=== Removed File Zope3/src/zope/proxy/context/tests/test_simplemethodwrapper.py ===