Ejemplo n.º 1
0
    def test_chained_emitters(self):
        """Chained emitters"""
        em1 = EventEmitter(source=None, type='test_event1')
        em2 = EventEmitter(source=self, type='test_event2')
        em1.connect(em2)
        em1.connect(self.record_event)
        self.result = None
        ev = em1()
        self.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(self.record_event)
        em2.connect(self.record_event)
        self.result = None
        ev = em1()
        self.assert_result(event=ev, event_class=Event, source=self, type='test_event1', sources=[None,self])
Ejemplo n.º 2
0
    def test_emission_order(self):
        """Event emission order"""
        em = EventEmitter(type='test_event')
        def cb1(ev):
            self.result = 1
        def cb2(ev):
            self.result = 2
            
        em.connect(cb1)
        em.connect(cb2)
        self.result = None
        em()
        assert self.result == 1, "Events emitted in wrong order"

        em.disconnect()
        em.connect(cb2)
        em.connect(cb1)
        self.result = None
        em()
        assert self.result == 2, "Events emitted in wrong order"
Ejemplo n.º 3
0
 def test_disconnect(self):
     """Emitter disconnection"""
     em = EventEmitter(type='test_event')
     def cb1(ev):
         self.result = 1
     def cb2(ev):
         self.result = 2
         
     em.connect((self, 'record_event'))
     em.connect(cb1)
     em.connect(cb2)
     self.result = None
     em.disconnect(cb2)
     ev = em()
     self.assert_result(event=ev)
     
     self.result = None
     em.disconnect((self, 'record_event'))
     ev = em()
     assert self.result == 1
     
     self.result = None
     em.connect(cb1)
     em.connect(cb2)
     em.connect((self, 'record_event'))
     em.disconnect()
     em()
     assert self.result == None
Ejemplo n.º 4
0
 def test_source_stack_integrity(self):
     """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 cb(ev):
         ev._sources = []
     em.connect(cb)
     
     try:
         em()
     except IndexError:
         pass