From f077653f208f6f7c89c78c87c2abb0ea7031dbc0 Mon Sep 17 00:00:00 2001 From: Chris McDonough Date: Wed, 28 Jul 2010 03:00:58 +0000 Subject: - A ``repoze.bfg.events.subscriber`` decorator was added. This decorator decorates module-scope functions, which are then treated as event listeners after a scan() is performed. See the Events narrative documentation chapter and the ``repoze.bfg.events`` module documentation for more information. --- repoze/bfg/tests/test_events.py | 54 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) (limited to 'repoze/bfg/tests') diff --git a/repoze/bfg/tests/test_events.py b/repoze/bfg/tests/test_events.py index f1cbfff5b..c258c4ad2 100644 --- a/repoze/bfg/tests/test_events.py +++ b/repoze/bfg/tests/test_events.py @@ -92,6 +92,60 @@ class AfterTraversalEventTests(unittest.TestCase): inst = self._makeOne(request) self.assertEqual(inst.request, request) +class TestSubscriber(unittest.TestCase): + def setUp(self): + registry = DummyRegistry() + from repoze.bfg.configuration import Configurator + self.config = Configurator(registry) + self.config.begin() + + def tearDown(self): + self.config.end() + + def _makeOne(self, *ifaces): + from repoze.bfg.events import subscriber + return subscriber(*ifaces) + + def test_register(self): + from zope.interface import Interface + class IFoo(Interface): pass + class IBar(Interface): pass + dec = self._makeOne(IFoo, IBar) + def foo(): pass + config = DummyConfigurator() + scanner = Dummy() + scanner.config = config + dec.register(scanner, None, foo) + self.assertEqual(config.subscribed, [(foo, (IFoo, IBar))]) + + def test___call__(self): + dec = self._makeOne() + dummy_venusian = DummyVenusian() + dec.venusian = dummy_venusian + def foo(): pass + dec(foo) + self.assertEqual(dummy_venusian.attached, [(foo, dec.register, 'bfg')]) + +class DummyConfigurator(object): + def __init__(self): + self.subscribed = [] + + def add_subscriber(self, wrapped, ifaces): + self.subscribed.append((wrapped, ifaces)) + +class DummyRegistry(object): + pass + +class DummyVenusian(object): + def __init__(self): + self.attached = [] + + def attach(self, wrapped, fn, category=None): + self.attached.append((wrapped, fn, category)) + +class Dummy: + pass + class DummyRequest: pass -- cgit v1.2.3