def test_source_stack_integrity(): """Emitter checks source stack""" em = EventEmitter(type="test_event") def cb(ev): ev._sources.append("x") em.connect(cb) try: em() except RuntimeError as err: if str(err) != "Event source-stack mismatch.": raise em.disconnect() def cb2(ev): ev._sources = [] em.connect(cb2) try: em() except IndexError: pass
def test_chained_emitters(): """Chained emitters""" source = type("Source", (), {})() em1 = EventEmitter(source=None, type="test_event1") em2 = EventEmitter(source=source, type="test_event2") em1.connect(em2) em1.connect(record_event) record_event.result = None ev = em1() record_event.assert_result(event=ev, event_class=Event, source=None, type="test_event1", sources=[None]) # sources look different from second emitter, but type is the same. em1.disconnect(record_event) em2.connect(record_event) record_event.result = None ev = em1() record_event.assert_result( event=ev, event_class=Event, source=source, type="test_event1", sources=[None, source], )
def test_disconnect(): """Emitter disconnection""" em = EventEmitter(type="test_event") def cb1(ev): record_event.result = 1 def cb2(ev): record_event.result = 2 em.connect((record_event, "__call__")) # type: ignore em.connect(cb1) em.connect(cb2) record_event.result = None em.disconnect(cb2) em.disconnect(cb2) # should pass silently ev = em() record_event.assert_result(event=ev) record_event.result = None em.disconnect((record_event, "__call__")) # type: ignore ev = em() assert record_event.result == 1 record_event.result = None em.connect(cb1) em.connect(cb2) em.connect((record_event, "__call__")) # type: ignore em.disconnect() em() assert record_event.result is None
def test_emission_order(): """Event emission order""" em = EventEmitter(type="test_event") def cb1(ev): record_event.result = 1 def cb2(ev): record_event.result = 2 em.connect(cb1) em.connect(cb2) record_event.result = None em() assert record_event.result == 1, "Events emitted in wrong order" em.disconnect() em.connect(cb2) em.connect(cb1) record_event.result = None em() assert record_event.result == 2, "Events emitted in wrong order"
def test_event_connect_order(): """Test event connection order""" def a(e): return def b(e): return def c(e): return def d(e): return def e(e): return def f(e): return em = EventEmitter(type="test_event") assert_raises(ValueError, em.connect, c, before=["c", "foo"]) assert_raises(ValueError, em.connect, c, position="foo") assert_raises(TypeError, em.connect, c, ref=dict()) em.connect(c, ref=True) assert_equal((c, ), tuple(em.callbacks)) em.connect(c) assert_equal((c, ), tuple(em.callbacks)) em.connect(d, ref=True, position="last") assert_equal((c, d), tuple(em.callbacks)) em.connect(b, ref=True) # position='first' assert_equal((b, c, d), tuple(em.callbacks)) assert_raises(RuntimeError, em.connect, a, before="c", after="d") # can't em.connect(a, ref=True, before=["c", "d"]) # first possible pos == 0 assert_equal((a, b, c, d), tuple(em.callbacks)) em.connect(f, ref=True, after=["c", "d"]) assert_equal((a, b, c, d, f), tuple(em.callbacks)) em.connect(e, ref=True, after="d", before="f") assert_equal(("a", "b", "c", "d", "e", "f"), tuple(em.callback_refs)) em.disconnect(e) em.connect(e, ref=True, after="a", before="f", position="last") assert_equal(("a", "b", "c", "d", "e", "f"), tuple(em.callback_refs)) em.disconnect(e) em.connect(e, ref="e", after="d", before="f", position="last") assert_equal(("a", "b", "c", "d", "e", "f"), tuple(em.callback_refs)) em.disconnect(e) em.connect(e, after="d", before="f", position="first") # no name assert_equal(("a", "b", "c", "d", None, "f"), tuple(em.callback_refs)) em.disconnect(e) assert_raises(ValueError, em.connect, e, ref="d") # duplicate name em.connect(e, ref=True, after=[], before="f", position="last") assert_equal(("a", "b", "c", "d", "e", "f"), tuple(em.callback_refs)) assert_equal((a, b, c, d, e, f), tuple(em.callbacks)) old_e = e def e(): # type: ignore return assert_raises(ValueError, em.connect, e, ref=True) # duplicate name em.connect(e) assert_equal((None, "a", "b", "c", "d", "e", "f"), tuple(em.callback_refs)) assert_equal((e, a, b, c, d, old_e, f), tuple(em.callbacks))