1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
|
# -*- coding: utf-8 -*-
# ******************************************************************************
#
# Copyright (C) 2009 Olivier Tilloy <olivier@tilloy.net>
#
# This file is part of the pyexiv2 distribution.
#
# pyexiv2 is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# pyexiv2 is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with pyexiv2; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, 5th Floor, Boston, MA 02110-1301 USA.
#
# Author: Olivier Tilloy <olivier@tilloy.net>
#
# ******************************************************************************
import unittest
from pyexiv2 import ListenerInterface, NotifyingList
import random
class SimpleListener(ListenerInterface):
# Total number of notifications
notifications = 0
# Last notification: (method_name, args)
last = None
def _notify(self, method_name, *args):
self.notifications += 1
self.last = (method_name, args)
def item_changed(self, index, item):
self._notify('item_changed', index, item)
def item_deleted(self, index):
self._notify('item_deleted', index)
def item_appended(self, item):
self._notify('item_appended', item)
def extended(self, items):
self._notify('extended', items)
def item_inserted(self, index, item):
self._notify('item_inserted', index, item)
def item_popped(self, index):
self._notify('item_popped', index)
def item_removed(self, item):
self._notify('item_removed', item)
class TestNotifyingList(unittest.TestCase):
def setUp(self):
self.values = NotifyingList([5, 7, 9, 14, 57, 3, 2])
def test_no_listener(self):
# No listener is registered, nothing should happen.
self.values[3] = 13
# TODO: test all operations (insertion, deletion, slicing, ...)
def test_listener_interface(self):
self.values.register_listener(ListenerInterface())
self.failUnlessRaises(NotImplementedError, self.values.__setitem__, 3, 13)
self.failUnlessRaises(NotImplementedError, self.values.__delitem__, 5)
self.failUnlessRaises(NotImplementedError, self.values.append, 17)
self.failUnlessRaises(NotImplementedError, self.values.extend, [11, 22])
self.failUnlessRaises(NotImplementedError, self.values.insert, 4, 24)
self.failUnlessRaises(NotImplementedError, self.values.pop)
# TODO: test all operations (insertion, slicing, ...)
def test_multiple_listeners(self):
# Register a random number of listeners
listeners = [SimpleListener() for i in xrange(random.randint(3, 20))]
for listener in listeners:
self.values.register_listener(listener)
self.values[3] = 13
self.failUnlessEqual(self.values, [5, 7, 9, 13, 57, 3, 2])
for listener in listeners:
self.failUnlessEqual(listener.notifications, 1)
self.failUnlessEqual(listener.last, ('item_changed', (3, 13)))
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(listener.notifications, 1)
self.failUnlessEqual(listener.last, ('item_changed', (3, 13)))
del self.values[5]
self.failUnlessEqual(self.values, [5, 7, 9, 13, 57, 2])
for listener in listeners:
self.failUnlessEqual(listener.notifications, 2)
self.failUnlessEqual(listener.last, ('item_deleted', (5,)))
self.failUnlessRaises(IndexError, self.values.__delitem__, 9)
self.failUnlessEqual(self.values, [5, 7, 9, 13, 57, 2])
for listener in listeners:
self.failUnlessEqual(listener.notifications, 2)
self.failUnlessEqual(listener.last, ('item_deleted', (5,)))
self.values.append(17)
self.failUnlessEqual(self.values, [5, 7, 9, 13, 57, 2, 17])
for listener in listeners:
self.failUnlessEqual(listener.notifications, 3)
self.failUnlessEqual(listener.last, ('item_appended', (17,)))
self.values.extend([11, 22])
self.failUnlessEqual(self.values, [5, 7, 9, 13, 57, 2, 17, 11, 22])
for listener in listeners:
self.failUnlessEqual(listener.notifications, 4)
self.failUnlessEqual(listener.last, ('extended', ([11, 22],)))
self.failUnlessRaises(TypeError, self.values.extend, 26)
self.failUnlessEqual(self.values, [5, 7, 9, 13, 57, 2, 17, 11, 22])
for listener in listeners:
self.failUnlessEqual(listener.notifications, 4)
self.failUnlessEqual(listener.last, ('extended', ([11, 22],)))
self.values.insert(4, 24)
self.failUnlessEqual(self.values, [5, 7, 9, 13, 24, 57, 2, 17, 11, 22])
for listener in listeners:
self.failUnlessEqual(listener.notifications, 5)
self.failUnlessEqual(listener.last, ('item_inserted', (4, 24)))
self.values.pop()
self.failUnlessEqual(self.values, [5, 7, 9, 13, 24, 57, 2, 17, 11])
for listener in listeners:
self.failUnlessEqual(listener.notifications, 6)
self.failUnlessEqual(listener.last, ('item_popped', (9,)))
self.values.pop(4)
self.failUnlessEqual(self.values, [5, 7, 9, 13, 57, 2, 17, 11])
for listener in listeners:
self.failUnlessEqual(listener.notifications, 7)
self.failUnlessEqual(listener.last, ('item_popped', (4,)))
self.failUnlessRaises(IndexError, self.values.pop, 33)
self.failUnlessEqual(self.values, [5, 7, 9, 13, 57, 2, 17, 11])
for listener in listeners:
self.failUnlessEqual(listener.notifications, 7)
self.failUnlessEqual(listener.last, ('item_popped', (4,)))
self.values.remove(9)
self.failUnlessEqual(self.values, [5, 7, 13, 57, 2, 17, 11])
for listener in listeners:
self.failUnlessEqual(listener.notifications, 8)
self.failUnlessEqual(listener.last, ('item_removed', (9,)))
self.failUnlessRaises(ValueError, self.values.remove, 33)
self.failUnlessEqual(self.values, [5, 7, 13, 57, 2, 17, 11])
for listener in listeners:
self.failUnlessEqual(listener.notifications, 8)
self.failUnlessEqual(listener.last, ('item_removed', (9,)))
# TODO: test all operations (slicing, ...)
|