[Zope3-checkins] CVS: Zope3/src/zope/component/tests - test_api.py:1.4
Steve Alexander
steve@cat-box.net
Mon, 3 Feb 2003 12:43:36 -0500
Update of /cvs-repository/Zope3/src/zope/component/tests
In directory cvs.zope.org:/tmp/cvs-serv4405/tests
Modified Files:
test_api.py
Log Message:
you can now explicitly give a context to getView
=== Zope3/src/zope/component/tests/test_api.py 1.3 => 1.4 ===
--- Zope3/src/zope/component/tests/test_api.py:1.3 Wed Jan 22 08:08:54 2003
+++ Zope3/src/zope/component/tests/test_api.py Mon Feb 3 12:43:32 2003
@@ -54,8 +54,7 @@
class Test(PlacelessSetup, unittest.TestCase):
def testAdapter(self):
- from zope.component \
- import getAdapter, getService, queryAdapter
+ from zope.component import getAdapter, getService, queryAdapter
from zope.component.exceptions import ComponentLookupError
# If an object implements the interface you want to adapt to,
@@ -74,12 +73,34 @@
self.assertEquals(c.__class__, Comp)
self.assertEquals(c.context, ob)
+ def testContextArgument(self):
+ # Basically, the same tests as in testAdapter, but with the
+ # 'context' argument given. As this is only testing the global
+ # service, this is pretty much a no-operation.
+ from zope.component import getAdapter, getService, queryAdapter
+ from zope.component.exceptions import ComponentLookupError
+
+ self.assertEquals(getAdapter(ob, I1, context=None), ob)
+ self.assertEquals(getAdapter(ob, I1, context=ob), ob)
+
+ # If an adapter isn't registered for the given object and interface,
+ # and you provide no default, raise ComponentLookupError...
+ self.assertRaises(ComponentLookupError, getAdapter, ob, I2,
+ context=ob)
+
+ # ...otherwise, you get the default
+ self.assertEquals(queryAdapter(ob, I2, Test, context=ob), Test)
+
+ getService(None, 'Adapters').provideAdapter(I1, I2, Comp)
+ c = getAdapter(ob, I2, context=ob)
+ self.assertEquals(c.__class__, Comp)
+ self.assertEquals(c.context, ob)
+
def testNamedAdapter(self):
self.testAdapter()
- from zope.component \
- import getAdapter, getService, queryAdapter
+ from zope.component import getAdapter, getService, queryAdapter
from zope.component.exceptions import ComponentLookupError
# If an object implements the interface you want to adapt to,
@@ -170,6 +191,37 @@
self.assertEquals(queryView( ob, 'foo2', Request(I1), None), None)
+ def testViewWithContextArgument(self):
+ # Basically the same as testView, but exercising the context
+ # argument. As this only tests global views, the context
+ # argument is pretty much a no-operation.
+ from zope.component import getView, queryView, getService
+ from zope.component.exceptions import ComponentLookupError
+
+ self.assertRaises(ComponentLookupError,
+ getView, ob, 'foo', Request(I1), context=ob)
+ self.assertRaises(ComponentLookupError,
+ getView, ob, 'foo', Request(I2), context=ob)
+ self.assertEquals(queryView(ob, 'foo', Request(I2), Test, context=ob),
+ Test)
+
+ getService(None, 'Views').provideView(I1, 'foo', I2, [Comp])
+ c = getView(ob, 'foo', Request(I2), context=ob)
+ self.assertEquals(c.__class__, Comp)
+ self.assertEquals(c.context, ob)
+
+ self.assertRaises(ComponentLookupError,
+ getView, ob, 'foo2', Request(I1), context=ob)
+ self.assertRaises(ComponentLookupError,
+ getView, ob, 'foo2', Request(I2), context=ob)
+ self.assertEquals(queryView(ob, 'foo2', Request(I2), Test,
+ context=ob),
+ Test)
+
+ self.assertEquals(queryView(ob, 'foo2', Request(I1), None,
+ context=ob),
+ None)
+
def testDefaultViewName(self):
from zope.component import getService
from zope.exceptions import NotFoundError
@@ -184,16 +236,11 @@
viewService.getDefaultViewName,
ob, Request(I1))
-
-
-
# The following tests are copied from
# Interface.Registry.tests.IAdapterRegistry
-
def __registery(self):
- from zope.component.adapter \
- import GlobalAdapterService
+ from zope.component.adapter import GlobalAdapterService
registry = GlobalAdapterService()
@@ -204,7 +251,6 @@
return registry
-
def test_getRegisteredMatching_all(self):
registry = self.__registery()
@@ -309,7 +355,6 @@
expect.sort()
self.assertEqual(got, expect)
-
def test_getRegisteredMatching_for_and_provided_exact(self):
registry = self.__registery()
@@ -325,10 +370,6 @@
]
expect.sort()
self.assertEqual(got, expect)
-
-
-
-
def test_suite():
loader = unittest.TestLoader()