def testRegisterTwice(self): """EventManager.register breaks if already registered.""" event_manager = EventManager() listener = SubListener() event_manager.register(listener) self.assertRaises(AlreadyRegisteredError, event_manager.register, listener)
def testRegister(self): """EventManager.register adds the listener to its list.""" event_manager = EventManager() listener = SubListener() event_manager.register(listener) handlers = event_manager._handlers self.assertEquals(len(handlers), 2) self.assertEquals(len(handlers['SubEvent']), 1) self.assertEquals(handlers['SubEvent'][listener], listener.onSubEvent.im_func)
def testUnregisterIfNotRegistered(self): """EventManager.unregister complains if not registered.""" event_manager = EventManager() listener = SubListener() self.assertRaises(NotRegisteredError, event_manager.unregister, listener) event_manager.register(listener) event_manager.unregister(listener) self.assertRaises(NotRegisteredError, event_manager.unregister, listener)
def testUnregister(self): """EventManager.unregister removes the handlers of a listener.""" event_manager = EventManager() listener1 = SubListener() listener2 = SubListener() event_manager.register(listener1) event_manager.register(listener2) event_manager.unregister(listener1) self.assertFalse(listener1 in event_manager._handlers['SubEvent']) self.assertTrue(listener2 in event_manager._handlers['SubEvent'])
def testWeakReference(self): """EventManager forgets dead listeners.""" event_manager = EventManager() listener = SubListener() event_manager.register(listener) del listener gc.collect() handlers = event_manager._handlers self.assertEquals(len(handlers), 2) self.assertEquals(len(handlers['SubEvent']), 0)
def testPostAndPump(self): """EventManager.pump sends all the posted events to all the listeners. """ SubListener.egg_plus_spam = 0 event_manager = EventManager() listener1 = SubListener() listener2 = SubListener() event_manager.register(listener1) event_manager.register(listener2) event_manager.register(Listener()) # This one won't react. event1 = SubEvent(666, 42) event2 = SubEvent(13, 7) event_manager.post(event1) event_manager.post(event2) event_manager.post(Event()) # This one won't trigger anything. event_manager.pump() self.assertEquals(SubListener.egg_plus_spam, (666 + 42 + 13 + 7) * 2)
def testInit(self): """EventManager.__init__ doesn't crash.""" unused = EventManager()