[Zope3-checkins] CVS: Zope3/lib/python/Zope/App/schemagen/tests - test_modulegen.py:1.1
Martijn Faassen
m.faassen@vet.uu.nl
Wed, 11 Dec 2002 14:07:23 -0500
Update of /cvs-repository/Zope3/lib/python/Zope/App/schemagen/tests
In directory cvs.zope.org:/tmp/cvs-serv11175/tests
Added Files:
test_modulegen.py
Log Message:
Added code that can generate module source from field information
automatically. The generated source contains an interface based on the
provided fields, as well as a class implementing that schema using
FieldProperty.
=== Added File Zope3/lib/python/Zope/App/schemagen/tests/test_modulegen.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.
#
##############################################################################
"""XXX short summary goes here.
XXX longer description goes here.
$Id: test_modulegen.py,v 1.1 2002/12/11 19:07:23 faassen Exp $
"""
from unittest import TestCase, makeSuite, TestSuite
from Interface import Interface
from Zope.Schema import Text, Int, Float, getFields
from Zope.App.schemagen.modulegen import generateModuleSource
class GenerateModuleSourceTestsBase(TestCase):
fields = []
def test_generateModuleSource(self):
source = generateModuleSource('IFoo', self.fields, "Foo")
g = {}
exec source in g
del g['__builtins__'] # to ease inspection during debugging
IFoo = g['IFoo']
fieldsorter = lambda x, y: cmp(x[1].order, y[1].order)
new_fields = getFields(IFoo).items()
new_fields.sort(fieldsorter)
self.assertEquals(self.fields, new_fields)
# XXX we'd like to test the whole roundtrip eventually,
# by execing generated module source again and then generating
# module source for the schema in that. This requires the arguments
# to fields (their properties) to be in their own schema order.
class GenerateModuleSourceTestsEmpty(GenerateModuleSourceTestsBase):
fields = []
class GenerateModuleSourceTests1(GenerateModuleSourceTestsBase):
fields = [('foo', Text(title=u"Foo")),
('bar', Int(title=u"Bar")),
('hoi', Float(title=u"Float"))]
def test_suite():
return TestSuite(
(makeSuite(GenerateModuleSourceTestsEmpty),
makeSuite(GenerateModuleSourceTests1)
))