[Checkins] SVN: zope.component/tseaver-test_cleanup/ Move getUtilty / queryUtility API doctests to Sphinx / unittests.

Tres Seaver cvs-admin at zope.org
Sun Jun 17 21:36:52 UTC 2012


Log message for revision 126902:
  Move getUtilty / queryUtility API doctests to Sphinx / unittests.

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:36:44 UTC (rev 126901)
+++ zope.component/tseaver-test_cleanup/docs/api.rst	2012-06-17 21:36:48 UTC (rev 126902)
@@ -2,6 +2,9 @@
 =========================
 
 
+Site Manager APIs
+-----------------
+
 .. automodule:: zope.component
 
    .. autofunction:: getGlobalSiteManager
@@ -91,8 +94,65 @@
         <InterfaceClass zope...interfaces.IComponentLookup>)
 
 
+Utility Registration APIs
+-------------------------
+
+Utilities are components that simply provide an interface. They are
+instantiated at the time or before they are registered. Here we test the
+simple query interface.
+
+Before we register any utility, there is no utility available, of
+course. The pure instatiation of an object does not make it a utility. If
+you do not specify a default, you get a `ComponentLookupError`.
+
+.. doctest::
+
+   >>> from zope.component import getUtility
+   >>> from zope.component import queryUtility
+   >>> from zope.component.testing import setUp, tearDown
+   >>> from zope.component.tests.test_doctests import I1
+   >>> setUp()
+   >>> getUtility(I1) #doctest: +NORMALIZE_WHITESPACE
+   Traceback (most recent call last):
+   ...
+   ComponentLookupError: \
+   (<InterfaceClass zope.component.tests.test_doctests.I1>, '')
+
+Otherwise, you get the default:
+
+.. doctest::
+
+   >>> queryUtility(I1, default='<default>')
+   '<default>'
+
+Now we declare `ob` to be the utility providing `I1`:
+
+.. doctest::
+
+   >>> ob = object()
+   >>> from zope.component import getGlobalSiteManager
+   >>> getGlobalSiteManager().registerUtility(ob, I1)
+
+Now the component is available:
+
+.. doctest::
+
+   >>> getUtility(I1) is ob
+   True
+   >>> queryUtility(I1) is ob
+   True
+   >>> tearDown()
+
+.. automodule:: zope.component
+
+   .. autofunction:: getUtility
+
+   .. autofunction:: queryUtility
+
+
+
 :mod:`zope.component.interfaces`
---------------------------------
+================================
 
 .. automodule:: zope.component.interfaces
 

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:36:44 UTC (rev 126901)
+++ zope.component/tseaver-test_cleanup/src/zope/component/tests/test___init__.py	2012-06-17 21:36:48 UTC (rev 126902)
@@ -16,6 +16,8 @@
 
 class PackageAPITests(unittest.TestCase):
 
+    from zope.component.testing import setUp, tearDown
+
     def test_module_conforms_to_IComponentArchitecture(self):
         from zope.interface.verify import verifyObject
         from zope.component.interfaces import IComponentArchitecture
@@ -31,39 +33,165 @@
     def test_getGlobalSiteManager(self):
         from zope.component.globalregistry import base
         from zope.component.interfaces import IComponentLookup
-        import zope.component as zc
-        gsm = zc.getGlobalSiteManager()
+        from zope.component import getGlobalSiteManager
+        gsm = getGlobalSiteManager()
         self.assertTrue(gsm is base)
         self.assertTrue(IComponentLookup.providedBy(gsm))
-        self.assertTrue(zc.getGlobalSiteManager() is gsm)
+        self.assertTrue(getGlobalSiteManager() is gsm)
 
     def test_getSiteManager_no_args(self):
         from zope.component.globalregistry import base
         from zope.component.interfaces import IComponentLookup
-        import zope.component as zc
-        sm = zc.getSiteManager()
+        from zope.component import getSiteManager
+        sm = getSiteManager()
         self.assertTrue(sm is base)
         self.assertTrue(IComponentLookup.providedBy(sm))
-        self.assertTrue(zc.getSiteManager() is sm)
+        self.assertTrue(getSiteManager() is sm)
 
     def test_getSiteManager_w_None(self):
-        import zope.component as zc
-        self.assertTrue(zc.getSiteManager(None) is zc.getSiteManager())
+        from zope.component import getSiteManager
+        self.assertTrue(getSiteManager(None) is getSiteManager())
 
     def test_getSiteManager_w_conforming_context(self):
-        import zope.component as zc
+        from zope.component import getSiteManager
         from zope.component.tests.test_doctests \
             import ConformsToIComponentLookup
         sitemanager = object()
         context = ConformsToIComponentLookup(sitemanager)
-        self.assertTrue(zc.getSiteManager(context) is sitemanager)
+        self.assertTrue(getSiteManager(context) is sitemanager)
 
     def test_getSiteManager_w_invalid_context(self):
-        import zope.component as zc
+        from zope.component import getSiteManager
         from zope.component.interfaces import ComponentLookupError
-        self.assertRaises(ComponentLookupError, zc.getSiteManager, object())
+        self.assertRaises(ComponentLookupError, getSiteManager, object())
 
+    def test_getUtilty_anonymous_nonesuch(self):
+        from zope.interface import Interface
+        from zope.component import getUtility
+        from zope.component.interfaces import ComponentLookupError
+        class IFoo(Interface):
+            pass
+        self.assertRaises(ComponentLookupError, getUtility, IFoo)
 
+    def test_getUtilty_named_nonesuch(self):
+        from zope.interface import Interface
+        from zope.component import getUtility
+        from zope.component.interfaces import ComponentLookupError
+        class IFoo(Interface):
+            pass
+        self.assertRaises(ComponentLookupError, getUtility, IFoo, name='bar')
+
+    def test_getUtilty_anonymous_hit(self):
+        from zope.interface import Interface
+        from zope.component import getGlobalSiteManager
+        from zope.component import getUtility
+        class IFoo(Interface):
+            pass
+        obj = object()
+        getGlobalSiteManager().registerUtility(obj, IFoo)
+        self.assertTrue(getUtility(IFoo) is obj)
+
+    def test_getUtilty_named_hit(self):
+        from zope.interface import Interface
+        from zope.component import getUtility
+        from zope.component import getGlobalSiteManager
+        class IFoo(Interface):
+            pass
+        obj = object()
+        getGlobalSiteManager().registerUtility(obj, IFoo, name='bar')
+        self.assertTrue(getUtility(IFoo, name='bar') is obj)
+
+    def test_getUtility_w_conforming_context(self):
+        from zope.interface import Interface
+        from zope.component import getGlobalSiteManager
+        from zope.component import getUtility
+        from zope.component.tests.test_doctests \
+            import ConformsToIComponentLookup
+        class SM(object):
+            def __init__(self, obj):
+                self._obj = obj
+            def queryUtility(self, interface, name, default):
+                return self._obj
+        class IFoo(Interface):
+            pass
+        obj1 = object()
+        obj2 = object()
+        sm = SM(obj2)
+        context = ConformsToIComponentLookup(sm)
+        getGlobalSiteManager().registerUtility(obj1, IFoo)
+        self.assertTrue(getUtility(IFoo, context=context) is obj2)
+
+    def test_queryUtilty_anonymous_nonesuch(self):
+        from zope.interface import Interface
+        from zope.component import queryUtility
+        class IFoo(Interface):
+            pass
+        self.assertEqual(queryUtility(IFoo), None)
+
+    def test_queryUtilty_anonymous_nonesuch_w_default(self):
+        from zope.interface import Interface
+        from zope.component import queryUtility
+        class IFoo(Interface):
+            pass
+        obj = object()
+        self.assertTrue(queryUtility(IFoo, default=obj) is obj)
+
+    def test_queryUtilty_named_nonesuch(self):
+        from zope.interface import Interface
+        from zope.component import queryUtility
+        class IFoo(Interface):
+            pass
+        self.assertEqual(queryUtility(IFoo, name='bar'), None)
+
+    def test_queryUtilty_named_nonesuch_w_default(self):
+        from zope.interface import Interface
+        from zope.component import queryUtility
+        class IFoo(Interface):
+            pass
+        obj = object()
+        self.assertTrue(queryUtility(IFoo, name='bar', default=obj) is obj)
+
+    def test_queryUtilty_anonymous_hit(self):
+        from zope.interface import Interface
+        from zope.component import getGlobalSiteManager
+        from zope.component import queryUtility
+        class IFoo(Interface):
+            pass
+        obj = object()
+        getGlobalSiteManager().registerUtility(obj, IFoo)
+        self.assertTrue(queryUtility(IFoo) is obj)
+
+    def test_queryUtilty_named_hit(self):
+        from zope.interface import Interface
+        from zope.component import queryUtility
+        from zope.component import getGlobalSiteManager
+        class IFoo(Interface):
+            pass
+        obj = object()
+        getGlobalSiteManager().registerUtility(obj, IFoo, name='bar')
+        self.assertTrue(queryUtility(IFoo, name='bar') is obj)
+
+    def test_queryUtility_w_conforming_context(self):
+        from zope.interface import Interface
+        from zope.component import getGlobalSiteManager
+        from zope.component import queryUtility
+        from zope.component.tests.test_doctests \
+            import ConformsToIComponentLookup
+        class SM(object):
+            def __init__(self, obj):
+                self._obj = obj
+            def queryUtility(self, interface, name, default):
+                return self._obj
+        class IFoo(Interface):
+            pass
+        obj1 = object()
+        obj2 = object()
+        sm = SM(obj2)
+        context = ConformsToIComponentLookup(sm)
+        getGlobalSiteManager().registerUtility(obj1, IFoo)
+        self.assertTrue(queryUtility(IFoo, context=context) is obj2)
+
+
 def test_suite():
     return unittest.TestSuite((
         unittest.makeSuite(PackageAPITests),

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:36:44 UTC (rev 126901)
+++ zope.component/tseaver-test_cleanup/src/zope/component/tests/test_doctests.py	2012-06-17 21:36:48 UTC (rev 126902)
@@ -492,41 +492,6 @@
       >>> tearDown()
     """
 
-def testUtility():
-    """Utilities are components that simply provide an interface. They are
-    instantiated at the time or before they are registered. Here we test the
-    simple query interface.
-
-    Before we register any utility, there is no utility available, of
-    course. The pure instatiation of an object does not make it a utility. If
-    you do not specify a default, you get a `ComponentLookupError`...
-
-      >>> from zope.component.testing import setUp, tearDown
-      >>> setUp()
-      >>> component.getUtility(I1) #doctest: +NORMALIZE_WHITESPACE
-      Traceback (most recent call last):
-      ...
-      ComponentLookupError: \
-      (<InterfaceClass zope.component.tests.test_doctests.I1>, '')
-
-    ...otherwise, you get the default
-
-      >>> component.queryUtility(I1, default='<default>')
-      '<default>'
-      >>> component.queryUtility(I2, default='<default>')
-      '<default>'
-
-    Now we declare `ob` to be the utility providing `I1`
-
-      >>> component.getGlobalSiteManager().registerUtility(ob, I1)
-
-    so that the component is now available:
-
-      >>> component.getUtility(I1) is ob
-      True
-      >>> tearDown()
-    """
-
 def testNamedUtility():
     """Like adapters, utilities can be named.
 



More information about the checkins mailing list