def test_many_events_with_dictionary(): subject = Observable() subject.on({ # setting events of an observable with dictionary 'one': one_handler, 'two': two_handler, 'custom': CustomEvent(), 'many': [one_handler, two_handler]}) subject.trigger('one two custom many', 1, 2, a=3, b=4)
def test_event_with_many_handlers_attached(): subject = Observable() subject.on('many', [one_handler, two_handler]) assert(subject.events['many'] is subject.many) assert(one_handler and two_handler in subject.many.handlers) # remove a handler subject.off('many', one_handler) assert(one_handler not in subject.many.handlers) # remove the event subject.off('many') assert('many' not in subject.events) with raises(AttributeError): subject.many
def test_event_with_handler_attached(): subject = Observable() subject.on('one', one_handler) subject.on('two', two_handler) assert(subject.events['one'] is subject.one) assert(subject.events['two'] is subject.two) # trigger subject.one(1, 2, a=3, b=4) subject.one.trigger(1, 2, a=3, b=4) subject.trigger('one', 1, 2, a=3, b=4) subject.events['one'].trigger(1, 2, a=3, b=4) subject.trigger('one two', 1, 2, a=3, b=4) subject.trigger(['one', 'two'], 1, 2, a=3, b=4)
def test_custom_event(): subject = Observable() custom_event = CustomEvent() subject.on('custom', custom_event) subject.on(['custom', 'custom_alias'], custom_event) # same reference assert(subject.custom and subject.custom_alias is custom_event) assert(one_handler and two_handler in subject.custom.handlers) # update an event subject.on('custom', CustomEvent()) assert(subject.custom is not custom_event)
"""Alarm Event names""" from observer import Observable import logging LOGGER = logging.getLogger('alarm') # Events MOTION_DETECTED = 'motion_detected' # Basic handlers def log_it(*args, **kwargs): LOGGER.debug("%s, %s", args, kwargs) # Setup events observable = Observable() observable.on(MOTION_DETECTED, [log_it])
def test_no_callable_handler(): subject = Observable() with raises(TypeError): subject.on('error', NotCallable())
def test_events_with_same_handlers_attached(): subject = Observable() subject.on('many many2', [one_handler, two_handler]) assert(subject.many is not subject.many2) assert(one_handler and two_handler in subject.many.handlers) assert(one_handler and two_handler in subject.many2.handlers)
def test_events_with_same_handler_attached(): subject = Observable() subject.on(['one', 'one2'], one_handler) assert(subject.one is not subject.one2) assert(one_handler in subject.one.handlers) assert(one_handler in subject.one2.handlers)