[Zope3-checkins] CVS: Zope3/src/zope/schema/tests - test_accessors.py:1.1

Jim Fulton jim@zope.com
Mon, 14 Apr 2003 04:21:18 -0400


Update of /cvs-repository/Zope3/src/zope/schema/tests
In directory cvs.zope.org:/tmp/cvs-serv4964/tests

Added Files:
	test_accessors.py 
Log Message:
Added support for fields implemented by accessor functions rather than
attributes. See the doc string for accessors.py.

To do this, I added three new field methods: ``get``, ``query``, and
``set``, which are used for getting and setting data defined by
fields. These methods should be used by infrastructure software rather
than assuming that field implementations are accessed as attributes.





=== Added File Zope3/src/zope/schema/tests/test_accessors.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_accessors.py,v 1.1 2003/04/14 08:21:17 jim Exp $
"""

import unittest
from zope.interface import Interface
from zope.schema import Text, accessors
from zope.schema.interfaces import IText
from zope.schema.accessors import FieldReadAccessor, FieldWriteAccessor
from zope.interface.verify import verifyClass, verifyObject
from zope.interface import document

class Test(unittest.TestCase):

    def test(self):

        field = Text(title=u"Foo thing")

        class I(Interface):

            getFoo, setFoo = accessors(field)

        class Bad:
            __implements__ = I

        class Good:
            __implements__ = I

            def getFoo(self):
                return u"foo"

            def setFoo(self, v):
                pass

        names = I.names()
        names.sort()
        self.assertEqual(names, ['getFoo', 'setFoo'])
        self.assertEqual(I['getFoo'].field, field)
        self.assertEqual(I['getFoo'].__name__, 'getFoo')
        self.assertEqual(I['getFoo'].__doc__, u'get Foo thing')
        self.assertEqual(I['getFoo'].__class__, FieldReadAccessor)
        self.assertEqual(I['getFoo'].writer, I['setFoo'])

        # test some field attrs
        for attr in ('title', 'description', 'readonly'):
            self.assertEqual(getattr(I['getFoo'], attr), getattr(field, attr))

        self.assert_(IText.isImplementedBy(I['getFoo']))
        
        self.assertEqual(I['setFoo'].field, field)
        self.assertEqual(I['setFoo'].__name__, 'setFoo')
        self.assertEqual(I['setFoo'].__doc__, u'set Foo thing')
        self.assertEqual(I['setFoo'].__class__, FieldWriteAccessor)

        self.assertRaises(Exception, verifyClass, I, Bad)
        self.assertRaises(Exception, verifyObject, I, Bad())
        
        verifyClass(I, Good)
        verifyObject(I, Good())

    def test_doc(self):

        field = Text(title=u"Foo thing")

        class I(Interface):

            getFoo, setFoo = accessors(field)
            def bar(): pass
            x = Text()

        d = document.asStructuredText(I)
        self.assertEqual(d,
                         "I\n"
                         "\n"
                         " Attributes:\n"
                         "\n"
                         "  x -- no documentation\n"
                         "\n"
                         " Methods:\n"
                         "\n"
                         "  bar() -- no documentation\n"
                         "\n"
                         "  getFoo() -- get Foo thing\n"
                         "\n"
                         "  setFoo(newvalue) -- set Foo thing\n"
                         "\n"
                         )


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


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