def test_subscribe(self): """Test Event() can subscribe callback functions.""" # Create function for capturing event data. event_data = list() def callback(data): event_data.append(data) # Create Event(). event = Event() # Validate Event() can detect when callbacks have NOT been # subscribed. self.assertFalse(event.is_subscribed(callback)) # Validate Event() can detect when callbacks HAVE been subscribed. self.assertTrue(event.subscribe(callback)) self.assertTrue(event.is_subscribed(callback)) self.assertEqual(event.num_subscriptions(), 1) # Validate Event() will not re-subscribe callbacks. self.assertFalse(event.subscribe(callback)) # Test subscribe catches callbacks which do not contain a__call__ # method. with self.assertRaises(TypeError): event.subscribe(int())
def test_unsubscribe_from_callback(self): """Test Event() callback functions can unsubscribe themselves.""" # Create Event(). event = Event() # Create function which will unsubscribe itself when called. def unsubscriber(): event.unsubscribe(unsubscriber) # Subscribe the function which will unsubscribe itself when called. event.subscribe(unsubscriber) self.assertTrue(event.is_subscribed(unsubscriber)) # Trigger event and ensure function unsubscribed itself. event.__trigger__() self.assertFalse(event.is_subscribed(unsubscriber))
def test_unsubscribe(self): """Test Event() can unsubscribe callback functions.""" # Create function for capturing event data. event_data = list() def callback(data): event_data.append(data) # Create Event(). event = Event() # Validate Event() can detect when callbacks have been UNsubscribed. event.subscribe(callback) self.assertTrue(event.is_subscribed(callback)) self.assertTrue(event.unsubscribe(callback)) self.assertFalse(event.is_subscribed(callback)) self.assertEqual(event.num_subscriptions(), 0) # Validate Event() will not unsubscribe a callback which does not # exist. self.assertFalse(event.unsubscribe(callback))
def test_subscribe_from_callback(self): """Test Event() callback functions can be subscribed from callbacks.""" # Create Event(). event = Event() # Create testing function. def noop(): pass # Create function which will subscribe the testing function. def subscriber(): event.subscribe(noop) # Subscribe the function which will subscribe another function when # called. event.subscribe(subscriber) self.assertTrue(event.is_subscribed(subscriber)) # Trigger event and ensure testing function was subscribed. event.__trigger__() self.assertTrue(event.is_subscribed(noop))