[Zope3-checkins] CVS: Zope3/src/zope/interface/tests - test_adapter.py:1.3 iadapter.py:NONE test_surrogate.py:NONE

Jim Fulton jim at zope.com
Mon Mar 8 12:26:58 EST 2004


Update of /cvs-repository/Zope3/src/zope/interface/tests
In directory cvs.zope.org:/tmp/cvs-serv4511/src/zope/interface/tests

Modified Files:
	test_adapter.py 
Removed Files:
	iadapter.py test_surrogate.py 
Log Message:
Removed the old adapter registry code.

Renamed the surrogate registry to AdapterRegistry and moved the
surrogate code to zope.interface.adapter.

Removed the old surrogate module.


=== Zope3/src/zope/interface/tests/test_adapter.py 1.2 => 1.3 ===
--- Zope3/src/zope/interface/tests/test_adapter.py:1.2	Wed Dec 25 09:15:12 2002
+++ Zope3/src/zope/interface/tests/test_adapter.py	Mon Mar  8 12:26:57 2004
@@ -1,6 +1,6 @@
 ##############################################################################
 #
-# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
+# Copyright (c) 2003 Zope Corporation and Contributors.
 # All Rights Reserved.
 #
 # This software is subject to the provisions of the Zope Public License,
@@ -11,23 +11,258 @@
 # FOR A PARTICULAR PURPOSE.
 #
 ##############################################################################
-"""
+"""XXX short summary goes here.
 
-Revision information:
 $Id$
 """
+import unittest
+from zope.testing.doctestunit import DocTestSuite
+from zope.interface.adapter import AdapterRegistry
+import zope.interface
+
+class IF0(zope.interface.Interface):
+    pass
+class IF1(IF0):
+    pass
+
+class F1:
+    zope.interface.implements(IF1)
+
+class IB0(zope.interface.Interface):
+    pass
+class IB1(IB0):
+    pass
+
+class IR0(zope.interface.Interface):
+    pass
+class IR1(IR0):
+    pass
+
+class R1:
+    zope.interface.implements(IR1)
+
+class Adapter:
+    def __init__(self, *args):
+        self.args = args
+
+class A1(Adapter):
+    pass
+
+class A2(Adapter):
+    pass
+
+def test_multi_adapter_w_default():
+    """
+    >>> c = F1()
+    >>> r = R1()
+
+    >>> registry = AdapterRegistry()
+    
+    >>> registry.provideAdapter(None, IB1, [A1], name='bob', with=[IR0])
+
+    >>> a = registry.queryMultiAdapter((c, r), IB0, 'bob')
+    >>> a.__class__ is A1
+    True
+    >>> a.args == (c, r)
+    True
+    
+    >>> registry.queryMultiAdapter((c, r), IB0, 'bruce')
+
+    >>> registry.provideAdapter(None, IB1, [A2], name='bob', with=[IR1])
+    >>> a = registry.queryMultiAdapter((c, r), IB0, 'bob')
+    >>> a.__class__ is A2
+    True
+    >>> a.args == (c, r)
+    True
+    
+    """
+
+def test_multi_adapter_w_inherited_and_multiple_registrations():
+    """
+    >>> c = F1()
+    >>> r = R1()
+
+    >>> registry = AdapterRegistry()
+
+    >>> class IX(zope.interface.Interface):
+    ...    pass
+
+    >>> class AX(Adapter):
+    ...     pass
+    
+    >>> registry.provideAdapter(IF0, IB1, [A1], name='bob', with=[IR0])
+    >>> registry.provideAdapter(IF1, IB1, [AX], name='bob', with=[IX])
+
+    >>> a = registry.queryMultiAdapter((c, r), IB0, 'bob')
+    >>> a.__class__ is A1
+    True
+    >>> a.args == (c, r)
+    True
+    """
+
+def test_named_adapter_with_default():
+    """Query a named simple adapter
+
+    >>> import zope.interface
+
+    >>> c = F1()
+
+    >>> registry = AdapterRegistry()
+
+    If we ask for a named adapter, we won't get a result unless there
+    is a named adapter, even if the object implements the interface:
+
+    >>> registry.queryNamedAdapter(c, IF0, 'bob')
+
+    >>> registry.provideAdapter(None, IB1, [A1], name='bob')
+    >>> a = registry.queryNamedAdapter(c, IB0, 'bob')
+    >>> a.__class__ is A1
+    True
+    >>> a.args == (c, )
+    True
+
+    >>> registry.queryNamedAdapter(c, IB0, 'bruce')
+
+    >>> registry.provideAdapter(None, IB0, [A2], name='bob')
+    >>> a = registry.queryNamedAdapter(c, IB0, 'bob')
+    >>> a.__class__ is A2
+    True
+    >>> a.args == (c, )
+    True
+
+
+    """
+
+def test_multi_adapter_gets_closest_provided():
+    """
+    >>> c = F1()
+    >>> r = R1()
+
+    >>> registry = AdapterRegistry()
+    >>> registry.provideAdapter(IF1, IB0, [A1], name='bob', with=[IR0])
+    >>> registry.provideAdapter(IF1, IB1, [A2], name='bob', with=[IR0])
+    >>> a = registry.queryMultiAdapter((c, r), IB0, 'bob')
+    >>> a.__class__ is A1
+    True
+
+    >>> registry = AdapterRegistry()
+    >>> registry.provideAdapter(IF1, IB1, [A2], name='bob', with=[IR0])
+    >>> registry.provideAdapter(IF1, IB0, [A1], name='bob', with=[IR0])
+    >>> a = registry.queryMultiAdapter((c, r), IB0, 'bob')
+    >>> a.__class__ is A1
+    True
+
+    >>> registry = AdapterRegistry()
+    >>> registry.provideAdapter(IF1, IB0, [A1], name='bob', with=[IR0])
+    >>> registry.provideAdapter(IF1, IB1, [A2], name='bob', with=[IR1])
+    >>> a = registry.queryMultiAdapter((c, r), IB0, 'bob')
+    >>> a.__class__ is A2
+    True
+
+    >>> registry = AdapterRegistry()
+    >>> registry.provideAdapter(IF1, IB1, [A2], name='bob', with=[IR1])
+    >>> registry.provideAdapter(IF1, IB0, [A1], name='bob', with=[IR0])
+    >>> a = registry.queryMultiAdapter((c, r), IB0, 'bob')
+    >>> a.__class__ is A2
+    True
+
+    """
+
+def test_multi_adapter_check_non_default_dont_hide_default():
+    """
+    >>> c = F1()
+    >>> r = R1()
+
+    >>> registry = AdapterRegistry()
+
+    >>> class IX(zope.interface.Interface):
+    ...     pass
+
+    
+    >>> registry.provideAdapter(None, IB0, [A1], name='bob', with=[IR0])
+    >>> registry.provideAdapter(IF1,  IB0, [A2], name='bob', with=[IX])
+    >>> a = registry.queryMultiAdapter((c, r), IB0, 'bob')
+    >>> a.__class__.__name__
+    'A1'
+
+    """
+
+
+def test_getRegisteredMatching_with_with():
+    """
+    >>> registry = AdapterRegistry()
+    >>> registry.provideAdapter(None, IB0, '_0')
+    >>> registry.provideAdapter(IF0, IB0, '00')
+    >>> registry.provideAdapter(IF1, IB0, '10')
+    >>> registry.provideAdapter(IF1, IB1, '11')
+    >>> registry.provideAdapter(IF0, IB0, '000', with=(IR0,))
+    >>> registry.provideAdapter(IF1, IB0, '100', with=(IR0,))
+    >>> registry.provideAdapter(IF1, IB1, '110', with=(IR0,))
+    >>> registry.provideAdapter(IF0, IB0, '001', with=(IR1,))
+    >>> registry.provideAdapter(IF1, IB0, '101', with=(IR1,))
+    >>> registry.provideAdapter(IF1, IB1, '111', with=(IR1,))
+
+    >>> from pprint import PrettyPrinter
+    >>> pprint = PrettyPrinter(width=60).pprint
+    >>> def sorted(x):
+    ...    x = [(getattr(r, '__name__', None), p.__name__,
+    ...          [w.__name__ for w in rwith], n, f)
+    ...         for (r, p, rwith, n, f) in x]
+    ...    x.sort()
+    ...    pprint(x)
+
+    >>> sorted(registry.getRegisteredMatching())
+    [(None, 'IB0', [], u'', '_0'),
+     ('IF0', 'IB0', [], u'', '00'),
+     ('IF0', 'IB0', ['IR0'], u'', '000'),
+     ('IF0', 'IB0', ['IR1'], u'', '001'),
+     ('IF1', 'IB0', [], u'', '10'),
+     ('IF1', 'IB0', ['IR0'], u'', '100'),
+     ('IF1', 'IB0', ['IR1'], u'', '101'),
+     ('IF1', 'IB1', [], u'', '11'),
+     ('IF1', 'IB1', ['IR0'], u'', '110'),
+     ('IF1', 'IB1', ['IR1'], u'', '111')]
+    >>> sorted(registry.getRegisteredMatching(required=[IF0]))
+    [(None, 'IB0', [], u'', '_0'),
+     ('IF0', 'IB0', [], u'', '00'),
+     ('IF0', 'IB0', ['IR0'], u'', '000'),
+     ('IF0', 'IB0', ['IR1'], u'', '001')]
+    >>> sorted(registry.getRegisteredMatching(required=[IF1],
+    ...                                       provided=[IB0]))
+    [(None, 'IB0', [], u'', '_0'),
+     ('IF0', 'IB0', [], u'', '00'),
+     ('IF0', 'IB0', ['IR0'], u'', '000'),
+     ('IF0', 'IB0', ['IR1'], u'', '001'),
+     ('IF1', 'IB0', [], u'', '10'),
+     ('IF1', 'IB0', ['IR0'], u'', '100'),
+     ('IF1', 'IB0', ['IR1'], u'', '101'),
+     ('IF1', 'IB1', [], u'', '11'),
+     ('IF1', 'IB1', ['IR0'], u'', '110'),
+     ('IF1', 'IB1', ['IR1'], u'', '111')]
+    >>> sorted(registry.getRegisteredMatching(required=[IF1],
+    ...                                       provided=[IB0],
+    ...                                       with=[IR0]))
+    [('IF0', 'IB0', ['IR0'], u'', '000'),
+     ('IF1', 'IB0', ['IR0'], u'', '100'),
+     ('IF1', 'IB1', ['IR0'], u'', '110')]
+    >>> sorted(registry.getRegisteredMatching(required=[IF1],
+    ...                                       provided=[IB0],
+    ...                                       with=[IR1]))
+    [('IF0', 'IB0', ['IR0'], u'', '000'),
+     ('IF0', 'IB0', ['IR1'], u'', '001'),
+     ('IF1', 'IB0', ['IR0'], u'', '100'),
+     ('IF1', 'IB0', ['IR1'], u'', '101'),
+     ('IF1', 'IB1', ['IR0'], u'', '110'),
+     ('IF1', 'IB1', ['IR1'], u'', '111')]
+    """
 
-from unittest import TestCase, main, makeSuite
-from zope.interface.tests.iadapter import TestIAdapterRegistry
 
-class Test(TestIAdapterRegistry, TestCase):
 
-    def _new(self):
-        from zope.interface.adapter import AdapterRegistry
-        return AdapterRegistry()
 
 def test_suite():
-    return makeSuite(Test)
+    return unittest.TestSuite((
+        DocTestSuite('zope.interface.adapter'),
+        DocTestSuite(),
+        ))
 
-if __name__=='__main__':
-    main(defaultTest='test_suite')
+if __name__ == '__main__': unittest.main()

=== Removed File Zope3/src/zope/interface/tests/iadapter.py ===

=== Removed File Zope3/src/zope/interface/tests/test_surrogate.py ===




More information about the Zope3-Checkins mailing list