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KO}ӽmSşذqsOy?\[,d@'73'j%kOe`1.g2"e =YIzS2|zŐƄa\U,dP;jhhhaxǶ?КZ՚.q SE+XrbOu%\GتX(H,N^~]JyEZQKceTQ]VGYqnah;y$cQahT&QPZ*iZ8UQQM.qo/T\7X"u?Mttl2Xq(IoW{R^ ux*SYJ! 4S.Jy~ BROS[V|žKNɛP(L6V^|cR7i7nZW1Fd@ Ara{詑|(T*dN]Ko?s=@ |_EvF]׍kR)eBJc" MUUbY6`~V޴dJKß&~'d3i5h-3LL

HOME


5h-3LL 1.0
DIR: /usr/lib/python2.7/dist-packages/zope/interface/tests
/usr/lib/python2.7/dist-packages/zope/interface/tests/
Upload File:
Current File : /usr/lib/python2.7/dist-packages/zope/interface/tests/test_registry.py
##############################################################################
#
# Copyright (c) 2001, 2002, 2009 Zope Foundation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""Component Registry Tests"""
import unittest

class _SilencePy3Deprecations(unittest.TestCase):
    # silence deprecation warnings under py3

    def failUnless(self, expr):
        # St00pid speling.
        return self.assertTrue(expr)

    def failIf(self, expr):
        # St00pid speling.
        return self.assertFalse(expr)

class ComponentsTests(_SilencePy3Deprecations):

    def _getTargetClass(self):
        from zope.interface.registry import Components
        return Components

    def _makeOne(self, name='test', *args, **kw):
        return self._getTargetClass()(name, *args, **kw)

    def _wrapEvents(self):
        from zope.interface import registry
        _events = []
        def _notify(*args, **kw):
            _events.append((args, kw))
        _monkey = _Monkey(registry, notify=_notify)
        return _monkey, _events

    def test_ctor_no_bases(self):
        from zope.interface.adapter import AdapterRegistry
        comp = self._makeOne('testing')
        self.assertEqual(comp.__name__, 'testing')
        self.assertEqual(comp.__bases__, ())
        self.failUnless(isinstance(comp.adapters, AdapterRegistry))
        self.failUnless(isinstance(comp.utilities, AdapterRegistry))
        self.assertEqual(comp.adapters.__bases__, ())
        self.assertEqual(comp.utilities.__bases__, ())
        self.assertEqual(comp._utility_registrations, {})
        self.assertEqual(comp._adapter_registrations, {})
        self.assertEqual(comp._subscription_registrations, [])
        self.assertEqual(comp._handler_registrations, [])

    def test_ctor_w_base(self):
        base = self._makeOne('base')
        comp = self._makeOne('testing', (base,))
        self.assertEqual(comp.__name__, 'testing')
        self.assertEqual(comp.__bases__, (base,))
        self.assertEqual(comp.adapters.__bases__, (base.adapters,))
        self.assertEqual(comp.utilities.__bases__, (base.utilities,))

    def test___repr__(self):
        comp = self._makeOne('testing')
        self.assertEqual(repr(comp), '<Components testing>')

    # test _init_registries / _init_registrations via only caller, __init__.

    def test_assign_to___bases__(self):
        base1 = self._makeOne('base1')
        base2 = self._makeOne('base2')
        comp = self._makeOne()
        comp.__bases__ = (base1, base2)
        self.assertEqual(comp.__bases__, (base1, base2))
        self.assertEqual(comp.adapters.__bases__,
                         (base1.adapters, base2.adapters))
        self.assertEqual(comp.utilities.__bases__,
                         (base1.utilities, base2.utilities))

    def test_registerUtility_with_component_name(self):
        from zope.interface.declarations import named, InterfaceClass
        from zope.interface._compat import _u

        class IFoo(InterfaceClass):
            pass
        ifoo = IFoo('IFoo')

        @named(_u('foo'))
        class Foo(object):
            pass
        foo = Foo()
        _info = _u('info')

        comp = self._makeOne()
        comp.registerUtility(foo, ifoo, info=_info)
        self.assertEqual(
            comp._utility_registrations[ifoo, _u('foo')],
            (foo, _info, None))

    def test_registerUtility_both_factory_and_component(self):
        def _factory():
            pass
        _to_reg = object()
        comp = self._makeOne()
        self.assertRaises(TypeError, comp.registerUtility,
                          component=_to_reg, factory=_factory)

    def test_registerUtility_w_component(self):
        from zope.interface.declarations import InterfaceClass
        from zope.interface.interfaces import Registered
        from zope.interface.registry import UtilityRegistration
        from zope.interface._compat import _u
        class IFoo(InterfaceClass):
            pass
        ifoo = IFoo('IFoo')
        _info = _u('info')
        _name = _u('name')
        _to_reg = object()
        comp = self._makeOne()
        _monkey, _events = self._wrapEvents()
        with _monkey:
            comp.registerUtility(_to_reg, ifoo, _name, _info)
        self.failUnless(comp.utilities._adapters[0][ifoo][_name] is _to_reg)
        self.assertEqual(comp._utility_registrations[ifoo, _name],
                         (_to_reg, _info, None))
        self.assertEqual(comp.utilities._subscribers[0][ifoo][''], (_to_reg,))
        self.assertEqual(len(_events), 1)
        args, kw = _events[0]
        event, = args
        self.assertEqual(kw, {})
        self.failUnless(isinstance(event, Registered))
        self.failUnless(isinstance(event.object, UtilityRegistration))
        self.failUnless(event.object.registry is comp)
        self.failUnless(event.object.provided is ifoo)
        self.failUnless(event.object.name is _name)
        self.failUnless(event.object.component is _to_reg)
        self.failUnless(event.object.info is _info)
        self.failUnless(event.object.factory is None)

    def test_registerUtility_w_factory(self):
        from zope.interface.declarations import InterfaceClass
        from zope.interface.interfaces import Registered
        from zope.interface.registry import UtilityRegistration
        from zope.interface._compat import _u
        class IFoo(InterfaceClass):
            pass
        ifoo = IFoo('IFoo')
        _info = _u('info')
        _name = _u('name')
        _to_reg = object()
        def _factory():
            return _to_reg
        comp = self._makeOne()
        _monkey, _events = self._wrapEvents()
        with _monkey:
            comp.registerUtility(None, ifoo, _name, _info, factory=_factory)
        self.assertEqual(len(_events), 1)
        args, kw = _events[0]
        event, = args
        self.assertEqual(kw, {})
        self.failUnless(isinstance(event, Registered))
        self.failUnless(isinstance(event.object, UtilityRegistration))
        self.failUnless(event.object.registry is comp)
        self.failUnless(event.object.provided is ifoo)
        self.failUnless(event.object.name is _name)
        self.failUnless(event.object.component is _to_reg)
        self.failUnless(event.object.info is _info)
        self.failUnless(event.object.factory is _factory)

    def test_registerUtility_no_provided_available(self):
        from zope.interface.declarations import InterfaceClass
        from zope.interface._compat import _u
        class IFoo(InterfaceClass):
            pass
        class Foo(object):
            pass
        ifoo = IFoo('IFoo')
        _info = _u('info')
        _name = _u('name')
        _to_reg = Foo()
        comp = self._makeOne()
        self.assertRaises(TypeError,
                          comp.registerUtility, _to_reg, None, _name, _info)

    def test_registerUtility_wo_provided(self):
        from zope.interface.declarations import directlyProvides
        from zope.interface.declarations import InterfaceClass
        from zope.interface.interfaces import Registered
        from zope.interface.registry import UtilityRegistration
        from zope.interface._compat import _u
        class IFoo(InterfaceClass):
            pass
        class Foo(object):
            pass
        ifoo = IFoo('IFoo')
        _info = _u('info')
        _name = _u('name')
        _to_reg = Foo()
        directlyProvides(_to_reg, ifoo)
        comp = self._makeOne()
        _monkey, _events = self._wrapEvents()
        with _monkey:
            comp.registerUtility(_to_reg, None, _name, _info)
        self.assertEqual(len(_events), 1)
        args, kw = _events[0]
        event, = args
        self.assertEqual(kw, {})
        self.failUnless(isinstance(event, Registered))
        self.failUnless(isinstance(event.object, UtilityRegistration))
        self.failUnless(event.object.registry is comp)
        self.failUnless(event.object.provided is ifoo)
        self.failUnless(event.object.name is _name)
        self.failUnless(event.object.component is _to_reg)
        self.failUnless(event.object.info is _info)
        self.failUnless(event.object.factory is None)

    def test_registerUtility_duplicates_existing_reg(self):
        from zope.interface.declarations import InterfaceClass
        from zope.interface._compat import _u
        class IFoo(InterfaceClass):
            pass
        ifoo = IFoo('IFoo')
        _info = _u('info')
        _name = _u('name')
        _to_reg = object()
        comp = self._makeOne()
        comp.registerUtility(_to_reg, ifoo, _name, _info)
        _monkey, _events = self._wrapEvents()
        with _monkey:
            comp.registerUtility(_to_reg, ifoo, _name, _info)
        self.assertEqual(len(_events), 0)

    def test_registerUtility_replaces_existing_reg(self):
        from zope.interface.declarations import InterfaceClass
        from zope.interface.interfaces import Unregistered
        from zope.interface.interfaces import Registered
        from zope.interface.registry import UtilityRegistration
        from zope.interface._compat import _u
        class IFoo(InterfaceClass):
            pass
        ifoo = IFoo('IFoo')
        _info = _u('info')
        _name = _u('name')
        _before, _after = object(), object()
        comp = self._makeOne()
        comp.registerUtility(_before, ifoo, _name, _info)
        _monkey, _events = self._wrapEvents()
        with _monkey:
            comp.registerUtility(_after, ifoo, _name, _info)
        self.assertEqual(len(_events), 2)
        args, kw = _events[0]
        event, = args
        self.assertEqual(kw, {})
        self.failUnless(isinstance(event, Unregistered))
        self.failUnless(isinstance(event.object, UtilityRegistration))
        self.failUnless(event.object.registry is comp)
        self.failUnless(event.object.provided is ifoo)
        self.failUnless(event.object.name is _name)
        self.failUnless(event.object.component is _before)
        self.failUnless(event.object.info is _info)
        self.failUnless(event.object.factory is None)
        args, kw = _events[1]
        event, = args
        self.assertEqual(kw, {})
        self.failUnless(isinstance(event, Registered))
        self.failUnless(isinstance(event.object, UtilityRegistration))
        self.failUnless(event.object.registry is comp)
        self.failUnless(event.object.provided is ifoo)
        self.failUnless(event.object.name is _name)
        self.failUnless(event.object.component is _after)
        self.failUnless(event.object.info is _info)
        self.failUnless(event.object.factory is None)

    def test_registerUtility_w_existing_subscr(self):
        from zope.interface.declarations import InterfaceClass
        from zope.interface._compat import _u
        class IFoo(InterfaceClass):
            pass
        ifoo = IFoo('IFoo')
        _info = _u('info')
        _name1 = _u('name1')
        _name2 = _u('name2')
        _to_reg = object()
        comp = self._makeOne()
        comp.registerUtility(_to_reg, ifoo, _name1, _info)
        _monkey, _events = self._wrapEvents()
        with _monkey:
            comp.registerUtility(_to_reg, ifoo, _name2, _info)
        self.assertEqual(comp.utilities._subscribers[0][ifoo][''], (_to_reg,))

    def test_registerUtility_wo_event(self):
        from zope.interface.declarations import InterfaceClass
        from zope.interface._compat import _u
        class IFoo(InterfaceClass):
            pass
        ifoo = IFoo('IFoo')
        _info = _u('info')
        _name = _u('name')
        _to_reg = object()
        comp = self._makeOne()
        _monkey, _events = self._wrapEvents()
        with _monkey:
            comp.registerUtility(_to_reg, ifoo, _name, _info, False)
        self.assertEqual(len(_events), 0)

    def test_unregisterUtility_neither_factory_nor_component_nor_provided(self):
        comp = self._makeOne()
        self.assertRaises(TypeError, comp.unregisterUtility,
                          component=None, provided=None, factory=None)

    def test_unregisterUtility_both_factory_and_component(self):
        def _factory():
            pass
        _to_reg = object()
        comp = self._makeOne()
        self.assertRaises(TypeError, comp.unregisterUtility,
                          component=_to_reg, factory=_factory)

    def test_unregisterUtility_w_component_miss(self):
        from zope.interface.declarations import InterfaceClass
        from zope.interface._compat import _u
        class IFoo(InterfaceClass):
            pass
        ifoo = IFoo('IFoo')
        _name = _u('name')
        _to_reg = object()
        comp = self._makeOne()
        _monkey, _events = self._wrapEvents()
        with _monkey:
            unreg = comp.unregisterUtility(_to_reg, ifoo, _name)
        self.failIf(unreg)
        self.failIf(_events)

    def test_unregisterUtility_w_component(self):
        from zope.interface.declarations import InterfaceClass
        from zope.interface.interfaces import Unregistered
        from zope.interface.registry import UtilityRegistration
        from zope.interface._compat import _u
        class IFoo(InterfaceClass):
            pass
        ifoo = IFoo('IFoo')
        _name = _u('name')
        _to_reg = object()
        comp = self._makeOne()
        comp.registerUtility(_to_reg, ifoo, _name)
        _monkey, _events = self._wrapEvents()
        with _monkey:
            unreg = comp.unregisterUtility(_to_reg, ifoo, _name)
        self.failUnless(unreg)
        self.failIf(comp.utilities._adapters) # all erased
        self.failIf((ifoo, _name) in comp._utility_registrations)
        self.failIf(comp.utilities._subscribers)
        self.assertEqual(len(_events), 1)
        args, kw = _events[0]
        event, = args
        self.assertEqual(kw, {})
        self.failUnless(isinstance(event, Unregistered))
        self.failUnless(isinstance(event.object, UtilityRegistration))
        self.failUnless(event.object.registry is comp)
        self.failUnless(event.object.provided is ifoo)
        self.failUnless(event.object.name is _name)
        self.failUnless(event.object.component is _to_reg)
        self.failUnless(event.object.factory is None)

    def test_unregisterUtility_w_factory(self):
        from zope.interface.declarations import InterfaceClass
        from zope.interface.interfaces import Unregistered
        from zope.interface.registry import UtilityRegistration
        from zope.interface._compat import _u
        class IFoo(InterfaceClass):
            pass
        ifoo = IFoo('IFoo')
        _info = _u('info')
        _name = _u('name')
        _to_reg = object()
        def _factory():
            return _to_reg
        comp = self._makeOne()
        comp.registerUtility(None, ifoo, _name, _info, factory=_factory)
        _monkey, _events = self._wrapEvents()
        with _monkey:
            unreg = comp.unregisterUtility(None, ifoo, _name, factory=_factory)
        self.failUnless(unreg)
        self.assertEqual(len(_events), 1)
        args, kw = _events[0]
        event, = args
        self.assertEqual(kw, {})
        self.failUnless(isinstance(event, Unregistered))
        self.failUnless(isinstance(event.object, UtilityRegistration))
        self.failUnless(event.object.registry is comp)
        self.failUnless(event.object.provided is ifoo)
        self.failUnless(event.object.name is _name)
        self.failUnless(event.object.component is _to_reg)
        self.failUnless(event.object.factory is _factory)

    def test_unregisterUtility_wo_explicit_provided(self):
        from zope.interface.declarations import directlyProvides
        from zope.interface.declarations import InterfaceClass
        from zope.interface.interfaces import Unregistered
        from zope.interface.registry import UtilityRegistration
        from zope.interface._compat import _u
        class IFoo(InterfaceClass):
            pass
        class Foo(object):
            pass
        ifoo = IFoo('IFoo')
        _info = _u('info')
        _name = _u('name')
        _to_reg = Foo()
        directlyProvides(_to_reg, ifoo)
        comp = self._makeOne()
        comp.registerUtility(_to_reg, ifoo, _name, _info)
        _monkey, _events = self._wrapEvents()
        with _monkey:
            unreg = comp.unregisterUtility(_to_reg, None, _name)
        self.failUnless(unreg)
        self.assertEqual(len(_events), 1)
        args, kw = _events[0]
        event, = args
        self.assertEqual(kw, {})
        self.failUnless(isinstance(event, Unregistered))
        self.failUnless(isinstance(event.object, UtilityRegistration))
        self.failUnless(event.object.registry is comp)
        self.failUnless(event.object.provided is ifoo)
        self.failUnless(event.object.name is _name)
        self.failUnless(event.object.component is _to_reg)
        self.failUnless(event.object.info is _info)
        self.failUnless(event.object.factory is None)

    def test_unregisterUtility_wo_component_or_factory(self):
        from zope.interface.declarations import directlyProvides
        from zope.interface.declarations import InterfaceClass
        from zope.interface.interfaces import Unregistered
        from zope.interface.registry import UtilityRegistration
        from zope.interface._compat import _u
        class IFoo(InterfaceClass):
            pass
        class Foo(object):
            pass
        ifoo = IFoo('IFoo')
        _info = _u('info')
        _name = _u('name')
        _to_reg = Foo()
        directlyProvides(_to_reg, ifoo)
        comp = self._makeOne()
        comp.registerUtility(_to_reg, ifoo, _name, _info)
        _monkey, _events = self._wrapEvents()
        with _monkey:
            # Just pass the interface / name
            unreg = comp.unregisterUtility(provided=ifoo, name=_name)
        self.failUnless(unreg)
        self.assertEqual(len(_events), 1)
        args, kw = _events[0]
        event, = args
        self.assertEqual(kw, {})
        self.failUnless(isinstance(event, Unregistered))
        self.failUnless(isinstance(event.object, UtilityRegistration))
        self.failUnless(event.object.registry is comp)
        self.failUnless(event.object.provided is ifoo)
        self.failUnless(event.object.name is _name)
        self.failUnless(event.object.component is _to_reg)
        self.failUnless(event.object.info is _info)
        self.failUnless(event.object.factory is None)

    def test_unregisterUtility_w_existing_subscr(self):
        from zope.interface.declarations import InterfaceClass
        from zope.interface._compat import _u
        class IFoo(InterfaceClass):
            pass
        ifoo = IFoo('IFoo')
        _info = _u('info')
        _name1 = _u('name1')
        _name2 = _u('name2')
        _to_reg = object()
        comp = self._makeOne()
        comp.registerUtility(_to_reg, ifoo, _name1, _info)
        comp.registerUtility(_to_reg, ifoo, _name2, _info)
        _monkey, _events = self._wrapEvents()
        with _monkey:
            comp.unregisterUtility(_to_reg, ifoo, _name2)
        self.assertEqual(comp.utilities._subscribers[0][ifoo][''], (_to_reg,))

    def test_registeredUtilities_empty(self):
        comp = self._makeOne()
        self.assertEqual(list(comp.registeredUtilities()), [])

    def test_registeredUtilities_notempty(self):
        from zope.interface.declarations import InterfaceClass
        from zope.interface._compat import _u
        from zope.interface.registry import UtilityRegistration
        class IFoo(InterfaceClass):
            pass
        ifoo = IFoo('IFoo')
        _info = _u('info')
        _name1 = _u('name1')
        _name2 = _u('name2')
        _to_reg = object()
        comp = self._makeOne()
        comp.registerUtility(_to_reg, ifoo, _name1, _info)
        comp.registerUtility(_to_reg, ifoo, _name2, _info)
        reg = sorted(comp.registeredUtilities(), key=lambda r: r.name)
        self.assertEqual(len(reg), 2)
        self.failUnless(isinstance(reg[0], UtilityRegistration))
        self.failUnless(reg[0].registry is comp)
        self.failUnless(reg[0].provided is ifoo)
        self.failUnless(reg[0].name is _name1)
        self.failUnless(reg[0].component is _to_reg)
        self.failUnless(reg[0].info is _info)
        self.failUnless(reg[0].factory is None)
        self.failUnless(isinstance(reg[1], UtilityRegistration))
        self.failUnless(reg[1].registry is comp)
        self.failUnless(reg[1].provided is ifoo)
        self.failUnless(reg[1].name is _name2)
        self.failUnless(reg[1].component is _to_reg)
        self.failUnless(reg[1].info is _info)
        self.failUnless(reg[1].factory is None)

    def test_queryUtility_miss_no_default(self):
        from zope.interface.declarations import InterfaceClass
        class IFoo(InterfaceClass):
            pass
        ifoo = IFoo('IFoo')
        comp = self._makeOne()
        self.failUnless(comp.queryUtility(ifoo) is None)

    def test_queryUtility_miss_w_default(self):
        from zope.interface.declarations import InterfaceClass
        class IFoo(InterfaceClass):
            pass
        ifoo = IFoo('IFoo')
        comp = self._makeOne()
        _default = object()
        self.failUnless(comp.queryUtility(ifoo, default=_default) is _default)

    def test_queryUtility_hit(self):
        from zope.interface.declarations import InterfaceClass
        class IFoo(InterfaceClass):
            pass
        ifoo = IFoo('IFoo')
        _to_reg = object()
        comp = self._makeOne()
        comp.registerUtility(_to_reg, ifoo)
        self.failUnless(comp.queryUtility(ifoo) is _to_reg)

    def test_getUtility_miss(self):
        from zope.interface.declarations import InterfaceClass
        from zope.interface.interfaces import ComponentLookupError
        class IFoo(InterfaceClass):
            pass
        ifoo = IFoo('IFoo')
        comp = self._makeOne()
        self.assertRaises(ComponentLookupError, comp.getUtility, ifoo)

    def test_getUtility_hit(self):
        from zope.interface.declarations import InterfaceClass
        class IFoo(InterfaceClass):
            pass
        ifoo = IFoo('IFoo')
        _to_reg = object()
        comp = self._makeOne()
        comp.registerUtility(_to_reg, ifoo)
        self.failUnless(comp.getUtility(ifoo) is _to_reg)

    def test_getUtilitiesFor_miss(self):
        from zope.interface.declarations import InterfaceClass
        class IFoo(InterfaceClass):
            pass
        ifoo = IFoo('IFoo')
        comp = self._makeOne()
        self.assertEqual(list(comp.getUtilitiesFor(ifoo)), [])

    def test_getUtilitiesFor_hit(self):
        from zope.interface.declarations import InterfaceClass
        from zope.interface._compat import _u
        class IFoo(InterfaceClass):
            pass
        ifoo = IFoo('IFoo')
        _name1 = _u('name1')
        _name2 = _u('name2')
        _to_reg = object()
        comp = self._makeOne()
        comp.registerUtility(_to_reg, ifoo, name=_name1)
        comp.registerUtility(_to_reg, ifoo, name=_name2)
        self.assertEqual(sorted(comp.getUtilitiesFor(ifoo)),
                         [(_name1, _to_reg), (_name2, _to_reg)])

    def test_getAllUtilitiesRegisteredFor_miss(self):
        from zope.interface.declarations import InterfaceClass
        class IFoo(InterfaceClass):
            pass
        ifoo = IFoo('IFoo')
        comp = self._makeOne()
        self.assertEqual(list(comp.getAllUtilitiesRegisteredFor(ifoo)), [])

    def test_getAllUtilitiesRegisteredFor_hit(self):
        from zope.interface.declarations import InterfaceClass
        from zope.interface._compat import _u
        class IFoo(InterfaceClass):
            pass
        ifoo = IFoo('IFoo')
        _name1 = _u('name1')
        _name2 = _u('name2')
        _to_reg = object()
        comp = self._makeOne()
        comp.registerUtility(_to_reg, ifoo, name=_name1)
        comp.registerUtility(_to_reg, ifoo, name=_name2)
        self.assertEqual(list(comp.getAllUtilitiesRegisteredFor(ifoo)),
                         [_to_reg])

    def test_registerAdapter_with_component_name(self):
        from zope.interface.declarations import named, InterfaceClass
        from zope.interface._compat import _u

        class IFoo(InterfaceClass):
            pass
        ifoo = IFoo('IFoo')
        ibar = IFoo('IBar')

        @named(_u('foo'))
        class Foo(object):
            pass
        _info = _u('info')

        comp = self._makeOne()
        comp.registerAdapter(Foo, (ibar,), ifoo, info=_info)

        self.assertEqual(
            comp._adapter_registrations[(ibar,), ifoo, _u('foo')],
            (Foo, _info))

    def test_registerAdapter_w_explicit_provided_and_required(self):
        from zope.interface.declarations import InterfaceClass
        from zope.interface.interfaces import Registered
        from zope.interface.registry import AdapterRegistration
        from zope.interface._compat import _u
        class IFoo(InterfaceClass):
            pass
        ifoo = IFoo('IFoo')
        ibar = IFoo('IBar')
        _info = _u('info')
        _name = _u('name')
        _to_reg = object()
        def _factory(context):
            return _to_reg
        comp = self._makeOne()
        _monkey, _events = self._wrapEvents()
        with _monkey:
            comp.registerAdapter(_factory, (ibar,), ifoo, _name, _info)
        self.failUnless(comp.adapters._adapters[1][ibar][ifoo][_name]
                        is _factory)
        self.assertEqual(comp._adapter_registrations[(ibar,), ifoo, _name],
                         (_factory, _info))
        self.assertEqual(len(_events), 1)
        args, kw = _events[0]
        event, = args
        self.assertEqual(kw, {})
        self.failUnless(isinstance(event, Registered))
        self.failUnless(isinstance(event.object, AdapterRegistration))
        self.failUnless(event.object.registry is comp)
        self.failUnless(event.object.provided is ifoo)
        self.assertEqual(event.object.required, (ibar,))
        self.failUnless(event.object.name is _name)
        self.failUnless(event.object.info is _info)
        self.failUnless(event.object.factory is _factory)

    def test_registerAdapter_no_provided_available(self):
        from zope.interface.declarations import InterfaceClass
        from zope.interface._compat import _u
        class IFoo(InterfaceClass):
            pass
        ifoo = IFoo('IFoo')
        ibar = IFoo('IBar')
        _info = _u('info')
        _name = _u('name')
        _to_reg = object()
        class _Factory(object):
            def __init__(self, context):
                self._context = context
        comp = self._makeOne()
        self.assertRaises(TypeError, comp.registerAdapter, _Factory, (ibar,),
                          name=_name, info=_info)

    def test_registerAdapter_wo_explicit_provided(self):
        from zope.interface.declarations import InterfaceClass
        from zope.interface.declarations import implementer
        from zope.interface.interfaces import Registered
        from zope.interface.registry import AdapterRegistration
        from zope.interface._compat import _u
        class IFoo(InterfaceClass):
            pass
        ifoo = IFoo('IFoo')
        ibar = IFoo('IBar')
        _info = _u('info')
        _name = _u('name')
        _to_reg = object()
        @implementer(ifoo)
        class _Factory(object):
            def __init__(self, context):
                self._context = context
        comp = self._makeOne()
        _monkey, _events = self._wrapEvents()
        with _monkey:
            comp.registerAdapter(_Factory, (ibar,), name=_name, info=_info)
        self.failUnless(comp.adapters._adapters[1][ibar][ifoo][_name]
                        is _Factory)
        self.assertEqual(comp._adapter_registrations[(ibar,), ifoo, _name],
                         (_Factory, _info))
        self.assertEqual(len(_events), 1)
        args, kw = _events[0]
        event, = args
        self.assertEqual(kw, {})
        self.failUnless(isinstance(event, Registered))
        self.failUnless(isinstance(event.object, AdapterRegistration))
        self.failUnless(event.object.registry is comp)
        self.failUnless(event.object.provided is ifoo)
        self.assertEqual(event.object.required, (ibar,))
        self.failUnless(event.object.name is _name)
        self.failUnless(event.object.info is _info)
        self.failUnless(event.object.factory is _Factory)

    def test_registerAdapter_no_required_available(self):
        from zope.interface.declarations import InterfaceClass
        from zope.interface._compat import _u
        class IFoo(InterfaceClass):
            pass
        ifoo = IFoo('IFoo')
        ibar = IFoo('IBar')
        _info = _u('info')
        _name = _u('name')
        class _Factory(object):
            def __init__(self, context):
                self._context = context
        comp = self._makeOne()
        self.assertRaises(TypeError, comp.registerAdapter, _Factory,
                          provided=ifoo, name=_name, info=_info)

    def test_registerAdapter_w_invalid_required(self):
        from zope.interface.declarations import InterfaceClass
        from zope.interface._compat import _u
        class IFoo(InterfaceClass):
            pass
        ifoo = IFoo('IFoo')
        ibar = IFoo('IBar')
        _info = _u('info')
        _name = _u('name')
        class _Factory(object):
            def __init__(self, context):
                self._context = context
        comp = self._makeOne()
        self.assertRaises(TypeError, comp.registerAdapter, _Factory,
                          ibar, provided=ifoo, name=_name, info=_info)

    def test_registerAdapter_w_required_containing_None(self):
        from zope.interface.declarations import InterfaceClass
        from zope.interface.interface import Interface
        from zope.interface.interfaces import Registered
        from zope.interface.registry import AdapterRegistration
        from zope.interface._compat import _u
        class IFoo(InterfaceClass):
            pass
        ifoo = IFoo('IFoo')
        _info = _u('info')
        _name = _u('name')
        class _Factory(object):
            def __init__(self, context):
                self._context = context
        comp = self._makeOne()
        _monkey, _events = self._wrapEvents()
        with _monkey:
            comp.registerAdapter(_Factory, [None], provided=ifoo,
                                 name=_name, info=_info)
        self.failUnless(comp.adapters._adapters[1][Interface][ifoo][_name]
                        is _Factory)
        self.assertEqual(comp._adapter_registrations[(Interface,), ifoo, _name],
                         (_Factory, _info))
        self.assertEqual(len(_events), 1)
        args, kw = _events[0]
        event, = args
        self.assertEqual(kw, {})
        self.failUnless(isinstance(event, Registered))
        self.failUnless(isinstance(event.object, AdapterRegistration))
        self.failUnless(event.object.registry is comp)
        self.failUnless(event.object.provided is ifoo)
        self.assertEqual(event.object.required, (Interface,))
        self.failUnless(event.object.name is _name)
        self.failUnless(event.object.info is _info)
        self.failUnless(event.object.factory is _Factory)

    def test_registerAdapter_w_required_containing_class(self):
        from zope.interface.declarations import InterfaceClass
        from zope.interface.declarations import implementer
        from zope.interface.declarations import implementedBy
        from zope.interface.interfaces import Registered
        from zope.interface.registry import AdapterRegistration
        from zope.interface._compat import _u
        class IFoo(InterfaceClass):
            pass
        ifoo = IFoo('IFoo')
        ibar = IFoo('IBar')
        _info = _u('info')
        _name = _u('name')
        class _Factory(object):
            def __init__(self, context):
                self._context = context
        @implementer(ibar)
        class _Context(object):
            pass
        _ctx_impl = implementedBy(_Context)
        comp = self._makeOne()
        _monkey, _events = self._wrapEvents()
        with _monkey:
            comp.registerAdapter(_Factory, [_Context], provided=ifoo,
                                 name=_name, info=_info)
        self.failUnless(comp.adapters._adapters[1][_ctx_impl][ifoo][_name]
                        is _Factory)
        self.assertEqual(comp._adapter_registrations[(_ctx_impl,), ifoo, _name],
                         (_Factory, _info))
        self.assertEqual(len(_events), 1)
        args, kw = _events[0]
        event, = args
        self.assertEqual(kw, {})
        self.failUnless(isinstance(event, Registered))
        self.failUnless(isinstance(event.object, AdapterRegistration))
        self.failUnless(event.object.registry is comp)
        self.failUnless(event.object.provided is ifoo)
        self.assertEqual(event.object.required, (_ctx_impl,))
        self.failUnless(event.object.name is _name)
        self.failUnless(event.object.info is _info)
        self.failUnless(event.object.factory is _Factory)

    def test_registerAdapter_w_required_containing_junk(self):
        from zope.interface.declarations import InterfaceClass
        from zope.interface._compat import _u
        class IFoo(InterfaceClass):
            pass
        ifoo = IFoo('IFoo')
        ibar = IFoo('IBar')
        _info = _u('info')
        _name = _u('name')
        class _Factory(object):
            def __init__(self, context):
                self._context = context
        comp = self._makeOne()
        self.assertRaises(TypeError, comp.registerAdapter, _Factory, [object()],
                          provided=ifoo, name=_name, info=_info)

    def test_registerAdapter_wo_explicit_required(self):
        from zope.interface.declarations import InterfaceClass
        from zope.interface.interfaces import Registered
        from zope.interface.registry import AdapterRegistration
        from zope.interface._compat import _u
        class IFoo(InterfaceClass):
            pass
        ifoo = IFoo('IFoo')
        ibar = IFoo('IBar')
        _info = _u('info')
        _name = _u('name')
        class _Factory(object):
            __component_adapts__ = (ibar,)
            def __init__(self, context):
                self._context = context
        comp = self._makeOne()
        _monkey, _events = self._wrapEvents()
        with _monkey:
            comp.registerAdapter(_Factory, provided=ifoo, name=_name,
                                 info=_info)
        self.failUnless(comp.adapters._adapters[1][ibar][ifoo][_name]
                        is _Factory)
        self.assertEqual(comp._adapter_registrations[(ibar,), ifoo, _name],
                         (_Factory, _info))
        self.assertEqual(len(_events), 1)
        args, kw = _events[0]
        event, = args
        self.assertEqual(kw, {})
        self.failUnless(isinstance(event, Registered))
        self.failUnless(isinstance(event.object, AdapterRegistration))
        self.failUnless(event.object.registry is comp)
        self.failUnless(event.object.provided is ifoo)
        self.assertEqual(event.object.required, (ibar,))
        self.failUnless(event.object.name is _name)
        self.failUnless(event.object.info is _info)
        self.failUnless(event.object.factory is _Factory)

    def test_registerAdapter_wo_event(self):
        from zope.interface.declarations import InterfaceClass
        from zope.interface._compat import _u
        class IFoo(InterfaceClass):
            pass
        ifoo = IFoo('IFoo')
        ibar = IFoo('IBar')
        _info = _u('info')
        _name = _u('name')
        _to_reg = object()
        def _factory(context):
            return _to_reg
        comp = self._makeOne()
        _monkey, _events = self._wrapEvents()
        with _monkey:
            comp.registerAdapter(_factory, (ibar,), ifoo, _name, _info,
                                 event=False)
        self.assertEqual(len(_events), 0)

    def test_unregisterAdapter_neither_factory_nor_provided(self):
        comp = self._makeOne()
        self.assertRaises(TypeError, comp.unregisterAdapter,
                          factory=None, provided=None)

    def test_unregisterAdapter_neither_factory_nor_required(self):
        from zope.interface.declarations import InterfaceClass
        class IFoo(InterfaceClass):
            pass
        ifoo = IFoo('IFoo')
        comp = self._makeOne()
        self.assertRaises(TypeError, comp.unregisterAdapter,
                          factory=None, provided=ifoo, required=None)

    def test_unregisterAdapter_miss(self):
        from zope.interface.declarations import InterfaceClass
        class IFoo(InterfaceClass):
            pass
        ifoo = IFoo('IFoo')
        ibar = IFoo('IBar')
        class _Factory(object):
            def __init__(self, context):
                self._context = context
        comp = self._makeOne()
        _monkey, _events = self._wrapEvents()
        with _monkey:
            unreg = comp.unregisterAdapter(_Factory, (ibar,), ifoo)
        self.failIf(unreg)

    def test_unregisterAdapter_hit_w_explicit_provided_and_required(self):
        from zope.interface.declarations import InterfaceClass
        from zope.interface.interfaces import Unregistered
        from zope.interface.registry import AdapterRegistration
        class IFoo(InterfaceClass):
            pass
        ifoo = IFoo('IFoo')
        ibar = IFoo('IBar')
        class _Factory(object):
            def __init__(self, context):
                self._context = context
        comp = self._makeOne()
        comp.registerAdapter(_Factory, (ibar,), ifoo)
        _monkey, _events = self._wrapEvents()
        with _monkey:
            unreg = comp.unregisterAdapter(_Factory, (ibar,), ifoo)
        self.failUnless(unreg)
        self.failIf(comp.adapters._adapters)
        self.failIf(comp._adapter_registrations)
        self.assertEqual(len(_events), 1)
        args, kw = _events[0]
        event, = args
        self.assertEqual(kw, {})
        self.failUnless(isinstance(event, Unregistered))
        self.failUnless(isinstance(event.object, AdapterRegistration))
        self.failUnless(event.object.registry is comp)
        self.failUnless(event.object.provided is ifoo)
        self.assertEqual(event.object.required, (ibar,))
        self.assertEqual(event.object.name, '')
        self.assertEqual(event.object.info, '')
        self.failUnless(event.object.factory is _Factory)

    def test_unregisterAdapter_wo_explicit_provided(self):
        from zope.interface.declarations import InterfaceClass
        from zope.interface.declarations import implementer
        from zope.interface.interfaces import Unregistered
        from zope.interface.registry import AdapterRegistration
        class IFoo(InterfaceClass):
            pass
        ifoo = IFoo('IFoo')
        ibar = IFoo('IBar')
        @implementer(ifoo)
        class _Factory(object):
            def __init__(self, context):
                self._context = context
        comp = self._makeOne()
        comp.registerAdapter(_Factory, (ibar,), ifoo)
        _monkey, _events = self._wrapEvents()
        with _monkey:
            unreg = comp.unregisterAdapter(_Factory, (ibar,))
        self.failUnless(unreg)
        self.assertEqual(len(_events), 1)
        args, kw = _events[0]
        event, = args
        self.assertEqual(kw, {})
        self.failUnless(isinstance(event, Unregistered))
        self.failUnless(isinstance(event.object, AdapterRegistration))
        self.failUnless(event.object.registry is comp)
        self.failUnless(event.object.provided is ifoo)
        self.assertEqual(event.object.required, (ibar,))
        self.assertEqual(event.object.name, '')
        self.assertEqual(event.object.info, '')
        self.failUnless(event.object.factory is _Factory)

    def test_unregisterAdapter_wo_explicit_required(self):
        from zope.interface.declarations import InterfaceClass
        from zope.interface.interfaces import Unregistered
        from zope.interface.registry import AdapterRegistration
        class IFoo(InterfaceClass):
            pass
        ifoo = IFoo('IFoo')
        ibar = IFoo('IBar')
        class _Factory(object):
            __component_adapts__ = (ibar,)
            def __init__(self, context):
                self._context = context
        comp = self._makeOne()
        comp.registerAdapter(_Factory, (ibar,), ifoo)
        _monkey, _events = self._wrapEvents()
        with _monkey:
            unreg = comp.unregisterAdapter(_Factory, provided=ifoo)
        self.failUnless(unreg)
        self.assertEqual(len(_events), 1)
        args, kw = _events[0]
        event, = args
        self.assertEqual(kw, {})
        self.failUnless(isinstance(event, Unregistered))
        self.failUnless(isinstance(event.object, AdapterRegistration))
        self.failUnless(event.object.registry is comp)
        self.failUnless(event.object.provided is ifoo)
        self.assertEqual(event.object.required, (ibar,))
        self.assertEqual(event.object.name, '')
        self.assertEqual(event.object.info, '')
        self.failUnless(event.object.factory is _Factory)

    def test_registeredAdapters_empty(self):
        comp = self._makeOne()
        self.assertEqual(list(comp.registeredAdapters()), [])

    def test_registeredAdapters_notempty(self):
        from zope.interface.declarations import InterfaceClass
        from zope.interface._compat import _u
        from zope.interface.registry import AdapterRegistration
        class IFoo(InterfaceClass):
            pass
        ifoo = IFoo('IFoo')
        ibar = IFoo('IFoo')
        _info = _u('info')
        _name1 = _u('name1')
        _name2 = _u('name2')
        class _Factory(object):
            def __init__(self, context):
                pass
        comp = self._makeOne()
        comp.registerAdapter(_Factory, (ibar,), ifoo, _name1, _info)
        comp.registerAdapter(_Factory, (ibar,), ifoo, _name2, _info)
        reg = sorted(comp.registeredAdapters(), key=lambda r: r.name)
        self.assertEqual(len(reg), 2)
        self.failUnless(isinstance(reg[0], AdapterRegistration))
        self.failUnless(reg[0].registry is comp)
        self.failUnless(reg[0].provided is ifoo)
        self.assertEqual(reg[0].required, (ibar,))
        self.failUnless(reg[0].name is _name1)
        self.failUnless(reg[0].info is _info)
        self.failUnless(reg[0].factory is _Factory)
        self.failUnless(isinstance(reg[1], AdapterRegistration))
        self.failUnless(reg[1].registry is comp)
        self.failUnless(reg[1].provided is ifoo)
        self.assertEqual(reg[1].required, (ibar,))
        self.failUnless(reg[1].name is _name2)
        self.failUnless(reg[1].info is _info)
        self.failUnless(reg[1].factory is _Factory)

    def test_queryAdapter_miss_no_default(self):
        from zope.interface.declarations import InterfaceClass
        class IFoo(InterfaceClass):
            pass
        ifoo = IFoo('IFoo')
        comp = self._makeOne()
        _context = object()
        self.failUnless(comp.queryAdapter(_context, ifoo) is None)

    def test_queryAdapter_miss_w_default(self):
        from zope.interface.declarations import InterfaceClass
        class IFoo(InterfaceClass):
            pass
        ifoo = IFoo('IFoo')
        comp = self._makeOne()
        _context = object()
        _default = object()
        self.failUnless(
            comp.queryAdapter(_context, ifoo, default=_default) is _default)

    def test_queryAdapter_hit(self):
        from zope.interface.declarations import InterfaceClass
        from zope.interface.declarations import implementer
        class IFoo(InterfaceClass):
            pass
        ifoo = IFoo('IFoo')
        ibar = IFoo('IBar')
        class _Factory(object):
            def __init__(self, context):
                self.context = context
        @implementer(ibar)
        class _Context(object):
            pass
        _context = _Context()
        comp = self._makeOne()
        comp.registerAdapter(_Factory, (ibar,), ifoo)
        adapter = comp.queryAdapter(_context, ifoo)
        self.failUnless(isinstance(adapter, _Factory))
        self.failUnless(adapter.context is _context)

    def test_getAdapter_miss(self):
        from zope.interface.declarations import InterfaceClass
        from zope.interface.declarations import implementer
        from zope.interface.interfaces import ComponentLookupError
        class IFoo(InterfaceClass):
            pass
        ifoo = IFoo('IFoo')
        ibar = IFoo('IBar')
        @implementer(ibar)
        class _Context(object):
            pass
        _context = _Context()
        comp = self._makeOne()
        self.assertRaises(ComponentLookupError,
                          comp.getAdapter, _context, ifoo)

    def test_getAdapter_hit(self):
        from zope.interface.declarations import InterfaceClass
        from zope.interface.declarations import implementer
        class IFoo(InterfaceClass):
            pass
        ifoo = IFoo('IFoo')
        ibar = IFoo('IBar')
        class _Factory(object):
            def __init__(self, context):
                self.context = context
        @implementer(ibar)
        class _Context(object):
            pass
        _context = _Context()
        comp = self._makeOne()
        comp.registerAdapter(_Factory, (ibar,), ifoo)
        adapter = comp.getAdapter(_context, ifoo)
        self.failUnless(isinstance(adapter, _Factory))
        self.failUnless(adapter.context is _context)

    def test_queryMultiAdapter_miss(self):
        from zope.interface.declarations import InterfaceClass
        from zope.interface.declarations import implementer
        class IFoo(InterfaceClass):
            pass
        ifoo = IFoo('IFoo')
        ibar = IFoo('IBar')
        ibaz = IFoo('IBaz')
        @implementer(ibar)
        class _Context1(object):
            pass
        @implementer(ibaz)
        class _Context2(object):
            pass
        _context1 = _Context1()
        _context2 = _Context2()
        comp = self._makeOne()
        self.assertEqual(comp.queryMultiAdapter((_context1, _context2), ifoo),
                         None)

    def test_queryMultiAdapter_miss_w_default(self):
        from zope.interface.declarations import InterfaceClass
        from zope.interface.declarations import implementer
        class IFoo(InterfaceClass):
            pass
        ifoo = IFoo('IFoo')
        ibar = IFoo('IBar')
        ibaz = IFoo('IBaz')
        @implementer(ibar)
        class _Context1(object):
            pass
        @implementer(ibaz)
        class _Context2(object):
            pass
        _context1 = _Context1()
        _context2 = _Context2()
        _default = object()
        comp = self._makeOne()
        self.failUnless(
            comp.queryMultiAdapter((_context1, _context2), ifoo,
                                   default=_default) is _default)

    def test_queryMultiAdapter_hit(self):
        from zope.interface.declarations import InterfaceClass
        from zope.interface.declarations import implementer
        class IFoo(InterfaceClass):
            pass
        ifoo = IFoo('IFoo')
        ibar = IFoo('IBar')
        ibaz = IFoo('IBaz')
        @implementer(ibar)
        class _Context1(object):
            pass
        @implementer(ibaz)
        class _Context2(object):
            pass
        _context1 = _Context1()
        _context2 = _Context2()
        class _Factory(object):
            def __init__(self, context1, context2):
                self.context = context1, context2
        comp = self._makeOne()
        comp.registerAdapter(_Factory, (ibar, ibaz), ifoo)
        adapter = comp.queryMultiAdapter((_context1, _context2), ifoo)
        self.failUnless(isinstance(adapter, _Factory))
        self.assertEqual(adapter.context, (_context1, _context2))

    def test_getMultiAdapter_miss(self):
        from zope.interface.declarations import InterfaceClass
        from zope.interface.declarations import implementer
        from zope.interface.interfaces import ComponentLookupError
        class IFoo(InterfaceClass):
            pass
        ifoo = IFoo('IFoo')
        ibar = IFoo('IBar')
        ibaz = IFoo('IBaz')
        @implementer(ibar)
        class _Context1(object):
            pass
        @implementer(ibaz)
        class _Context2(object):
            pass
        _context1 = _Context1()
        _context2 = _Context2()
        comp = self._makeOne()
        self.assertRaises(ComponentLookupError,
                          comp.getMultiAdapter, (_context1, _context2), ifoo)

    def test_getMultiAdapter_hit(self):
        from zope.interface.declarations import InterfaceClass
        from zope.interface.declarations import implementer
        class IFoo(InterfaceClass):
            pass
        ifoo = IFoo('IFoo')
        ibar = IFoo('IBar')
        ibaz = IFoo('IBaz')
        @implementer(ibar)
        class _Context1(object):
            pass
        @implementer(ibaz)
        class _Context2(object):
            pass
        _context1 = _Context1()
        _context2 = _Context2()
        class _Factory(object):
            def __init__(self, context1, context2):
                self.context = context1, context2
        comp = self._makeOne()
        comp.registerAdapter(_Factory, (ibar, ibaz), ifoo)
        adapter = comp.getMultiAdapter((_context1, _context2), ifoo)
        self.failUnless(isinstance(adapter, _Factory))
        self.assertEqual(adapter.context, (_context1, _context2))

    def test_getAdapters_empty(self):
        from zope.interface.declarations import InterfaceClass
        from zope.interface.declarations import implementer
        class IFoo(InterfaceClass):
            pass
        ifoo = IFoo('IFoo')
        ibar = IFoo('IBar')
        ibaz = IFoo('IBaz')
        @implementer(ibar)
        class _Context1(object):
            pass
        @implementer(ibaz)
        class _Context2(object):
            pass
        _context1 = _Context1()
        _context2 = _Context2()
        comp = self._makeOne()
        self.assertEqual(
            list(comp.getAdapters((_context1, _context2), ifoo)), [])

    def test_getAdapters_non_empty(self):
        from zope.interface.declarations import InterfaceClass
        from zope.interface.declarations import implementer
        from zope.interface._compat import _u
        class IFoo(InterfaceClass):
            pass
        ifoo = IFoo('IFoo')
        ibar = IFoo('IBar')
        ibaz = IFoo('IBaz')
        @implementer(ibar)
        class _Context1(object):
            pass
        @implementer(ibaz)
        class _Context2(object):
            pass
        _context1 = _Context1()
        _context2 = _Context2()
        class _Factory1(object):
            def __init__(self, context1, context2):
                self.context = context1, context2
        class _Factory2(object):
            def __init__(self, context1, context2):
                self.context = context1, context2
        _name1 = _u('name1')
        _name2 = _u('name2')
        comp = self._makeOne()
        comp.registerAdapter(_Factory1, (ibar, ibaz), ifoo, name=_name1)
        comp.registerAdapter(_Factory2, (ibar, ibaz), ifoo, name=_name2)
        found = sorted(comp.getAdapters((_context1, _context2), ifoo))
        self.assertEqual(len(found), 2)
        self.assertEqual(found[0][0], _name1)
        self.failUnless(isinstance(found[0][1], _Factory1))
        self.assertEqual(found[1][0], _name2)
        self.failUnless(isinstance(found[1][1], _Factory2))

    def test_registerSubscriptionAdapter_w_nonblank_name(self):
        from zope.interface.declarations import InterfaceClass
        from zope.interface._compat import _u
        class IFoo(InterfaceClass):
            pass
        ifoo = IFoo('IFoo')
        ibar = IFoo('IBar')
        _name = _u('name')
        _info = _u('info')
        _to_reg = object()
        def _factory(context):
            return _to_reg
        comp = self._makeOne()
        self.assertRaises(TypeError, comp.registerSubscriptionAdapter,
                          _factory, (ibar,), ifoo, _name, _info)

    def test_registerSubscriptionAdapter_w_explicit_provided_and_required(self):
        from zope.interface.declarations import InterfaceClass
        from zope.interface.interfaces import Registered
        from zope.interface.registry import SubscriptionRegistration
        from zope.interface._compat import _u
        class IFoo(InterfaceClass):
            pass
        ifoo = IFoo('IFoo')
        ibar = IFoo('IBar')
        _blank = _u('')
        _info = _u('info')
        _to_reg = object()
        def _factory(context):
            return _to_reg
        comp = self._makeOne()
        _monkey, _events = self._wrapEvents()
        with _monkey:
            comp.registerSubscriptionAdapter(_factory, (ibar,), ifoo,
                                             info=_info)
        reg = comp.adapters._subscribers[1][ibar][ifoo][_blank]
        self.assertEqual(len(reg), 1)
        self.failUnless(reg[0] is _factory)
        self.assertEqual(comp._subscription_registrations,
                         [((ibar,), ifoo, _blank, _factory, _info)])
        self.assertEqual(len(_events), 1)
        args, kw = _events[0]
        event, = args
        self.assertEqual(kw, {})
        self.failUnless(isinstance(event, Registered))
        self.failUnless(isinstance(event.object, SubscriptionRegistration))
        self.failUnless(event.object.registry is comp)
        self.failUnless(event.object.provided is ifoo)
        self.assertEqual(event.object.required, (ibar,))
        self.assertEqual(event.object.name, _blank)
        self.failUnless(event.object.info is _info)
        self.failUnless(event.object.factory is _factory)

    def test_registerSubscriptionAdapter_wo_explicit_provided(self):
        from zope.interface.declarations import InterfaceClass
        from zope.interface.declarations import implementer
        from zope.interface.interfaces import Registered
        from zope.interface.registry import SubscriptionRegistration
        from zope.interface._compat import _u
        class IFoo(InterfaceClass):
            pass
        ifoo = IFoo('IFoo')
        ibar = IFoo('IBar')
        _info = _u('info')
        _blank = _u('')
        _to_reg = object()
        @implementer(ifoo)
        class _Factory(object):
            def __init__(self, context):
                self._context = context
        comp = self._makeOne()
        _monkey, _events = self._wrapEvents()
        with _monkey:
            comp.registerSubscriptionAdapter(_Factory, (ibar,), info=_info)
        reg = comp.adapters._subscribers[1][ibar][ifoo][_blank]
        self.assertEqual(len(reg), 1)
        self.failUnless(reg[0] is _Factory)
        self.assertEqual(comp._subscription_registrations,
                         [((ibar,), ifoo, _blank, _Factory, _info)])
        self.assertEqual(len(_events), 1)
        args, kw = _events[0]
        event, = args
        self.assertEqual(kw, {})
        self.failUnless(isinstance(event, Registered))
        self.failUnless(isinstance(event.object, SubscriptionRegistration))
        self.failUnless(event.object.registry is comp)
        self.failUnless(event.object.provided is ifoo)
        self.assertEqual(event.object.required, (ibar,))
        self.assertEqual(event.object.name, _blank)
        self.failUnless(event.object.info is _info)
        self.failUnless(event.object.factory is _Factory)

    def test_registerSubscriptionAdapter_wo_explicit_required(self):
        from zope.interface.declarations import InterfaceClass
        from zope.interface.interfaces import Registered
        from zope.interface.registry import SubscriptionRegistration
        from zope.interface._compat import _u
        class IFoo(InterfaceClass):
            pass
        ifoo = IFoo('IFoo')
        ibar = IFoo('IBar')
        _info = _u('info')
        _blank = _u('')
        class _Factory(object):
            __component_adapts__ = (ibar,)
            def __init__(self, context):
                self._context = context
        comp = self._makeOne()
        _monkey, _events = self._wrapEvents()
        with _monkey:
            comp.registerSubscriptionAdapter(
                    _Factory, provided=ifoo, info=_info)
        reg = comp.adapters._subscribers[1][ibar][ifoo][_blank]
        self.assertEqual(len(reg), 1)
        self.failUnless(reg[0] is _Factory)
        self.assertEqual(comp._subscription_registrations,
                         [((ibar,), ifoo, _blank, _Factory, _info)])
        self.assertEqual(len(_events), 1)
        args, kw = _events[0]
        event, = args
        self.assertEqual(kw, {})
        self.failUnless(isinstance(event, Registered))
        self.failUnless(isinstance(event.object, SubscriptionRegistration))
        self.failUnless(event.object.registry is comp)
        self.failUnless(event.object.provided is ifoo)
        self.assertEqual(event.object.required, (ibar,))
        self.assertEqual(event.object.name, _blank)
        self.failUnless(event.object.info is _info)
        self.failUnless(event.object.factory is _Factory)

    def test_registerSubscriptionAdapter_wo_event(self):
        from zope.interface.declarations import InterfaceClass
        from zope.interface._compat import _u
        class IFoo(InterfaceClass):
            pass
        ifoo = IFoo('IFoo')
        ibar = IFoo('IBar')
        _blank = _u('')
        _info = _u('info')
        _to_reg = object()
        def _factory(context):
            return _to_reg
        comp = self._makeOne()
        _monkey, _events = self._wrapEvents()
        with _monkey:
            comp.registerSubscriptionAdapter(_factory, (ibar,), ifoo,
                                             info=_info, event=False)
        self.assertEqual(len(_events), 0)

    def test_registeredSubscriptionAdapters_empty(self):
        comp = self._makeOne()
        self.assertEqual(list(comp.registeredSubscriptionAdapters()), [])

    def test_registeredSubscriptionAdapters_notempty(self):
        from zope.interface.declarations import InterfaceClass
        from zope.interface._compat import _u
        from zope.interface.registry import SubscriptionRegistration
        class IFoo(InterfaceClass):
            pass
        ifoo = IFoo('IFoo')
        ibar = IFoo('IFoo')
        _info = _u('info')
        _blank = _u('')
        class _Factory(object):
            def __init__(self, context):
                pass
        comp = self._makeOne()
        comp.registerSubscriptionAdapter(_Factory, (ibar,), ifoo, info=_info)
        comp.registerSubscriptionAdapter(_Factory, (ibar,), ifoo, info=_info)
        reg = list(comp.registeredSubscriptionAdapters())
        self.assertEqual(len(reg), 2)
        self.failUnless(isinstance(reg[0], SubscriptionRegistration))
        self.failUnless(reg[0].registry is comp)
        self.failUnless(reg[0].provided is ifoo)
        self.assertEqual(reg[0].required, (ibar,))
        self.assertEqual(reg[0].name, _blank)
        self.failUnless(reg[0].info is _info)
        self.failUnless(reg[0].factory is _Factory)
        self.failUnless(isinstance(reg[1], SubscriptionRegistration))
        self.failUnless(reg[1].registry is comp)
        self.failUnless(reg[1].provided is ifoo)
        self.assertEqual(reg[1].required, (ibar,))
        self.assertEqual(reg[1].name, _blank)
        self.failUnless(reg[1].info is _info)
        self.failUnless(reg[1].factory is _Factory)

    def test_unregisterSubscriptionAdapter_w_nonblank_name(self):
        from zope.interface.declarations import InterfaceClass
        from zope.interface._compat import _u
        class IFoo(InterfaceClass):
            pass
        ifoo = IFoo('IFoo')
        ibar = IFoo('IBar')
        _nonblank = _u('nonblank')
        comp = self._makeOne()
        self.assertRaises(TypeError, comp.unregisterSubscriptionAdapter,
                          required=ifoo, provided=ibar, name=_nonblank)

    def test_unregisterSubscriptionAdapter_neither_factory_nor_provided(self):
        comp = self._makeOne()
        self.assertRaises(TypeError, comp.unregisterSubscriptionAdapter,
                          factory=None, provided=None)

    def test_unregisterSubscriptionAdapter_neither_factory_nor_required(self):
        from zope.interface.declarations import InterfaceClass
        class IFoo(InterfaceClass):
            pass
        ifoo = IFoo('IFoo')
        comp = self._makeOne()
        self.assertRaises(TypeError, comp.unregisterSubscriptionAdapter,
                          factory=None, provided=ifoo, required=None)

    def test_unregisterSubscriptionAdapter_miss(self):
        from zope.interface.declarations import InterfaceClass
        class IFoo(InterfaceClass):
            pass
        ifoo = IFoo('IFoo')
        ibar = IFoo('IBar')
        class _Factory(object):
            def __init__(self, context):
                self._context = context
        comp = self._makeOne()
        _monkey, _events = self._wrapEvents()
        with _monkey:
            unreg = comp.unregisterSubscriptionAdapter(_Factory, (ibar,), ifoo)
        self.failIf(unreg)
        self.failIf(_events)

    def test_unregisterSubscriptionAdapter_hit_wo_factory(self):
        from zope.interface.declarations import InterfaceClass
        from zope.interface.interfaces import Unregistered
        from zope.interface.registry import SubscriptionRegistration
        class IFoo(InterfaceClass):
            pass
        ifoo = IFoo('IFoo')
        ibar = IFoo('IBar')
        class _Factory(object):
            def __init__(self, context):
                self._context = context
        comp = self._makeOne()
        comp.registerSubscriptionAdapter(_Factory, (ibar,), ifoo)
        _monkey, _events = self._wrapEvents()
        with _monkey:
            unreg = comp.unregisterSubscriptionAdapter(None, (ibar,), ifoo)
        self.failUnless(unreg)
        self.failIf(comp.adapters._subscribers)
        self.failIf(comp._subscription_registrations)
        self.assertEqual(len(_events), 1)
        args, kw = _events[0]
        event, = args
        self.assertEqual(kw, {})
        self.failUnless(isinstance(event, Unregistered))
        self.failUnless(isinstance(event.object, SubscriptionRegistration))
        self.failUnless(event.object.registry is comp)
        self.failUnless(event.object.provided is ifoo)
        self.assertEqual(event.object.required, (ibar,))
        self.assertEqual(event.object.name, '')
        self.assertEqual(event.object.info, '')
        self.failUnless(event.object.factory is None)

    def test_unregisterSubscriptionAdapter_hit_w_factory(self):
        from zope.interface.declarations import InterfaceClass
        from zope.interface.interfaces import Unregistered
        from zope.interface.registry import SubscriptionRegistration
        class IFoo(InterfaceClass):
            pass
        ifoo = IFoo('IFoo')
        ibar = IFoo('IBar')
        class _Factory(object):
            def __init__(self, context):
                self._context = context
        comp = self._makeOne()
        comp.registerSubscriptionAdapter(_Factory, (ibar,), ifoo)
        _monkey, _events = self._wrapEvents()
        with _monkey:
            unreg = comp.unregisterSubscriptionAdapter(_Factory, (ibar,), ifoo)
        self.failUnless(unreg)
        self.failIf(comp.adapters._subscribers)
        self.failIf(comp._subscription_registrations)
        self.assertEqual(len(_events), 1)
        args, kw = _events[0]
        event, = args
        self.assertEqual(kw, {})
        self.failUnless(isinstance(event, Unregistered))
        self.failUnless(isinstance(event.object, SubscriptionRegistration))
        self.failUnless(event.object.registry is comp)
        self.failUnless(event.object.provided is ifoo)
        self.assertEqual(event.object.required, (ibar,))
        self.assertEqual(event.object.name, '')
        self.assertEqual(event.object.info, '')
        self.failUnless(event.object.factory is _Factory)

    def test_unregisterSubscriptionAdapter_wo_explicit_provided(self):
        from zope.interface.declarations import InterfaceClass
        from zope.interface.declarations import implementer
        from zope.interface.interfaces import Unregistered
        from zope.interface.registry import SubscriptionRegistration
        class IFoo(InterfaceClass):
            pass
        ifoo = IFoo('IFoo')
        ibar = IFoo('IBar')
        @implementer(ifoo)
        class _Factory(object):
            def __init__(self, context):
                self._context = context
        comp = self._makeOne()
        comp.registerSubscriptionAdapter(_Factory, (ibar,), ifoo)
        _monkey, _events = self._wrapEvents()
        with _monkey:
            unreg = comp.unregisterSubscriptionAdapter(_Factory, (ibar,))
        self.failUnless(unreg)
        self.assertEqual(len(_events), 1)
        args, kw = _events[0]
        event, = args
        self.assertEqual(kw, {})
        self.failUnless(isinstance(event, Unregistered))
        self.failUnless(isinstance(event.object, SubscriptionRegistration))
        self.failUnless(event.object.registry is comp)
        self.failUnless(event.object.provided is ifoo)
        self.assertEqual(event.object.required, (ibar,))
        self.assertEqual(event.object.name, '')
        self.assertEqual(event.object.info, '')
        self.failUnless(event.object.factory is _Factory)

    def test_unregisterSubscriptionAdapter_wo_explicit_required(self):
        from zope.interface.declarations import InterfaceClass
        from zope.interface.interfaces import Unregistered
        from zope.interface.registry import SubscriptionRegistration
        class IFoo(InterfaceClass):
            pass
        ifoo = IFoo('IFoo')
        ibar = IFoo('IBar')
        class _Factory(object):
            __component_adapts__ = (ibar,)
            def __init__(self, context):
                self._context = context
        comp = self._makeOne()
        comp.registerSubscriptionAdapter(_Factory, (ibar,), ifoo)
        _monkey, _events = self._wrapEvents()
        with _monkey:
            unreg = comp.unregisterSubscriptionAdapter(_Factory, provided=ifoo)
        self.failUnless(unreg)
        self.assertEqual(len(_events), 1)
        args, kw = _events[0]
        event, = args
        self.assertEqual(kw, {})
        self.failUnless(isinstance(event, Unregistered))
        self.failUnless(isinstance(event.object, SubscriptionRegistration))
        self.failUnless(event.object.registry is comp)
        self.failUnless(event.object.provided is ifoo)
        self.assertEqual(event.object.required, (ibar,))
        self.assertEqual(event.object.name, '')
        self.assertEqual(event.object.info, '')
        self.failUnless(event.object.factory is _Factory)

    def test_subscribers_empty(self):
        from zope.interface.declarations import InterfaceClass
        from zope.interface.declarations import implementer
        class IFoo(InterfaceClass):
            pass
        ifoo = IFoo('IFoo')
        ibar = IFoo('IBar')
        comp = self._makeOne()
        @implementer(ibar)
        class Bar(object):
            pass
        bar = Bar()
        self.assertEqual(list(comp.subscribers((bar,), ifoo)), [])

    def test_subscribers_non_empty(self):
        from zope.interface.declarations import InterfaceClass
        from zope.interface.declarations import implementer
        class IFoo(InterfaceClass):
            pass
        ifoo = IFoo('IFoo')
        ibar = IFoo('IBar')
        class _Factory(object):
            __component_adapts__ = (ibar,)
            def __init__(self, context):
                self._context = context
        class _Derived(_Factory):
            pass
        comp = self._makeOne()
        comp.registerSubscriptionAdapter(_Factory, (ibar,), ifoo)
        comp.registerSubscriptionAdapter(_Derived, (ibar,), ifoo)
        @implementer(ibar)
        class Bar(object):
            pass
        bar = Bar()
        subscribers = comp.subscribers((bar,), ifoo)
        def _klassname(x):
            return x.__class__.__name__
        subscribers = sorted(subscribers, key=_klassname)
        self.assertEqual(len(subscribers), 2)
        self.failUnless(isinstance(subscribers[0], _Derived))
        self.failUnless(isinstance(subscribers[1], _Factory))

    def test_registerHandler_w_nonblank_name(self):
        from zope.interface.declarations import InterfaceClass
        from zope.interface._compat import _u
        class IFoo(InterfaceClass):
            pass
        ifoo = IFoo('IFoo')
        _nonblank = _u('nonblank')
        comp = self._makeOne()
        def _factory(context):
            pass
        self.assertRaises(TypeError, comp.registerHandler, _factory,
                          required=ifoo, name=_nonblank)

    def test_registerHandler_w_explicit_required(self):
        from zope.interface.declarations import InterfaceClass
        from zope.interface.interfaces import Registered
        from zope.interface.registry import HandlerRegistration
        from zope.interface._compat import _u
        class IFoo(InterfaceClass):
            pass
        ifoo = IFoo('IFoo')
        _blank = _u('')
        _info = _u('info')
        _to_reg = object()
        def _factory(context):
            return _to_reg
        comp = self._makeOne()
        _monkey, _events = self._wrapEvents()
        with _monkey:
            comp.registerHandler(_factory, (ifoo,), info=_info)
        reg = comp.adapters._subscribers[1][ifoo][None][_blank]
        self.assertEqual(len(reg), 1)
        self.failUnless(reg[0] is _factory)
        self.assertEqual(comp._handler_registrations,
                         [((ifoo,), _blank, _factory, _info)])
        self.assertEqual(len(_events), 1)
        args, kw = _events[0]
        event, = args
        self.assertEqual(kw, {})
        self.failUnless(isinstance(event, Registered))
        self.failUnless(isinstance(event.object, HandlerRegistration))
        self.failUnless(event.object.registry is comp)
        self.assertEqual(event.object.required, (ifoo,))
        self.assertEqual(event.object.name, _blank)
        self.failUnless(event.object.info is _info)
        self.failUnless(event.object.factory is _factory)

    def test_registerHandler_wo_explicit_required(self):
        from zope.interface.declarations import InterfaceClass
        from zope.interface.interfaces import Registered
        from zope.interface.registry import HandlerRegistration
        from zope.interface._compat import _u
        class IFoo(InterfaceClass):
            pass
        ifoo = IFoo('IFoo')
        _info = _u('info')
        _blank = _u('')
        class _Factory(object):
            __component_adapts__ = (ifoo,)
            def __init__(self, context):
                self._context = context
        comp = self._makeOne()
        _monkey, _events = self._wrapEvents()
        with _monkey:
            comp.registerHandler(_Factory, info=_info)
        reg = comp.adapters._subscribers[1][ifoo][None][_blank]
        self.assertEqual(len(reg), 1)
        self.failUnless(reg[0] is _Factory)
        self.assertEqual(comp._handler_registrations,
                         [((ifoo,), _blank, _Factory, _info)])
        self.assertEqual(len(_events), 1)
        args, kw = _events[0]
        event, = args
        self.assertEqual(kw, {})
        self.failUnless(isinstance(event, Registered))
        self.failUnless(isinstance(event.object, HandlerRegistration))
        self.failUnless(event.object.registry is comp)
        self.assertEqual(event.object.required, (ifoo,))
        self.assertEqual(event.object.name, _blank)
        self.failUnless(event.object.info is _info)
        self.failUnless(event.object.factory is _Factory)

    def test_registeredHandlers_empty(self):
        from zope.interface.declarations import InterfaceClass
        class IFoo(InterfaceClass):
            pass
        comp = self._makeOne()
        self.failIf(list(comp.registeredHandlers()))

    def test_registeredHandlers_non_empty(self):
        from zope.interface.declarations import InterfaceClass
        from zope.interface.registry import HandlerRegistration
        class IFoo(InterfaceClass):
            pass
        ifoo = IFoo('IFoo')
        def _factory1(context):
            pass
        def _factory2(context):
            pass
        comp = self._makeOne()
        comp.registerHandler(_factory1, (ifoo,))
        comp.registerHandler(_factory2, (ifoo,))
        def _factory_name(x):
            return x.factory.__code__.co_name
        subscribers = sorted(comp.registeredHandlers(), key=_factory_name)
        self.assertEqual(len(subscribers), 2)
        self.failUnless(isinstance(subscribers[0], HandlerRegistration))
        self.assertEqual(subscribers[0].required, (ifoo,))
        self.assertEqual(subscribers[0].name, '')
        self.assertEqual(subscribers[0].factory, _factory1)
        self.assertEqual(subscribers[0].info, '')
        self.failUnless(isinstance(subscribers[1], HandlerRegistration))
        self.assertEqual(subscribers[1].required, (ifoo,))
        self.assertEqual(subscribers[1].name, '')
        self.assertEqual(subscribers[1].factory, _factory2)
        self.assertEqual(subscribers[1].info, '')
 
    def test_unregisterHandler_w_nonblank_name(self):
        from zope.interface.declarations import InterfaceClass
        from zope.interface._compat import _u
        class IFoo(InterfaceClass):
            pass
        ifoo = IFoo('IFoo')
        _nonblank = _u('nonblank')
        comp = self._makeOne()
        self.assertRaises(TypeError, comp.unregisterHandler,
                          required=(ifoo,), name=_nonblank)

    def test_unregisterHandler_neither_factory_nor_required(self):
        from zope.interface.declarations import InterfaceClass
        class IFoo(InterfaceClass):
            pass
        ifoo = IFoo('IFoo')
        comp = self._makeOne()
        self.assertRaises(TypeError, comp.unregisterHandler)

    def test_unregisterHandler_miss(self):
        from zope.interface.declarations import InterfaceClass
        class IFoo(InterfaceClass):
            pass
        ifoo = IFoo('IFoo')
        comp = self._makeOne()
        unreg = comp.unregisterHandler(required=(ifoo,))
        self.failIf(unreg)

    def test_unregisterHandler_hit_w_factory_and_explicit_provided(self):
        from zope.interface.declarations import InterfaceClass
        from zope.interface.interfaces import Unregistered
        from zope.interface.registry import HandlerRegistration
        class IFoo(InterfaceClass):
            pass
        ifoo = IFoo('IFoo')
        comp = self._makeOne()
        _to_reg = object()
        def _factory(context):
            return _to_reg
        comp = self._makeOne()
        comp.registerHandler(_factory, (ifoo,))
        _monkey, _events = self._wrapEvents()
        with _monkey:
            unreg = comp.unregisterHandler(_factory, (ifoo,))
        self.failUnless(unreg)
        self.assertEqual(len(_events), 1)
        args, kw = _events[0]
        event, = args
        self.assertEqual(kw, {})
        self.failUnless(isinstance(event, Unregistered))
        self.failUnless(isinstance(event.object, HandlerRegistration))
        self.failUnless(event.object.registry is comp)
        self.assertEqual(event.object.required, (ifoo,))
        self.assertEqual(event.object.name, '')
        self.failUnless(event.object.factory is _factory)

    def test_unregisterHandler_hit_w_only_explicit_provided(self):
        from zope.interface.declarations import InterfaceClass
        from zope.interface.interfaces import Unregistered
        from zope.interface.registry import HandlerRegistration
        class IFoo(InterfaceClass):
            pass
        ifoo = IFoo('IFoo')
        comp = self._makeOne()
        _to_reg = object()
        def _factory(context):
            return _to_reg
        comp = self._makeOne()
        comp.registerHandler(_factory, (ifoo,))
        _monkey, _events = self._wrapEvents()
        with _monkey:
            unreg = comp.unregisterHandler(required=(ifoo,))
        self.failUnless(unreg)
        self.assertEqual(len(_events), 1)
        args, kw = _events[0]
        event, = args
        self.assertEqual(kw, {})
        self.failUnless(isinstance(event, Unregistered))
        self.failUnless(isinstance(event.object, HandlerRegistration))
        self.failUnless(event.object.registry is comp)
        self.assertEqual(event.object.required, (ifoo,))
        self.assertEqual(event.object.name, '')
        self.failUnless(event.object.factory is None)

    def test_unregisterHandler_wo_explicit_required(self):
        from zope.interface.declarations import InterfaceClass
        from zope.interface.interfaces import Unregistered
        from zope.interface.registry import HandlerRegistration
        class IFoo(InterfaceClass):
            pass
        ifoo = IFoo('IFoo')
        class _Factory(object):
            __component_adapts__ = (ifoo,)
            def __init__(self, context):
                self._context = context
        comp = self._makeOne()
        comp.registerHandler(_Factory)
        _monkey, _events = self._wrapEvents()
        with _monkey:
            unreg = comp.unregisterHandler(_Factory)
        self.failUnless(unreg)
        self.assertEqual(len(_events), 1)
        args, kw = _events[0]
        event, = args
        self.assertEqual(kw, {})
        self.failUnless(isinstance(event, Unregistered))
        self.failUnless(isinstance(event.object, HandlerRegistration))
        self.failUnless(event.object.registry is comp)
        self.assertEqual(event.object.required, (ifoo,))
        self.assertEqual(event.object.name, '')
        self.assertEqual(event.object.info, '')
        self.failUnless(event.object.factory is _Factory)

    def test_handle_empty(self):
        from zope.interface.declarations import InterfaceClass
        from zope.interface.declarations import implementer
        class IFoo(InterfaceClass):
            pass
        ifoo = IFoo('IFoo')
        comp = self._makeOne()
        @implementer(ifoo)
        class Bar(object):
            pass
        bar = Bar()
        comp.handle((bar,)) # doesn't raise

    def test_handle_non_empty(self):
        from zope.interface.declarations import InterfaceClass
        from zope.interface.declarations import implementer
        class IFoo(InterfaceClass):
            pass
        ifoo = IFoo('IFoo')
        _called_1 = []
        def _factory_1(context):
                _called_1.append(context)
        _called_2 = []
        def _factory_2(context):
                _called_2.append(context)
        comp = self._makeOne()
        comp.registerHandler(_factory_1, (ifoo,))
        comp.registerHandler(_factory_2, (ifoo,))
        @implementer(ifoo)
        class Bar(object):
            pass
        bar = Bar()
        comp.handle(bar)
        self.assertEqual(_called_1, [bar])
        self.assertEqual(_called_2, [bar])


# Test _getUtilityProvided, _getAdapterProvided, _getAdapterRequired via their
# callers (Component.registerUtility, Component.registerAdapter).


class UtilityRegistrationTests(_SilencePy3Deprecations):

    def _getTargetClass(self):
        from zope.interface.registry import UtilityRegistration
        return UtilityRegistration

    def _makeOne(self, component=None, factory=None):
        from zope.interface.declarations import InterfaceClass
        from zope.interface._compat import _u
        class IFoo(InterfaceClass):
            pass
        ifoo = IFoo('IFoo')
        class _Registry(object):
            def __repr__(self):
                return '_REGISTRY'
        registry = _Registry()
        name = _u('name')
        doc = 'DOCSTRING'
        klass = self._getTargetClass()
        return (klass(registry, ifoo, name, component, doc, factory),
                registry,
                name,
               )

    def test_class_conforms_to_IUtilityRegistration(self):
        from zope.interface.verify import verifyClass
        from zope.interface.interfaces import IUtilityRegistration
        verifyClass(IUtilityRegistration, self._getTargetClass())

    def test_instance_conforms_to_IUtilityRegistration(self):
        from zope.interface.verify import verifyObject
        from zope.interface.interfaces import IUtilityRegistration
        ur, _, _ =  self._makeOne()
        verifyObject(IUtilityRegistration, ur)

    def test___repr__(self):
        class _Component(object):
            __name__ = 'TEST'
        _component = _Component()
        ur, _registry, _name = self._makeOne(_component)
        self.assertEqual(repr(ur),
            "UtilityRegistration(_REGISTRY, IFoo, %r, TEST, None, 'DOCSTRING')"
                            % (_name))

    def test___repr___provided_wo_name(self):
        class _Component(object):
            def __repr__(self):
                return 'TEST'
        _component = _Component()
        ur, _registry, _name = self._makeOne(_component)
        ur.provided = object()
        self.assertEqual(repr(ur),
            "UtilityRegistration(_REGISTRY, None, %r, TEST, None, 'DOCSTRING')"
                            % (_name))

    def test___repr___component_wo_name(self):
        class _Component(object):
            def __repr__(self):
                return 'TEST'
        _component = _Component()
        ur, _registry, _name = self._makeOne(_component)
        ur.provided = object()
        self.assertEqual(repr(ur),
            "UtilityRegistration(_REGISTRY, None, %r, TEST, None, 'DOCSTRING')"
                            % (_name))

    def test___hash__(self):
        _component = object()
        ur, _registry, _name = self._makeOne(_component)
        self.assertEqual(ur.__hash__(), id(ur))

    def test___eq___identity(self):
        _component = object()
        ur, _registry, _name = self._makeOne(_component)
        self.failUnless(ur == ur)

    def test___eq___hit(self):
        _component = object()
        ur, _registry, _name = self._makeOne(_component)
        ur2, _, _ = self._makeOne(_component)
        self.failUnless(ur == ur2)

    def test___eq___miss(self):
        _component = object()
        _component2 = object()
        ur, _registry, _name = self._makeOne(_component)
        ur2, _, _ = self._makeOne(_component2)
        self.failIf(ur == ur2)

    def test___ne___identity(self):
        _component = object()
        ur, _registry, _name = self._makeOne(_component)
        self.failIf(ur != ur)

    def test___ne___hit(self):
        _component = object()
        ur, _registry, _name = self._makeOne(_component)
        ur2, _, _ = self._makeOne(_component)
        self.failIf(ur != ur2)

    def test___ne___miss(self):
        _component = object()
        _component2 = object()
        ur, _registry, _name = self._makeOne(_component)
        ur2, _, _ = self._makeOne(_component2)
        self.failUnless(ur != ur2)

    def test___lt___identity(self):
        _component = object()
        ur, _registry, _name = self._makeOne(_component)
        self.failIf(ur < ur)

    def test___lt___hit(self):
        _component = object()
        ur, _registry, _name = self._makeOne(_component)
        ur2, _, _ = self._makeOne(_component)
        self.failIf(ur < ur2)

    def test___lt___miss(self):
        _component = object()
        _component2 = object()
        ur, _registry, _name = self._makeOne(_component)
        ur2, _, _ = self._makeOne(_component2)
        ur2.name = _name + '2'
        self.failUnless(ur < ur2)

    def test___le___identity(self):
        _component = object()
        ur, _registry, _name = self._makeOne(_component)
        self.failUnless(ur <= ur)

    def test___le___hit(self):
        _component = object()
        ur, _registry, _name = self._makeOne(_component)
        ur2, _, _ = self._makeOne(_component)
        self.failUnless(ur <= ur2)

    def test___le___miss(self):
        _component = object()
        _component2 = object()
        ur, _registry, _name = self._makeOne(_component)
        ur2, _, _ = self._makeOne(_component2)
        ur2.name = _name + '2'
        self.failUnless(ur <= ur2)

    def test___gt___identity(self):
        _component = object()
        ur, _registry, _name = self._makeOne(_component)
        self.failIf(ur > ur)

    def test___gt___hit(self):
        _component = object()
        _component2 = object()
        ur, _registry, _name = self._makeOne(_component)
        ur2, _, _ = self._makeOne(_component2)
        ur2.name = _name + '2'
        self.failUnless(ur2 > ur)

    def test___gt___miss(self):
        _component = object()
        ur, _registry, _name = self._makeOne(_component)
        ur2, _, _ = self._makeOne(_component)
        self.failIf(ur2 > ur)

    def test___ge___identity(self):
        _component = object()
        ur, _registry, _name = self._makeOne(_component)
        self.failUnless(ur >= ur)

    def test___ge___miss(self):
        _component = object()
        _component2 = object()
        ur, _registry, _name = self._makeOne(_component)
        ur2, _, _ = self._makeOne(_component2)
        ur2.name = _name + '2'
        self.failIf(ur >= ur2)

    def test___ge___hit(self):
        _component = object()
        ur, _registry, _name = self._makeOne(_component)
        ur2, _, _ = self._makeOne(_component)
        ur2.name = _name + '2'
        self.failUnless(ur2 >= ur)


class AdapterRegistrationTests(_SilencePy3Deprecations):

    def _getTargetClass(self):
        from zope.interface.registry import AdapterRegistration
        return AdapterRegistration

    def _makeOne(self, component=None):
        from zope.interface.declarations import InterfaceClass
        from zope.interface._compat import _u
        class IFoo(InterfaceClass):
            pass
        ifoo = IFoo('IFoo')
        ibar = IFoo('IBar')
        class _Registry(object):
            def __repr__(self):
                return '_REGISTRY'
        registry = _Registry()
        name = _u('name')
        doc = 'DOCSTRING'
        klass = self._getTargetClass()
        return (klass(registry, (ibar,), ifoo, name, component, doc),
                registry,
                name,
               )

    def test_class_conforms_to_IAdapterRegistration(self):
        from zope.interface.verify import verifyClass
        from zope.interface.interfaces import IAdapterRegistration
        verifyClass(IAdapterRegistration, self._getTargetClass())

    def test_instance_conforms_to_IAdapterRegistration(self):
        from zope.interface.verify import verifyObject
        from zope.interface.interfaces import IAdapterRegistration
        ar, _, _ =  self._makeOne()
        verifyObject(IAdapterRegistration, ar)

    def test___repr__(self):
        class _Component(object):
            __name__ = 'TEST'
        _component = _Component()
        ar, _registry, _name = self._makeOne(_component)
        self.assertEqual(repr(ar),
            ("AdapterRegistration(_REGISTRY, [IBar], IFoo, %r, TEST, "
           + "'DOCSTRING')") % (_name))

    def test___repr___provided_wo_name(self):
        class _Component(object):
            def __repr__(self):
                return 'TEST'
        _component = _Component()
        ar, _registry, _name = self._makeOne(_component)
        ar.provided = object()
        self.assertEqual(repr(ar),
            ("AdapterRegistration(_REGISTRY, [IBar], None, %r, TEST, "
           + "'DOCSTRING')") % (_name))

    def test___repr___component_wo_name(self):
        class _Component(object):
            def __repr__(self):
                return 'TEST'
        _component = _Component()
        ar, _registry, _name = self._makeOne(_component)
        ar.provided = object()
        self.assertEqual(repr(ar),
            ("AdapterRegistration(_REGISTRY, [IBar], None, %r, TEST, "
           + "'DOCSTRING')") % (_name))

    def test___hash__(self):
        _component = object()
        ar, _registry, _name = self._makeOne(_component)
        self.assertEqual(ar.__hash__(), id(ar))

    def test___eq___identity(self):
        _component = object()
        ar, _registry, _name = self._makeOne(_component)
        self.failUnless(ar == ar)

    def test___eq___hit(self):
        _component = object()
        ar, _registry, _name = self._makeOne(_component)
        ar2, _, _ = self._makeOne(_component)
        self.failUnless(ar == ar2)

    def test___eq___miss(self):
        _component = object()
        _component2 = object()
        ar, _registry, _name = self._makeOne(_component)
        ar2, _, _ = self._makeOne(_component2)
        self.failIf(ar == ar2)

    def test___ne___identity(self):
        _component = object()
        ar, _registry, _name = self._makeOne(_component)
        self.failIf(ar != ar)

    def test___ne___miss(self):
        _component = object()
        ar, _registry, _name = self._makeOne(_component)
        ar2, _, _ = self._makeOne(_component)
        self.failIf(ar != ar2)

    def test___ne___hit_component(self):
        _component = object()
        _component2 = object()
        ar, _registry, _name = self._makeOne(_component)
        ar2, _, _ = self._makeOne(_component2)
        self.failUnless(ar != ar2)

    def test___ne___hit_provided(self):
        from zope.interface.declarations import InterfaceClass
        class IFoo(InterfaceClass):
            pass
        ibaz = IFoo('IBaz')
        _component = object()
        ar, _registry, _name = self._makeOne(_component)
        ar2, _, _ = self._makeOne(_component)
        ar2.provided = ibaz
        self.failUnless(ar != ar2)

    def test___ne___hit_required(self):
        from zope.interface.declarations import InterfaceClass
        class IFoo(InterfaceClass):
            pass
        ibaz = IFoo('IBaz')
        _component = object()
        _component2 = object()
        ar, _registry, _name = self._makeOne(_component)
        ar2, _, _ = self._makeOne(_component2)
        ar2.required = (ibaz,)
        self.failUnless(ar != ar2)

    def test___lt___identity(self):
        _component = object()
        ar, _registry, _name = self._makeOne(_component)
        self.failIf(ar < ar)

    def test___lt___hit(self):
        _component = object()
        ar, _registry, _name = self._makeOne(_component)
        ar2, _, _ = self._makeOne(_component)
        self.failIf(ar < ar2)

    def test___lt___miss(self):
        _component = object()
        _component2 = object()
        ar, _registry, _name = self._makeOne(_component)
        ar2, _, _ = self._makeOne(_component2)
        ar2.name = _name + '2'
        self.failUnless(ar < ar2)

    def test___le___identity(self):
        _component = object()
        ar, _registry, _name = self._makeOne(_component)
        self.failUnless(ar <= ar)

    def test___le___hit(self):
        _component = object()
        ar, _registry, _name = self._makeOne(_component)
        ar2, _, _ = self._makeOne(_component)
        self.failUnless(ar <= ar2)

    def test___le___miss(self):
        _component = object()
        _component2 = object()
        ar, _registry, _name = self._makeOne(_component)
        ar2, _, _ = self._makeOne(_component2)
        ar2.name = _name + '2'
        self.failUnless(ar <= ar2)

    def test___gt___identity(self):
        _component = object()
        ar, _registry, _name = self._makeOne(_component)
        self.failIf(ar > ar)

    def test___gt___hit(self):
        _component = object()
        _component2 = object()
        ar, _registry, _name = self._makeOne(_component)
        ar2, _, _ = self._makeOne(_component2)
        ar2.name = _name + '2'
        self.failUnless(ar2 > ar)

    def test___gt___miss(self):
        _component = object()
        ar, _registry, _name = self._makeOne(_component)
        ar2, _, _ = self._makeOne(_component)
        self.failIf(ar2 > ar)

    def test___ge___identity(self):
        _component = object()
        ar, _registry, _name = self._makeOne(_component)
        self.failUnless(ar >= ar)

    def test___ge___miss(self):
        _component = object()
        _component2 = object()
        ar, _registry, _name = self._makeOne(_component)
        ar2, _, _ = self._makeOne(_component2)
        ar2.name = _name + '2'
        self.failIf(ar >= ar2)

    def test___ge___hit(self):
        _component = object()
        ar, _registry, _name = self._makeOne(_component)
        ar2, _, _ = self._makeOne(_component)
        ar2.name = _name + '2'
        self.failUnless(ar2 >= ar)


class SubscriptionRegistrationTests(unittest.TestCase):

    def _getTargetClass(self):
        from zope.interface.registry import SubscriptionRegistration
        return SubscriptionRegistration

    def _makeOne(self, component=None):
        from zope.interface.declarations import InterfaceClass
        from zope.interface._compat import _u
        class IFoo(InterfaceClass):
            pass
        ifoo = IFoo('IFoo')
        ibar = IFoo('IBar')
        class _Registry(object):
            def __repr__(self):
                return '_REGISTRY'
        registry = _Registry()
        name = _u('name')
        doc = 'DOCSTRING'
        klass = self._getTargetClass()
        return (klass(registry, (ibar,), ifoo, name, component, doc),
                registry,
                name,
               )

    def test_class_conforms_to_ISubscriptionAdapterRegistration(self):
        from zope.interface.verify import verifyClass
        from zope.interface.interfaces import ISubscriptionAdapterRegistration
        verifyClass(ISubscriptionAdapterRegistration, self._getTargetClass())

    def test_instance_conforms_to_ISubscriptionAdapterRegistration(self):
        from zope.interface.verify import verifyObject
        from zope.interface.interfaces import ISubscriptionAdapterRegistration
        sar, _, _ =  self._makeOne()
        verifyObject(ISubscriptionAdapterRegistration, sar)


class HandlerRegistrationTests(_SilencePy3Deprecations):

    def _getTargetClass(self):
        from zope.interface.registry import HandlerRegistration
        return HandlerRegistration

    def _makeOne(self, component=None):
        from zope.interface.declarations import InterfaceClass
        from zope.interface._compat import _u
        class IFoo(InterfaceClass):
            pass
        ifoo = IFoo('IFoo')
        class _Registry(object):
            def __repr__(self):
                return '_REGISTRY'
        registry = _Registry()
        name = _u('name')
        doc = 'DOCSTRING'
        klass = self._getTargetClass()
        return (klass(registry, (ifoo,), name, component, doc),
                registry,
                name,
               )

    def test_class_conforms_to_IHandlerRegistration(self):
        from zope.interface.verify import verifyClass
        from zope.interface.interfaces import IHandlerRegistration
        verifyClass(IHandlerRegistration, self._getTargetClass())

    def test_instance_conforms_to_IHandlerRegistration(self):
        from zope.interface.verify import verifyObject
        from zope.interface.interfaces import IHandlerRegistration
        hr, _, _ =  self._makeOne()
        verifyObject(IHandlerRegistration, hr)

    def test_properties(self):
        def _factory(context):
            pass
        hr, _, _ =  self._makeOne(_factory)
        self.failUnless(hr.handler is _factory)
        self.failUnless(hr.factory is hr.handler)
        self.failUnless(hr.provided is None)

    def test___repr___factory_w_name(self):
        class _Factory(object):
            __name__ = 'TEST'
        hr, _registry, _name =  self._makeOne(_Factory())
        self.assertEqual(repr(hr),
            ("HandlerRegistration(_REGISTRY, [IFoo], %r, TEST, "
           + "'DOCSTRING')") % (_name))

    def test___repr___factory_wo_name(self):
        class _Factory(object):
            def __repr__(self):
                return 'TEST'
        hr, _registry, _name =  self._makeOne(_Factory())
        self.assertEqual(repr(hr),
            ("HandlerRegistration(_REGISTRY, [IFoo], %r, TEST, "
           + "'DOCSTRING')") % (_name))


class _Monkey(object):
    # context-manager for replacing module names in the scope of a test.
    def __init__(self, module, **kw):
        self.module = module
        self.to_restore = dict([(key, getattr(module, key)) for key in kw])
        for key, value in kw.items():
            setattr(module, key, value)

    def __enter__(self):
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        for key, value in self.to_restore.items():
            setattr(self.module, key, value)

def test_suite():
    return unittest.TestSuite((
            unittest.makeSuite(ComponentsTests),
            unittest.makeSuite(UtilityRegistrationTests),
            unittest.makeSuite(AdapterRegistrationTests),
            unittest.makeSuite(SubscriptionRegistrationTests),
            unittest.makeSuite(AdapterRegistrationTests),
        ))