[Checkins] SVN: zope.component/tseaver-test_cleanup/ Convert {get, query}AdapterInContext doctest to Sphinx + unittests.

Tres Seaver cvs-admin at zope.org
Mon Jun 18 18:17:07 UTC 2012


Log message for revision 126954:
  Convert {get,query}AdapterInContext doctest 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-18 18:17:00 UTC (rev 126953)
+++ zope.component/tseaver-test_cleanup/docs/api.rst	2012-06-18 18:17:03 UTC (rev 126954)
@@ -383,18 +383,121 @@
 Adapter Registration APIs
 -------------------------
 
+Conforming Adapter Lookup
+#########################
+
+.. testsetup::
+
+   from zope.component.testing import setUp
+   setUp()
+
+.. autofunction:: zope.component.getAdapterInContext
+
+.. autofunction:: zope.component.queryAdapterInContext
+
+The :function:`~zope.component.getAdapterInContext` and
+:function:`~zope.component.queryAdapterInContext` APIs first check the
+context object to see if it already conforms to the requested interface.
+If so, the object is returned immediately.  Otherwise, the adapter factory
+is looked up in the site manager, and called.
+
+Let's start by creating a component that supports the `__conform__()` method:
+
+.. doctest::
+
+   >>> from zope.interface import implementer
+   >>> from zope.component.tests.test_doctests import I1
+   >>> from zope.component.tests.test_doctests import I2
+   >>> @implementer(I1)
+   ... class Component(object):
+   ...     def __conform__(self, iface, default=None):
+   ...         if iface == I2:
+   ...             return 42
+   >>> ob = Component()
+
+We also gave the component a custom representation, so it will be easier
+to use in these tests.
+
+We now have to create a site manager (other than the default global one)
+with which we can register adapters for `I1`.
+
+.. doctest::
+
+   >>> from zope.component.globalregistry import BaseGlobalComponents
+   >>> sitemanager = BaseGlobalComponents()
+
+Now we create a new `context` that knows how to get to our custom site
+manager.
+
+.. doctest::
+
+   >>> from zope.component.tests.test_doctests \
+   ...      import ConformsToIComponentLookup
+   >>> context = ConformsToIComponentLookup(sitemanager)
+
+If an object implements the interface you want to adapt to,
+`getAdapterInContext()` should simply return the object.
+
+.. doctest::
+
+   >>> from zope.component import getAdapterInContext
+   >>> from zope.component import queryAdapterInContext
+   >>> getAdapterInContext(ob, I1, context) is ob
+   True
+   >>> queryAdapterInContext(ob, I1, context) is ob
+   True
+
+If an object conforms to the interface you want to adapt to,
+`getAdapterInContext()` should simply return the conformed object.
+
+.. doctest::
+
+   >>> getAdapterInContext(ob, I2, context)
+   42
+   >>> queryAdapterInContext(ob, I2, context)
+   42
+
+If an adapter isn't registered for the given object and interface, and you
+provide no default, the `getAdapterInContext` API raises ComponentLookupError:
+
+.. doctest::
+
+   >>> from zope.interface import Interface
+   >>> class I4(Interface):
+   ...     pass
+
+   >>> getAdapterInContext(ob, I4, context)
+   Traceback (most recent call last):
+   ...
+   ComponentLookupError: (<Component implementing 'I1'>,
+                          <InterfaceClass zope.component.tests.test_doctests.I4>)
+
+While the `queryAdapterInContext` API returns the default:
+
+.. doctest::
+
+   >>> queryAdapterInContext(ob, I4, context, 44)
+   44
+
+If you ask for an adapter for which something's registered you get the
+registered adapter:
+
+.. doctest::
+
+   >>> from zope.component.tests.test_doctests import I3
+   >>> sitemanager.registerAdapter(lambda x: 43, (I1,), I3, '')
+   >>> getAdapterInContext(ob, I3, context)
+   43
+   >>> queryAdapterInContext(ob, I3, context)
+   43
+
 Named Adapter Lookup
 ####################
 
 .. autofunction:: zope.component.getAdapter
 
-.. autofunction:: zope.component.getAdapterInContext
+.. autofunction:: zope.component.queryAdapter
 
-.. testsetup::
-
-   from zope.component.testing import setUp
-   setUp()
-
 The ``getAdapter`` and ``queryAdapter`` API functions are similar to
 ``{get|query}AdapterInContext()`` functions, except that they do not care
 about the ``__conform__()`` but also handle named adapters. (Actually, the
@@ -455,10 +558,6 @@
 
 .. autofunction:: zope.component.getMultiAdapter
 
-.. autofunction:: zope.component.queryAdapter
-
-.. autofunction:: zope.component.queryAdapterInContext
-
 .. autofunction:: zope.component.queryMultiAdapter
 
 .. autofunction:: zope.component.getAdapters

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-18 18:17:00 UTC (rev 126953)
+++ zope.component/tseaver-test_cleanup/src/zope/component/tests/test___init__.py	2012-06-18 18:17:03 UTC (rev 126954)
@@ -454,7 +454,112 @@
         self.assertTrue(adapted.__class__ is Local)
         self.assertTrue(adapted.context is bar)
 
+    def test_getAdapterInContext_miss(self):
+        from zope.interface import Interface
+        from zope.component import getAdapterInContext
+        from zope.component.interfaces import ComponentLookupError
+        class IFoo(Interface):
+            pass
+        self.assertRaises(ComponentLookupError,
+                          getAdapterInContext, object, IFoo, context=None)
 
+    def test_getAdapterInContext_hit_via_sm(self):
+        from zope.interface import Interface
+        from zope.interface import implementer
+        from zope.interface.registry import Components
+        from zope.component import getGlobalSiteManager
+        from zope.component import getAdapterInContext
+        from zope.component.tests.test_doctests \
+            import ConformsToIComponentLookup
+        class IFoo(Interface):
+            pass
+        class IBar(Interface):
+            pass
+        @implementer(IFoo)
+        class Global(object):
+            def __init__(self, context):
+                self.context = context
+        @implementer(IFoo)
+        class Local(object):
+            def __init__(self, context):
+                self.context = context
+        @implementer(IBar)
+        class Bar(object):
+            pass
+        class Context(ConformsToIComponentLookup):
+            def __init__(self, sm):
+                self.sitemanager = sm
+        gsm = getGlobalSiteManager()
+        gsm.registerAdapter(Global, (IBar,), IFoo, '')
+        sm1 = Components('sm1', bases=(gsm, ))
+        sm1.registerAdapter(Local, (IBar,), IFoo, '')
+        bar = Bar()
+        adapted = getAdapterInContext(bar, IFoo, context=Context(sm1))
+        self.assertTrue(adapted.__class__ is Local)
+        self.assertTrue(adapted.context is bar)
+
+    def test_queryAdapterInContext_miss(self):
+        from zope.interface import Interface
+        from zope.component import queryAdapterInContext
+        class IFoo(Interface):
+            pass
+        self.assertEqual(
+            queryAdapterInContext(object, IFoo, context=None), None)
+
+    def test_queryAdapterInContext_w_object_conforming(self):
+        from zope.interface import Interface
+        from zope.component import queryAdapterInContext
+        class IFoo(Interface):
+            pass
+        _adapted = object()
+        class Foo(object):
+            def __conform__(self, iface, default=None):
+                if iface is IFoo:
+                    return _adapted
+                return default
+        self.assertTrue(
+                queryAdapterInContext(Foo(), IFoo, context=None) is _adapted)
+
+    def test_queryAdapterInContext___conform___raises_TypeError_via_class(self):
+        from zope.interface import Interface
+        from zope.component import queryAdapterInContext
+        class IFoo(Interface):
+            pass
+        _adapted = object()
+        class Foo(object):
+            def __conform__(self, iface, default=None):
+                if iface is IFoo:
+                    return _adapted
+                return default
+        # call via class, triggering TypeError
+        self.assertEqual(queryAdapterInContext(Foo, IFoo, context=None), None)
+
+    def test_queryAdapterInContext___conform___raises_TypeError_via_inst(self):
+        from zope.interface import Interface
+        from zope.component import queryAdapterInContext
+        class IFoo(Interface):
+            pass
+        _adapted = object()
+        class Foo(object):
+            def __conform__(self, iface, default=None):
+                raise TypeError
+        self.assertRaises(TypeError,
+                         queryAdapterInContext, Foo(), IFoo, context=None)
+
+    def test_queryAdapterInContext_w_object_implementing(self):
+        from zope.interface import Interface
+        from zope.interface import implementer
+        from zope.component import queryAdapterInContext
+        class IFoo(Interface):
+            pass
+        @implementer(IFoo)
+        class Foo(object):
+              pass
+        foo = Foo()
+        self.assertTrue(
+                queryAdapterInContext(foo, IFoo, context=None) is foo)
+
+
 IMyUtility = None
 def _makeMyUtility(name, sm):
     global IMyUtility

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-18 18:17:00 UTC (rev 126953)
+++ zope.component/tseaver-test_cleanup/src/zope/component/tests/test_doctests.py	2012-06-18 18:17:03 UTC (rev 126954)
@@ -165,90 +165,6 @@
         if interface is IComponentLookup:
             return self.sitemanager
 
-
-def testAdapterInContext(self):
-    """The `getAdapterInContext()` and `queryAdapterInContext()` API functions
-    do not only use the site manager to look up the adapter, but first tries
-    to use the `__conform__()` method of the object to find an adapter as
-    specified by PEP 246.
-
-    Let's start by creating a component that support's the PEP 246's
-    `__conform__()` method:
-
-      >>> from zope.component.testing import setUp, tearDown
-      >>> setUp()
-      >>> class Component(object):
-      ...     interface.implements(I1)
-      ...     def __conform__(self, iface, default=None):
-      ...         if iface == I2:
-      ...             return 42
-      ...     def __repr__(self):
-      ...         return '''<Component implementing 'I1'>'''
-
-      >>> ob = Component()
-
-    We also gave the component a custom representation, so it will be easier
-    to use in these tests.
-
-    We now have to create a site manager (other than the default global one)
-    with which we can register adapters for `I1`.
-
-      >>> from zope.component.globalregistry import BaseGlobalComponents
-      >>> sitemanager = BaseGlobalComponents()
-
-    Now we create a new `context` that knows how to get to our custom site
-    manager.
-
-      >>> context = ConformsToIComponentLookup(sitemanager)
-
-    We now register an adapter from `I1` to `I3`:
-
-      >>> sitemanager.registerAdapter(lambda x: 43, (I1,), I3, '')
-
-    If an object implements the interface you want to adapt to,
-    `getAdapterInContext()` should simply return the object.
-
-      >>> component.getAdapterInContext(ob, I1, context)
-      <Component implementing 'I1'>
-      >>> component.queryAdapterInContext(ob, I1, context)
-      <Component implementing 'I1'>
-
-    If an object conforms to the interface you want to adapt to,
-    `getAdapterInContext()` should simply return the conformed object.
-
-      >>> component.getAdapterInContext(ob, I2, context)
-      42
-      >>> component.queryAdapterInContext(ob, I2, context)
-      42
-
-    If an adapter isn't registered for the given object and interface, and you
-    provide no default, raise ComponentLookupError...
-
-      >>> class I4(interface.Interface):
-      ...     pass
-
-      >>> component.getAdapterInContext(ob, I4, context) \\
-      ... #doctest: +NORMALIZE_WHITESPACE
-      Traceback (most recent call last):
-      ...
-      ComponentLookupError: (<Component implementing 'I1'>,
-                             <InterfaceClass zope.component.tests.test_doctests.I4>)
-
-    ...otherwise, you get the default:
-
-      >>> component.queryAdapterInContext(ob, I4, context, 44)
-      44
-
-    If you ask for an adapter for which something's registered you get the
-    registered adapter
-
-      >>> component.getAdapterInContext(ob, I3, context)
-      43
-      >>> component.queryAdapterInContext(ob, I3, context)
-      43
-      >>> tearDown()
-    """
-
 def testInterfaceCall():
     """Here we test the `adapter_hook()` function that we registered with the
     `zope.interface` adapter hook registry, so that we can call interfaces to



More information about the checkins mailing list