[Zope3-checkins] CVS: Zope3/lib/python/Zope/App/schemagen/tests - test_modulegen.py:1.5

Martijn Faassen m.faassen@vet.uu.nl
Thu, 12 Dec 2002 12:40:26 -0500


Update of /cvs-repository/Zope3/lib/python/Zope/App/schemagen/tests
In directory cvs.zope.org:/tmp/cvs-serv11012/tests

Modified Files:
	test_modulegen.py 
Log Message:
Added ability to supply extra imports for the generated module. Also
add ability to supply extra methods for the generated class.


=== Zope3/lib/python/Zope/App/schemagen/tests/test_modulegen.py 1.4 => 1.5 ===
--- Zope3/lib/python/Zope/App/schemagen/tests/test_modulegen.py:1.4	Thu Dec 12 07:14:52 2002
+++ Zope3/lib/python/Zope/App/schemagen/tests/test_modulegen.py	Thu Dec 12 12:40:26 2002
@@ -11,10 +11,7 @@
 # FOR A PARTICULAR PURPOSE.
 #
 ##############################################################################
-"""XXX short summary goes here.
-
-XXX longer description goes here.
-
+"""
 $Id$
 """
 
@@ -67,7 +64,7 @@
         self.assertEquals(foo.__schema_version__, 0)
         for field_name, field in self.fields:
             self.assertEquals(field.default, getattr(foo, field_name))
-        
+
 class GenerateModuleSourceTestsEmpty(GenerateModuleSourceTestsBase):
     fields = []
 
@@ -77,9 +74,40 @@
               ('hoi', Float(title=u"Float")),
               ('dag', Int(title=u"Dag", default=42)),]
 
+class ExtraImportsAndMethodsTests(TestCase):
+    fields = [('foo', Text(title=u"Foo")),
+              ('bar', Int(title=u"Bar")),
+              ('hoi', Float(title=u"Float")),
+              ('dag', Int(title=u"Dag", default=42)),]
+    
+    def test_extraMethods(self):
+        extra_methods = """\
+    def forGreatJustice(self):
+        return 'zig!'
+"""
+        source = generateModuleSource('IFoo', self.fields, "Foo",
+                                      extra_methods=extra_methods)
+        g = {}
+        exec source in g
+        del g['__builtins__'] # to ease inspection during debugging
+        foo = g['Foo']()
+        self.assertEquals('zig!', foo.forGreatJustice())
+
+    def test_extraImports(self):
+        # we import ourselves, as then there's no dependencies
+        from Zope.App.schemagen.tests import test_modulegen
+        extra_imports = "from Zope.App.schemagen.tests import test_modulegen"
+        source = generateModuleSource('IFoo', self.fields, "Foo",
+                                      extra_imports=extra_imports)
+        g = {}
+        exec source in g
+        del g['__builtins__'] # to ease inspection during debugging
+        self.assert_(g['test_modulegen'] is test_modulegen)
+        
 def test_suite():
     return TestSuite(
         (makeSuite(GenerateModuleSourceTestsEmpty),
          makeSuite(GenerateModuleSourceTests1),
+         makeSuite(ExtraImportsAndMethodsTests),
          ))