[Zope3-checkins] CVS: Zope3/src/zope/context/tests - test_iterators.py:1.1.4.1 test_wrapper.py:1.12.4.1 test_wrapperinteraction.py:1.4.10.1 test_ContextAware.py:NONE test_containmentiterator.py:NONE test_decorator.py:NONE
Grégoire Weber
zope@i-con.ch
Sun, 22 Jun 2003 10:24:11 -0400
Update of /cvs-repository/Zope3/src/zope/context/tests
In directory cvs.zope.org:/tmp/cvs-serv24874/src/zope/context/tests
Modified Files:
Tag: cw-mail-branch
test_wrapper.py test_wrapperinteraction.py
Added Files:
Tag: cw-mail-branch
test_iterators.py
Removed Files:
Tag: cw-mail-branch
test_ContextAware.py test_containmentiterator.py
test_decorator.py
Log Message:
Synced up with HEAD
=== Added File Zope3/src/zope/context/tests/test_iterators.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_iterators.py,v 1.1.4.1 2003/06/22 14:23:40 gregweb Exp $
"""
import unittest
from zope.context import Wrapper, getbaseobject, ContainmentIterator
from zope.context import ContextIterator
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 ])
self.assertEqual(map(getbaseobject, ContextIterator(ob)),
[ 3, 4 ])
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/src/zope/context/tests/test_wrapper.py 1.12 => 1.12.4.1 ===
--- Zope3/src/zope/context/tests/test_wrapper.py:1.12 Mon May 12 11:44:41 2003
+++ Zope3/src/zope/context/tests/test_wrapper.py Sun Jun 22 10:23:40 2003
@@ -14,15 +14,47 @@
"""
$Id$
"""
-import pickle
import unittest
-
-from zope.proxy.context import wrapper, getcontext, getobject, ContextWrapper
-from zope.proxy.context import ContextMethod, ContextProperty, ContextAware
+from zope.proxy import getProxiedObject
+from zope.context import Wrapper, wrapper, ContextMethod, ContextProperty
from zope.proxy.tests.test_proxy import Thing, ProxyTestCase
_marker = object()
+class WrapperProperty(object):
+ def __init__(self, name, dictname, default=_marker):
+ self.name = name
+ self.dictname = dictname
+ self.default = default
+
+ def __get__(self, obj, tp=None):
+ if obj is None:
+ return self
+ d = wrapper.getdict(obj)
+ if d:
+ try:
+ return d[self.dictname]
+ except KeyError:
+ pass
+ if self.default is not _marker:
+ return self.default
+ raise AttributeError, self.name
+
+ def __set__(self, obj, value):
+ self.default = _marker
+ wrapper.getdictcreate(obj)[self.dictname] = value
+
+ def __delete__(self, obj):
+ self.default = _marker
+ d = wrapper.getdict(obj)
+ if d:
+ try:
+ del d[self.dictname]
+ return
+ except KeyError:
+ pass
+ raise AttributeError, self.name
+
class WrapperTestCase(ProxyTestCase):
proxy_class = wrapper.Wrapper
@@ -35,7 +67,7 @@
o2 = object()
o3 = object()
w = self.new_proxy((o1, o2), o3)
- self.assertEquals(wrapper.getobject(w), (o1, o2))
+ self.assertEquals(getProxiedObject(w), (o1, o2))
self.assert_(wrapper.getcontext(w) is o3)
def test_subclass_constructor(self):
@@ -44,7 +76,7 @@
super(MyWrapper, self).__init__('foo', **kwds)
w = MyWrapper(1, 2, key='value')
- self.assertEquals(wrapper.getobject(w), 'foo')
+ self.assertEquals(getProxiedObject(w), 'foo')
self.assertEquals(wrapper.getdict(w), {'key': 'value'})
# __new__ catches too many positional args:
@@ -54,7 +86,7 @@
o1 = 1
o2 = 12
w = self.new_proxy(o1)
- self.assert_(o1 is wrapper.getobject(w))
+ self.assert_(o1 is getProxiedObject(w))
self.assert_(wrapper.getdict(w) is None)
d = wrapper.getdictcreate(w)
self.assert_(wrapper.getdictcreate(w) is d)
@@ -66,12 +98,12 @@
self.assert_(wrapper.getcontext(w) is None)
wrapper.setobject(w, o2)
- self.assert_(wrapper.getobject(w) is o2)
+ self.assert_(getProxiedObject(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_(getProxiedObject(w) is o)
self.assert_(wrapper.getcontext(w) is c)
def test_wrapper_subclass_attributes(self):
@@ -91,7 +123,7 @@
context = object()
def doit(self, *args):
- self.retval = getcontext(self), args
+ self.retval = wrapper.getcontext(self), args
if fixed_retval is _marker:
return self.retval
else:
@@ -102,17 +134,12 @@
setattr(ContextUnawareObj, slot, doit)
proxy1 = self.new_proxy(ContextUnawareObj(), context)
- class ContextAwareObj(ContextAware):
- pass
- setattr(ContextAwareObj, slot, doit)
- proxy2 = self.new_proxy(ContextAwareObj(), context)
-
class ContextMethodObj(object):
pass
setattr(ContextMethodObj, slot, ContextMethod(doit))
- proxy3 = self.new_proxy(ContextMethodObj(), context)
+ proxy2 = self.new_proxy(ContextMethodObj(), context)
- return proxy1, proxy2, proxy3, context
+ return proxy1, proxy2, context
def test_normal_getattr(self):
class X(object):
@@ -138,35 +165,6 @@
self.assertEquals(p.getArgs(), (x, 'foo'))
self.assert_(p.getArgs()[0] is x)
- def test_ContextAware_getattr(self):
- class Y(ContextAware):
- def __init__(self, retval):
- self.args = None
- self.retval = retval
- def __getattr__(self, name):
- if name == '__del__':
- # We don't want Python's gc to think that we have a
- # __del__, otherwise cycles will not be collected.
- raise AttributeError, name
- self.args = self, name
- return self.__dict__['retval']
- def getArgs(self):
- # Need to get __dict__ from the clean object, because it
- # is a special descriptor and complains bitterly about
- # being got from the wrong kind of object.
- return getobject(self).__dict__['args']
-
- y = Y(23)
- p = self.new_proxy(y, 23)
- self.assertEquals(p.foo, 23)
- # Nothing special happens; we don't rebind the self of __getattr__
- self.assertEquals(p.getArgs(), (y, 'foo'))
- self.assert_(p.getArgs()[0] is y)
-
- def test_ContextAware_doesnt_mess_up___class__(self):
- class C(ContextAware): pass
- self.assertEqual(ContextWrapper(C(), None).__class__, C)
-
def test_ContextMethod_getattr(self):
class Z(object):
def __getattr__(self, name):
@@ -210,22 +208,6 @@
self.assertEquals(x.called_with, (p, 24))
self.assert_(x.called_with[0] is p)
- def test_ContextAware_property(self):
- class Y(ContextAware):
- def getFoo(self):
- self.called_with = self
- return 42
- def setFoo(self, value):
- self.called_with = self, value
- foo = property(getFoo, setFoo)
- y = Y()
- p = self.new_proxy(y)
- self.assertEquals(p.foo, 42)
- self.assert_(y.called_with is p)
- p.foo = 24
- self.assertEquals(y.called_with, (p, 24))
- self.assert_(y.called_with[0] is p)
-
def test_setattr(self):
class X(object):
def __setattr__(self, name, value):
@@ -237,14 +219,6 @@
self.assertEqual(x.value_called, (p, 'foo', 'bar'))
self.assert_(x.value_called[0] is x)
- class ContextAwareX(X, ContextAware):
- pass
- cax = ContextAwareX()
- p = self.new_proxy(cax)
- p.foo = 'bar'
- self.assertEqual(cax.value_called, (p, 'foo', 'bar'))
- self.assert_(cax.value_called[0] is cax)
-
X.__setattr__ = ContextMethod(X.__setattr__)
x = X()
p = self.new_proxy(x)
@@ -265,118 +239,96 @@
self.assertRaises(UnicodeError, setattr, p, u'baz\u1234', 23)
def test_getitem(self):
- p1, p2, p3, context = self.make_proxies('__getitem__')
+ p1, p2, 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)
+ p = self.new_proxy((1, 2), context)
+ self.assertEquals(p[0], 1)
+ self.assertEquals(p[1], 2)
+ self.assertRaises(IndexError, p.__getitem__, 2)
def test_setitem(self):
- p1, p2, p3, context = self.make_proxies('__setitem__')
+ p1, p2, 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)
+ p = self.new_proxy([1, 2], context)
+ p[1] = 3
+ self.assertEquals(p[1], 3)
+ self.assertRaises(IndexError, p.__setitem__, 2, 4)
def test_delitem(self):
- p1, p2, p3, context = self.make_proxies('__delitem__')
+ p1, p2, 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)
+ p = self.new_proxy([1, 2], context)
+ del p[1]
+ self.assertEquals(p, [1])
+ self.assertRaises(IndexError, p.__delitem__, 2)
def test_iter(self):
- p1, p2, p3, context = self.make_proxies('__iter__', iter(()))
+ p1, p2, 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__')
+ p1, p2, 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')
+ p1, p2, 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)
+ p1, p2, 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)
+ p1, p2, 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, ()))
- p1, p2, p3, context = self.make_proxies('__len__', 5)
+ p1, p2, context = self.make_proxies('__len__', 5)
self.assertEquals(bool(p1), True)
self.assertEquals(bool(p2), True)
- self.assertEquals(bool(p3), True)
self.assertEquals(p1.retval, (None, ()))
self.assertEquals(p2.retval, (context, ()))
- self.assertEquals(p3.retval, (context, ()))
- p1, p2, p3, context = self.make_proxies('__len__', 0)
+ p1, p2, context = self.make_proxies('__len__', 0)
self.assertEquals(bool(p1), False)
self.assertEquals(bool(p2), False)
- self.assertEquals(bool(p3), False)
self.assertEquals(p1.retval, (None, ()))
self.assertEquals(p2.retval, (context, ()))
- self.assertEquals(p3.retval, (context, ()))
def test_nonzero(self):
- p1, p2, p3, context = self.make_proxies('__nonzero__', True)
+ p1, p2, context = self.make_proxies('__nonzero__', True)
self.assertEquals(bool(p1), True)
self.assertEquals(bool(p2), True)
- self.assertEquals(bool(p3), True)
self.assertEquals(p1.retval, (None, ()))
self.assertEquals(p2.retval, (context, ()))
- self.assertEquals(p3.retval, (context, ()))
- p1, p2, p3, context = self.make_proxies('__nonzero__', False)
+ p1, p2, context = self.make_proxies('__nonzero__', False)
self.assertEquals(bool(p1), False)
self.assertEquals(bool(p2), False)
- self.assertEquals(bool(p3), False)
self.assertEquals(p1.retval, (None, ()))
self.assertEquals(p2.retval, (context, ()))
- self.assertEquals(p3.retval, (context, ()))
def test_nonzero_with_len(self):
class ThingWithLenAndNonzero(object):
@@ -413,33 +365,6 @@
# Tests for wrapper module globals
- 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)
@@ -496,9 +421,9 @@
obj1 = object()
obj2 = object()
w = self.new_proxy(obj1)
- self.assert_(wrapper.getobject(w) is obj1)
+ self.assert_(getProxiedObject(w) is obj1)
wrapper.setobject(w, obj2)
- self.assert_(wrapper.getobject(w) is obj2)
+ self.assert_(getProxiedObject(w) is obj2)
def test_setcontext(self):
w = self.new_proxy(None)
@@ -506,14 +431,186 @@
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_simple_subclass(self):
+
+ class Foo(self.proxy_class):
+ def bar(self, *args):
+ inner = getProxiedObject(self)
+ inner += args
+ return len(args)
+
+ baz = 23
+ spoo = WrapperProperty('spoo', 'spooprop', default=23)
+
+ self.assert_(self.proxy_class in Foo.__mro__)
+
+ l = []
+ w = Foo(l)
+
+ self.assert_(w.__class__ is l.__class__)
+ self.assert_(type(w) is Foo)
+
+ self.assertEquals(w.bar(1, 2, 3), 3)
+ self.assertEquals(l, [1, 2, 3])
+ w.append('x')
+ self.assertEquals(l, [1, 2, 3, 'x'])
+
+ self.assertEquals(w.spoo, 23)
+ w.spoo = 24
+ self.assertEquals(w.spoo, 24)
+
+ self.assertEquals(w.baz, 23)
+ self.assertRaises(TypeError, setattr, w, 'baz', 24)
+
+ def test_subclass_with_slots(self):
+ obj = object()
+ names = ('__len__', '__getitem__', '__setitem__', '__str__',
+ '__contains__', '__call__', '__nonzero__', '__iter__',
+ 'next')
+
+ dummy_iter = iter(range(5))
+ proxy_class = self.proxy_class
+ class WrapperWithSlots(proxy_class):
+ def __init__(self, *args, **kw):
+ super(WrapperWithSlots, self).__init__(*args, **kw)
+ d = wrapper.getdictcreate(self)
+ d['initargs'] = args
+ d['initkw'] = kw
+
+ count = WrapperProperty('count', 'wrapper_count', default=0)
+ called = WrapperProperty('called', 'wrapper_called')
+ def __len__(self):
+ self.called = 'len'
+ return 5
+ def __nonzero__(self):
+ self.called = 'nonzero'
+ return False
+ def __getitem__(self, key):
+ self.called = 'getitem'
+ return 5
+ def __setitem__(self, key, value):
+ self.called = 'setitem'
+ def __str__(self):
+ self.called = 'str'
+ return '5'
+ def __contains__(self, key):
+ self.called = 'contains'
+ return True
+ def __call__(self):
+ self.called = 'call'
+ return 'skidoo'
+ def __iter__(self):
+ self.called = 'iter'
+ return self
+ def next(self):
+ self.called = 'next'
+ self.count += 1
+ if self.count == 5:
+ self.count = 0
+ raise StopIteration
+ return self.count
+
+ w = WrapperWithSlots(obj, None)
+
+ self.assertEquals(len(w), 5)
+ self.assertEquals(w.called, 'len')
+ del w.called
+
+ self.assertEquals(w[3], 5)
+ self.assertEquals(w.called, 'getitem')
+ del w.called
+
+ w[3] = 5
+ self.assertEquals(w.called, 'setitem')
+ del w.called
+
+ self.assertEquals(str(w), '5')
+ self.assertEquals(w.called, 'str')
+ del w.called
+
+ self.assert_(5 in w)
+ self.assertEquals(w.called, 'contains')
+ del w.called
+
+ self.assertEquals(w(), 'skidoo')
+ self.assertEquals(w.called, 'call')
+ del w.called
+
+ self.assertEquals(bool(w), False)
+ self.assertEquals(w.called, 'nonzero')
+ del w.called
+
+ # Test case where w doesn't provide a __nonzero__ but does
+ # provide a __len__.
+ del WrapperWithSlots.__nonzero__
+ self.assertEquals(bool(w), True)
+ self.assertEquals(w.called, 'len')
+ del w.called
+
+ self.assertEquals(iter(w), w)
+ self.assertEquals(w.called, 'iter')
+ del w.called
+
+ self.assertEquals(w.next(), 1)
+ self.assertEquals(w.called, 'next')
+ self.assertEquals([i for i in iter(w)], [2, 3, 4])
+ del w.called
+
+ def test_decorated_iterable(self):
+ obj = object()
+ a = [1, 2, 3]
+ b = []
+ class IterableDecorator(self.proxy_class):
+ def __iter__(self):
+ return iter(a)
+ for x in IterableDecorator(obj):
+ b.append(x)
+ self.assertEquals(a, b)
+
+ def test_iteration_using_decorator(self):
+ # Wrap an iterator within the iteration protocol, expecting it
+ # still to work. PyObject_GetIter() will not be called on the
+ # proxy, so the tp_iter slot won't unwrap it.
+
+ class Iterable:
+ def __init__(self, test, data):
+ self.test = test
+ self.data = data
+ def __iter__(self):
+ obj = object()
+ it = iter(self.data)
+ class IterableDecorator(self.test.proxy_class):
+ def __iter__(self):
+ return it.__iter__()
+ def next(self):
+ return it.next()
+ return IterableDecorator(obj)
+
+ a = [1, 2, 3]
+ b = []
+ for x in Iterable(self, a):
+ b.append(x)
+ self.assertEquals(a, b)
+
+
+class WrapperSubclass(wrapper.Wrapper):
+
+ def someMethodOrOther(self):
+ pass
+
+class WrapperSubclassTestCase(WrapperTestCase):
+
+ proxy_class = WrapperSubclass
+
+ def new_proxy(self, o, c=None):
+ return self.proxy_class(o, c)
def test_suite():
- return unittest.makeSuite(WrapperTestCase)
+ return unittest.TestSuite((
+ unittest.makeSuite(WrapperTestCase),
+ unittest.makeSuite(WrapperSubclassTestCase),
+ ))
if __name__ == "__main__":
=== Zope3/src/zope/context/tests/test_wrapperinteraction.py 1.4 => 1.4.10.1 ===
--- Zope3/src/zope/context/tests/test_wrapperinteraction.py:1.4 Thu May 1 15:35:45 2003
+++ Zope3/src/zope/context/tests/test_wrapperinteraction.py Sun Jun 22 10:23:40 2003
@@ -11,11 +11,10 @@
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
-"""Testing the interaction of Wrapper, ContextWrapper, ContextMethod etc.
+"""Testing the interaction of Wrapper, 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
+ContextMethod and ContextProperty, and checks for misuse of ContextDescriptors
as members of classic classes. (Descriptors generally don't work properly
as members of classic classes.)
@@ -24,8 +23,8 @@
import sys
import unittest
-from zope.proxy.context import Wrapper, ContextMethod, ContextProperty
-from zope.proxy.context import ContextSuper
+from zope.context import Wrapper, ContextMethod, ContextProperty
+from zope.context import ContextSuper
class NewStyleClass(object):
@@ -472,31 +471,141 @@
self.assertEqual(wrapped[0], 23)
self.assertEqual(wrapped.__getitem__(0), 23)
-class TestContextWrapperFactory(unittest.TestCase):
- pass
-
-# XXX commented this test out because of Jim's change in
-# src/zope/proxy/context/__init__.py
+class LoggingDescriptor:
+ def __init__(self, getval=None, doc=''):
+ self.getval = getval
+ self.doc = doc
+ self.log = []
+
+ def __get__(self, inst, cls=None):
+ self.log.append(('get', inst, cls))
+ return self.getval
+
+ def getDoc(self):
+ return self.doc
+ __doc__ = property(getDoc)
+
+ def checkClear(self):
+ l = self.log
+ self.log = []
+ return l
+
+class LoggingDataDescriptor(LoggingDescriptor):
+
+ def __set__(self, inst, value):
+ self.log.append(('set', inst, value))
+
+ def __delete__(self, inst):
+ self.log.append(('del', inst))
+
+class TestContextAwareDescriptor(unittest.TestCase):
+
+ def newInstance(self, value):
+ from zope.context import ContextAwareDescriptor
+ return ContextAwareDescriptor(value)
-## 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)
+ def setUp(self):
+ self.d = LoggingDataDescriptor(23, 'my doc')
+ self.cd = self.newInstance(self.d)
-## # ok if there are no ContextDescriptors
-## w = ContextWrapper(Classic(), None)
+ def test_is_ContextDescriptor(self):
+ from zope.context import ContextDescriptor
+ self.assert_(isinstance(self.cd, ContextDescriptor))
+
+ def test_descriptor(self):
+ inst = object() # any old object
+ cls = object # any old type
+ cd = self.cd
+ d = self.d
+
+ self.assertEquals(cd.__get__(inst, cls), 23)
+ self.assertEquals(d.checkClear(), [('get', inst, cls)])
+ self.assertEquals(cd.__doc__, d.__doc__)
+
+
+class TestContextAwareDataDescriptor(TestContextAwareDescriptor):
+
+ def newInstance(self, value):
+ from zope.context import ContextAwareDataDescriptor
+ return ContextAwareDataDescriptor(value)
+
+ def test_datadescriptor(self):
+ inst = object() # any old object
+ cls = object # any old type
+ cd = self.cd
+ d = self.d
+
+ cd.__set__(inst, 88)
+ self.assertEquals(d.checkClear(), [('set', inst, 88)])
+ cd.__delete__(inst)
+ self.assertEquals(d.checkClear(), [('del', inst)])
+
+
+class Test_contextAwareDescriptors(unittest.TestCase):
+
+ def test_advice(self):
+ from zope.context import ContextMethod, ContextProperty
+ from zope.context import ContextAwareDescriptors
+ from zope.interface import implements
+ normal_data_descriptor = LoggingDataDescriptor(23)
+ normal_descriptor = LoggingDescriptor(23)
+ context_method = ContextMethod(lambda s: None)
+ context_descriptor = ContextProperty()
+
+ # Check that nothing in particular happens if the class doesn't
+ # have the advice, but its superclass does.
+ class SuperX(object):
+
+ ndd = normal_data_descriptor
+ nd = normal_descriptor
+
+ class X(SuperX):
+ ContextAwareDescriptors()
+
+ self.assert_(X.ndd is normal_data_descriptor)
+ self.assert_(X.nd is normal_descriptor)
+
+ class X(object):
+ ContextAwareDescriptors()
+
+ cm = context_method
+ ndd = normal_data_descriptor
+ nd = normal_descriptor
+ cd = context_descriptor
+
+ self.assert_(X.cm is context_method)
+ self.assert_(X.cd is context_descriptor)
+ normal_descriptor.checkClear()
+ self.assertEquals(X.nd, 23)
+ self.assertEquals(normal_descriptor.checkClear(),
+ [('get', None, X)]
+ )
+ normal_data_descriptor.checkClear()
+ self.assertEquals(X.ndd, 23)
+ self.assertEquals(normal_data_descriptor.checkClear(),
+ [('get', None, X)]
+ )
+ X.ndd = 42
+ # We've established that our descriptor is behind the context
+ # descriptor. The rest is covered by the ContextAware(Data)Descriptor
+ # unit tests.
+
+ def test_implementsInSubclassInteraction(self):
+ from zope.interface import implements
+ from zope.context import ContextAwareDescriptors
+
+ class Bar:
+ implements()
+ ContextAwareDescriptors()
+
+ # If __providedBy__ is made context-aware, this will fail with a
+ # TypeError.
+ # If __implements__ is made context-aware, this will fail with a
+ # BadImplements error.
+ class Foo(Bar):
+ implements()
-## # raises if there is a ContextDescriptor
-## self.assertRaises(TypeError, ContextWrapper, BadClassic(), None)
def test_suite():
return unittest.TestSuite((
@@ -504,7 +613,9 @@
unittest.makeSuite(TestNewStyleClassWithSlots),
unittest.makeSuite(TestClassicClass),
unittest.makeSuite(TestWrapperOnObjectsWithDifferentSlots),
- unittest.makeSuite(TestContextWrapperFactory),
+ unittest.makeSuite(TestContextAwareDataDescriptor),
+ unittest.makeSuite(TestContextAwareDescriptor),
+ unittest.makeSuite(Test_contextAwareDescriptors),
))
=== Removed File Zope3/src/zope/context/tests/test_ContextAware.py ===
=== Removed File Zope3/src/zope/context/tests/test_containmentiterator.py ===
=== Removed File Zope3/src/zope/context/tests/test_decorator.py ===