[Zope-CVS] CVS: Packages/pypes/pypes/tests - test_util.py:1.1

Casey Duncan casey at zope.com
Tue Apr 27 23:24:34 EDT 2004


Update of /cvs-repository/Packages/pypes/pypes/tests
In directory cvs.zope.org:/tmp/cvs-serv17651/tests

Added Files:
	test_util.py 
Log Message:
Add missing util module and tests
Use inspect.getmro() in place of home-grown function


=== Added File Packages/pypes/pypes/tests/test_util.py ===
##############################################################################
#
# Copyright (c) 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.
#
##############################################################################
"""Pypes utility tests

$Id: test_util.py,v 1.1 2004/04/28 03:24:33 caseman Exp $"""

import unittest
from pypes.tests.common import sort

class ClassicBase:
    pass

class ClassicSubclass1(ClassicBase):
    pass

class ClassicSubclass2(ClassicSubclass1):
    pass

class ClassicMixin1:
    pass

class ClassicMixin2(ClassicBase):
    pass

class ClassicDerived(ClassicMixin1, ClassicMixin2, ClassicSubclass1):
    pass

class ClassicComplex(ClassicSubclass2, ClassicDerived):
    pass
    
class NewBase(object):
    pass

class NewSubclass(NewBase):
    pass
    
class MixedSimple(ClassicBase, NewBase):
    pass

class MixedSubclass(NewSubclass, ClassicSubclass1):
    pass
    
class SwapOut:
    pass

class ClassKeyTest(unittest.TestCase):
    """Test class key functions"""
    
    def testKeyForClassWithClass(self):
        from pypes.util import classKey
        key1 = classKey(ClassicBase)
        self.failUnless(isinstance(key1, str))
        key2 = classKey(NewBase)
        self.failUnless(isinstance(key2, str))
        self.assertNotEqual(key1, key2)
    
    def testKeyForClassWithInstance(self):
        from pypes.util import classKey
        key1 = classKey(ClassicBase())
        self.failUnless(isinstance(key1, str))
        self.assertEqual(classKey(ClassicBase), key1)
        key2 = classKey(NewBase())
        self.failUnless(isinstance(key2, str))
        self.assertEqual(classKey(NewBase), key2)
        self.assertNotEqual(key1, key2)
        
    def testClassFromKey(self):
        from pypes.util import classKey, classFromKey
        key = classKey(ClassicBase)
        self.failUnless(classFromKey(key) is ClassicBase)
        key = classKey(NewBase)
        self.failUnless(classFromKey(key) is NewBase)
    
    def testClassFromKeyRaisesTypeError(self):
        from pypes.util import classFromKey
        self.assertRaises(TypeError, classFromKey, object)
        self.assertRaises(TypeError, classFromKey, object())
        self.assertRaises(TypeError, classFromKey, 321)
    
    def testClassFromKeyCatchesTypeChange(self):
        # Tests that classFromKey always returns a class or type
        # Although well behaved code should not contrive
        # keys other than using keyFromClass, changes to modules can
        # change a the type of the object refered from the key.
        from pypes.util import classKey, classFromKey
        global SwapOut
        cls = SwapOut
        key = classKey(SwapOut)
        SwapOut = 'I am not a class'
        self.assertRaises(ValueError, classFromKey, key)
    
    def testClassFromKeyCatchesDelClass(self):
        # Make sure classFromKey properly handles the case where the
        # class for the key no longer exists
        from pypes.util import classKey, classFromKey
        global SwapOut
        cls = SwapOut
        key = classKey(SwapOut)
        del SwapOut
        self.assertRaises(AttributeError, classFromKey, key)
        SwapOut = cls
                

if __name__ == '__main__':
    unittest.main()




More information about the Zope-CVS mailing list