From 556989058b7a63eaee7738b1b8b7547d82dd4b0b Mon Sep 17 00:00:00 2001 From: Olivier Tilloy Date: Tue, 9 Jun 2009 08:26:35 +0200 Subject: Very simplistic implementation of the notifying list (only one notification). --- unittest/notifying_list.py | 166 ++++++++++++--------------------------------- 1 file changed, 43 insertions(+), 123 deletions(-) (limited to 'unittest/notifying_list.py') diff --git a/unittest/notifying_list.py b/unittest/notifying_list.py index 346216b..c64f7b3 100644 --- a/unittest/notifying_list.py +++ b/unittest/notifying_list.py @@ -32,23 +32,10 @@ import random class SimpleListener(ListenerInterface): def __init__(self): - # Notification stack: list of (method_name, args) - self.stack = [] + self.changes = 0 - def _notify(self, method_name, *args): - self.stack.append((method_name, args)) - - def items_changed(self, start_index, items): - self._notify('items_changed', start_index, items) - - def items_deleted(self, start_index, end_index): - self._notify('items_deleted', start_index, end_index) - - def items_inserted(self, start_index, items): - self._notify('items_inserted', start_index, items) - - def reordered(self): - self._notify('reordered') + def contents_changed(self): + self.changes += 1 class TestNotifyingList(unittest.TestCase): @@ -104,16 +91,12 @@ class TestNotifyingList(unittest.TestCase): self.values[3] = 13 self.failUnlessEqual(self.values, [5, 7, 9, 13, 57, 3, 2]) for listener in listeners: - self.failUnlessEqual(len(listener.stack), 1) - self.failUnlessEqual(listener.stack[-1], - ('items_changed', (3, [13]))) + self.failUnlessEqual(listener.changes, 1) self.failUnlessRaises(IndexError, self.values.__setitem__, 9, 27) self.failUnlessEqual(self.values, [5, 7, 9, 13, 57, 3, 2]) for listener in listeners: - self.failUnlessEqual(len(listener.stack), 1) - self.failUnlessEqual(listener.stack[-1], - ('items_changed', (3, [13]))) + self.failUnlessEqual(listener.changes, 1) def test_delitem(self): listeners = self._register_listeners() @@ -121,14 +104,12 @@ class TestNotifyingList(unittest.TestCase): del self.values[5] self.failUnlessEqual(self.values, [5, 7, 9, 14, 57, 2]) for listener in listeners: - self.failUnlessEqual(len(listener.stack), 1) - self.failUnlessEqual(listener.stack[-1], ('items_deleted', (5, 6))) + self.failUnlessEqual(listener.changes, 1) self.failUnlessRaises(IndexError, self.values.__delitem__, 9) self.failUnlessEqual(self.values, [5, 7, 9, 14, 57, 2]) for listener in listeners: - self.failUnlessEqual(len(listener.stack), 1) - self.failUnlessEqual(listener.stack[-1], ('items_deleted', (5, 6))) + self.failUnlessEqual(listener.changes, 1) def test_append(self): listeners = self._register_listeners() @@ -136,9 +117,7 @@ class TestNotifyingList(unittest.TestCase): self.values.append(17) self.failUnlessEqual(self.values, [5, 7, 9, 14, 57, 3, 2, 17]) for listener in listeners: - self.failUnlessEqual(len(listener.stack), 1) - self.failUnlessEqual(listener.stack[-1], - ('items_inserted', (7, [17]))) + self.failUnlessEqual(listener.changes, 1) def test_extend(self): listeners = self._register_listeners() @@ -146,16 +125,12 @@ class TestNotifyingList(unittest.TestCase): self.values.extend([11, 22]) self.failUnlessEqual(self.values, [5, 7, 9, 14, 57, 3, 2, 11, 22]) for listener in listeners: - self.failUnlessEqual(len(listener.stack), 1) - self.failUnlessEqual(listener.stack[-1], - ('items_inserted', (7, [11, 22]))) + self.failUnlessEqual(listener.changes, 1) self.failUnlessRaises(TypeError, self.values.extend, 26) self.failUnlessEqual(self.values, [5, 7, 9, 14, 57, 3, 2, 11, 22]) for listener in listeners: - self.failUnlessEqual(len(listener.stack), 1) - self.failUnlessEqual(listener.stack[-1], - ('items_inserted', (7, [11, 22]))) + self.failUnlessEqual(listener.changes, 1) def test_insert(self): listeners = self._register_listeners() @@ -163,9 +138,7 @@ class TestNotifyingList(unittest.TestCase): self.values.insert(4, 24) self.failUnlessEqual(self.values, [5, 7, 9, 14, 24, 57, 3, 2]) for listener in listeners: - self.failUnlessEqual(len(listener.stack), 1) - self.failUnlessEqual(listener.stack[-1], - ('items_inserted', (4, [24]))) + self.failUnlessEqual(listener.changes, 1) def test_pop(self): listeners = self._register_listeners() @@ -173,26 +146,22 @@ class TestNotifyingList(unittest.TestCase): self.values.pop() self.failUnlessEqual(self.values, [5, 7, 9, 14, 57, 3]) for listener in listeners: - self.failUnlessEqual(len(listener.stack), 1) - self.failUnlessEqual(listener.stack[-1], ('items_deleted', (6, 7))) + self.failUnlessEqual(listener.changes, 1) self.values.pop(4) self.failUnlessEqual(self.values, [5, 7, 9, 14, 3]) for listener in listeners: - self.failUnlessEqual(len(listener.stack), 2) - self.failUnlessEqual(listener.stack[-1], ('items_deleted', (4, 5))) + self.failUnlessEqual(listener.changes, 2) self.values.pop(-2) self.failUnlessEqual(self.values, [5, 7, 9, 3]) for listener in listeners: - self.failUnlessEqual(len(listener.stack), 3) - self.failUnlessEqual(listener.stack[-1], ('items_deleted', (3, 4))) + self.failUnlessEqual(listener.changes, 3) self.failUnlessRaises(IndexError, self.values.pop, 33) self.failUnlessEqual(self.values, [5, 7, 9, 3]) for listener in listeners: - self.failUnlessEqual(len(listener.stack), 3) - self.failUnlessEqual(listener.stack[-1], ('items_deleted', (3, 4))) + self.failUnlessEqual(listener.changes, 3) def test_remove(self): listeners = self._register_listeners() @@ -200,14 +169,12 @@ class TestNotifyingList(unittest.TestCase): self.values.remove(9) self.failUnlessEqual(self.values, [5, 7, 14, 57, 3, 2]) for listener in listeners: - self.failUnlessEqual(len(listener.stack), 1) - self.failUnlessEqual(listener.stack[-1], ('items_deleted', (2, 3))) + self.failUnlessEqual(listener.changes, 1) self.failUnlessRaises(ValueError, self.values.remove, 33) self.failUnlessEqual(self.values, [5, 7, 14, 57, 3, 2]) for listener in listeners: - self.failUnlessEqual(len(listener.stack), 1) - self.failUnlessEqual(listener.stack[-1], ('items_deleted', (2, 3))) + self.failUnlessEqual(listener.changes, 1) def test_reverse(self): listeners = self._register_listeners() @@ -215,8 +182,7 @@ class TestNotifyingList(unittest.TestCase): self.values.reverse() self.failUnlessEqual(self.values, [2, 3, 57, 14, 9, 7, 5]) for listener in listeners: - self.failUnlessEqual(len(listener.stack), 1) - self.failUnlessEqual(listener.stack[-1], ('reordered', ())) + self.failUnlessEqual(listener.changes, 1) def test_sort(self): listeners = self._register_listeners() @@ -224,26 +190,22 @@ class TestNotifyingList(unittest.TestCase): self.values.sort() self.failUnlessEqual(self.values, [2, 3, 5, 7, 9, 14, 57]) for listener in listeners: - self.failUnlessEqual(len(listener.stack), 1) - self.failUnlessEqual(listener.stack[-1], ('reordered', ())) + self.failUnlessEqual(listener.changes, 1) self.values.sort(cmp=lambda x, y: y - x) self.failUnlessEqual(self.values, [57, 14, 9, 7, 5, 3, 2]) for listener in listeners: - self.failUnlessEqual(len(listener.stack), 2) - self.failUnlessEqual(listener.stack[-1], ('reordered', ())) + self.failUnlessEqual(listener.changes, 2) self.values.sort(key=lambda x: x * x) self.failUnlessEqual(self.values, [2, 3, 5, 7, 9, 14, 57]) for listener in listeners: - self.failUnlessEqual(len(listener.stack), 3) - self.failUnlessEqual(listener.stack[-1], ('reordered', ())) + self.failUnlessEqual(listener.changes, 3) self.values.sort(reverse=True) self.failUnlessEqual(self.values, [57, 14, 9, 7, 5, 3, 2]) for listener in listeners: - self.failUnlessEqual(len(listener.stack), 4) - self.failUnlessEqual(listener.stack[-1], ('reordered', ())) + self.failUnlessEqual(listener.changes, 4) def test_iadd(self): listeners = self._register_listeners() @@ -251,9 +213,7 @@ class TestNotifyingList(unittest.TestCase): self.values += [44, 31, 19] self.failUnlessEqual(self.values, [5, 7, 9, 14, 57, 3, 2, 44, 31, 19]) for listener in listeners: - self.failUnlessEqual(len(listener.stack), 1) - self.failUnlessEqual(listener.stack[-1], - ('items_inserted', (7, [44, 31, 19]))) + self.failUnlessEqual(listener.changes, 1) def test_imul(self): listeners = self._register_listeners() @@ -264,11 +224,7 @@ class TestNotifyingList(unittest.TestCase): 5, 7, 9, 14, 57, 3, 2, 5, 7, 9, 14, 57, 3, 2]) for listener in listeners: - self.failUnlessEqual(len(listener.stack), 1) - self.failUnlessEqual(listener.stack[-1], - ('items_inserted', - (7, [5, 7, 9, 14, 57, 3, 2, - 5, 7, 9, 14, 57, 3, 2]))) + self.failUnlessEqual(listener.changes, 1) def test_setslice(self): listeners = self._register_listeners() @@ -278,89 +234,61 @@ class TestNotifyingList(unittest.TestCase): self.values[2:4] = [3, 4] self.failUnlessEqual(self.values, [5, 7, 3, 4, 57, 3, 2]) for listener in listeners: - self.failUnlessEqual(len(listener.stack), 1) - self.failUnlessEqual(listener.stack[-1], - ('items_changed', (2, [3, 4]))) + self.failUnlessEqual(listener.changes, 1) self.values[3:5] = [77, 8, 12] self.failUnlessEqual(self.values, [5, 7, 3, 77, 8, 12, 3, 2]) for listener in listeners: - self.failUnlessEqual(len(listener.stack), 3) - self.failUnlessEqual(listener.stack[-2], ('items_deleted', (3, 5))) - self.failUnlessEqual(listener.stack[-1], - ('items_inserted', (3, [77, 8, 12]))) + self.failUnlessEqual(listener.changes, 2) self.values[2:5] = [1, 0] self.failUnlessEqual(self.values, [5, 7, 1, 0, 12, 3, 2]) for listener in listeners: - self.failUnlessEqual(len(listener.stack), 5) - self.failUnlessEqual(listener.stack[-2], - ('items_changed', (2, [1, 0]))) - self.failUnlessEqual(listener.stack[-1], ('items_deleted', (4, 5))) + self.failUnlessEqual(listener.changes, 3) self.values[0:2] = [] self.failUnlessEqual(self.values, [1, 0, 12, 3, 2]) for listener in listeners: - self.failUnlessEqual(len(listener.stack), 6) - self.failUnlessEqual(listener.stack[-1], ('items_deleted', (0, 2))) + self.failUnlessEqual(listener.changes, 4) self.values[2:2] = [7, 5] self.failUnlessEqual(self.values, [1, 0, 7, 5, 12, 3, 2]) for listener in listeners: - self.failUnlessEqual(len(listener.stack), 7) - self.failUnlessEqual(listener.stack[-1], - ('items_inserted', (2, [7, 5]))) + self.failUnlessEqual(listener.changes, 5) # With negatives indexes self.values[4:-2] = [9] self.failUnlessEqual(self.values, [1, 0, 7, 5, 9, 3, 2]) for listener in listeners: - self.failUnlessEqual(len(listener.stack), 8) - self.failUnlessEqual(listener.stack[-1], - ('items_changed', (4, [9]))) + self.failUnlessEqual(listener.changes, 6) self.values[-2:1] = [6, 4] self.failUnlessEqual(self.values, [1, 0, 7, 5, 9, 6, 4, 3, 2]) for listener in listeners: - self.failUnlessEqual(len(listener.stack), 9) - self.failUnlessEqual(listener.stack[-1], - ('items_inserted', (5, [6, 4]))) + self.failUnlessEqual(listener.changes, 7) self.values[-5:-2] = [8] self.failUnlessEqual(self.values, [1, 0, 7, 5, 8, 3, 2]) for listener in listeners: - self.failUnlessEqual(len(listener.stack), 11) - self.failUnlessEqual(listener.stack[-2], - ('items_changed', (4, [8]))) - self.failUnlessEqual(listener.stack[-1], ('items_deleted', (5, 7))) + self.failUnlessEqual(listener.changes, 8) # With missing (implicit) indexes self.values[:2] = [4] self.failUnlessEqual(self.values, [4, 7, 5, 8, 3, 2]) for listener in listeners: - self.failUnlessEqual(len(listener.stack), 13) - self.failUnlessEqual(listener.stack[-2], - ('items_changed', (0, [4]))) - self.failUnlessEqual(listener.stack[-1], ('items_deleted', (1, 2))) + self.failUnlessEqual(listener.changes, 9) self.values[4:] = [1] self.failUnlessEqual(self.values, [4, 7, 5, 8, 1]) for listener in listeners: - self.failUnlessEqual(len(listener.stack), 15) - self.failUnlessEqual(listener.stack[-2], - ('items_changed', (4, [1]))) - self.failUnlessEqual(listener.stack[-1], ('items_deleted', (5, 6))) + self.failUnlessEqual(listener.changes, 10) self.values[:] = [5, 7, 9, 14, 57, 3, 2] self.failUnlessEqual(self.values, [5, 7, 9, 14, 57, 3, 2]) for listener in listeners: - self.failUnlessEqual(len(listener.stack), 17) - self.failUnlessEqual(listener.stack[-2], ('items_deleted', (0, 5))) - self.failUnlessEqual(listener.stack[-1], - ('items_inserted', - (0, [5, 7, 9, 14, 57, 3, 2]))) + self.failUnlessEqual(listener.changes, 11) def test_delslice(self): listeners = self._register_listeners() @@ -368,51 +296,43 @@ class TestNotifyingList(unittest.TestCase): del self.values[2:3] self.failUnlessEqual(self.values, [5, 7, 14, 57, 3, 2]) for listener in listeners: - self.failUnlessEqual(len(listener.stack), 1) - self.failUnlessEqual(listener.stack[-1], ('items_deleted', (2, 3))) + self.failUnlessEqual(listener.changes, 1) del self.values[2:2] self.failUnlessEqual(self.values, [5, 7, 14, 57, 3, 2]) for listener in listeners: - self.failUnlessEqual(len(listener.stack), 1) - self.failUnlessEqual(listener.stack[-1], ('items_deleted', (2, 3))) + self.failUnlessEqual(listener.changes, 1) # With negatives indexes del self.values[4:-1] self.failUnlessEqual(self.values, [5, 7, 14, 57, 2]) for listener in listeners: - self.failUnlessEqual(len(listener.stack), 2) - self.failUnlessEqual(listener.stack[-1], ('items_deleted', (4, 5))) + self.failUnlessEqual(listener.changes, 2) del self.values[-1:5] self.failUnlessEqual(self.values, [5, 7, 14, 57]) for listener in listeners: - self.failUnlessEqual(len(listener.stack), 3) - self.failUnlessEqual(listener.stack[-1], ('items_deleted', (4, 5))) + self.failUnlessEqual(listener.changes, 3) del self.values[-2:-1] self.failUnlessEqual(self.values, [5, 7, 57]) for listener in listeners: - self.failUnlessEqual(len(listener.stack), 4) - self.failUnlessEqual(listener.stack[-1], ('items_deleted', (2, 3))) + self.failUnlessEqual(listener.changes, 4) # With missing (implicit) indexes del self.values[:1] self.failUnlessEqual(self.values, [7, 57]) for listener in listeners: - self.failUnlessEqual(len(listener.stack), 5) - self.failUnlessEqual(listener.stack[-1], ('items_deleted', (0, 1))) + self.failUnlessEqual(listener.changes, 5) del self.values[1:] self.failUnlessEqual(self.values, [7]) for listener in listeners: - self.failUnlessEqual(len(listener.stack), 6) - self.failUnlessEqual(listener.stack[-1], ('items_deleted', (1, 2))) + self.failUnlessEqual(listener.changes, 6) del self.values[:] self.failUnlessEqual(self.values, []) for listener in listeners: - self.failUnlessEqual(len(listener.stack), 7) - self.failUnlessEqual(listener.stack[-1], ('items_deleted', (0, 1))) + self.failUnlessEqual(listener.changes, 7) -- cgit