[Zope-Checkins] CVS: Zope3/lib/python/Zope/ContextWrapper/tests - __init__.py:1.2 testContainmentIterator.py:1.2 testSimpleMethodWrapper.py:1.2 test_proxy.py:1.2 test_wrapper.py:1.2

Jim Fulton jim@zope.com
Mon, 10 Jun 2002 19:29:57 -0400


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

Added Files:
	__init__.py testContainmentIterator.py 
	testSimpleMethodWrapper.py test_proxy.py test_wrapper.py 
Log Message:
Merged Zope-3x-branch into newly forked Zope3 CVS Tree.

=== Zope3/lib/python/Zope/ContextWrapper/tests/__init__.py 1.1 => 1.2 ===
+#
+# 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.
+# 
+##############################################################################
+""" Unit tests for ContainmentIterator """


=== Zope3/lib/python/Zope/ContextWrapper/tests/testContainmentIterator.py 1.1 => 1.2 ===
+#
+# 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$
+"""
+
+import unittest, sys
+
+from Zope.ContextWrapper import Wrapper, getbaseobject
+from Zope.ContextWrapper.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())


=== Zope3/lib/python/Zope/ContextWrapper/tests/testSimpleMethodWrapper.py 1.1 => 1.2 ===
+#
+# 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 sys
+import unittest
+
+# Note that this is testing both that SimpleMethodWrapper works,
+# and that SimpleMethodWrapper is available as
+# from Zope.ContextWrapper import Wrapper
+#
+# 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
+
+class NewStyleClass(object):
+    def thisIsAContextMethod(wrapped_self):
+        if type(wrapped_self) not in wrapperTypes:
+            raise AssertionError
+    thisIsAContextMethod = ContextMethod(thisIsAContextMethod)
+
+    def __call__(wrapped_self):
+        if type(wrapped_self) not in wrapperTypes:
+            raise AssertionError
+    __call__ = ContextMethod(__call__)
+
+    def thisIsNotAContextMethod(self):
+        if type(self) in wrapperTypes:
+            raise AssertionError
+
+       
+class ClassicClass:
+    def thisIsAContextMethod(wrapped_self):
+        if type(wrapped_self) not in wrapperTypes:
+            raise AssertionError
+    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
+    __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 test_suite():
+    return unittest.makeSuite(SimpleMethodWrapperTestCase)
+
+
+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/lib/python/Zope/ContextWrapper/tests/test_proxy.py 1.1 => 1.2 ===
+import unittest
+
+from Zope.ContextWrapper import proxy
+
+
+class Thing:
+    pass
+
+class Comparable:
+    def __init__(self, value):
+        self.value = value
+
+    def __eq__(self, other):
+        if hasattr(other, "value"):
+            other = other.value
+        return self.value == other
+
+    def __ne__(self, other):
+        return not self.__eq__(other)
+
+    def __lt__(self, other):
+        if hasattr(other, "value"):
+            other = other.value
+        return self.value < other
+
+    def __ge__(self, other):
+        return not self.__lt__(other)
+
+    def __le__(self, other):
+        if hasattr(other, "value"):
+            other = other.value
+        return self.value <= other
+
+    def __gt__(self, other):
+        return not self.__le__(other)
+
+    def __repr__(self):
+        return "<Comparable: %r>" % self.value
+
+
+class ProxyTestCase(unittest.TestCase):
+    def setUp(self):
+        self.x = Thing()
+        self.p = self.new_proxy(self.x)
+
+    def new_proxy(self, o):
+        return proxy.proxy(o)
+
+    def test_constructor(self):
+        o = object()
+        self.assertRaises(TypeError, proxy.proxy, o, o)
+        self.assertRaises(TypeError, proxy.proxy, o, key='value')
+        self.assertRaises(TypeError, proxy.proxy, key='value')
+
+    def test_subclass_constructor(self):
+        class MyProxy(proxy.proxy):
+            def __new__(cls, *args, **kwds):
+                return super(MyProxy, cls).__new__(cls, *args, **kwds)
+            def __init__(self, *args, **kwds):
+                super(MyProxy, self).__init__(*args, **kwds)
+        o1 = object()
+        o2 = object()
+        o = MyProxy((o1, o2))
+        self.assertEquals(o1, o[0])
+        self.assertEquals(o2, o[1])
+
+        self.assertRaises(TypeError, MyProxy, o1, o2)
+        self.assertRaises(TypeError, MyProxy, o1, key='value')
+        self.assertRaises(TypeError, MyProxy, key='value')
+
+        # Check that are passed to __init__() overrides what's passed
+        # to __new__().
+        class MyProxy2(proxy.proxy):
+            def __new__(cls, *args, **kwds):
+                return super(MyProxy2, cls).__new__(cls, 'value')
+
+        p = MyProxy2('splat!')
+        self.assertEquals(list(p), list('splat!'))
+
+        class MyProxy3(MyProxy2):
+            def __init__(self, arg):
+                assert list(self) == list('value')
+                super(MyProxy3, self).__init__('another')
+
+        p = MyProxy3('notused')
+        self.assertEquals(list(p), list('another'))
+
+    def test_proxy_attributes(self):
+        o = Thing()
+        o.foo = 1
+        w = self.new_proxy(o)
+        self.assert_(w.foo == 1)
+
+    def test_getobject(self):
+        obj1 = object()
+        w = self.new_proxy(obj1)
+        self.assert_(proxy.getobject(w) is obj1)
+
+    def test___class__(self):
+        o = object()
+        w = self.new_proxy(o)
+        self.assert_(w.__class__ is o.__class__)
+
+    def test_pickle_prevention(self):
+        w = self.new_proxy(Thing())
+        self.assertRaises(pickle.PicklingError,
+                          pickle.dumps, w)
+
+    def test_proxy_equality(self):
+        w = self.new_proxy('foo')
+        self.assertEquals(w, 'foo')
+
+        o1 = Comparable(1)
+        o2 = Comparable(1.0)
+        o3 = Comparable("splat!")
+
+        w1 = self.new_proxy(o1)
+        w2 = self.new_proxy(o2)
+        w3 = self.new_proxy(o3)
+
+        self.assertEquals(o1, w1)
+        self.assertEquals(o1, w2)
+        self.assertEquals(o2, w1)
+        self.assertEquals(w1, o2)
+        self.assertEquals(w2, o1)
+
+        self.assertNotEquals(o3, w1)
+        self.assertNotEquals(w1, o3)
+        self.assertNotEquals(w3, o1)
+        self.assertNotEquals(o1, w3)
+
+    def test_proxy_ordering_lt(self):
+        o1 = Comparable(1)
+        o2 = Comparable(2.0)
+
+        w1 = self.new_proxy(o1)
+        w2 = self.new_proxy(o2)
+
+        self.assert_(w1 < w2)
+        self.assert_(w1 <= w2)
+        self.assert_(o1 < w2)
+        self.assert_(o1 <= w2)
+        self.assert_(w1 < o2)
+        self.assert_(w2 <= o2)
+
+    def test_proxy_callable(self):
+        w = self.new_proxy({}.get)
+        self.assert_(callable(w))
+
+    def test_wrapped_iterable(self):
+        a = [1, 2, 3]
+        b = []
+        for x in self.new_proxy(a):
+            b.append(x)
+        self.assertEquals(a, b)
+
+    def test_bool_wrapped_None(self):
+        w = self.new_proxy(None)
+        self.assertEquals(not w, 1)
+
+    # Numeric ops.
+
+    unops = [
+        "-x", "+x", "abs(x)", "~x",
+        "int(x)", "long(x)", "float(x)",
+        ]
+
+    def test_unops(self):
+        P = self.new_proxy
+        for expr in self.unops:
+            x = 1
+            y = eval(expr)
+            x = P(1)
+            z = eval(expr)
+            self.assertEqual(z, y,
+                             "x=%r; expr=%r" % (x, expr))
+
+    def test_odd_unops(self):
+        # unops that don't return a proxy
+        P = self.new_proxy
+        for func in hex, oct, lambda x: not x:
+            self.assertEqual(func(P(100)), func(100))
+
+    binops = [
+        "x+y", "x-y", "x*y", "x/y", "divmod(x, y)", "x**y", "x//y",
+        "x<<y", "x>>y", "x&y", "x|y", "x^y",
+        ]
+
+    def test_binops(self):
+        P = self.new_proxy
+        for expr in self.binops:
+            first = 1
+            for x in [1, P(1)]:
+                for y in [2, P(2)]:
+                    if first:
+                        z = eval(expr)
+                        first = 0
+                    else:
+                        self.assertEqual(eval(expr), z,
+                                         "x=%r; y=%r; expr=%r" % (x, y, expr))
+
+    def test_inplace(self):
+        # XXX should test all inplace operators...
+        P = self.new_proxy
+
+        pa = P(1)
+        pa += 2
+        self.assertEqual(pa, 3)
+
+        a = [1, 2, 3]
+        pa = qa = P(a)
+        pa += [4, 5, 6]
+        self.failUnless(pa is qa)
+        self.assertEqual(a, [1, 2, 3, 4, 5, 6])
+
+        pa = P(2)
+        pa **= 2
+        self.assertEqual(pa, 4)
+
+    def test_coerce(self):
+        P = self.new_proxy
+
+        # Before 2.3, coerce() of two proxies returns them unchanged
+        import sys
+        fixed_coerce = sys.version_info >= (2, 3, 0)
+
+        x = P(1)
+        y = P(2)
+        a, b = coerce(x, y)
+        self.failUnless(a is x and b is y)
+
+        x = P(1)
+        y = P(2.1)
+        a, b = coerce(x, y)
+        self.failUnless(a == 1.0)
+        self.failUnless(b is y)
+        if fixed_coerce:
+            self.failUnless(a.__class__ is float, a.__class__)
+
+        x = P(1.1)
+        y = P(2)
+        a, b = coerce(x, y)
+        self.failUnless(a is x)
+        self.failUnless(b == 2.0)
+        if fixed_coerce:
+            self.failUnless(b.__class__ is float, b.__class__)
+
+        x = P(1)
+        y = 2
+        a, b = coerce(x, y)
+        self.failUnless(a is x)
+        self.failUnless(b is y)
+
+        x = P(1)
+        y = 2.1
+        a, b = coerce(x, y)
+        self.failUnless(a.__class__ is float, a.__class__)
+        self.failUnless(b is y)
+
+        x = P(1.1)
+        y = 2
+        a, b = coerce(x, y)
+        self.failUnless(a is x)
+        self.failUnless(b.__class__ is float, b.__class__)
+
+        x = 1
+        y = P(2)
+        a, b = coerce(x, y)
+        self.failUnless(a is x)
+        self.failUnless(b is y)
+
+        x = 1.1
+        y = P(2)
+        a, b = coerce(x, y)
+        self.failUnless(a is x)
+        self.failUnless(b.__class__ is float, b.__class__)
+
+        x = 1
+        y = P(2.1)
+        a, b = coerce(x, y)
+        self.failUnless(a.__class__ is float, a.__class__)
+        self.failUnless(b is y)
+
+
+def test_suite():
+    return unittest.makeSuite(ProxyTestCase)
+
+
+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)


=== Zope3/lib/python/Zope/ContextWrapper/tests/test_wrapper.py 1.1 => 1.2 ===
+#
+# 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.ContextWrapper import wrapper
+from Zope.ContextWrapper.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)