def testUnregisterIfNotRegistered(self): """EventManager.unregister complains if not registered.""" event_manager = EventManager() listener = SubListener() self.assertRaises(evtman.NotRegisteredError, event_manager.unregister, listener) event_manager.register(listener) event_manager.unregister(listener) self.assertRaises(evtman.NotRegisteredError, event_manager.unregister, 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), 1) self.assertEquals(len(handlers["SubEvent"]), 1) self.assertEquals(handlers["SubEvent"][listener], listener.onSubEvent.im_func)
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 import gc gc.collect() handlers = event_manager._handlers self.assertEquals(len(handlers), 1) self.assertEquals(len(handlers["SubEvent"]), 0)
def testRegisterTwice(self): """EventManager.register breaks if already registered.""" event_manager = EventManager() listener = SubListener() event_manager.register(listener) self.assertRaises(evtman.AlreadyRegisteredError, event_manager.register, listener)
def testPostAndPump(self): """EventManager.pump sends all the posted events to all the listeners. """ 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) global egg_plus_spam egg_plus_spam = 0 event_manager.post(event1) event_manager.post(event2) event_manager.post(Event()) # This one won't trigger anything. event_manager.pump() self.assertEquals(egg_plus_spam, (666 + 42 + 13 + 7) * 2)