[Zope3-checkins] CVS: Zope3/src/zope/proxy/context/tests - test_ContextAware.py:1.1

Jim Fulton jim@zope.com
Sat, 25 Jan 2003 10:32:54 -0500


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

Added Files:
	test_ContextAware.py 
Log Message:
Added ContextAware (zope.proxy.context.ContextAware) base (marker)
class.

All methods (*) and properties of context aware classes are context
aware, meaning that they are bound to context-wrapped instances.

(*) Currently this doesn't apply to operators (__foo__) methods except
__call__ and __getitem__.


=== Added File Zope3/src/zope/proxy/context/tests/test_ContextAware.py ===
##############################################################################
#
# 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.
#
##############################################################################
"""XXX short summary goes here.

XXX longer description goes here.

$Id: test_ContextAware.py,v 1.1 2003/01/25 15:32:52 jim Exp $
"""

from unittest import TestCase, TestSuite, main, makeSuite
from zope.proxy.context import ContextAware, ContextWrapper
from zope.proxy.context import getWrapperContainer

def setter(self, v):
    self.v = getWrapperContainer(self), v

class B(object):

    p1B = property(getWrapperContainer)

    p2B = property(getWrapperContainer,
                   setter)

    def fB(self):
        return getWrapperContainer(self)

class C(B, ContextAware):

    p1C = property(getWrapperContainer)

    p2C = property(getWrapperContainer,
                   setter)

    def fC(self):
        return getWrapperContainer(self)
        

class Test(TestCase):

    def test(self):

        # Reality check no context case:
        b = B()
        self.assertEqual(b.p1B, None)
        self.assertEqual(b.p2B, None)
        self.assertEqual(b.fB(), None)
        b.p2B = 1
        self.assertEqual(b.v, (None, 1))

        # Reality check no wrapper case:
        b = C()
        self.assertEqual(b.p1B, None)
        self.assertEqual(b.p2B, None)
        self.assertEqual(b.fB(), None)
        b.p2B = 1
        self.assertEqual(b.v, (None, 1))

        self.assertEqual(b.p1C, None)
        self.assertEqual(b.p2C, None)
        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)
        self.assertEqual(b.p2B, 42)
        self.assertEqual(b.fB(), 42)
        b.p2B = 1
        self.assertEqual(b.v, (42, 1))

        self.assertEqual(b.p1C, 42)
        self.assertEqual(b.p2C, 42)
        self.assertEqual(b.fC(), 42)
        b.p2C = 2
        self.assertEqual(b.v, (42, 2))

def test_suite():
    return TestSuite((
        makeSuite(Test),
        ))

if __name__=='__main__':
    main(defaultTest='test_suite')