1 |
bh |
1123 |
# Copyright (c) 2002, 2003 by Intevation GmbH |
2 |
bh |
327 |
# Authors: |
3 |
|
|
# Bernhard Herzog <[email protected]> |
4 |
|
|
# |
5 |
|
|
# This program is free software under the GPL (>=v2) |
6 |
|
|
# Read the file COPYING coming with Thuban for details. |
7 |
|
|
|
8 |
|
|
""" |
9 |
|
|
Test the TitledObject and Modifiable |
10 |
|
|
""" |
11 |
|
|
|
12 |
|
|
__version__ = "$Revision$" |
13 |
|
|
# $Source$ |
14 |
|
|
# $Id$ |
15 |
|
|
|
16 |
|
|
import unittest |
17 |
|
|
|
18 |
|
|
import support |
19 |
|
|
support.initthuban() |
20 |
|
|
|
21 |
|
|
from Thuban.Model.base import TitledObject, Modifiable |
22 |
|
|
from Thuban.Model.messages import TITLE_CHANGED, CHANGED |
23 |
|
|
|
24 |
|
|
SOMETHING_ELSE = "SOMETHING_ELSE" |
25 |
|
|
|
26 |
bh |
1123 |
class SomeTitledObject(TitledObject, Modifiable): |
27 |
bh |
327 |
|
28 |
|
|
"""TitledObject for test purposes. |
29 |
|
|
|
30 |
|
|
TitledObject is not used directly. It's a mixin to be used together |
31 |
bh |
1123 |
with Modifiable. |
32 |
bh |
327 |
""" |
33 |
|
|
|
34 |
|
|
class SomeModifiable(Modifiable): |
35 |
|
|
|
36 |
|
|
"""Modifiable for test purposes. |
37 |
|
|
|
38 |
|
|
Modifiable is intended to be used as a base class for classes that |
39 |
|
|
need to inform other classes when they change and that have to keep |
40 |
|
|
track of whether they have changed. Thus we use a derived class for |
41 |
|
|
testing that provides some methods that simulate modificatios. |
42 |
|
|
""" |
43 |
|
|
|
44 |
|
|
def do_something(self): |
45 |
|
|
"""Call self.changed without parameters""" |
46 |
|
|
self.changed() |
47 |
|
|
|
48 |
|
|
def do_something_else(self): |
49 |
|
|
"""Call self.changed with a message parameter""" |
50 |
|
|
self.changed(SOMETHING_ELSE) |
51 |
|
|
|
52 |
|
|
|
53 |
|
|
class SubscriberTest(unittest.TestCase, support.SubscriberMixin): |
54 |
|
|
|
55 |
|
|
"""Base class for test cases that use SubscriberMixin. |
56 |
|
|
|
57 |
|
|
Just provide the default implementations of setUp and tearDown. |
58 |
|
|
""" |
59 |
|
|
|
60 |
|
|
def setUp(self): |
61 |
|
|
"""Clear the list of received messages""" |
62 |
|
|
self.clear_messages() |
63 |
|
|
|
64 |
|
|
def tearDown(self): |
65 |
|
|
"""Clear the list of received messages""" |
66 |
|
|
self.clear_messages() |
67 |
|
|
|
68 |
|
|
|
69 |
|
|
class TestTitledObject(SubscriberTest): |
70 |
|
|
|
71 |
|
|
"""Test cases for TitledObject""" |
72 |
|
|
|
73 |
|
|
def test_titled_object(self): |
74 |
|
|
"""Test TitledObject""" |
75 |
|
|
titled = SomeTitledObject("Some Title") |
76 |
|
|
titled.Subscribe(TITLE_CHANGED, |
77 |
|
|
self.subscribe_with_params, TITLE_CHANGED) |
78 |
|
|
|
79 |
|
|
self.assertEquals(titled.Title(), "Some Title") |
80 |
|
|
|
81 |
|
|
titled.SetTitle("New Title") |
82 |
|
|
self.assertEquals(titled.Title(), "New Title") |
83 |
|
|
|
84 |
|
|
self.check_messages([(titled, TITLE_CHANGED)]) |
85 |
|
|
|
86 |
|
|
|
87 |
|
|
class TestModifiable(SubscriberTest): |
88 |
|
|
|
89 |
|
|
"""Test cases for Modifiable""" |
90 |
|
|
|
91 |
|
|
def setUp(self): |
92 |
|
|
"""Extend the inherited method to create a Modifiable instance. |
93 |
|
|
|
94 |
|
|
Bind the Modifiable instance (actually an instance of |
95 |
|
|
SomeModifiable) to self.modifiable and subscribe to its CHANGED |
96 |
|
|
and SOMETHING_ELSE channels. |
97 |
|
|
""" |
98 |
|
|
SubscriberTest.setUp(self) |
99 |
|
|
self.modifiable = SomeModifiable() |
100 |
|
|
self.modifiable.Subscribe(CHANGED, self.subscribe_with_params, CHANGED) |
101 |
|
|
self.modifiable.Subscribe(SOMETHING_ELSE, |
102 |
|
|
self.subscribe_with_params, SOMETHING_ELSE) |
103 |
|
|
|
104 |
|
|
def tearDown(self): |
105 |
|
|
"""Extend the inherited method to explicitly destroy self.modifiable. |
106 |
|
|
""" |
107 |
|
|
self.modifiable.Destroy() |
108 |
|
|
SubscriberTest.tearDown(self) |
109 |
|
|
|
110 |
|
|
def test_initial_state(self): |
111 |
|
|
"""Test Modifiable initial state""" |
112 |
|
|
# initially a modifiable is unmodified |
113 |
|
|
self.failIf(self.modifiable.WasModified()) |
114 |
|
|
# this test should not have resulted in any messages |
115 |
|
|
self.check_messages([]) |
116 |
|
|
|
117 |
|
|
def test_silent_change(self): |
118 |
|
|
"""Test Modifiable method which doesn't issue messages""" |
119 |
|
|
self.modifiable.do_something() |
120 |
|
|
# Now it should be modified and we should not have received any |
121 |
|
|
# message |
122 |
|
|
self.assert_(self.modifiable.WasModified()) |
123 |
|
|
self.check_messages([]) |
124 |
|
|
|
125 |
|
|
def test_unset_modified(self): |
126 |
|
|
"""Test Modifiable.UnsetModified. |
127 |
|
|
|
128 |
|
|
Test whether the UnsetModified does in fact clear the modified |
129 |
|
|
flag and whether it issues a CHANGED message if it changes the |
130 |
|
|
modified flag from true to false. |
131 |
|
|
""" |
132 |
|
|
# We start with an unmodified object, so when we call |
133 |
|
|
# UnsetModified now it shouldn't result in any message. |
134 |
|
|
self.modifiable.UnsetModified() |
135 |
|
|
self.check_messages([]) |
136 |
|
|
self.failIf(self.modifiable.WasModified()) |
137 |
|
|
|
138 |
|
|
# Call a method that modifies the object silently. |
139 |
|
|
self.modifiable.do_something() |
140 |
|
|
self.check_messages([]) |
141 |
|
|
self.assert_(self.modifiable.WasModified()) |
142 |
|
|
|
143 |
|
|
# when we now call UnsetModified it should result in a CHANGED |
144 |
|
|
# message |
145 |
|
|
self.modifiable.UnsetModified() |
146 |
|
|
self.check_messages([(CHANGED,)]) |
147 |
|
|
self.failIf(self.modifiable.WasModified()) |
148 |
|
|
|
149 |
|
|
def test_issuing_change(self): |
150 |
|
|
"""Test Modifiable method that issues messages""" |
151 |
|
|
self.modifiable.do_something_else() |
152 |
|
|
# Now it should be modified and we should have received a |
153 |
|
|
# CHANGED message |
154 |
|
|
self.check_messages([(SOMETHING_ELSE,)]) |
155 |
|
|
self.assert_(self.modifiable.WasModified()) |
156 |
|
|
|
157 |
|
|
|
158 |
|
|
|
159 |
|
|
if __name__ == "__main__": |
160 |
|
|
unittest.main() |