1 | /*
|
---|
2 | * manipulateAtomsTest.cpp
|
---|
3 | *
|
---|
4 | * Created on: Feb 18, 2010
|
---|
5 | * Author: crueger
|
---|
6 | */
|
---|
7 |
|
---|
8 | // include config.h
|
---|
9 | #ifdef HAVE_CONFIG_H
|
---|
10 | #include <config.h>
|
---|
11 | #endif
|
---|
12 |
|
---|
13 | #include "manipulateAtomsTest.hpp"
|
---|
14 |
|
---|
15 | #include <cppunit/CompilerOutputter.h>
|
---|
16 | #include <cppunit/extensions/TestFactoryRegistry.h>
|
---|
17 | #include <cppunit/ui/text/TestRunner.h>
|
---|
18 | #include <iostream>
|
---|
19 | #include <boost/bind.hpp>
|
---|
20 |
|
---|
21 | #include "Descriptors/AtomDescriptor.hpp"
|
---|
22 | #include "Descriptors/AtomIdDescriptor.hpp"
|
---|
23 | #include "Actions/ManipulateAtomsProcess.hpp"
|
---|
24 | #include "Actions/ActionRegistry.hpp"
|
---|
25 | #include "Actions/ActionHistory.hpp"
|
---|
26 |
|
---|
27 | #include "World.hpp"
|
---|
28 | #include "atom.hpp"
|
---|
29 |
|
---|
30 | #include "DummyUI.hpp"
|
---|
31 |
|
---|
32 | #ifdef HAVE_TESTRUNNER
|
---|
33 | #include "UnitTestMain.hpp"
|
---|
34 | #endif /*HAVE_TESTRUNNER*/
|
---|
35 |
|
---|
36 | // Registers the fixture into the 'registry'
|
---|
37 | CPPUNIT_TEST_SUITE_REGISTRATION( manipulateAtomsTest );
|
---|
38 |
|
---|
39 | // some stubs
|
---|
40 | class AtomStub : public atom {
|
---|
41 | public:
|
---|
42 | AtomStub(int _id) :
|
---|
43 | atom(),
|
---|
44 | manipulated(false),
|
---|
45 | id(_id)
|
---|
46 | {}
|
---|
47 |
|
---|
48 | virtual atomId_t getId(){
|
---|
49 | return id;
|
---|
50 | }
|
---|
51 |
|
---|
52 | virtual void doSomething(){
|
---|
53 | manipulated = true;
|
---|
54 | }
|
---|
55 |
|
---|
56 | bool manipulated;
|
---|
57 | private:
|
---|
58 | atomId_t id;
|
---|
59 | };
|
---|
60 |
|
---|
61 | class countObserver : public Observer{
|
---|
62 | public:
|
---|
63 | countObserver() :
|
---|
64 | Observer("countObserver"),
|
---|
65 | count(0)
|
---|
66 | {}
|
---|
67 | virtual ~countObserver(){}
|
---|
68 |
|
---|
69 | void update(Observable *){
|
---|
70 | count++;
|
---|
71 | }
|
---|
72 |
|
---|
73 | void subjectKilled(Observable *)
|
---|
74 | {}
|
---|
75 |
|
---|
76 | int count;
|
---|
77 | };
|
---|
78 |
|
---|
79 | // set up and tear down
|
---|
80 | void manipulateAtomsTest::setUp(){
|
---|
81 | static bool hasDescriptor = false;
|
---|
82 | ActionHistory::init();
|
---|
83 | World::getInstance();
|
---|
84 | // TODO: find a way to really reset the factory to a clean state in tear-down
|
---|
85 | if(!hasDescriptor){
|
---|
86 | UIFactory::registerFactory(new DummyUIFactory::description());
|
---|
87 | hasDescriptor = true;
|
---|
88 | }
|
---|
89 | UIFactory::makeUserInterface("Dummy");
|
---|
90 | for(int i=0;i<ATOM_COUNT;++i){
|
---|
91 | atoms[i]= new AtomStub(i);
|
---|
92 | World::getInstance().registerAtom(atoms[i]);
|
---|
93 | }
|
---|
94 | }
|
---|
95 | void manipulateAtomsTest::tearDown(){
|
---|
96 | World::purgeInstance();
|
---|
97 | ActionRegistry::purgeInstance();
|
---|
98 | ActionHistory::purgeInstance();
|
---|
99 | UIFactory::purgeInstance();
|
---|
100 | }
|
---|
101 |
|
---|
102 | static void operation(atom* _atom){
|
---|
103 | AtomStub *atom = dynamic_cast<AtomStub*>(_atom);
|
---|
104 | CPPUNIT_ASSERT(atom);
|
---|
105 | atom->doSomething();
|
---|
106 | }
|
---|
107 |
|
---|
108 |
|
---|
109 | void manipulateAtomsTest::testManipulateSimple(){
|
---|
110 | ManipulateAtomsProcess *proc = World::getInstance().manipulateAtoms(boost::bind(operation,_1),"FOO",AllAtoms());
|
---|
111 | proc->call();
|
---|
112 | std::vector<atom*> allAtoms = World::getInstance().getAllAtoms(AllAtoms());
|
---|
113 | std::vector<atom*>::iterator iter;
|
---|
114 | for(iter=allAtoms.begin();iter!=allAtoms.end();++iter){
|
---|
115 | AtomStub *atom;
|
---|
116 | atom = dynamic_cast<AtomStub*>(*iter);
|
---|
117 | CPPUNIT_ASSERT(atom);
|
---|
118 | CPPUNIT_ASSERT(atom->manipulated);
|
---|
119 | }
|
---|
120 | }
|
---|
121 |
|
---|
122 | void manipulateAtomsTest::testManipulateExcluded(){
|
---|
123 |
|
---|
124 | ManipulateAtomsProcess *proc = World::getInstance().manipulateAtoms(boost::bind(operation,_1),"FOO",AllAtoms() && !AtomById(ATOM_COUNT/2));
|
---|
125 | proc->call();
|
---|
126 | std::vector<atom*> allAtoms = World::getInstance().getAllAtoms(AllAtoms());
|
---|
127 | std::vector<atom*>::iterator iter;
|
---|
128 | for(iter=allAtoms.begin();iter!=allAtoms.end();++iter){
|
---|
129 | AtomStub *atom;
|
---|
130 | atom = dynamic_cast<AtomStub*>(*iter);
|
---|
131 | CPPUNIT_ASSERT(atom);
|
---|
132 | if(atom->getId()!=(int)ATOM_COUNT/2)
|
---|
133 | CPPUNIT_ASSERT(atom->manipulated);
|
---|
134 | else
|
---|
135 | CPPUNIT_ASSERT(!atom->manipulated);
|
---|
136 | }
|
---|
137 | }
|
---|
138 |
|
---|
139 | void manipulateAtomsTest::testObserver(){
|
---|
140 | countObserver *obs = new countObserver();
|
---|
141 | World::getInstance().signOn(obs);
|
---|
142 | ManipulateAtomsProcess *proc = World::getInstance().manipulateAtoms(boost::bind(operation,_1),"FOO",AllAtoms());
|
---|
143 | proc->call();
|
---|
144 |
|
---|
145 | CPPUNIT_ASSERT_EQUAL(1,obs->count);
|
---|
146 | World::getInstance().signOff(obs);
|
---|
147 | delete obs;
|
---|
148 | }
|
---|