[Zope3-checkins] CVS: Zope3/src/zope/proxy/context/tests - __init__.py:1.1.2.1 test_containmentiterator.py:1.1.2.1 test_simplemethodwrapper.py:1.1.2.1 test_wrapper.py:1.1.2.1

Jim Fulton jim@zope.com
Mon, 23 Dec 2002 14:33:08 -0500


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

Added Files:
      Tag: NameGeddon-branch
	__init__.py test_containmentiterator.py 
	test_simplemethodwrapper.py test_wrapper.py 
Log Message:
Initial renaming before debugging

=== Added File Zope3/src/zope/proxy/context/tests/__init__.py ===
#
# This file is necessary to make this directory a package.


=== Added File Zope3/src/zope/proxy/context/tests/test_containmentiterator.py ===
##############################################################################
#
# Copyright (c) 2001, 2002 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.
# 
##############################################################################
"""

$Id: test_containmentiterator.py,v 1.1.2.1 2002/12/23 19:33:06 jim Exp $
"""

import unittest, sys

from zope.proxy.context import Wrapper, getbaseobject
from zope.proxy.context.containmentiterator import ContainmentIterator

class Test(unittest.TestCase):

    def  testWrapped(self):
        ob1 = 1
        ob2 = 2
        ob3 = 3
        ob4 = 4

        ob = Wrapper(Wrapper(ob3, Wrapper(ob2, ob1)), ob4)
        self.assertEqual(map(getbaseobject, ContainmentIterator(ob)),
                        [ 3, 2, 1 ])

    def  testUnWrapped(self):
        self.assertEqual(map(getbaseobject, ContainmentIterator(9)), [ 9 ])
        
def test_suite():
    loader=unittest.TestLoader()
    return loader.loadTestsFromTestCase(Test)

if __name__=='__main__':
    unittest.TextTestRunner().run(test_suite())


=== Added File Zope3/src/zope/proxy/context/tests/test_simplemethodwrapper.py ===
##############################################################################
#
# Copyright (c) 2001, 2002 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.
# 
##############################################################################
import sys
import unittest

# Note that this is testing both that SimpleMethodWrapper works,
# and that SimpleMethodWrapper is available as
# from zope.proxy.context import Wrapper
#
# However, this test suite can form the basis of a test for the improved C 
# implementation, when that lands.
#
from zope.proxy.context import \
        Wrapper, wrapperTypes, ContextMethod, \
        ContextProperty, ContextGetProperty, ContextSetProperty, \
        ContextSuper

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

    def this_is_any_old_method(self):
        return 'base', self

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

    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)

    def this_is_any_old_method(self):
        return 'base', self
    
   
class ClassicClass:

    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
        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 TestClassicClass(unittest.TestCase):

    _class = ClassicClass

    def createObject(self):
        # to be overridden in tests that subclass this one
        return self._class()

    def setUp(self):
        unittest.TestCase.setUp(self)
        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 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)

    def testGetContextGetProperty(self):
        self.assertEqual(self.wrapped.thisIsAContextGetProperty, True)
        self.assert_(self.obj.result is self.wrapped)

    def testNotFound(self):
        self.assertRaises(AttributeError,
                          getattr, self.wrapped, 'noSuchAttribute')
        

class TestNewStyleClassWithSlots(TestClassicClass):

    _class = NewStyleClassWithSlots

    # Setting properties doesn't work with classic classes,
    # so this class has extra tests for setting properties in
    # new-style classes.

    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 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 testContextSuper(self):

        def this_is_any_old_method(self):
            return 'sub', self
        
        Sub = type('sub', (self._class, ), {
            'this_is_any_old_method': this_is_any_old_method,
            })

        inst = Wrapper(Sub())
        name, rinst = inst.this_is_any_old_method()
        self.assertEqual(name,  'sub')
        self.assert_(rinst is not inst)

        name, rinst = ContextSuper(Sub, inst).this_is_any_old_method()
        self.assertEqual(name,  'base')
        self.assert_(rinst is inst)


class TestNewStyleClass(TestNewStyleClassWithSlots):

    _class = NewStyleClass

    def testGetContextProperty_w_name_in_dict(self):
        self.obj.__dict__['thisIsAContextProperty'] = False
        self.assertEqual(self.obj.thisIsAContextProperty, True)
        self.assert_(self.obj.result is self.obj)
        self.assertEqual(self.wrapped.thisIsAContextProperty, True)
        self.assert_(self.obj.result is self.wrapped)

    def testSetContextProperty_w_name_in_dict(self):
        self.obj.__dict__['thisIsAContextProperty'] = False
        value = 23
        self.obj.thisIsAContextProperty = value
        result_obj, result_value = self.obj.result
        self.assert_(result_obj is self.obj)
        self.assert_(result_value is value)

        self.wrapped.thisIsAContextProperty = value
        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.proxy.context.context \
            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(TestWrapperTypeSelection),
        ))


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)



=== Added File Zope3/src/zope/proxy/context/tests/test_wrapper.py ===
##############################################################################
#
# Copyright (c) 2001, 2002 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.
# 
##############################################################################
import pickle
import unittest

from zope.proxy.context import wrapper
from zope.proxy.tests.test_proxy \
     import Comparable, Thing, ProxyTestCase


class WrapperTestCase(ProxyTestCase):
    def new_proxy(self, o, c=None):
        return wrapper.Wrapper(o, c)

    def test_constructor(self):
        o1 = object()
        o2 = object()
        o3 = object()
        w = self.new_proxy((o1, o2), o3)
        self.assertEquals(wrapper.getobject(w), (o1, o2))
        self.assert_(wrapper.getcontext(w) is o3)


    def test_subclass_constructor(self):
        class MyWrapper(wrapper.Wrapper):
            def __init__(self, *args, **kwds):
                super(MyWrapper, self).__init__('foo', **kwds)

        w = MyWrapper(1, 2, key='value')
        self.assertEquals(wrapper.getobject(w), 'foo')
        self.assertEquals(wrapper.getdict(w), {'key': 'value'})

        # __new__ catches too many positional args:
        self.assertRaises(TypeError, MyWrapper, 1, 2, 3)

    def test_wrapper_basics(self):
        o1 = 1
        o2 = 12
        w = self.new_proxy(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 = self.new_proxy(o, c)
        self.assert_(wrapper.getobject(w) is o)
        self.assert_(wrapper.getcontext(w) is c)

    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 = self.new_proxy(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 = self.new_proxy(obj)
        self.assert_(wrapper.getbaseobject(w1) is obj)
        w = self.new_proxy(w1)
        w = self.new_proxy(w)
        w = self.new_proxy(w)
        w = self.new_proxy(w)
        w = self.new_proxy(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 = self.new_proxy(None, context)
        self.assert_(wrapper.getcontext(w) is context)
        self.assert_(wrapper.getcontext(self.new_proxy(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 = self.new_proxy(obj, context)
        self.assert_(wrapper.getinnercontext(w1) is context)
        w = self.new_proxy(w1, object())
        w = self.new_proxy(w)
        w = self.new_proxy(w, object())
        w = self.new_proxy(w)
        w = self.new_proxy(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 = self.new_proxy(o)
        w2 = self.new_proxy(w1, context)
        x = wrapper.getinnerwrapper(w2)
        self.assert_(x is w1)
        self.assert_(wrapper.getinnerwrapper(o) is o)

    def test_getdict(self):
        w = self.new_proxy(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 = self.new_proxy(obj1)
        self.assert_(wrapper.getobject(w) is obj1)
        wrapper.setobject(w, obj2)
        self.assert_(wrapper.getobject(w) is obj2)

    def test_setcontext(self):
        w = self.new_proxy(None)
        context = object()
        wrapper.setcontext(w, context)
        self.assert_(wrapper.getcontext(w) is context)

    def test_pickle_prevention(self):
        w = self.new_proxy(Thing())
        self.assertRaises(pickle.PicklingError,
                          pickle.dumps, w)


def test_suite():
    return unittest.makeSuite(WrapperTestCase)


if __name__ == "__main__":
    import sys
    runner = unittest.TextTestRunner(sys.stdout)
    result = runner.run(test_suite())
    newerrs = len(result.errors) + len(result.failures)
    sys.exit(newerrs and 1 or 0)