[Zope-Checkins] CVS: Zope3/lib/python/Zope/Misc/tests - testGetDescr.py:1.1.2.1

Guido van Rossum guido@python.org
Wed, 20 Mar 2002 16:13:26 -0500


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

Added Files:
      Tag: Zope-3x-branch
	testGetDescr.py 
Log Message:
Utility to get Descriptor, written at Jim's request.

=== Added File Zope3/lib/python/Zope/Misc/tests/testGetDescr.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
# 
##############################################################################
"""
Test GetDescr.

Revision information:
$Id: testGetDescr.py,v 1.1.2.1 2002/03/20 21:13:25 gvanrossum Exp $
"""

import unittest
from Zope.Misc.GetDescr import GetDescr

class TestGetDescr(unittest.TestCase):

    def test_errors(self):
        class C: pass
        # obj not a new-style instance
        self.assertRaises(TypeError, GetDescr, C(), "foo")
        # name not a string
        self.assertRaises(TypeError, GetDescr, 0, 0)

    def test_simple(self):
        # Simple cases
        class C(object):
            def foo(self): pass
        c = C()
        self.assertEqual(GetDescr(c, "foo"), C.__dict__["foo"])
        self.assertEqual(GetDescr(c, "bar"), None)
        c.bar = 12
        self.assertEqual(GetDescr(c, "bar"), None)
        c.foo = 12
        self.assertEqual(GetDescr(c, "foo"), None)
        # Make sure method overrides overrid
        class D(C):
            def foo(self): pass
        d = D()
        self.assertEqual(GetDescr(d, "foo"), D.__dict__["foo"])
        # Make sure properties always win
        class E(C):
            foo = property(lambda self: 42, lambda self, value: None)
        e = E()
        self.assertEqual(GetDescr(e, "foo"), E.__dict__["foo"])
        e.foo = 12 # Ignored
        self.assertEqual(e.foo, 42)
        self.assertEqual(GetDescr(e, "foo"), E.__dict__["foo"])
        e.__dict__["foo"] = 23 # Still ignored!
        self.assertEqual(e.foo, 42)
        self.assertEqual(GetDescr(e, "foo"), E.__dict__["foo"])

def test_suite():
    return unittest.makeSuite(TestGetDescr)

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