def test_pickle_events(self): event = EventSystem(marker) variable = self.variable_type(event=event, value_factory=list) changes = [] def changed(owner, variable, old_value, new_value, fromdb): changes.append((variable, old_value, new_value, fromdb)) event.emit("start-tracking-changes", event) event.hook("changed", changed) variable.checkpoint() event.emit("flush") self.assertEquals(changes, []) lst = variable.get() self.assertEquals(lst, []) self.assertEquals(changes, []) lst.append("a") self.assertEquals(changes, []) event.emit("flush") self.assertEquals(changes, [(variable, None, ["a"], False)]) del changes[:] event.emit("object-deleted") self.assertEquals(changes, [(variable, None, ["a"], False)])
def test_list_events(): event = EventSystem(marker) variable = ListVariable(RawStrVariable, event=event, value_factory=list) changes = [] def changed(owner, variable, old_value, new_value, fromdb): changes.append((variable, old_value, new_value, fromdb)) event.emit("start-tracking-changes", event) event.hook("changed", changed) variable.checkpoint() event.emit("flush") assert changes == [] lst = variable.get() assert lst == [] assert changes == [] lst.append("a") assert changes == [] event.emit("flush") assert changes == [(variable, None, ["a"], False)] del changes[:] event.emit("object-deleted") assert changes == [(variable, None, ["a"], False)]
def test_weak_reference(): marker = Marker() called = [] def callback(owner): called.append(owner) event = EventSystem(marker) event.hook("event", callback) event.emit("event") assert called == [marker] del called[:] del marker event.emit("event") assert called == []
class EventTest(TestHelper): def setUp(self): TestHelper.setUp(self) self.event = EventSystem(marker) def test_hook_unhook_emit(self): called1 = [] called2 = [] def callback1(owner, arg1, arg2): called1.append((owner, arg1, arg2)) def callback2(owner, arg1, arg2, data1, data2): called2.append((owner, arg1, arg2, data1, data2)) self.event.hook("one", callback1) self.event.hook("one", callback1) self.event.hook("one", callback2, 10, 20) self.event.hook("two", callback2, 10, 20) self.event.hook("two", callback2, 10, 20) self.event.hook("two", callback2, 30, 40) self.event.hook("three", callback1) self.event.emit("one", 1, 2) self.event.emit("two", 3, 4) self.event.unhook("two", callback2, 10, 20) self.event.emit("two", 3, 4) self.event.emit("three", 5, 6) self.assertEquals(sorted(called1), [ (marker, 1, 2), (marker, 5, 6), ]) self.assertEquals(sorted(called2), [ (marker, 1, 2, 10, 20), (marker, 3, 4, 10, 20), (marker, 3, 4, 30, 40), (marker, 3, 4, 30, 40), ]) def test_unhook_by_returning_false(self): called = [] def callback(owner): called.append(owner) return len(called) < 2 self.event.hook("event", callback) self.event.emit("event") self.event.emit("event") self.event.emit("event") self.event.emit("event") self.assertEquals(called, [marker, marker]) def test_weak_reference(self): marker = Marker() called = [] def callback(owner): called.append(owner) self.event = EventSystem(marker) self.event.hook("event", callback) self.event.emit("event") self.assertEquals(called, [marker]) del called[:] del marker self.event.emit("event") self.assertEquals(called, [])