[Checkins] SVN: zope.component/tseaver-test_cleanup/ Convert getAdapter / queryAdapter doctest to Sphinx + unittests.
Tres Seaver
cvs-admin at zope.org
Mon Jun 18 18:17:03 UTC 2012
Log message for revision 126953:
Convert getAdapter / queryAdapter 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:16:55 UTC (rev 126952)
+++ zope.component/tseaver-test_cleanup/docs/api.rst 2012-06-18 18:17:00 UTC (rev 126953)
@@ -383,10 +383,76 @@
Adapter Registration APIs
-------------------------
+Named Adapter Lookup
+####################
+
.. autofunction:: zope.component.getAdapter
.. autofunction:: zope.component.getAdapterInContext
+.. 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
+name is a required argument.)
+
+If no adapter is registered for the given object, interface, and name,
+``getAdapter`` raises ``ComponentLookupError``, while ``queryAdapter``
+returns the default:
+
+.. doctest::
+
+ >>> from zope.component import getAdapter
+ >>> from zope.component import queryAdapter
+ >>> from zope.component.tests.test_doctests import I2
+ >>> from zope.component.tests.test_doctests import ob
+ >>> getAdapter(ob, I2, '')
+ Traceback (most recent call last):
+ ...
+ ComponentLookupError: (<instance Ob>,
+ <InterfaceClass zope.component.tests.test_doctests.I2>,
+ '')
+ >>> queryAdapter(ob, I2, '', '<default>')
+ '<default>'
+
+The 'requires' argument to `registerAdapter` must be a sequence, rather than
+a single interface:
+
+.. doctest::
+
+ >>> gsm.registerAdapter(Comp, I1, I2, '')
+ Traceback (most recent call last):
+ ...
+ TypeError: the required argument should be a list of interfaces, not a single interface
+
+After register an adapter from `I1` to `I2` with the global site manager:
+
+.. doctest::
+
+ >>> from zope.component import getGlobalSiteManager
+ >>> from zope.component.tests.test_doctests import Comp
+ >>> gsm = getGlobalSiteManager()
+ >>> gsm.registerAdapter(Comp, (I1,), I2, '')
+
+We can access the adapter using the `getAdapter()` API:
+
+.. doctest::
+
+ >>> adapter = getAdapter(ob, I2, '')
+ >>> adapter.__class__ is Comp
+ True
+ >>> adapter.context is ob
+ True
+ >>> adapter = queryAdapter(ob, I2, '')
+ >>> adapter.__class__ is Comp
+ True
+ >>> adapter.context is ob
+ True
+
.. autofunction:: zope.component.getMultiAdapter
.. autofunction:: zope.component.queryAdapter
@@ -408,6 +474,11 @@
.. autofunction:: zope.component.adapts
+.. testcleanup::
+
+ from zope.component.testing import tearDown
+ tearDown()
+
Factory APIs
------------
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:16:55 UTC (rev 126952)
+++ zope.component/tseaver-test_cleanup/src/zope/component/tests/test___init__.py 2012-06-18 18:17:00 UTC (rev 126953)
@@ -65,7 +65,7 @@
from zope.component.interfaces import ComponentLookupError
self.assertRaises(ComponentLookupError, getSiteManager, object())
- def test_getUtilty_anonymous_nonesuch(self):
+ def test_getUtility_anonymous_nonesuch(self):
from zope.interface import Interface
from zope.component import getUtility
from zope.component.interfaces import ComponentLookupError
@@ -73,7 +73,7 @@
pass
self.assertRaises(ComponentLookupError, getUtility, IFoo)
- def test_getUtilty_named_nonesuch(self):
+ def test_getUtility_named_nonesuch(self):
from zope.interface import Interface
from zope.component import getUtility
from zope.component.interfaces import ComponentLookupError
@@ -81,7 +81,7 @@
pass
self.assertRaises(ComponentLookupError, getUtility, IFoo, name='bar')
- def test_getUtilty_anonymous_hit(self):
+ def test_getUtility_anonymous_hit(self):
from zope.interface import Interface
from zope.component import getGlobalSiteManager
from zope.component import getUtility
@@ -91,7 +91,7 @@
getGlobalSiteManager().registerUtility(obj, IFoo)
self.assertTrue(getUtility(IFoo) is obj)
- def test_getUtilty_named_hit(self):
+ def test_getUtility_named_hit(self):
from zope.interface import Interface
from zope.component import getUtility
from zope.component import getGlobalSiteManager
@@ -121,14 +121,14 @@
getGlobalSiteManager().registerUtility(obj1, IFoo)
self.assertTrue(getUtility(IFoo, context=context) is obj2)
- def test_queryUtilty_anonymous_nonesuch(self):
+ def test_queryUtility_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):
+ def test_queryUtility_anonymous_nonesuch_w_default(self):
from zope.interface import Interface
from zope.component import queryUtility
class IFoo(Interface):
@@ -136,14 +136,14 @@
obj = object()
self.assertTrue(queryUtility(IFoo, default=obj) is obj)
- def test_queryUtilty_named_nonesuch(self):
+ def test_queryUtility_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):
+ def test_queryUtility_named_nonesuch_w_default(self):
from zope.interface import Interface
from zope.component import queryUtility
class IFoo(Interface):
@@ -151,7 +151,7 @@
obj = object()
self.assertTrue(queryUtility(IFoo, name='bar', default=obj) is obj)
- def test_queryUtilty_anonymous_hit(self):
+ def test_queryUtility_anonymous_hit(self):
from zope.interface import Interface
from zope.component import getGlobalSiteManager
from zope.component import queryUtility
@@ -161,7 +161,7 @@
getGlobalSiteManager().registerUtility(obj, IFoo)
self.assertTrue(queryUtility(IFoo) is obj)
- def test_queryUtilty_named_hit(self):
+ def test_queryUtility_named_hit(self):
from zope.interface import Interface
from zope.component import queryUtility
from zope.component import getGlobalSiteManager
@@ -300,7 +300,161 @@
self.assertTrue(queryNextUtility(sm1, IMyUtility, 'myutil')
is gutil)
+ def test_getAdapter_anonymous_nonesuch(self):
+ from zope.interface import Interface
+ from zope.component import getAdapter
+ from zope.component.interfaces import ComponentLookupError
+ class IFoo(Interface):
+ pass
+ self.assertRaises(ComponentLookupError, getAdapter, object, IFoo, '')
+ def test_getAdapter_named_nonesuch(self):
+ from zope.interface import Interface
+ from zope.component import getAdapter
+ from zope.component.interfaces import ComponentLookupError
+ class IFoo(Interface):
+ pass
+ self.assertRaises(ComponentLookupError, getAdapter, object, IFoo, 'bar')
+
+ def test_getAdapter_anonymous_hit(self):
+ from zope.interface import Interface
+ from zope.interface import implementer
+ from zope.component import getGlobalSiteManager
+ from zope.component import getAdapter
+ 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 = getAdapter(bar, IFoo, '')
+ self.assertTrue(adapted.__class__ is Baz)
+ self.assertTrue(adapted.context is bar)
+
+ def test_getAdapter_named_hit(self):
+ from zope.interface import Interface
+ from zope.interface import implementer
+ from zope.component import getAdapter
+ 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, 'named')
+ bar = Bar()
+ adapted = getAdapter(bar, IFoo, 'named')
+ self.assertTrue(adapted.__class__ is Baz)
+ self.assertTrue(adapted.context is bar)
+
+ def test_queryAdapter_anonymous_nonesuch(self):
+ from zope.interface import Interface
+ from zope.component import queryAdapter
+ class IFoo(Interface):
+ pass
+ self.assertEqual(queryAdapter(object(), IFoo, '', '<default>'),
+ '<default>')
+
+ def test_queryAdapter_named_nonesuch(self):
+ from zope.interface import Interface
+ from zope.component import queryAdapter
+ class IFoo(Interface):
+ pass
+ self.assertEqual(queryAdapter(object(), IFoo, 'bar'), None)
+
+ def test_queryAdapter_anonymous_hit(self):
+ from zope.interface import Interface
+ from zope.interface import implementer
+ from zope.component import getGlobalSiteManager
+ from zope.component import queryAdapter
+ 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 = queryAdapter(bar, IFoo, '')
+ self.assertTrue(adapted.__class__ is Baz)
+ self.assertTrue(adapted.context is bar)
+
+ def test_queryAdapter_named_hit(self):
+ from zope.interface import Interface
+ from zope.interface import implementer
+ from zope.component import getGlobalSiteManager
+ from zope.component import queryAdapter
+ 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, 'named')
+ bar = Bar()
+ adapted = queryAdapter(bar, IFoo, 'named')
+ self.assertTrue(adapted.__class__ is Baz)
+ self.assertTrue(adapted.context is bar)
+
+ def test_queryAdapter_nested(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 queryAdapter
+ 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 = queryAdapter(bar, IFoo, '', context=Context(sm1))
+ self.assertTrue(adapted.__class__ is Local)
+ 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:16:55 UTC (rev 126952)
+++ zope.component/tseaver-test_cleanup/src/zope/component/tests/test_doctests.py 2012-06-18 18:17:00 UTC (rev 126953)
@@ -249,55 +249,6 @@
>>> tearDown()
"""
-def testAdapter():
- """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
- name is a required argument.)
-
- If an adapter isn't registered for the given object and interface, and you
- provide no default, raise `ComponentLookupError`...
-
- >>> from zope.component.testing import setUp, tearDown
- >>> setUp()
- >>> component.getAdapter(ob, I2, '') #doctest: +NORMALIZE_WHITESPACE
- Traceback (most recent call last):
- ...
- ComponentLookupError: (<instance Ob>,
- <InterfaceClass zope.component.tests.test_doctests.I2>,
- '')
-
- ...otherwise, you get the default
-
- >>> component.queryAdapter(ob, I2, '', '<default>')
- '<default>'
-
- Now get the global site manager and register an adapter from `I1` to `I2`
- without a name:
-
- >>> component.getGlobalSiteManager().registerAdapter(
- ... Comp, (I1,), I2, '')
-
- You should get a sensible error message if you forget that the 'requires'
- argument is supposed to be a sequence
-
- >>> component.getGlobalSiteManager().registerAdapter(
- ... Comp, I1, I2, '')
- Traceback (most recent call last):
- ...
- TypeError: the required argument should be a list of interfaces, not a single interface
-
- You can now simply access the adapter using the `getAdapter()` API
- function:
-
- >>> adapter = component.getAdapter(ob, I2, '')
- >>> adapter.__class__ is Comp
- True
- >>> adapter.context is ob
- True
- >>> 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