[Checkins] SVN: zope.component/branches/sylvain-zcmltestlayer/src/zope/component/test Add test for ZCMLFileLayer.

Martijn Faassen faassen at startifact.com
Thu Jan 21 10:42:41 EST 2010


Log message for revision 108360:
  Add test for ZCMLFileLayer.
  

Changed:
  A   zope.component/branches/sylvain-zcmltestlayer/src/zope/component/testfiles/testlayer.zcml
  U   zope.component/branches/sylvain-zcmltestlayer/src/zope/component/testlayer.py
  U   zope.component/branches/sylvain-zcmltestlayer/src/zope/component/testlayer.txt

-=-
Added: zope.component/branches/sylvain-zcmltestlayer/src/zope/component/testfiles/testlayer.zcml
===================================================================
--- zope.component/branches/sylvain-zcmltestlayer/src/zope/component/testfiles/testlayer.zcml	                        (rev 0)
+++ zope.component/branches/sylvain-zcmltestlayer/src/zope/component/testfiles/testlayer.zcml	2010-01-21 15:42:41 UTC (rev 108360)
@@ -0,0 +1,9 @@
+<configure xmlns="http://namespaces.zope.org/zope">
+  <include package="zope.component" file="meta.zcml" />
+
+  <adapter 
+    factory="zope.component.testfiles.components.Comp"
+    provides="zope.component.testfiles.components.IApp"
+    for="zope.component.testfiles.components.IContent" />
+
+</configure>

Modified: zope.component/branches/sylvain-zcmltestlayer/src/zope/component/testlayer.py
===================================================================
--- zope.component/branches/sylvain-zcmltestlayer/src/zope/component/testlayer.py	2010-01-21 15:22:44 UTC (rev 108359)
+++ zope.component/branches/sylvain-zcmltestlayer/src/zope/component/testlayer.py	2010-01-21 15:42:41 UTC (rev 108360)
@@ -79,7 +79,7 @@
         xmlconfig.registerCommonDirectives(context)
         for feature in self.features:
             context.provideFeature(feature)
-        self._load_zcml(context)
+        self.context = self._load_zcml(context)
 
     def tearDown(self):
         cleanUp()
@@ -95,13 +95,13 @@
     based on another ZCMLLayer's ZCML, just use a ZCML include
     statement in your own ZCML to load it.
     """
-    def __init__(self, package, name=None, zcml_string=None,
-                 features=None):
+    def __init__(self, package, zcml_string=None,
+                 name=None, features=None):
         super(ZCMLStringLayer, self).__init__(package, name, features)
         self.zcml_string = zcml_string
 
     def _load_zcml(self, context):
-        xmlconfig.string(self.zcml_string, context=context, execute=True)
+        return xmlconfig.string(self.zcml_string, context=context, execute=True)
 
 class ZCMLFileLayer(ZCMLLayerBase):
     """This layer can be used to run tests with a ZCML file loaded.
@@ -111,11 +111,11 @@
     based on another ZCMLLayer's ZCML, just use a ZCML include
     statement in your own ZCML to load it.
     """
-    def __init__(self, package, name=None, zcml_file='ftesting.zcml',
-                 features=None):
+    def __init__(self, package, zcml_file='ftesting.zcml',
+                 name=None, features=None):
         super(ZCMLFileLayer, self).__init__(package, name, features)
         self.zcml_file = os.path.join(os.path.dirname(package.__file__),
                                       zcml_file)
 
     def _load_zcml(self, context):
-        xmlconfig.file(self.zcml_file, context=context, execute=True)
+        return xmlconfig.file(self.zcml_file, context=context, execute=True)

Modified: zope.component/branches/sylvain-zcmltestlayer/src/zope/component/testlayer.txt
===================================================================
--- zope.component/branches/sylvain-zcmltestlayer/src/zope/component/testlayer.txt	2010-01-21 15:22:44 UTC (rev 108359)
+++ zope.component/branches/sylvain-zcmltestlayer/src/zope/component/testlayer.txt	2010-01-21 15:42:41 UTC (rev 108360)
@@ -13,7 +13,7 @@
 ---------
 
 We check whether our LayerBase can be used to create layers of our
-own. We do this simply by subclassing::
+own. We do this simply by subclassing:
 
   >>> from zope.component.testlayer import LayerBase
   >>> class OurLayer(LayerBase):
@@ -41,7 +41,7 @@
   >>> import zope.component
   >>> layer = OurLayer(zope.component)
 
-Now we run some tests with this layer::
+Now we run some tests with this layer:
 
   >>> import unittest
   >>> class TestCase(unittest.TestCase):
@@ -68,3 +68,32 @@
 ZCMLLayer
 ---------
 
+We now want a layer that loads up some ZCML from a file. The default
+is ``ftesting.zcml``, but here we'll load a test ``testlayer.zcml``.
+
+  >>> from zope.component.testlayer import ZCMLFileLayer
+  >>> zcml_file_layer = ZCMLFileLayer(
+  ...     zope.component.testfiles,
+  ...     'testlayer.zcml')
+
+  >>> class TestCase(unittest.TestCase):
+  ...    layer = zcml_file_layer
+  ...    
+  ...    def testFoo(self):
+  ...        # we should now have the adapter registered
+  ...        from zope import component
+  ...        from zope.component.testfiles import components
+  ...        self.assert_(isinstance(
+  ...            components.IApp(components.comp), components.Comp))
+
+Since the ZCML sets up an adapter, we expect the tests to pass::
+
+  >>> suite = unittest.TestSuite()
+  >>> suite.addTest(unittest.makeSuite(TestCase))
+  >>> runner = Runner(found_suites=[suite])
+  >>> succeeded = runner.run()
+  Running zope.component.testfiles.ZCMLFileLayer tests:
+    Set up zope.component.testfiles.ZCMLFileLayer in ... seconds.
+    Ran 1 tests with 0 failures and 0 errors in ... seconds.
+  Tearing down left over layers:
+    Tear down zope.component.testfiles.ZCMLFileLayer in ... seconds.



More information about the checkins mailing list