Exemple #1
0
    def testAdapterInContext(self):
        class I1(Interface):
            pass
        class I2(Interface):
            pass
        class C(object):
            implements(I1)
            def __conform__(self, iface, default=None):
                if iface == I2:
                    return 42
                
        ob = C()

        servicemanager = StubServiceService()
        context = ConformsToIServiceService(servicemanager)
        class I3(Interface):
            pass
        servicemanager.sm.provideAdapter((I1,), I3, '', lambda x: 43)

        # If an object implements the interface you want to adapt to,
        # getAdapterInContext should simply return the object.
        self.assertEquals(getAdapterInContext(ob, I1, context), ob)
        self.assertEquals(queryAdapterInContext(ob, I1, context), ob)

        # If an object conforms to the interface you want to adapt to,
        # getAdapterInContext should simply return the conformed object.
        self.assertEquals(getAdapterInContext(ob, I2, context), 42)
        self.assertEquals(queryAdapterInContext(ob, I2, context), 42)

        class I4(Interface):
            pass
        # If an adapter isn't registered for the given object and interface,
        # and you provide no default, raise ComponentLookupError...
        self.assertRaises(ComponentLookupError,
                          getAdapterInContext, ob, I4, context)

        # ...otherwise, you get the default
        self.assertEquals(queryAdapterInContext(ob, I4, context, 44), 44)

        # If you ask for an adapter for which something's registered
        # you get the registered adapter
        self.assertEquals(getAdapterInContext(ob, I3, context), 43)
        self.assertEquals(queryAdapterInContext(ob, I3, context), 43)
 def _callFUT(self, *args, **kw):
     from zope.component import getAdapterInContext
     return getAdapterInContext(*args, **kw)
Exemple #3
0
 def _callFUT(self, *args, **kw):
     from zope.component import getAdapterInContext
     return getAdapterInContext(*args, **kw)