[Zope3-checkins] CVS: Zope3/lib/python/Interface/Registry/tests - TestIAdapterRegistry.py:1.3

Jim Fulton jim@zope.com
Mon, 18 Nov 2002 15:40:17 -0500


Update of /cvs-repository/Zope3/lib/python/Interface/Registry/tests
In directory cvs.zope.org:/tmp/cvs-serv3952/tests

Modified Files:
	TestIAdapterRegistry.py 
Log Message:
Modified the constructor to take a mapping object to store internal
data. This was needed so that persistent subclasses could pass
persistent dictionaries.

Added a filter option to get and getForObject. Search will continue
until data that matches the filter is found.

Added getRegistered and getRegisteredMatching. (See IAdapterRegistry.)



=== Zope3/lib/python/Interface/Registry/tests/TestIAdapterRegistry.py 1.2 => 1.3 ===
--- Zope3/lib/python/Interface/Registry/tests/TestIAdapterRegistry.py:1.2	Thu Aug  1 12:06:41 2002
+++ Zope3/lib/python/Interface/Registry/tests/TestIAdapterRegistry.py	Mon Nov 18 15:40:17 2002
@@ -74,6 +74,198 @@
         for P in [P1, P2, P3]:
             self.assertEqual(registry.get((None, P)), 'default P3')
 
+    def test_getForObject(self):
+        registry = self.__registery()
+
+        class C: pass
+        c = C()
+
+        for R in [R2, R3, R4, (R12, R2), (R12, R4)]:
+            c.__implements__ = R
+            for P in [P1, P2, P3]:
+                self.assertEqual(registry.getForObject(c, P), 'R2 P3')
+
+        for R in [None, R1, R2, R3, R4, (R12, R2), (R12, R4)]:
+            c.__implements__ = R
+            self.assertEqual(registry.getForObject(c, None), None)
+
+        c.__implements__ = R1
+        for P in [P1, P2, P3]:
+            self.assertEqual(registry.getForObject(c, P), 'any P3')
+
+        c = C()
+        for P in [P1, P2, P3]:
+            self.assertEqual(registry.getForObject(c, P), 'default P3')
+
+    def test_get_w_filter(self):
+        registry = self.__registery()
+
+        for R in [R2, R3, R4, (R12, R2), (R12, R4)]:
+            for P in [P1, P2, P3]:
+                self.assertEqual(
+                    registry.get((R, P), filter=lambda o: o.startswith('a')),
+                    'any P3')
+                self.assertEqual(
+                    registry.get((R, P), filter=lambda o: o.startswith('d')),
+                    'default P3')
+                self.assertEqual(
+                    registry.get((R, P), filter=lambda o: o.startswith('z')),
+                    None)
+
+    def test_getForObject_w_filter(self):
+        registry = self.__registery()
+
+        class C: pass
+        c = C()
+
+        for R in [R2, R3, R4, (R12, R2), (R12, R4)]:
+            c.__implements__ = R
+            for P in [P1, P2, P3]:
+                self.assertEqual(
+                    registry.getForObject(c, P,
+                                          filter=lambda o: o.startswith('a')),
+                    'any P3')
+                self.assertEqual(
+                    registry.getForObject(c, P,
+                                          filter=lambda o: o.startswith('d')),
+                    'default P3')
+                self.assertEqual(
+                    registry.getForObject(c, P,
+                                          filter=lambda o: o.startswith('z')),
+                    None)
+
+    def test_getRegistered(self):
+        registry = self.__registery()
+         
+        # Get something that was registered directly
+        self.assertEqual(registry.getRegistered(R2, P3), 'R2 P3')
+        self.assertEqual(registry.getRegistered(Interface, P3), 'any P3')
+        self.assertEqual(registry.getRegistered(None, P3), 'default P3')
+         
+        # this mustn't return anything that was not registered directly
+        self.assertEqual(registry.getRegistered(R3, P3), None)
+        self.assertEqual(registry.getRegistered(R2, P2), None)
+
+    def test_getRegisteredMatching_all(self):
+        registry = self.__registery()
+        
+        got = list(registry.getRegisteredMatching())
+        got.sort()
+        expect = [
+            (None, P3, 'default P3'),
+            (Interface, P3, 'any P3'),
+            (R2, P3, 'R2 P3'),
+            ]
+        expect.sort()
+        self.assertEqual(got, expect)
+
+    def test_getRegisteredMatching_required_R1(self):
+        registry = self.__registery()
+        
+        got = list(registry.getRegisteredMatching(
+            required_interfaces = (R1, )
+            ))
+        got.sort()
+        expect = [
+            (None, P3, 'default P3'),
+            (Interface, P3, 'any P3'),
+            ]
+        expect.sort()
+        self.assertEqual(got, expect)
+
+    def test_getRegisteredMatching_required_multiple(self):
+        registry = self.__registery()
+        
+        got = list(registry.getRegisteredMatching(
+            required_interfaces = (R12, R2)
+            ))
+        got.sort()
+        expect = [
+            (None, P3, 'default P3'),
+            (Interface, P3, 'any P3'),
+            (R2, P3, 'R2 P3'),
+            ]
+        expect.sort()
+        self.assertEqual(got, expect)
+
+    def test_getRegisteredMatching_provided_P1(self):
+        registry = self.__registery()
+        
+        got = list(registry.getRegisteredMatching(
+            provided_interfaces = (P1, )
+            ))
+        got.sort()
+        expect = [
+            (None, P3, 'default P3'),
+            (Interface, P3, 'any P3'),
+            (R2, P3, 'R2 P3'),
+            ]
+        expect.sort()
+        self.assertEqual(got, expect)
+
+    def test_getRegisteredMatching_provided_P2(self):
+        registry = self.__registery()
+        
+        got = list(registry.getRegisteredMatching(
+            provided_interfaces = (P3, )
+            ))
+        got.sort()
+        expect = [
+            (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):
+        registry = self.__registery()
+        
+        got = list(registry.getRegisteredMatching(
+            required_interfaces = (R4, R12),
+            provided_interfaces = (P1, ),
+            ))
+        got.sort()
+        expect = [
+            (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):
+        registry = self.__registery()
+        
+        got = list(registry.getRegisteredMatching(
+            required_interfaces = (R4, R12),
+            provided_interfaces = (P3, ),
+            ))
+        got.sort()
+        expect = [
+            (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):
+        registry = self.__registery()
+        
+        got = list(registry.getRegisteredMatching(
+            required_interfaces = (R2, ),
+            provided_interfaces = (P3, ),
+            ))
+        got.sort()
+        expect = [
+            (None, P3, 'default P3'),
+            (Interface, P3, 'any P3'),
+            (R2, P3, 'R2 P3'),
+            ]
+        expect.sort()
+        self.assertEqual(got, expect)