def setUp(self): self.history = ChangeHistory() self.ls = LiveList() self.history.track(self.ls) self.prevxs = None self.newxs = None self.ls.changeListener = self._onChanged
def __init__(self, xs=None): LiveList.__init__(self, xs) _ProjectedList.__init__(self) def _on_change(old_contents, new_contents): if self.__change_listener is not None: self.__change_listener() self.changeListener = _on_change self.__change_listener = None
def test_eq(self): self.ls[:] = range(0, 5) self.assertTrue(self.ls == range(0, 5)) self.assertTrue(self.ls == LiveList(range(0, 5))) self.assertFalse(self.ls == range(0, 6)) self.assertFalse(self.ls == LiveList(range(0, 6))) self.assertTrue(self.ls != range(0, 6)) self.assertTrue(self.ls != LiveList(range(0, 6))) self.assertFalse(self.ls != range(0, 5)) self.assertFalse(self.ls != LiveList(range(0, 5)))
def __init__(self, name='test'): super( AbstractInlineTestTable, self ).__init__() self._name = TrackedLiveValue( name ) self._tests = LiveList() self.__change_history__ = None self.__methodNames = UniqueNameTable()
def __init__(self, field, object_instance, wrapped_source_value): super(ListFieldInstance, self).__init__(field, object_instance, wrapped_source_value) def on_change(old_contents, new_contents): if self._field._change_listener is not None: self._field._change_listener(self._object_instance, self, old_contents, new_contents) source_value = wrapped_source_value[ 0] if wrapped_source_value is not None else [] self.__live = LiveList(source_value) self.__live.changeListener = on_change if wrapped_source_value is not None: on_change([], source_value)
def __init__(self, field, object_instance, wrapped_source_value): super(ChildListFieldInstance, self).__init__(field, object_instance, wrapped_source_value) def on_change(old_contents, new_contents): o = set(old_contents) n = set(new_contents) removed = o - n added = n - o for n in removed: object_instance._unregisterChild(n) for n in added: object_instance._registerChild(n) source_value = wrapped_source_value[ 0] if wrapped_source_value is not None else None self.__live = LiveList(source_value) self.__live.changeListener = on_change if wrapped_source_value is not None: on_change([], source_value)
class Test_LiveList(unittest.TestCase): def setUp(self): self.history = ChangeHistory() self.ls = LiveList() self.history.track(self.ls) self.prevxs = None self.newxs = None self.ls.changeListener = self._onChanged def tearDown(self): self.ls.changeListener = None self.history.stopTracking(self.ls) self.ls = None self.history = None self.prevxs = None self.newxs = None def _onChanged(self, oldContents, newContents): self.prevxs = oldContents self.newxs = newContents def _test_changes(self, expectedOldContents, expectedNewContents): self.assertEqual(self.prevxs, expectedOldContents) self.assertEqual(self.newxs, expectedNewContents) self.prevxs = None self.newxs = None def test_setitem(self): self.assertEqual(self.ls[:], []) _two = _Value(-2) self.ls.append(_two) self.assertEqual(self.ls[:], [_two]) self.assertTrue(_two.isTracked()) self._test_changes([], [_two]) _one = _Value(-1) self.ls[0] = _one self.assertEqual(self.ls[:], [_one]) self.assertFalse(_two.isTracked()) self.assertTrue(_one.isTracked()) self._test_changes([_two], [_one]) _rng = [_Value(x) for x in xrange(0, 5)] self.ls[1:] = _rng self.assertEqual(self.ls[:], [_one] + _rng) self.assertFalse(_two.isTracked()) self.assertTrue(_one.isTracked()) for x in _rng: self.assertTrue(x.isTracked()) self._test_changes([_one], [_one] + _rng) self.history.undo() self.assertEqual(self.ls[:], [_one]) self.assertFalse(_two.isTracked()) self.assertTrue(_one.isTracked()) for x in _rng: self.assertFalse(x.isTracked()) self._test_changes([_one] + _rng, [_one]) self.history.undo() self.assertEqual(self.ls[:], [_two]) self.assertFalse(_one.isTracked()) self.assertTrue(_two.isTracked()) self._test_changes([_one], [_two]) self.history.redo() self.assertEqual(self.ls[:], [_one]) self.assertFalse(_two.isTracked()) self.assertTrue(_one.isTracked()) for x in _rng: self.assertFalse(x.isTracked()) self._test_changes([_two], [_one]) self.history.redo() self.assertEqual(self.ls[:], [_one] + _rng) self.assertFalse(_two.isTracked()) self.assertTrue(_one.isTracked()) for x in _rng: self.assertTrue(x.isTracked()) self._test_changes([_one], [_one] + _rng) def test_delitem(self): self.assertEqual(self.ls[:], []) _rng = [_Value(x) for x in xrange(0, 5)] self.ls[:] = _rng self.assertEqual(self.ls[:], _rng) self.assertEqual([x.isTracked() for x in _rng], [True, True, True, True, True]) self._test_changes([], _rng) del self.ls[0] self.assertEqual(self.ls[:], _rng[1:]) self.assertEqual([x.isTracked() for x in _rng], [False, True, True, True, True]) self._test_changes(_rng, _rng[1:]) del self.ls[1:3] self.assertEqual(self.ls[:], [_rng[1], _rng[4]]) self.assertEqual([x.isTracked() for x in _rng], [False, True, False, False, True]) self._test_changes(_rng[1:], [_rng[1], _rng[4]]) self.history.undo() self.assertEqual(self.ls[:], _rng[1:]) self.assertEqual([x.isTracked() for x in _rng], [False, True, True, True, True]) self._test_changes([_rng[1], _rng[4]], _rng[1:]) self.history.undo() self.assertEqual(self.ls[:], _rng) self.assertEqual([x.isTracked() for x in _rng], [True, True, True, True, True]) self._test_changes(_rng[1:], _rng) self.history.redo() self.assertEqual(self.ls[:], _rng[1:]) self.assertEqual([x.isTracked() for x in _rng], [False, True, True, True, True]) self._test_changes(_rng, _rng[1:]) self.history.redo() self.assertEqual(self.ls[:], [_rng[1], _rng[4]]) self.assertEqual([x.isTracked() for x in _rng], [False, True, False, False, True]) self._test_changes(_rng[1:], [_rng[1], _rng[4]]) def test_append(self): self.assertEqual(self.ls[:], []) v = _Value(2) self.ls.append(v) self.assertTrue(v.isTracked()) self.assertEqual(self.ls[:], [_Value(2)]) self._test_changes([], [v]) self.history.undo() self.assertEqual(self.ls[:], []) self.assertFalse(v.isTracked()) self._test_changes([v], []) self.history.redo() self.assertEqual(self.ls[:], [_Value(2)]) self.assertTrue(v.isTracked()) self._test_changes([], [v]) def test_extend(self): _rng = [_Value(x) for x in xrange(0, 5)] self.assertEqual(self.ls[:], []) self.assertEqual([x.isTracked() for x in _rng], [False, False, False, False, False]) self.ls.extend(_rng) self.assertEqual(self.ls[:], _rng) self.assertEqual([x.isTracked() for x in _rng], [True, True, True, True, True]) self._test_changes([], _rng) self.history.undo() self.assertEqual(self.ls[:], []) self.assertEqual([x.isTracked() for x in _rng], [False, False, False, False, False]) self._test_changes(_rng, []) self.history.redo() self.assertEqual(self.ls[:], _rng) self.assertEqual([x.isTracked() for x in _rng], [True, True, True, True, True]) self._test_changes([], _rng) def test_insert(self): v = _Value(20) _rng = [_Value(x) for x in xrange(0, 5)] self.ls[:] = _rng self.assertEqual(self.ls[:], _rng) self.assertEqual([x.isTracked() for x in _rng], [True, True, True, True, True]) self.assertFalse(v.isTracked()) self._test_changes([], _rng) self.ls.insert(2, v) self.assertEqual(self.ls[:], _rng[:2] + [v] + _rng[2:]) self.assertEqual([x.isTracked() for x in _rng], [True, True, True, True, True]) self.assertTrue(v.isTracked()) self._test_changes(_rng, _rng[:2] + [v] + _rng[2:]) self.history.undo() self.assertEqual(self.ls[:], _rng) self.assertEqual([x.isTracked() for x in _rng], [True, True, True, True, True]) self.assertFalse(v.isTracked()) self._test_changes(_rng[:2] + [v] + _rng[2:], _rng) self.history.redo() self.assertEqual(self.ls[:], _rng[:2] + [v] + _rng[2:]) self.assertEqual([x.isTracked() for x in _rng], [True, True, True, True, True]) self.assertTrue(v.isTracked()) self._test_changes(_rng, _rng[:2] + [v] + _rng[2:]) def test_pop(self): _rng = [_Value(x) for x in xrange(0, 5)] self.ls[:] = _rng self.assertEqual(self.ls[:], _rng) self.assertEqual([x.isTracked() for x in _rng], [True, True, True, True, True]) self._test_changes([], _rng) v = self.ls.pop() self.assertTrue(v is _rng[-1]) self.assertEqual(self.ls[:], _rng[:-1]) self.assertEqual([x.isTracked() for x in _rng], [True, True, True, True, False]) self._test_changes(_rng, _rng[:-1]) self.history.undo() self.assertEqual(self.ls[:], _rng) self.assertEqual([x.isTracked() for x in _rng], [True, True, True, True, True]) self._test_changes(_rng[:-1], _rng) self.history.redo() self.assertEqual(self.ls[:], _rng[:-1]) self.assertEqual([x.isTracked() for x in _rng], [True, True, True, True, False]) self._test_changes(_rng, _rng[:-1]) def test_remove(self): _rng = [_Value(x) for x in xrange(0, 5)] self.ls[:] = _rng self.assertEqual(self.ls[:], _rng) self.assertEqual([x.isTracked() for x in _rng], [True, True, True, True, True]) self._test_changes([], _rng) self.ls.remove(_Value(2)) self.assertEqual(self.ls[:], _rng[:2] + _rng[3:]) self.assertEqual([x.isTracked() for x in _rng], [True, True, False, True, True]) self._test_changes(_rng, _rng[:2] + _rng[3:]) self.history.undo() self.assertEqual(self.ls[:], _rng) self.assertEqual([x.isTracked() for x in _rng], [True, True, True, True, True]) self._test_changes(_rng[:2] + _rng[3:], _rng) self.history.redo() self.assertEqual(self.ls[:], _rng[:2] + _rng[3:]) self.assertEqual([x.isTracked() for x in _rng], [True, True, False, True, True]) self._test_changes(_rng, _rng[:2] + _rng[3:]) def test_reverse(self): _rng = [_Value(x) for x in xrange(0, 5)] _rev = _rng[:] _rev.reverse() self.ls[:] = _rng self.assertEqual(self.ls[:], _rng) self.assertEqual([x.isTracked() for x in _rng], [True, True, True, True, True]) self._test_changes([], _rng) self.ls.reverse() self.assertEqual(self.ls[:], _rev) self.assertEqual([x.isTracked() for x in _rng], [True, True, True, True, True]) self._test_changes(_rng, _rev) self.history.undo() self.assertEqual(self.ls[:], _rng) self.assertEqual([x.isTracked() for x in _rng], [True, True, True, True, True]) self._test_changes(_rev, _rng) self.history.redo() self.assertEqual(self.ls[:], _rev) self.assertEqual([x.isTracked() for x in _rng], [True, True, True, True, True]) self._test_changes(_rng, _rev) def test_sort(self): _rng = [_Value(x) for x in xrange(0, 5)] _shuf = _rng[:] r = random.Random(12345) r.shuffle(_shuf) self.ls[:] = _shuf self.assertEqual(self.ls[:], _shuf) self.assertEqual([x.isTracked() for x in _rng], [True, True, True, True, True]) self._test_changes([], _shuf) self.ls.sort() self.assertEqual(self.ls[:], _rng) self.assertEqual([x.isTracked() for x in _rng], [True, True, True, True, True]) self._test_changes(_shuf, _rng) self.history.undo() self.assertEqual(self.ls[:], _shuf) self.assertEqual([x.isTracked() for x in _rng], [True, True, True, True, True]) self._test_changes(_rng, _shuf) self.history.redo() self.assertEqual(self.ls[:], _rng) self.assertEqual([x.isTracked() for x in _rng], [True, True, True, True, True]) self._test_changes(_shuf, _rng) def test_eq(self): self.ls[:] = range(0, 5) self.assertTrue(self.ls == range(0, 5)) self.assertTrue(self.ls == LiveList(range(0, 5))) self.assertFalse(self.ls == range(0, 6)) self.assertFalse(self.ls == LiveList(range(0, 6))) self.assertTrue(self.ls != range(0, 6)) self.assertTrue(self.ls != LiveList(range(0, 6))) self.assertFalse(self.ls != range(0, 5)) self.assertFalse(self.ls != LiveList(range(0, 5))) def test_str(self): self.ls[:] = range(0, 5) self.assertEqual(str(self.ls), str(range(0, 5))) def test_repr(self): self.ls[:] = range(0, 5) self.assertEqual(repr(self.ls), 'LiveList( ' + repr(range(0, 5)) + ' )')