Example #1
0
    def test_can_call_observers_before_executing_decorated_message(self):
        event = Event([])
        observes(event)(self.probe)

        def guard_probe(*args, **kwargs):
            assert_not_contains('probe', kwargs)
            kwargs['guard_probe'] = True
            return (args, kwargs)

        observes(event, run_before=True)(guard_probe)

        def function(*args, **kwargs):
            return {'args': list(args), 'kwargs': kwargs}

        decorated_function = observable(event)(function)

        expected = {
            'args': ['foo'],
            'kwargs': {
                'bar': True,
                'guard_probe': True
            },
            'probe': True
        }
        assert_equals(expected, decorated_function('foo', bar=True))
Example #2
0
 def test_observers_can_request_priority(self):
     def second_probe(result):
         pass
     event = Event([])
     observes(event)(self.probe)
     observes(event, appendleft=True)(second_probe)
     
     assert_length(2, event.observers)
     assert_equals([second_probe, self.probe], list(event.observers))
Example #3
0
    def test_calls_observers_after_function_was_executed(self):
        event = Event([])
        observes(event)(self.probe)

        def function(*args, **kwargs):
            return {'args': list(args), 'kwargs': kwargs}

        decorated_function = observable(event)(function)

        result = decorated_function('foo', bar=True)
        assert_equals({
            'args': ['foo'],
            'kwargs': {
                'bar': True
            },
            'probe': True
        }, result)
Example #4
0
 def setUp(self):
     self.observers_called = 0
     self.event = Event()
Example #5
0
    def test_can_observe_event(self):
        event = Event([])
        observes(event)(self.probe)

        assert_length(1, event.observers)
        assert_equals(self.probe, event.observers[0])
Example #6
0
 def test_catches_unknown_keyword_parameters_in_constructor(self):
     e = assert_raises(TypeError, lambda: observes(Event(), invalid=True))
     assert_equals(
         "TypeError: observes() got an unexpected keyword argument 'invalid'",
         e.args[0])