[Zope-Checkins] CVS: Zope3/lib/python/Zope/ContextWrapper/tests - testSimpleMethodWrapper.py:1.1.2.1

Steve Alexander steve@cat-box.net
Wed, 17 Apr 2002 18:33:01 -0400


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

Added Files:
      Tag: Zope-3x-branch
	testSimpleMethodWrapper.py 
Log Message:
Amended various modules to import Wrapper, getdict and getcontext from
Zope.ContextWrapper rather than Zope.ContextWrapper.wrapper

Added a SimpleMethodWrapper, which is a rather hacky python way of
rebinding methods so that the im_self gets wrapped.

Now, you'll get a SimpleMethodWrapper rather than a Wrapper when you
say from Zope.ContextWrapper import Wrapper

You say that a particular method should get its im_self rebound by
making it into a ContextMethod, in a similar way to making a staticmethod
or a classmethod.

See the testSimpleMethodWrapper.py module for an example.



=== Added File Zope3/lib/python/Zope/ContextWrapper/tests/testSimpleMethodWrapper.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 sys
import unittest

from Zope.ContextWrapper import Wrapper
from Zope.ContextWrapper import ContextMethod


class NewStyleClass(object):
    def thisIsAContextMethod(self):
        assert isinstance(self, Wrapper)

    thisIsAContextMethod = ContextMethod(thisIsAContextMethod)
    
    def thisIsNotAContextMethod(self):
        assert not isinstance(self, Wrapper)
    
       
class ClassicClass:
    def thisIsAContextMethod(self):
        assert isinstance(self, Wrapper)

    thisIsAContextMethod = ContextMethod(thisIsAContextMethod)

    def thisIsNotAContextMethod(self):
        assert not isinstance(self, Wrapper)


class SimpleMethodWrapperTestCase(unittest.TestCase):
    def testNewStyleClass(self):
        newstyle = NewStyleClass()
        wrapped = Wrapper(newstyle)
        wrapped.thisIsAContextMethod()
        wrapped.thisIsNotAContextMethod()
        self.assertRaises(AssertionError, newstyle.thisIsAContextMethod)
        
    def testClassicClass(self):
        classic = ClassicClass()
        wrapped = Wrapper(classic)
        wrapped.thisIsAContextMethod()
        wrapped.thisIsNotAContextMethod()
        self.assertRaises(AssertionError, classic.thisIsAContextMethod)
        


def test_suite():
    suite = unittest.TestSuite()
    suite.addTest(unittest.makeSuite(SimpleMethodWrapperTestCase))
    return suite


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)