def test_error_notifyer(self): class MyExc(Exception): pass @init_properties class A(object): y = trait() seen_y_error = False @y.setter def y(self, value): self._y = value raise MyExc() @y.changed() def y_changed(self, event): self.seen_y = True @y.error() def y_error(self, event): self.seen_y_error = True a = A() init_events(a) with self.assertRaises(MyExc): a.y = 1 self.assertTrue(a.seen_y_error)
def test_getset_listen(self): @init_properties class A(object): t = trait() y = trait() @y.changed() def y_changed(self, event): self.seen_y = True a = A() init_events(a) a.t = 1 a.seen_t = False a.seen_y = False def printme(event): a.seen_t = True on_change(a, 't', printme) a.t = 1 self.assertTrue(a.seen_t) a.y = 1 self.assertTrue(a.seen_y)
def test_event_context(self): obj = AnyObject() init_events(obj) obj.id1 = 0 def seen3(event): obj.id1 += 1 events(obj).on('any_target', seen3) events(obj).etrigger('any_target', dispatcher=lambda event, listener: None) self.assertEqual(obj.id1, 0) events(obj).etrigger( 'any_target', dispatcher=lambda event, listener: listener(event)) self.assertEqual(obj.id1, 1) with events(obj).quiet(): events(obj).etrigger( 'any_target', dispatcher=lambda event, listener: listener(event)) self.assertEqual( obj.id1, 2) #event dispatcher has higher priority than 'quiet'
def test_add_global_litener(self): global id1, id2 id1, id2 = 0, 0 obj1 = AnyObject() obj2 = AnyObject() init_events(obj1, obj2) obj1.seen = 0 obj2.seen = 0 def pritn(event): event.obj.seen += 1 with global_listener(pritn): events(obj1).etrigger('x') self.assertEqual(obj1.seen, 1) events(obj2).etrigger('y') self.assertEqual(obj2.seen, 1) events(obj1).etrigger('x') self.assertEqual(obj1.seen, 1) events(obj2).etrigger('y') self.assertEqual(obj2.seen, 1) with global_listener(pritn, ('x', )): events(obj1).etrigger('x') self.assertEqual(obj1.seen, 2) events(obj2).etrigger('y') self.assertEqual(obj2.seen, 1)
def test_simple(self): global id1, id2 id1, id2 = 0, 0 obj = AnyObject() refcnt_before_init_events = sys.getrefcount(obj) init_events(obj) refcnt_after_init_events = sys.getrefcount(obj) self.assertEqual(refcnt_before_init_events, refcnt_after_init_events) events(obj).listen(('id1',), seen1) events(obj).listen(('id2',), seen2) events(obj).etrigger('x', id1=1, id2=2) self.assertEqual(id1, 0) self.assertEqual(id2, 0) events(obj).etrigger('id1', id1=1, id2=2) self.assertEqual(id1, 1) self.assertEqual(id2, 0) events(obj).etrigger('id2', id1=1, id2=2) self.assertEqual(id1, 1) self.assertEqual(id2, 2) events(obj).unlisten(('id1',), seen1) events(obj).etrigger('id1', id1=1, id2=2) self.assertEqual(id1, 1) self.assertEqual(id2, 2) events(obj).unlisten(('id2',)) events(obj).etrigger('id2', id1=1, id2=2) self.assertEqual(id1, 1) self.assertEqual(id2, 2)
def test_simple(self): global id1, id2 id1, id2 = 0, 0 obj = AnyObject() refcnt_before_init_events = sys.getrefcount(obj) init_events(obj) refcnt_after_init_events = sys.getrefcount(obj) self.assertEqual(refcnt_before_init_events, refcnt_after_init_events) events(obj).listen(('id1', ), seen1) events(obj).listen(('id2', ), seen2) events(obj).etrigger('x', id1=1, id2=2) self.assertEqual(id1, 0) self.assertEqual(id2, 0) events(obj).etrigger('id1', id1=1, id2=2) self.assertEqual(id1, 1) self.assertEqual(id2, 0) events(obj).etrigger('id2', id1=1, id2=2) self.assertEqual(id1, 1) self.assertEqual(id2, 2) events(obj).unlisten(('id1', ), seen1) events(obj).etrigger('id1', id1=1, id2=2) self.assertEqual(id1, 1) self.assertEqual(id2, 2) events(obj).unlisten(('id2', )) events(obj).etrigger('id2', id1=1, id2=2) self.assertEqual(id1, 1) self.assertEqual(id2, 2)
def test_add_global_litener(self): global id1, id2 id1, id2 = 0, 0 obj1 = AnyObject() obj2 = AnyObject() init_events(obj1, obj2) obj1.seen = 0 obj2.seen = 0 def pritn(event): event.obj.seen += 1 with global_listener(pritn): events(obj1).etrigger('x') self.assertEqual(obj1.seen, 1) events(obj2).etrigger('y') self.assertEqual(obj2.seen, 1) events(obj1).etrigger('x') self.assertEqual(obj1.seen, 1) events(obj2).etrigger('y') self.assertEqual(obj2.seen, 1) with global_listener(pritn, ('x',)): events(obj1).etrigger('x') self.assertEqual(obj1.seen, 2) events(obj2).etrigger('y') self.assertEqual(obj2.seen, 1)
def test_global_context(self): obj = AnyObject() obj2 = AnyObject() init_events(obj, obj2) obj.id1 = 0 obj2.id1 = 0 def seen4(event): obj2.id1 += 1 def seen3(event): obj.id1 += 1 events(obj).on('any_target', seen3) events(obj).etrigger('any_target') self.assertEqual(obj.id1, 1) obj.id1 = 0 events(obj2).on('any_target', seen4) events(obj2).etrigger('any_target') self.assertEqual(obj2.id1, 1) obj2.id1 = 0 self.assertFalse(obj.id1) with quiet(): events(obj).etrigger('any_target') events(obj2).etrigger('any_target') self.assertFalse(obj.id1) self.assertFalse(obj2.id1) obj.id1 = 0 obj2.id1 = 0 with queue() as tocall: with events(obj2).quiet(): events(obj).etrigger('any_target') events(obj).etrigger('any_target') events(obj2).etrigger('any_target') events(obj2).etrigger('any_target') self.assertFalse(obj.id1) self.assertFalse(obj2.id1) self.assertEqual(len(tocall), 2) for event, listener in tocall: event.dispatch(listener) self.assertEqual(obj.id1, 2) obj.id1 = 0 with unique(): events(obj).etrigger('any_target') events(obj).etrigger('any_target') self.assertFalse(obj.id1) self.assertEqual(obj.id1, 1)
def test_event_cycle(self): obj1 = AnyObject() obj2 = AnyObject() obj3 = AnyObject() init_events(obj1, obj2, obj3) self.assertFalse(connected(obj1, obj2)) connect(obj1, obj2, 'attr') connect(obj2, obj3, 'attr') with self.assertRaises(EventCycleError): connect(obj3, obj1, 'attr')
def test_static_listen(self): @init_properties class A(object): @on_trait_change('x') def x_changed(self, evetn): self.y += 1 a = A() init_events(a) a.y = 0 a.x_changed(None) self.assertEqual(a.y, 1) events(a).etrigger(('x', 'changed')) self.assertEqual(a.y, 2)
def test_refcount2(self): obj = AnyObject() init_events(obj) obj.x_called = 0 def x(event): obj.x_called += 1 self.assertEqual(obj.x_called, 0) events(obj).listen(('a',), x, weak=True) events(obj).etrigger('a') self.assertEqual(obj.x_called, 1) del x events(obj).etrigger('a') self.assertEqual(obj.x_called, 1)
def test_shortform(self): global id1, id2 id1, id2 = 0, 0 obj = AnyObject() init_events(obj) def seen3(event): global id1 id1 = True #Add a listener to target 'any_target' events(obj).on('any_target', seen3) #Add a trigger an 'any_target' event events(obj).etrigger('any_target') self.assertTrue(id1)
def test_refcount2(self): obj = AnyObject() init_events(obj) obj.x_called = 0 def x(event): obj.x_called += 1 self.assertEqual(obj.x_called, 0) events(obj).listen(('a', ), x, weak=True) events(obj).etrigger('a') self.assertEqual(obj.x_called, 1) del x events(obj).etrigger('a') self.assertEqual(obj.x_called, 1)
def test_disconnect(self): @init_properties class B(object): t = trait() t_count = 0 @t.changed() def t_changed(self, event): self.t_count += 1 @init_properties class A(object): b = trait(instance=B) bt_count = 0 @b.t.changed() def t_changed(self, event): self.bt_count += 1 a = A() b1 = B() b2 = B() init_events(a, b1, b2) a.b = b1 b1.t = 1 self.assertEqual(a.bt_count, 1) self.assertEqual(b1.t_count, 1) self.assertEqual(b2.t_count, 0) b2.t = 1 self.assertEqual(a.bt_count, 1) self.assertEqual(b1.t_count, 1) self.assertEqual(b2.t_count, 1) a.b = b2 b1.t = 1 self.assertEqual(a.bt_count, 1) self.assertEqual(b1.t_count, 2) self.assertEqual(b2.t_count, 1) b2.t = 1 self.assertEqual(a.bt_count, 2) self.assertEqual(b1.t_count, 2) self.assertEqual(b2.t_count, 2)
def test_connected(self): obj1 = AnyObject() obj2 = AnyObject() obj3 = AnyObject() init_events(obj1, obj2, obj3) self.assertFalse(connected(obj1, obj2)) connect(obj1, obj2, 'attr') self.assertTrue(connected(obj1, obj2)) self.assertFalse(connected(obj2, obj1)) connect(obj2, obj3, 'attr') self.assertTrue(connected(obj1, obj3)) self.assertTrue(connected(obj2, obj3)) self.assertFalse(connected(obj3, obj2)) self.assertFalse(connected(obj3, obj1))
def test_connected(self): obj1 = AnyObject() obj2 = AnyObject() obj3 = AnyObject() init_events(obj1, obj2, obj3) self.assertFalse(connected(obj1, obj2)) connect(obj1, obj2, 'attr') self.assertTrue(connected(obj1, obj2)) self.assertFalse(connected(obj2, obj1)) connect(obj2, obj3, 'attr') self.assertTrue(connected(obj1, obj3)) self.assertTrue(connected(obj2, obj3)) self.assertFalse(connected(obj3,obj2)) self.assertFalse(connected(obj3, obj1))
def test_chain(self): global id1, id2 id1, id2 = 0, 0 obj1 = AnyObject() obj2 = AnyObject() obj3 = AnyObject() obj4 = AnyObject() init_events( obj1, obj2, obj3, obj4, ) connect(obj1, obj2, 'c12') connect(obj2, obj3, 'c23') connect(obj2, obj4, 'c24') def seen3(event): global id1 id1 = True self.assertEqual(event.target, ('c12', 'c23', 'x')) events(obj1).listen(('c12', 'c23', 'x'), seen3) events(obj3).etrigger('x') self.assertTrue(id1) disconnect(obj1, obj2, 'c12') id1 = False events(obj3).etrigger('x') self.assertFalse(id1) connect(obj1, obj2, 'c12') del obj2 #Brake Link gc.collect() events(obj3).etrigger('x') self.assertFalse(id1)
def test_event_context(self): obj = AnyObject() init_events(obj) obj.id1 = 0 def seen3(event): obj.id1 += 1 events(obj).on('any_target', seen3) events(obj).etrigger('any_target', dispatcher=lambda event, listener:None) self.assertEqual(obj.id1, 0) events(obj).etrigger('any_target', dispatcher=lambda event, listener: listener(event)) self.assertEqual(obj.id1, 1) with events(obj).quiet(): events(obj).etrigger('any_target', dispatcher=lambda event, listener: listener(event)) self.assertEqual(obj.id1, 2) #event dispatcher has higher priority than 'quiet'
def test_chain(self): global id1, id2 id1, id2 = 0, 0 obj1 = AnyObject() obj2 = AnyObject() obj3 = AnyObject() obj4 = AnyObject() init_events(obj1, obj2, obj3, obj4,) connect(obj1, obj2, 'c12') connect(obj2, obj3, 'c23') connect(obj2, obj4, 'c24') def seen3(event): global id1 id1 = True self.assertEqual(event.target, ('c12', 'c23', 'x')) events(obj1).listen(('c12', 'c23', 'x'), seen3) events(obj3).etrigger('x') self.assertTrue(id1) disconnect(obj1, obj2, 'c12') id1 = False events(obj3).etrigger('x') self.assertFalse(id1) connect(obj1, obj2, 'c12') del obj2 #Brake Link gc.collect() events(obj3).etrigger('x') self.assertFalse(id1)
def test_refcount(self): obj = AnyObject() init_events(obj) class F(object): def m(self, event): return f = F() initial = sys.getrefcount(f) events(obj).listen('a', f.m) new = sys.getrefcount(f) self.assertEqual(initial, new) events(obj).unlisten('a', f.m) events(obj).listen('a', f.m, weak=False) new2 = sys.getrefcount(f) self.assertGreater(new2, initial) events(obj).unlisten('a', f.m) gc.collect() new = sys.getrefcount(f) self.assertEqual(initial, new)
def __init__(self, **kwargs): self.__dict__.update(kwargs) init_events(self)
def __init__(self, b): self.b_seen_c = False self.a_seen_c = False init_events(self) self.b = b