[Checkins] SVN: zope.component/trunk/src/zope/component/ Move testingNextUtility from zope.app.component.testing.

Dan Korostelev nadako at gmail.com
Wed Mar 11 20:01:37 EDT 2009


Log message for revision 97939:
  Move testingNextUtility from zope.app.component.testing.

Changed:
  A   zope.component/trunk/src/zope/component/nexttesting.py
  U   zope.component/trunk/src/zope/component/tests.py

-=-
Added: zope.component/trunk/src/zope/component/nexttesting.py
===================================================================
--- zope.component/trunk/src/zope/component/nexttesting.py	                        (rev 0)
+++ zope.component/trunk/src/zope/component/nexttesting.py	2009-03-12 00:01:36 UTC (rev 97939)
@@ -0,0 +1,106 @@
+##############################################################################
+#
+# Copyright (c) 2009 Zope Corporation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (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.
+#
+##############################################################################
+"""Helper functions for testing utilities that use get/queryNextUtility.
+
+$Id$
+"""
+import zope.interface
+from zope.component.interfaces import IComponentLookup, IComponents
+
+
+class SiteManagerStub(object):
+    zope.interface.implements(IComponents)
+
+    __bases__ = ()
+
+    def __init__(self):
+        self._utils = {}
+
+    def setNext(self, next):
+        self.__bases__ = (next, )
+
+    def provideUtility(self, iface, util, name=''):
+        self._utils[(iface, name)] = util
+
+    def queryUtility(self, iface, name='', default=None):
+        return self._utils.get((iface, name), default)
+
+
+def testingNextUtility(utility, nextutility, interface, name='',
+                       sitemanager=None, nextsitemanager=None):
+    """Provide a next utility for testing.
+
+    This function sets up two utilities, so the get/queryNextUtility functions
+    will see the second one as the "next" to the first one. 
+
+    To test it, we need to create a utility interface and implementation:
+
+      >>> from zope.interface import Interface, implements
+      >>> class IAnyUtility(Interface):
+      ...     pass
+      
+      >>> class AnyUtility(object):
+      ...     implements(IAnyUtility)
+      ...     def __init__(self, id):
+      ...         self.id = id
+      
+      >>> any1 = AnyUtility(1)
+      >>> any1next = AnyUtility(2)
+
+    Now, we can make the "any1next" be next to "any1". 
+
+      >>> testingNextUtility(any1, any1next, IAnyUtility)
+      
+      >>> from zope.component import getNextUtility
+      >>> getNextUtility(any1, IAnyUtility) is any1next
+      True
+
+    It will work for named utilities as well.
+
+      >>> testingNextUtility(any1, any1next, IAnyUtility, 'any')
+      >>> getNextUtility(any1, IAnyUtility, 'any') is any1next
+      True
+
+    We can also provide our custom component registries:
+    
+      >>> sm = SiteManagerStub()
+      >>> nextsm = SiteManagerStub()
+      
+      >>> testingNextUtility(any1, any1next, IAnyUtility,
+      ...     sitemanager=sm, nextsitemanager=nextsm)
+    
+      >>> IComponentLookup(any1) is sm
+      True
+      >>> IComponentLookup(any1next) is nextsm
+      True
+      >>> getNextUtility(any1, IAnyUtility) is any1next
+      True
+    
+    """
+    if sitemanager is None:
+        sitemanager = SiteManagerStub()
+    if nextsitemanager is None:
+        nextsitemanager = SiteManagerStub()
+    sitemanager.setNext(nextsitemanager)
+
+    sitemanager.provideUtility(interface, utility, name)
+    utility.__conform__ = (
+        lambda iface:
+        iface.isOrExtends(IComponentLookup) and sitemanager or None
+        )
+    nextsitemanager.provideUtility(interface, nextutility, name)
+    nextutility.__conform__ = (
+        lambda iface:
+        iface.isOrExtends(IComponentLookup) and nextsitemanager or None
+        )


Property changes on: zope.component/trunk/src/zope/component/nexttesting.py
___________________________________________________________________
Added: svn:keywords
   + Id

Modified: zope.component/trunk/src/zope/component/tests.py
===================================================================
--- zope.component/trunk/src/zope/component/tests.py	2009-03-11 23:38:56 UTC (rev 97938)
+++ zope.component/trunk/src/zope/component/tests.py	2009-03-12 00:01:36 UTC (rev 97939)
@@ -1215,6 +1215,7 @@
         doctest.DocTestSuite(setUp=setUp, tearDown=tearDown),
         doctest.DocTestSuite('zope.component.interface',
                              setUp=setUp, tearDown=tearDown),
+        doctest.DocTestSuite('zope.component.nexttesting'),
         doctest.DocFileSuite('README.txt',
                              setUp=setUp, tearDown=tearDown),
         doctest.DocFileSuite('socketexample.txt',



More information about the Checkins mailing list