[Checkins] SVN: zope.component/tseaver-test_cleanup/ Convert 'call interface' doctest to Sphinx + unittests.
Tres Seaver
cvs-admin at zope.org
Mon Jun 18 18:17:15 UTC 2012
Log message for revision 126956:
Convert 'call interface' 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:08 UTC (rev 126955)
+++ zope.component/tseaver-test_cleanup/docs/api.rst 2012-06-18 18:17:11 UTC (rev 126956)
@@ -545,15 +545,15 @@
.. doctest::
- >>> adapter = getAdapter(ob, I2, '')
- >>> adapter.__class__ is Comp
+ >>> adapted = getAdapter(ob, I2, '')
+ >>> adapted.__class__ is Comp
True
- >>> adapter.context is ob
+ >>> adapted.context is ob
True
- >>> adapter = queryAdapter(ob, I2, '')
- >>> adapter.__class__ is Comp
+ >>> adapted = queryAdapter(ob, I2, '')
+ >>> adapted.__class__ is Comp
True
- >>> adapter.context is ob
+ >>> adapted.context is ob
True
If we search using a non-anonymous name, before registering:
@@ -574,17 +574,50 @@
.. doctest::
>>> gsm.registerAdapter(Comp, (I1,), I2, 'named')
- >>> adapter = getAdapter(ob, I2, 'named')
- >>> adapter.__class__ is Comp
+ >>> adapted = getAdapter(ob, I2, 'named')
+ >>> adapted.__class__ is Comp
True
- >>> adapter.context is ob
+ >>> adapted.context is ob
True
- >>> adapter = queryAdapter(ob, I2, 'named')
- >>> adapter.__class__ is Comp
+ >>> adapted = queryAdapter(ob, I2, 'named')
+ >>> adapted.__class__ is Comp
True
- >>> adapter.context is ob
+ >>> adapted.context is ob
True
+Invoking an Interface to Perform Adapter Lookup
+===============================================
+
+:mod:`zope.component` registers an adapter hook with
+:mod:`zope.interface.interface`, allowing a convenient spelling for
+adapter lookup: just "call" the interface, passing the context:
+
+.. doctest::
+
+ >>> adapted = I2(ob)
+ >>> adapted.__class__ is Comp
+ True
+ >>> adapted.context is ob
+ True
+
+If the lookup fails, we get a `TypeError`:
+
+.. doctest::
+
+ >>> I2(object())
+ Traceback (most recent call last):
+ ...
+ TypeError: ('Could not adapt'...
+
+unless we pass a default:
+
+.. doctest::
+
+ >>> marker = object()
+ >>> adapted = I2(object(), marker)
+ >>> adapted is marker
+ True
+
.. autofunction:: zope.component.getMultiAdapter
.. autofunction:: zope.component.queryMultiAdapter
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:08 UTC (rev 126955)
+++ zope.component/tseaver-test_cleanup/src/zope/component/tests/test___init__.py 2012-06-18 18:17:11 UTC (rev 126956)
@@ -559,7 +559,41 @@
self.assertTrue(adapted.__class__ is Local)
self.assertTrue(adapted.context is bar)
+ def test_interface_call_miss(self):
+ from zope.interface import Interface
+ class IFoo(Interface):
+ pass
+ self.assertRaises(TypeError, IFoo, object())
+ def test_interface_call_miss_w_default(self):
+ from zope.interface import Interface
+ class IFoo(Interface):
+ pass
+ marker = object()
+ self.assertTrue(IFoo(object(), marker) is marker)
+
+ def test_interface_call_hit(self):
+ from zope.interface import Interface
+ from zope.interface import implementer
+ from zope.component import getGlobalSiteManager
+ class IFoo(Interface):
+ pass
+ class IBar(Interface):
+ pass
+ @implementer(IBar)
+ class Bar(object):
+ pass
+ @implementer(IFoo)
+ class Baz(object):
+ def __init__(self, context):
+ self.context = context
+ getGlobalSiteManager().registerAdapter(Baz, (IBar,), IFoo, '')
+ bar = Bar()
+ adapted = IFoo(bar)
+ self.assertTrue(adapted.__class__ is Baz)
+ self.assertTrue(adapted.context is bar)
+
+
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:08 UTC (rev 126955)
+++ zope.component/tseaver-test_cleanup/src/zope/component/tests/test_doctests.py 2012-06-18 18:17:11 UTC (rev 126956)
@@ -165,43 +165,6 @@
if interface is IComponentLookup:
return self.sitemanager
-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
- do adaptation.
-
- First, we need to register an adapter:
-
- >>> from zope.component.testing import setUp, tearDown
- >>> setUp()
- >>> component.getGlobalSiteManager().registerAdapter(
- ... Comp, [I1], I2, '')
-
- Then we try to adapt `ob` to provide an `I2` interface by calling the `I2`
- interface with the obejct as first argument:
-
- >>> adapter = I2(ob)
- >>> adapter.__class__ is Comp
- True
- >>> adapter.context is ob
- True
-
- If no adapter is found, a `TypeError is raised...
-
- >>> I1(Ob2()) #doctest: +NORMALIZE_WHITESPACE
- Traceback (most recent call last):
- ...
- TypeError: ('Could not adapt', <instance Ob2>,
- <InterfaceClass zope.component.tests.test_doctests.I1>)
-
- ...unless we specify an alternative adapter:
-
- >>> marker = object()
- >>> I2(object(), marker) is marker
- True
- >>> tearDown()
- """
-
def testMultiAdapter():
"""Adapting a combination of 2 objects to an interface
More information about the checkins
mailing list