source: src/unittests/ActionSequenceTest.cpp@ c449d9

Action_Thermostats Add_AtomRandomPerturbation Add_FitFragmentPartialChargesAction Add_RotateAroundBondAction Add_SelectAtomByNameAction Added_ParseSaveFragmentResults AddingActions_SaveParseParticleParameters Adding_Graph_to_ChangeBondActions Adding_MD_integration_tests Adding_ParticleName_to_Atom Adding_StructOpt_integration_tests AtomFragments Automaking_mpqc_open AutomationFragmentation_failures Candidate_v1.5.4 Candidate_v1.6.0 Candidate_v1.6.1 ChangeBugEmailaddress ChangingTestPorts ChemicalSpaceEvaluator CombiningParticlePotentialParsing Combining_Subpackages Debian_Package_split Debian_package_split_molecuildergui_only Disabling_MemDebug Docu_Python_wait EmpiricalPotential_contain_HomologyGraph EmpiricalPotential_contain_HomologyGraph_documentation Enable_parallel_make_install Enhance_userguide Enhanced_StructuralOptimization Enhanced_StructuralOptimization_continued Example_ManyWaysToTranslateAtom Exclude_Hydrogens_annealWithBondGraph FitPartialCharges_GlobalError Fix_BoundInBox_CenterInBox_MoleculeActions Fix_ChargeSampling_PBC Fix_ChronosMutex Fix_FitPartialCharges Fix_FitPotential_needs_atomicnumbers Fix_ForceAnnealing Fix_IndependentFragmentGrids Fix_ParseParticles Fix_ParseParticles_split_forward_backward_Actions Fix_PopActions Fix_QtFragmentList_sorted_selection Fix_Restrictedkeyset_FragmentMolecule Fix_StatusMsg Fix_StepWorldTime_single_argument Fix_Verbose_Codepatterns Fix_fitting_potentials Fixes ForceAnnealing_goodresults ForceAnnealing_oldresults ForceAnnealing_tocheck ForceAnnealing_with_BondGraph ForceAnnealing_with_BondGraph_continued ForceAnnealing_with_BondGraph_continued_betteresults ForceAnnealing_with_BondGraph_contraction-expansion FragmentAction_writes_AtomFragments FragmentMolecule_checks_bonddegrees GeometryObjects Gui_Fixes Gui_displays_atomic_force_velocity ImplicitCharges IndependentFragmentGrids IndependentFragmentGrids_IndividualZeroInstances IndependentFragmentGrids_IntegrationTest IndependentFragmentGrids_Sole_NN_Calculation JobMarket_RobustOnKillsSegFaults JobMarket_StableWorkerPool JobMarket_unresolvable_hostname_fix MoreRobust_FragmentAutomation ODR_violation_mpqc_open PartialCharges_OrthogonalSummation PdbParser_setsAtomName PythonUI_with_named_parameters QtGui_reactivate_TimeChanged_changes Recreated_GuiChecks Rewrite_FitPartialCharges RotateToPrincipalAxisSystem_UndoRedo SaturateAtoms_findBestMatching SaturateAtoms_singleDegree StoppableMakroAction Subpackage_CodePatterns Subpackage_JobMarket Subpackage_LinearAlgebra Subpackage_levmar Subpackage_mpqc_open Subpackage_vmg Switchable_LogView ThirdParty_MPQC_rebuilt_buildsystem TrajectoryDependenant_MaxOrder TremoloParser_IncreasedPrecision TremoloParser_MultipleTimesteps TremoloParser_setsAtomName Ubuntu_1604_changes stable
Last change on this file since c449d9 was 0b2ce9, checked in by Frederik Heber <heber@…>, 15 years ago

Implemented macros for automatically generating repetitive stuff around Actions.

The idea is that only the following items have to be provided for by the user

  • parameters (i.e. for each the following tupel: type, token and reference)
  • perform...(), ...Undo(), ...

Therefore, we have three new files:

  • Action_impl_header.hpp: Is for the declarations in the header
  • Action_impl_pre.hpp: Is before definition of functions
  • Action_impl_post.hpp: is after definition of functions (cleanup)

Changes:

  • Property mode set to 100644
File size: 7.0 KB
Line 
1/*
2 * Project: MoleCuilder
3 * Description: creates and alters molecular systems
4 * Copyright (C) 2010 University of Bonn. All rights reserved.
5 * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
6 */
7
8/*
9 * ActionSequenzTest.cpp
10 *
11 * Created on: Dec 17, 2009
12 * Author: crueger
13 */
14
15// include config.h
16#ifdef HAVE_CONFIG_H
17#include <config.h>
18#endif
19
20#include <cppunit/CompilerOutputter.h>
21#include <cppunit/extensions/TestFactoryRegistry.h>
22#include <cppunit/ui/text/TestRunner.h>
23
24#include "unittests/ActionSequenceTest.hpp"
25#include "Actions/Action.hpp"
26#include "Actions/ActionSequence.hpp"
27#include "Actions/MakroAction.hpp"
28#include "Actions/ActionHistory.hpp"
29#include "Actions/ActionRegistry.hpp"
30
31#include "DummyUI.hpp"
32
33#ifdef HAVE_TESTRUNNER
34#include "UnitTestMain.hpp"
35#endif /*HAVE_TESTRUNNER*/
36
37/********************************************** Test classes **************************************/
38
39// Registers the fixture into the 'registry'
40CPPUNIT_TEST_SUITE_REGISTRATION( ActionSequenceTest );
41
42/* some neccessary stubs for tests */
43class canUndoActionStub : public Action
44{
45public:
46 canUndoActionStub(): Action("canUndoActionStub",false){}
47 virtual ~canUndoActionStub(){}
48
49 virtual void getParametersfromValueStorage(){
50 }
51
52 virtual Dialog* fillDialog(Dialog *dialog){
53 ASSERT(dialog,"No Dialog given when filling action dialog");
54 return dialog;
55 }
56
57 virtual Action::state_ptr performCall(){
58 return Action::success;
59 }
60 virtual Action::state_ptr performUndo(Action::state_ptr){
61 return Action::success;
62 }
63 virtual Action::state_ptr performRedo(Action::state_ptr){
64 return Action::success;
65 }
66 virtual bool canUndo(){
67 return true;
68 }
69 virtual bool shouldUndo(){
70 return true;
71 }
72};
73
74class cannotUndoActionStub : public Action
75{
76public:
77 cannotUndoActionStub() : Action("cannotUndoActionStub",false){}
78 virtual ~cannotUndoActionStub(){}
79
80 virtual void getParametersfromValueStorage(){
81 }
82
83 virtual Dialog* fillDialog(Dialog *dialog){
84 ASSERT(dialog,"No Dialog given when filling action dialog");
85 return dialog;
86 }
87
88 virtual Action::state_ptr performCall(){
89 return Action::success;
90 }
91 virtual Action::state_ptr performUndo(Action::state_ptr){
92 return Action::success;
93 }
94 virtual Action::state_ptr performRedo(Action::state_ptr){
95 return Action::success;
96 }
97 virtual bool canUndo(){
98 return false;
99 }
100 virtual bool shouldUndo(){
101 return true;
102 }
103};
104
105class wasCalledActionStub : public Action
106{
107public:
108 wasCalledActionStub() :
109 Action("wasCalledActionStub",false),
110 called(false)
111 {}
112 virtual ~wasCalledActionStub(){}
113
114 virtual void getParametersfromValueStorage(){
115 }
116
117 virtual Dialog* fillDialog(Dialog *dialog){
118 return dialog;
119 }
120 virtual Action::state_ptr performCall(){
121 called = true;
122 return Action::success;
123 }
124 virtual Action::state_ptr performUndo(Action::state_ptr){
125 called = false;
126 return Action::success;
127 }
128 virtual Action::state_ptr performRedo(Action::state_ptr){
129 called = true;
130 return Action::success;
131 }
132 virtual bool canUndo(){
133 return true;
134 }
135 virtual bool shouldUndo(){
136 return true;
137 }
138 bool wasCalled(){
139 return called;
140 }
141private:
142 bool called;
143};
144
145void ActionSequenceTest::setUp(){
146 static bool hasDescriptor = false;
147 ActionHistory::init();
148 // TODO: find a way to really reset the factory to a clean state in tear-down
149 if(!hasDescriptor){
150 UIFactory::registerFactory(new DummyUIFactory::description());
151 hasDescriptor = true;
152 }
153 UIFactory::makeUserInterface("Dummy");
154 // create some necessary stubs used in this test
155 positive1 = new canUndoActionStub();
156 positive2 = new canUndoActionStub();
157 negative1 = new cannotUndoActionStub();
158 negative2 = new cannotUndoActionStub();
159
160 shouldCall1 = new wasCalledActionStub();
161 shouldCall2 = new wasCalledActionStub();
162 shouldNotCall1 = new wasCalledActionStub();
163 shouldNotCall2 = new wasCalledActionStub();
164
165}
166
167void ActionSequenceTest::tearDown(){
168 delete positive1;
169 delete positive2;
170 delete negative1;
171 delete negative2;
172
173 delete shouldCall1;
174 delete shouldCall2;
175 delete shouldNotCall1;
176 delete shouldNotCall2;
177
178 ActionHistory::purgeInstance();
179 ActionRegistry::purgeInstance();
180 UIFactory::purgeInstance();
181}
182
183void ActionSequenceTest::canUndoTest(){
184 // first section:
185 {
186 // test some combinations
187 {
188 ActionSequence *sequence = new ActionSequence();
189 sequence->addAction(positive1);
190 sequence->addAction(positive2);
191 CPPUNIT_ASSERT_EQUAL( true, sequence->canUndo() );
192 delete sequence;
193 }
194 {
195 ActionSequence *sequence = new ActionSequence();
196 sequence->addAction(positive1);
197 sequence->addAction(negative2);
198 CPPUNIT_ASSERT_EQUAL( false, sequence->canUndo() );
199 delete sequence;
200 }
201 {
202 ActionSequence *sequence = new ActionSequence();
203 sequence->addAction(negative1);
204 sequence->addAction(positive2);
205 CPPUNIT_ASSERT_EQUAL( false, sequence->canUndo() );
206 delete sequence;
207 }
208 {
209 ActionSequence *sequence = new ActionSequence();
210 sequence->addAction(negative1);
211 sequence->addAction(negative2);
212 CPPUNIT_ASSERT_EQUAL( false, sequence->canUndo() );
213 delete sequence;
214 }
215 }
216
217 // second section:
218 {
219 // empty sequence can be undone
220 ActionSequence *sequence = new ActionSequence();
221 CPPUNIT_ASSERT_EQUAL( true, sequence->canUndo() );
222 // if only a positive action is contained it can be undone
223 sequence->addAction(positive1);
224 CPPUNIT_ASSERT_EQUAL( true, sequence->canUndo() );
225 // the single negative action should block the process
226 sequence->addAction(negative1);
227 CPPUNIT_ASSERT_EQUAL( false, sequence->canUndo() );
228 // after removing the negative action all is well again
229 sequence->removeLastAction();
230 CPPUNIT_ASSERT_EQUAL( true, sequence->canUndo() );
231 delete sequence;
232 }
233}
234
235void ActionSequenceTest::doesCallTest(){
236 ActionSequence *sequence = new ActionSequence();
237 sequence->addAction(shouldCall1);
238 sequence->addAction(shouldCall2);
239 sequence->addAction(shouldNotCall1);
240 sequence->addAction(shouldNotCall2);
241 sequence->removeLastAction();
242 sequence->removeLastAction();
243
244 sequence->callAll();
245
246 CPPUNIT_ASSERT_EQUAL(true,shouldCall1->wasCalled());
247 CPPUNIT_ASSERT_EQUAL(true,shouldCall2->wasCalled());
248 CPPUNIT_ASSERT_EQUAL(false,shouldNotCall1->wasCalled());
249 CPPUNIT_ASSERT_EQUAL(false,shouldNotCall2->wasCalled());
250
251 delete sequence;
252}
253
254void ActionSequenceTest::doesUndoTest(){
255 ActionSequence *sequence = new ActionSequence();
256 wasCalledActionStub *wasCalled1 = new wasCalledActionStub();
257 wasCalledActionStub *wasCalled2 = new wasCalledActionStub();
258 sequence->addAction(wasCalled1);
259 sequence->addAction(wasCalled2);
260
261 MakroAction act("Test MakroAction",sequence,false);
262
263 act.call();
264
265 CPPUNIT_ASSERT_EQUAL(true,wasCalled1->wasCalled());
266 CPPUNIT_ASSERT_EQUAL(true,wasCalled2->wasCalled());
267
268 ActionHistory::getInstance().undoLast();
269
270 CPPUNIT_ASSERT_EQUAL(false,wasCalled1->wasCalled());
271 CPPUNIT_ASSERT_EQUAL(false,wasCalled2->wasCalled());
272
273}
274
275
Note: See TracBrowser for help on using the repository browser.