[Zope3-checkins] CVS: Zope3/lib/python/Zope/ComponentArchitecture/tests - testAPI.py:1.4

Jim Fulton jim@zope.com
Sun, 15 Dec 2002 15:16:39 -0500


Update of /cvs-repository/Zope3/lib/python/Zope/ComponentArchitecture/tests
In directory cvs.zope.org:/tmp/cvs-serv15449/Zope/ComponentArchitecture/tests

Modified Files:
	testAPI.py 
Log Message:
Utilities and adapters can now be named.

For example:

  x = getAdapter(self, Ifoo, name="spam")

but unnamed adapters and utilities are still the norm and default.


=== Zope3/lib/python/Zope/ComponentArchitecture/tests/testAPI.py 1.3 => 1.4 ===
--- Zope3/lib/python/Zope/ComponentArchitecture/tests/testAPI.py:1.3	Wed Dec  4 03:39:26 2002
+++ Zope3/lib/python/Zope/ComponentArchitecture/tests/testAPI.py	Sun Dec 15 15:16:38 2002
@@ -74,6 +74,33 @@
         self.assertEquals(c.__class__, Comp)
         self.assertEquals(c.context, ob)
 
+    def testNamedAdapter(self):
+
+        self.testAdapter()
+        
+        from Zope.ComponentArchitecture \
+             import getAdapter, getService, queryAdapter
+        from Zope.ComponentArchitecture.Exceptions import ComponentLookupError
+
+        # if an object implements the interface you want to adapt to,
+        # getAdapter should simply return the object UNLESS we are sking for a
+        # names adapter.
+        self.assertRaises(ComponentLookupError, getAdapter, ob, I1, 'test')
+        
+        # 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, 'test')
+        
+        # ...otherwise, you get the default
+        self.assertEquals(queryAdapter(ob, I2, Test, name='test'), Test)
+
+        class Comp2(Comp): pass
+        
+        getService(None, 'Adapters').provideAdapter(I1, I2, Comp2, name='test')
+        c = getAdapter(ob, I2, name='test')
+        self.assertEquals(c.__class__, Comp2)
+        self.assertEquals(c.context, ob)
+
     def testMultipleAdapterFactories(self):
         from Zope.ComponentArchitecture import getAdapter, getService
 
@@ -106,6 +133,20 @@
         getService(None, 'Utilities').provideUtility(I2, comp)
         self.assertEquals(id(getUtility(ob, I2)), id(comp))
 
+    def testNamedUtility(self):
+        from Zope.ComponentArchitecture import getUtility, queryUtility
+        from Zope.ComponentArchitecture import getService
+        from Zope.ComponentArchitecture.Exceptions import ComponentLookupError
+
+        self.testUtility()
+
+        self.assertRaises(ComponentLookupError, getUtility, ob, I1, 'test')
+        self.assertRaises(ComponentLookupError, getUtility, ob, I2, 'test')
+        self.assertEquals(queryUtility(ob, I2, Test, 'test'), Test)
+        
+        getService(None, 'Utilities').provideUtility(I2, comp, 'test')
+        self.assertEquals(id(getUtility(ob, I2, 'test')), id(comp))
+
     def testView(self):
         from Zope.ComponentArchitecture import getView, queryView, getService
         from Zope.ComponentArchitecture.Exceptions import ComponentLookupError
@@ -170,38 +211,38 @@
         got = list(registry.getRegisteredMatching())
         got.sort()
         expect = [
-            (None, P3, [default_P3]),
-            (Interface, P3, [any_P3]),
-            (R2, P3, [R2_P3]),
+            ('', None, P3, [default_P3]),
+            ('', Interface, P3, [any_P3]),
+            ('', R2, P3, [R2_P3]),
             ]
         expect.sort()
         self.assertEqual(got, expect)
 
-    def test_getRegisteredMatching_required_R1(self):
+    def test_getRegisteredMatching_for_R1(self):
         registry = self.__registery()
         
         got = list(registry.getRegisteredMatching(
-            required_interfaces = (R1, )
+            for_interfaces = (R1, )
             ))
         got.sort()
         expect = [
-            (None, P3, [default_P3]),
-            (Interface, P3, [any_P3]),
+            ('', None, P3, [default_P3]),
+            ('', Interface, P3, [any_P3]),
             ]
         expect.sort()
         self.assertEqual(got, expect)
 
-    def test_getRegisteredMatching_required_multiple(self):
+    def test_getRegisteredMatching_for_multiple(self):
         registry = self.__registery()
         
         got = list(registry.getRegisteredMatching(
-            required_interfaces = (R12, R2)
+            for_interfaces = (R12, R2)
             ))
         got.sort()
         expect = [
-            (None, P3, [default_P3]),
-            (Interface, P3, [any_P3]),
-            (R2, P3, [R2_P3]),
+            ('', None, P3, [default_P3]),
+            ('', Interface, P3, [any_P3]),
+            ('', R2, P3, [R2_P3]),
             ]
         expect.sort()
         self.assertEqual(got, expect)
@@ -214,9 +255,9 @@
             ))
         got.sort()
         expect = [
-            (None, P3, [default_P3]),
-            (Interface, P3, [any_P3]),
-            (R2, P3, [R2_P3]),
+            ('', None, P3, [default_P3]),
+            ('', Interface, P3, [any_P3]),
+            ('', R2, P3, [R2_P3]),
             ]
         expect.sort()
         self.assertEqual(got, expect)
@@ -229,58 +270,58 @@
             ))
         got.sort()
         expect = [
-            (None, P3, [default_P3]),
-            (Interface, P3, [any_P3]),
-            (R2, P3, [R2_P3]),
+            ('', None, P3, [default_P3]),
+            ('', Interface, P3, [any_P3]),
+            ('', R2, P3, [R2_P3]),
             ]
         expect.sort()
         self.assertEqual(got, expect)
 
-    def test_getRegisteredMatching_required_and_provided_1(self):
+    def test_getRegisteredMatching_for_and_provided_1(self):
         registry = self.__registery()
         
         got = list(registry.getRegisteredMatching(
-            required_interfaces = (R4, R12),
+            for_interfaces = (R4, R12),
             provided_interfaces = (P1, ),
             ))
         got.sort()
         expect = [
-            (None, P3, [default_P3]),
-            (Interface, P3, [any_P3]),
-            (R2, P3, [R2_P3]),
+            ('', None, P3, [default_P3]),
+            ('', Interface, P3, [any_P3]),
+            ('', R2, P3, [R2_P3]),
             ]
         expect.sort()
         self.assertEqual(got, expect)
 
-    def test_getRegisteredMatching_required_and_provided_2(self):
+    def test_getRegisteredMatching_for_and_provided_2(self):
         registry = self.__registery()
         
         got = list(registry.getRegisteredMatching(
-            required_interfaces = (R4, R12),
+            for_interfaces = (R4, R12),
             provided_interfaces = (P3, ),
             ))
         got.sort()
         expect = [
-            (None, P3, [default_P3]),
-            (Interface, P3, [any_P3]),
-            (R2, P3, [R2_P3]),
+            ('', None, P3, [default_P3]),
+            ('', Interface, P3, [any_P3]),
+            ('', R2, P3, [R2_P3]),
             ]
         expect.sort()
         self.assertEqual(got, expect)
         
 
-    def test_getRegisteredMatching_required_and_provided_exact(self):
+    def test_getRegisteredMatching_for_and_provided_exact(self):
         registry = self.__registery()
         
         got = list(registry.getRegisteredMatching(
-            required_interfaces = (R2, ),
+            for_interfaces = (R2, ),
             provided_interfaces = (P3, ),
             ))
         got.sort()
         expect = [
-            (None, P3, [default_P3]),
-            (Interface, P3, [any_P3]),
-            (R2, P3, [R2_P3]),
+            ('', None, P3, [default_P3]),
+            ('', Interface, P3, [any_P3]),
+            ('', R2, P3, [R2_P3]),
             ]
         expect.sort()
         self.assertEqual(got, expect)