[Checkins] SVN: zope.component/tseaver-test_cleanup/ Document / test 'getAllUtilitiesRegisteredFor' API.
Tres Seaver
cvs-admin at zope.org
Sun Jun 17 21:37:08 UTC 2012
Log message for revision 126906:
Document / test 'getAllUtilitiesRegisteredFor' API.
Changed:
_U zope.component/tseaver-test_cleanup/
U zope.component/tseaver-test_cleanup/docs/api.rst
U zope.component/tseaver-test_cleanup/src/zope/component/tests/test___init__.py
U zope.component/tseaver-test_cleanup/src/zope/component/tests/test_doctests.py
-=-
Modified: zope.component/tseaver-test_cleanup/docs/api.rst
===================================================================
--- zope.component/tseaver-test_cleanup/docs/api.rst 2012-06-17 21:37:00 UTC (rev 126905)
+++ zope.component/tseaver-test_cleanup/docs/api.rst 2012-06-17 21:37:04 UTC (rev 126906)
@@ -190,6 +190,56 @@
>>> ('foo', ob2) in tuples
True
+The :function:`~zope.component.getAllUtilitiesRegisteredFor` API returns
+utilities that have been registered for a particular interface. Utilities
+providing a derived interface are also listed.
+
+.. doctest::
+
+ >>> from zope.interface import implementer
+ >>> from zope.component.tests.test_doctests import Comp
+ >>> from zope.component.tests.test_doctests import I2
+ >>> from zope.component.tests.test_doctests import Ob
+ >>> class I11(I1):
+ ... pass
+
+ >>> @implementer(I11)
+ ... class Ob11(Ob):
+ ... pass
+
+ >>> ob11 = Ob11()
+ >>> ob_bob = Ob()
+
+Now we register the new utilities:
+
+.. doctest::
+
+ >>> gsm.registerUtility(ob, I1)
+ >>> gsm.registerUtility(ob11, I11)
+ >>> gsm.registerUtility(ob_bob, I1, name='bob')
+ >>> gsm.registerUtility(Comp(2), I2)
+
+We can now get all the utilities that provide interface `I1`:
+
+.. doctest::
+
+ >>> from zope.component import getAllUtilitiesRegisteredFor
+ >>> uts = list(getAllUtilitiesRegisteredFor(I1))
+ >>> len(uts)
+ 4
+ >>> ob in uts
+ True
+ >>> ob2 in uts
+ True
+ >>> ob_bob in uts
+ True
+ >>> ob11 in uts
+ True
+
+Note that `getAllUtilitiesRegisteredFor()` does not return the names of
+the utilities.
+
+
Delegated Utility Lookup
########################
Modified: zope.component/tseaver-test_cleanup/src/zope/component/tests/test___init__.py
===================================================================
--- zope.component/tseaver-test_cleanup/src/zope/component/tests/test___init__.py 2012-06-17 21:37:00 UTC (rev 126905)
+++ zope.component/tseaver-test_cleanup/src/zope/component/tests/test___init__.py 2012-06-17 21:37:04 UTC (rev 126906)
@@ -213,6 +213,33 @@
self.assertTrue(('', obj) in tuples)
self.assertTrue(('bar', obj1) in tuples)
+ def test_getAllUtilitiesRegisteredFor_nonesuch(self):
+ from zope.interface import Interface
+ from zope.component import getAllUtilitiesRegisteredFor
+ class IFoo(Interface):
+ pass
+ self.assertEqual(list(getAllUtilitiesRegisteredFor(IFoo)), [])
+
+ def test_getAllUtilitiesRegisteredFor_hit(self):
+ from zope.interface import Interface
+ from zope.component import getGlobalSiteManager
+ from zope.component import getAllUtilitiesRegisteredFor
+ class IFoo(Interface):
+ pass
+ class IBar(IFoo):
+ pass
+ obj = object()
+ obj1 = object()
+ obj2 = object()
+ getGlobalSiteManager().registerUtility(obj, IFoo)
+ getGlobalSiteManager().registerUtility(obj1, IFoo, name='bar')
+ getGlobalSiteManager().registerUtility(obj2, IBar)
+ uts = list(getAllUtilitiesRegisteredFor(IFoo))
+ self.assertEqual(len(uts), 3)
+ self.assertTrue(obj in uts)
+ self.assertTrue(obj1 in uts)
+ self.assertTrue(obj2 in uts)
+
def test_getNextUtility_global(self):
from zope.component import getGlobalSiteManager
from zope.component import getNextUtility
Modified: zope.component/tseaver-test_cleanup/src/zope/component/tests/test_doctests.py
===================================================================
--- zope.component/tseaver-test_cleanup/src/zope/component/tests/test_doctests.py 2012-06-17 21:37:00 UTC (rev 126905)
+++ zope.component/tseaver-test_cleanup/src/zope/component/tests/test_doctests.py 2012-06-17 21:37:04 UTC (rev 126906)
@@ -492,44 +492,6 @@
>>> tearDown()
"""
-def test_getAllUtilitiesRegisteredFor():
- """Again, like for adapters, it is often useful to get a list of all
- utilities that have been registered for a particular interface. Utilities
- providing a derived interface are also listed.
-
- Thus, let's create a derivative interface of `I1`:
-
- >>> from zope.component.testing import setUp, tearDown
- >>> setUp()
- >>> class I11(I1):
- ... pass
-
- >>> class Ob11(Ob):
- ... interface.implements(I11)
-
- >>> ob11 = Ob11()
- >>> ob_bob = Ob()
-
- Now we register the new utilities:
-
- >>> gsm = component.getGlobalSiteManager()
- >>> gsm.registerUtility(ob, I1)
- >>> gsm.registerUtility(ob11, I11)
- >>> gsm.registerUtility(ob_bob, I1, name='bob')
- >>> gsm.registerUtility(Comp(2), I2)
-
- We can now get all the utilities that provide interface `I1`:
-
- >>> uts = list(component.getAllUtilitiesRegisteredFor(I1))
- >>> uts = sorted([util.__class__.__name__ for util in uts])
- >>> uts
- ['Ob', 'Ob', 'Ob11']
- >>> tearDown()
-
- Note that `getAllUtilitiesRegisteredFor()` does not return the names of
- the utilities.
- """
-
def testNotBrokenWhenNoSiteManager():
"""Make sure that the adapter lookup is not broken, when no site manager
is available.
More information about the checkins
mailing list