class EventHub2(qcore.EnumBasedEventHub): __based_on__ = [Events, MoreEvents] on_work = qcore.EventHook() on_sleep = qcore.EventHook() on_eat = qcore.EventHook() on_some_other_member = None def some_method(self): pass
def test_events_hub_with_source(): def handler(): pass hook = qcore.EventHook() hook.subscribe(handler) assert_eq([handler], list(hook)) hub = qcore.EventHub(source={'on_something': hook}) assert_eq([handler], list(hub.on_something))
def test_events(): e = qcore.EventHook() e.subscribe(lambda *args: dump("Handler 1", *args)) e("argument") e.subscribe(lambda: fail("Handler 2")) e.subscribe(lambda: dump("Handler 3")) with AssertRaises(NotImplementedError): e() # prints 'Handler 1' with AssertRaises(NotImplementedError): e.safe_trigger() # prints 'Handler 1', 'Handler 3' h = qcore.EventHub() h.on_some_event.subscribe(lambda: dump("On some event")) h.on_some_other_event.subscribe(lambda: dump("On some other event")) h.on_some_event() h.on_some_other_event()
def test_events(): global count h0 = lambda: handler(0) h1 = lambda: handler(1) h2 = lambda: handler(2) h0e = lambda: handler(0, True) count = 0 events = qcore.EventHook() assert_eq('EventHook()', str(events)) assert_eq('EventHook()', repr(events)) events.subscribe(h0) assert_eq('EventHook(%r,)' % h0, str(events)) assert_eq('EventHook(%r,)' % h0, repr(events)) events() assert_eq(count, 1) count = 0 events = qcore.EventHook() assert_eq([], list(events)) events.subscribe(h0) events.subscribe(h1) assert_eq([h0, h1], list(events)) assert_in(h0, events) assert_in(h1, events) assert_not_in(h2, events) events() assert_eq(count, 2) count = 0 events = qcore.EventHook() events.subscribe(h0e) events.subscribe(h1) try: events() except BaseException: pass assert_eq(count, 1) count = 0 events = qcore.EventHook() events.subscribe(h0e) events.subscribe(h1) try: events.safe_trigger() except BaseException: pass assert_eq(count, 2) count = 0 events = qcore.EventHook() events.subscribe(h0) events.subscribe(h1) events() assert_eq(count, 2) count = 0 events.unsubscribe(h1) events() assert_eq(count, 1) count = 0 events.unsubscribe(h0) events() assert_eq(count, 0) events = qcore.EventHook() events.subscribe(h0) events.subscribe(h1) events.unsubscribe(h0) count = 1 events() assert_eq(count, 2) count = 0 events.unsubscribe(h1) events() assert_eq(count, 0) events = qcore.EventHook() events.subscribe(h0) events.subscribe(h1) events.subscribe(h2) events.unsubscribe(h1) events.unsubscribe(h0) events.unsubscribe(h2) count = 0 events() assert_eq(count, 0)
class BadEventHub7(qcore.EnumBasedEventHub): __based_on__ = [Events, MoreEvents, MoreEvents] # Duplicate members on_work = qcore.EventHook() on_sleep = qcore.EventHook() on_eat = qcore.EventHook()
class BadEventHub6(qcore.EnumBasedEventHub): __based_on__ = Events on_work = qcore.EventHook() on_sleep = 1
class BadEventHub5(qcore.EnumBasedEventHub): __based_on__ = [Events, MoreEvents] on_work = qcore.EventHook() on_sleep = qcore.EventHook() on_eat = qcore.EventHook() on_bad = qcore.EventHook()