[Zope3-checkins] CVS: Zope3/src/zope/interface/tests -
test_surrogate.py:1.1.2.4
Jim Fulton
cvs-admin at zope.org
Thu Nov 13 12:33:02 EST 2003
Update of /cvs-repository/Zope3/src/zope/interface/tests
In directory cvs.zope.org:/tmp/cvs-serv12014/src/zope/interface/tests
Modified Files:
Tag: adaptergeddon-branch
test_surrogate.py
Log Message:
Fixed some bugs in multi-adapters.
=== Zope3/src/zope/interface/tests/test_surrogate.py 1.1.2.3 => 1.1.2.4 ===
--- Zope3/src/zope/interface/tests/test_surrogate.py:1.1.2.3 Mon Nov 10 15:45:20 2003
+++ Zope3/src/zope/interface/tests/test_surrogate.py Thu Nov 13 12:33:01 2003
@@ -18,165 +18,174 @@
import unittest
from zope.testing.doctestunit import DocTestSuite
from zope.interface.surrogate import SurrogateRegistry
+import zope.interface
-def test_multi_adapter_w_default():
- """
- >>> import zope.interface
- >>> class IF0(zope.interface.Interface):
- ... pass
- >>> class IF1(IF0):
- ... pass
+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 IR0(zope.interface.Interface):
- ... pass
- >>> class IR1(IR0):
- ... pass
+class A2(Adapter):
+ pass
- >>> class F1:
- ... zope.interface.implements(IF1)
+def test_multi_adapter_w_default():
+ """
>>> c = F1()
-
- >>> class R1:
- ... zope.interface.implements(IR1)
>>> r = R1()
>>> registry = SurrogateRegistry()
-
- >>> class IB0(zope.interface.Interface):
- ... pass
- >>> class IB1(IB0):
- ... pass
-
- >>> class f1:
- ... def __init__(self, x, y):
- ... self.x, self.y = x, y
- >>> registry.provideAdapter(None, IB1, [f1], name='bob', with=[IR0])
+ >>> registry.provideAdapter(None, IB1, [A1], name='bob', with=[IR0])
>>> a = registry.queryMultiAdapter((c, r), IB0, 'bob')
- >>> a.__class__ is f1
- True
- >>> a.x is c
+ >>> a.__class__ is A1
True
- >>> a.y is r
+ >>> a.args == (c, r)
True
>>> registry.queryMultiAdapter((c, r), IB0, 'bruce')
- >>> class f2(f1):
- ... pass
-
- >>> registry.provideAdapter(None, IB1, [f2], name='bob', with=[IR1])
+ >>> registry.provideAdapter(None, IB1, [A2], name='bob', with=[IR1])
>>> a = registry.queryMultiAdapter((c, r), IB0, 'bob')
- >>> a.__class__ is f2
+ >>> a.__class__ is A2
True
- >>> a.x is c
- True
- >>> a.y is r
+ >>> a.args == (c, r)
True
"""
def test_multi_adapter_w_inherited_and_multiple_registrations():
"""
- >>> import zope.interface
- >>> class IF0(zope.interface.Interface):
- ... pass
- >>> class IF1(IF0):
- ... pass
-
-
- >>> class IR0(zope.interface.Interface):
- ... pass
- >>> class IR1(IR0):
- ... pass
-
- >>> class F1:
- ... zope.interface.implements(IF1)
>>> c = F1()
-
- >>> class R1:
- ... zope.interface.implements(IR1)
>>> r = R1()
>>> registry = SurrogateRegistry()
- >>> class IB0(zope.interface.Interface):
- ... pass
- >>> class IB1(IB0):
- ... pass
-
- >>> class f1:
- ... def __init__(self, x, y):
- ... self.x, self.y = x, y
-
>>> class IX(zope.interface.Interface):
... pass
- >>> class fx:
- ... def __init__(self, x, y):
- ... self.x, self.y = x, y
+ >>> class AX(Adapter):
+ ... pass
- >>> registry.provideAdapter(IF0, IB1, [f1], name='bob', with=[IR0])
- >>> registry.provideAdapter(IF1, IB1, [fx], name='bob', with=[IX])
+ >>> 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 f1
+ >>> a.__class__ is A1
True
- >>> a.x is c
+ >>> a.args == (c, r)
True
- >>> a.y is r
- True
-
-
"""
def test_named_adapter_with_default():
"""Query a named simple adapter
>>> import zope.interface
- >>> class F0(zope.interface.Interface):
- ... pass
- >>> class F1(F0):
- ... pass
- >>> class C:
- ... zope.interface.implements(F1)
- >>> c = C()
+ >>> c = F1()
>>> registry = SurrogateRegistry()
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, F0, 'bob')
+ >>> registry.queryNamedAdapter(c, IF0, 'bob')
- >>> class B0(zope.interface.Interface):
- ... pass
- >>> class B1(B0):
- ... pass
+ >>> 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')
- >>> def f1(ob):
- ... return 1
+ >>> registry.provideAdapter(None, IB0, [A2], name='bob')
+ >>> a = registry.queryNamedAdapter(c, IB0, 'bob')
+ >>> a.__class__ is A2
+ True
+ >>> a.args == (c, )
+ True
- >>> registry.provideAdapter(None, B1, [f1], name='bob')
- >>> registry.queryNamedAdapter(c, B0, 'bob')
- 1
- >>> registry.queryNamedAdapter(c, B0, 'bruce')
+ """
- >>> def f3(ob):
- ... return 3
+def test_multi_adapter_gets_closest_provided():
+ """
+ >>> c = F1()
+ >>> r = R1()
- >>> registry.provideAdapter(None, B0, [f3], name='bob')
- >>> registry.queryNamedAdapter(c, B0, 'bob')
- 3
+ >>> registry = SurrogateRegistry()
+ >>> 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 = SurrogateRegistry()
+ >>> 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 = SurrogateRegistry()
+ >>> 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 = SurrogateRegistry()
+ >>> 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 = SurrogateRegistry()
+
+ >>> 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_suite():
More information about the Zope3-Checkins
mailing list