def _multipleInterfacesForClassOrInterface(self, original): adapter = lambda o: None class FirstInterface(Interface): pass class SecondInterface(Interface): pass components.registerAdapter(adapter, original, FirstInterface, SecondInterface) self.assertIdentical( components.getAdapterFactory(original, FirstInterface, None), adapter) self.assertIdentical( components.getAdapterFactory(original, SecondInterface, None), adapter)
def _subclassAdapterRegistrationForClassOrInterface(self, original): firstAdapter = lambda o: True secondAdapter = lambda o: False class TheSubclass(original): pass class TheInterface(Interface): pass components.registerAdapter(firstAdapter, original, TheInterface) components.registerAdapter(secondAdapter, TheSubclass, TheInterface) self.assertIdentical( components.getAdapterFactory(original, TheInterface, None), firstAdapter) self.assertIdentical( components.getAdapterFactory(TheSubclass, TheInterface, None), secondAdapter)
def __conform__(self, interface, registry=None, default=None): for providedInterface in self.provided: if providedInterface.isOrExtends(interface): return self.load() if getAdapterFactory(providedInterface, interface, None) is not None: return interface(self.load(), default) return default
def _registerAdapterForClassOrInterface(self, original): adapter = lambda o: None class TheInterface(Interface): pass components.registerAdapter(adapter, original, TheInterface) self.assertIdentical( components.getAdapterFactory(original, TheInterface, None), adapter)
def _duplicateAdapterForClassOrInterface(self, original): firstAdapter = lambda o: False secondAdapter = lambda o: True class TheInterface(Interface): pass components.registerAdapter(firstAdapter, original, TheInterface) self.assertRaises(ValueError, components.registerAdapter, secondAdapter, original, TheInterface) # Make sure that the original adapter is still around as well self.assertIdentical( components.getAdapterFactory(original, TheInterface, None), firstAdapter)
def _duplicateAdapterForClassOrInterface(self, original): firstAdapter = lambda o: False secondAdapter = lambda o: True class TheInterface(Interface): pass components.registerAdapter(firstAdapter, original, TheInterface) self.assertRaises( ValueError, components.registerAdapter, secondAdapter, original, TheInterface) # Make sure that the original adapter is still around as well self.assertIdentical( components.getAdapterFactory(original, TheInterface, None), firstAdapter)
def _duplicateAdapterForClassOrInterfaceAllowed(self, original): firstAdapter = lambda o: False secondAdapter = lambda o: True class TheInterface(Interface): pass components.registerAdapter(firstAdapter, original, TheInterface) components.ALLOW_DUPLICATES = True try: components.registerAdapter(secondAdapter, original, TheInterface) self.assertIdentical( components.getAdapterFactory(original, TheInterface, None), secondAdapter) finally: components.ALLOW_DUPLICATES = False # It should be rejected again at this point self.assertRaises(ValueError, components.registerAdapter, firstAdapter, original, TheInterface) self.assertIdentical( components.getAdapterFactory(original, TheInterface, None), secondAdapter)
def _duplicateAdapterForClassOrInterfaceAllowed(self, original): firstAdapter = lambda o: False secondAdapter = lambda o: True class TheInterface(Interface): pass components.registerAdapter(firstAdapter, original, TheInterface) components.ALLOW_DUPLICATES = True try: components.registerAdapter(secondAdapter, original, TheInterface) self.assertIdentical( components.getAdapterFactory(original, TheInterface, None), secondAdapter) finally: components.ALLOW_DUPLICATES = False # It should be rejected again at this point self.assertRaises( ValueError, components.registerAdapter, firstAdapter, original, TheInterface) self.assertIdentical( components.getAdapterFactory(original, TheInterface, None), secondAdapter)