1 | /*
|
---|
2 | * ActionSequenzTest.cpp
|
---|
3 | *
|
---|
4 | * Created on: Dec 17, 2009
|
---|
5 | * Author: crueger
|
---|
6 | */
|
---|
7 |
|
---|
8 | #include <cppunit/CompilerOutputter.h>
|
---|
9 | #include <cppunit/extensions/TestFactoryRegistry.h>
|
---|
10 | #include <cppunit/ui/text/TestRunner.h>
|
---|
11 |
|
---|
12 | #include "unittests/ActionSequenceTest.hpp"
|
---|
13 | #include "Actions/Action.hpp"
|
---|
14 | #include "Actions/ActionSequence.hpp"
|
---|
15 |
|
---|
16 | // Registers the fixture into the 'registry'
|
---|
17 | CPPUNIT_TEST_SUITE_REGISTRATION( ActionSequenceTest );
|
---|
18 |
|
---|
19 | void setUp();
|
---|
20 | void tearDown();
|
---|
21 |
|
---|
22 | void canUndoTest();
|
---|
23 |
|
---|
24 | /* some neccessary stubs for tests */
|
---|
25 | class canUndoActionStub : public Action
|
---|
26 | {
|
---|
27 | public:
|
---|
28 | canUndoActionStub(): Action("canUndoActionStub",false){}
|
---|
29 | virtual ~canUndoActionStub(){}
|
---|
30 |
|
---|
31 | virtual void call(){}
|
---|
32 | virtual void undo(){}
|
---|
33 | virtual bool canUndo(){
|
---|
34 | return true;
|
---|
35 | }
|
---|
36 | };
|
---|
37 |
|
---|
38 | class cannotUndoActionStub : public Action
|
---|
39 | {
|
---|
40 | public:
|
---|
41 | cannotUndoActionStub() : Action("cannotUndoActionStub",false){}
|
---|
42 | virtual ~cannotUndoActionStub(){}
|
---|
43 |
|
---|
44 | virtual void call(){}
|
---|
45 | virtual void undo(){}
|
---|
46 | virtual bool canUndo(){
|
---|
47 | return false;
|
---|
48 | }
|
---|
49 | };
|
---|
50 |
|
---|
51 | class wasCalledActionStub : public Action
|
---|
52 | {
|
---|
53 | public:
|
---|
54 | wasCalledActionStub() :
|
---|
55 | Action("wasCalledActionStub",false),
|
---|
56 | called(false)
|
---|
57 | {}
|
---|
58 | virtual ~wasCalledActionStub(){}
|
---|
59 |
|
---|
60 | virtual void call(){
|
---|
61 | called = true;
|
---|
62 | }
|
---|
63 | virtual void undo(){
|
---|
64 | called = false;
|
---|
65 | }
|
---|
66 | virtual bool canUndo(){
|
---|
67 | return true;
|
---|
68 | }
|
---|
69 | bool wasCalled(){
|
---|
70 | return called;
|
---|
71 | }
|
---|
72 | private:
|
---|
73 | bool called;
|
---|
74 | };
|
---|
75 |
|
---|
76 | void ActionSequenceTest::setUp(){
|
---|
77 | // create some necessary stubs used in this test
|
---|
78 | positive1 = new canUndoActionStub();
|
---|
79 | positive2 = new canUndoActionStub();
|
---|
80 | negative1 = new cannotUndoActionStub();
|
---|
81 | negative2 = new cannotUndoActionStub();
|
---|
82 |
|
---|
83 | shouldCall1 = new wasCalledActionStub();
|
---|
84 | shouldCall2 = new wasCalledActionStub();
|
---|
85 | shouldNotCall1 = new wasCalledActionStub();
|
---|
86 | shouldNotCall2 = new wasCalledActionStub();
|
---|
87 |
|
---|
88 | }
|
---|
89 |
|
---|
90 | void ActionSequenceTest::tearDown(){
|
---|
91 | delete positive1;
|
---|
92 | delete positive2;
|
---|
93 | delete negative1;
|
---|
94 | delete negative2;
|
---|
95 |
|
---|
96 | delete shouldCall1;
|
---|
97 | delete shouldCall2;
|
---|
98 | delete shouldNotCall1;
|
---|
99 | delete shouldNotCall2;
|
---|
100 |
|
---|
101 | }
|
---|
102 |
|
---|
103 | void ActionSequenceTest::canUndoTest(){
|
---|
104 | // first section:
|
---|
105 | {
|
---|
106 | // test some combinations
|
---|
107 | {
|
---|
108 | ActionSequence *sequence = new ActionSequence();
|
---|
109 | sequence->addAction(positive1);
|
---|
110 | sequence->addAction(positive2);
|
---|
111 | CPPUNIT_ASSERT_EQUAL( true, sequence->canUndo() );
|
---|
112 | delete sequence;
|
---|
113 | }
|
---|
114 | {
|
---|
115 | ActionSequence *sequence = new ActionSequence();
|
---|
116 | sequence->addAction(positive1);
|
---|
117 | sequence->addAction(negative2);
|
---|
118 | CPPUNIT_ASSERT_EQUAL( false, sequence->canUndo() );
|
---|
119 | delete sequence;
|
---|
120 | }
|
---|
121 | {
|
---|
122 | ActionSequence *sequence = new ActionSequence();
|
---|
123 | sequence->addAction(negative1);
|
---|
124 | sequence->addAction(positive2);
|
---|
125 | CPPUNIT_ASSERT_EQUAL( false, sequence->canUndo() );
|
---|
126 | delete sequence;
|
---|
127 | }
|
---|
128 | {
|
---|
129 | ActionSequence *sequence = new ActionSequence();
|
---|
130 | sequence->addAction(negative1);
|
---|
131 | sequence->addAction(negative2);
|
---|
132 | CPPUNIT_ASSERT_EQUAL( false, sequence->canUndo() );
|
---|
133 | delete sequence;
|
---|
134 | }
|
---|
135 | }
|
---|
136 |
|
---|
137 | // second section:
|
---|
138 | {
|
---|
139 | // empty sequence can be undone
|
---|
140 | ActionSequence *sequence = new ActionSequence();
|
---|
141 | CPPUNIT_ASSERT_EQUAL( true, sequence->canUndo() );
|
---|
142 | // if only a positive action is contained it can be undone
|
---|
143 | sequence->addAction(positive1);
|
---|
144 | CPPUNIT_ASSERT_EQUAL( true, sequence->canUndo() );
|
---|
145 | // the single negative action should block the process
|
---|
146 | sequence->addAction(negative1);
|
---|
147 | CPPUNIT_ASSERT_EQUAL( false, sequence->canUndo() );
|
---|
148 | // after removing the negative action all is well again
|
---|
149 | sequence->removeLastAction();
|
---|
150 | CPPUNIT_ASSERT_EQUAL( true, sequence->canUndo() );
|
---|
151 | delete sequence;
|
---|
152 | }
|
---|
153 | }
|
---|
154 |
|
---|
155 | void ActionSequenceTest::doesCallTest(){
|
---|
156 | ActionSequence *sequence = new ActionSequence();
|
---|
157 | sequence->addAction(shouldCall1);
|
---|
158 | sequence->addAction(shouldCall2);
|
---|
159 | sequence->addAction(shouldNotCall1);
|
---|
160 | sequence->addAction(shouldNotCall2);
|
---|
161 | sequence->removeLastAction();
|
---|
162 | sequence->removeLastAction();
|
---|
163 |
|
---|
164 | sequence->callAll();
|
---|
165 |
|
---|
166 | CPPUNIT_ASSERT_EQUAL(true,shouldCall1->wasCalled());
|
---|
167 | CPPUNIT_ASSERT_EQUAL(true,shouldCall2->wasCalled());
|
---|
168 | CPPUNIT_ASSERT_EQUAL(false,shouldNotCall1->wasCalled());
|
---|
169 | CPPUNIT_ASSERT_EQUAL(false,shouldNotCall2->wasCalled());
|
---|
170 |
|
---|
171 | }
|
---|
172 |
|
---|
173 | void ActionSequenceTest::doesUndoTest(){
|
---|
174 | ActionSequence *sequence = new ActionSequence();
|
---|
175 | sequence->addAction(shouldNotCall1);
|
---|
176 | sequence->addAction(shouldNotCall2);
|
---|
177 | sequence->addAction(shouldCall1);
|
---|
178 | sequence->addAction(shouldCall2);
|
---|
179 |
|
---|
180 | sequence->callAll();
|
---|
181 |
|
---|
182 | sequence->removeLastAction();
|
---|
183 | sequence->removeLastAction();
|
---|
184 |
|
---|
185 | sequence->undoAll();
|
---|
186 |
|
---|
187 | CPPUNIT_ASSERT_EQUAL(true,shouldCall1->wasCalled());
|
---|
188 | CPPUNIT_ASSERT_EQUAL(true,shouldCall2->wasCalled());
|
---|
189 | CPPUNIT_ASSERT_EQUAL(false,shouldNotCall1->wasCalled());
|
---|
190 | CPPUNIT_ASSERT_EQUAL(false,shouldNotCall2->wasCalled());
|
---|
191 |
|
---|
192 | }
|
---|
193 |
|
---|
194 |
|
---|
195 | /********************************************** Main routine **************************************/
|
---|
196 |
|
---|
197 | int main(int argc, char **argv)
|
---|
198 | {
|
---|
199 | // Get the top level suite from the registry
|
---|
200 | CppUnit::Test *suite = CppUnit::TestFactoryRegistry::getRegistry().makeTest();
|
---|
201 |
|
---|
202 | // Adds the test to the list of test to run
|
---|
203 | CppUnit::TextUi::TestRunner runner;
|
---|
204 | runner.addTest( suite );
|
---|
205 |
|
---|
206 | // Change the default outputter to a compiler error format outputter
|
---|
207 | runner.setOutputter( new CppUnit::CompilerOutputter( &runner.result(),
|
---|
208 | std::cerr ) );
|
---|
209 | // Run the tests.
|
---|
210 | bool wasSucessful = runner.run();
|
---|
211 |
|
---|
212 | // Return error code 1 if the one of test failed.
|
---|
213 | return wasSucessful ? 0 : 1;
|
---|
214 | };
|
---|