Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/core7/test/core/command/CommandTest.cc @ 10403

Last change on this file since 10403 was 10403, checked in by landauf, 10 years ago

added function to destroy the class hierarchy (i.e. reset all information about parents and children in Identifiers).
tests now use a fixture to create and destroy class hierarchy. this makes them independent of the order of execution (and also fixes the three *_NoFixture tests)

  • Property svn:eol-style set to native
File size: 5.9 KB
Line 
1#include <gtest/gtest.h>
2#include "core/class/Identifier.h"
3#include "core/class/IdentifierManager.h"
4#include "core/command/ConsoleCommandIncludes.h"
5#include "core/command/CommandExecutor.h"
6#include "core/object/Destroyable.h"
7#include "core/module/ModuleInstance.h"
8
9namespace orxonox
10{
11    namespace
12    {
13        int functionNr = 0;
14        int valueA = 0;
15        int valueB = 0;
16        int valueC = 0;
17
18        void testfunction1(int a);
19        void testfunction2(int a, int b);
20        void testfunction3(int a, int b);
21        void testfunction4(int a, int b, int c);
22
23        class Testclass : public Destroyable
24        {
25            public:
26                void testfunction5(int a);
27                void testfunction6(int a);
28
29                Testclass* other_;
30                int b_;
31        };
32
33        SetConsoleCommand("test", &testfunction1);
34
35        Testclass* class1 = new Testclass();
36        Testclass* class2 = new Testclass();
37
38        void testfunction1(int a)
39        {
40            functionNr = 1;
41            valueA = a;
42            valueB = 0;
43            valueC = 0;
44
45            if (a == 1)
46                ModifyConsoleCommand("test").pushFunction().defaultValue(1, 10).setFunction(&testfunction2);
47            else if (a == -1)
48                ModifyConsoleCommand("test").popFunction();
49        }
50
51        void testfunction2(int a, int b)
52        {
53            functionNr = 2;
54            valueA = a;
55            valueB = b;
56            valueC = 0;
57
58            if (a == 1)
59                ModifyConsoleCommand("test").pushFunction().defaultValue(1, 20).setFunction(&testfunction3);
60            else if (a == -1)
61                ModifyConsoleCommand("test").popFunction();
62        }
63
64        void testfunction3(int a, int b)
65        {
66            functionNr = 3;
67            valueA = a;
68            valueB = b;
69            valueC = 0;
70
71            if (a == 1)
72                ModifyConsoleCommand("test").pushFunction().defaultValue(1, 30).defaultValue(2, 40).setFunction(&testfunction4);
73            else if (a == -1)
74                ModifyConsoleCommand("test").popFunction();
75        }
76
77        void testfunction4(int a, int b, int c)
78        {
79            functionNr = 4;
80            valueA = a;
81            valueB = b;
82            valueC = c;
83
84            if (a == 1)
85                ModifyConsoleCommand("test").pushFunction().setFunction(&Testclass::testfunction5).setObject(class1);
86            else if (a == -1)
87                ModifyConsoleCommand("test").popFunction();
88        }
89
90        void Testclass::testfunction5(int a)
91        {
92            class1->b_ = 11;
93            class2->b_ = 22;
94
95            class1->other_ = class2;
96            class2->other_ = class1;
97
98            functionNr = 5;
99            valueA = a;
100            valueB = b_;
101            valueC = 0;
102
103            if (a == 2)
104                ModifyConsoleCommand("test").pushObject(this->other_);
105            else if (a == -2)
106                ModifyConsoleCommand("test").popObject();
107            else if (a == 1)
108                ModifyConsoleCommand("test").pushFunction(&Testclass::testfunction6, this);
109            else if (a == -1)
110                ModifyConsoleCommand("test").popFunction();
111        }
112
113        void Testclass::testfunction6(int a)
114        {
115            class1->b_ = 11;
116            class2->b_ = 22;
117
118            class1->other_ = class2;
119            class2->other_ = class1;
120
121            functionNr = 6;
122            valueA = a;
123            valueB = b_;
124            valueC = 0;
125
126            if (a == 2)
127                ModifyConsoleCommand("test").pushObject(this->other_);
128            else if (a == -2)
129                ModifyConsoleCommand("test").popObject();
130            else if (a == 1)
131                ModifyConsoleCommand("test").setFunction(FunctorPtr(0));
132            else if (a == -1)
133                ModifyConsoleCommand("test").popFunction();
134        }
135
136        // Fixture
137        class CommandTest : public ::testing::Test
138        {
139            public:
140                virtual void SetUp()
141                {
142                    ModuleInstance::getCurrentModuleInstance()->loadAllStaticallyInitializedInstances();
143                    ModuleInstance::setCurrentModuleInstance(new ModuleInstance()); // overwrite ModuleInstance because the old one is now loaded and shouln't be used anymore. TODO: better solution?
144                    Identifier::initConfigValues_s = false; // TODO: hack!
145                    IdentifierManager::getInstance().createClassHierarchy();
146                }
147
148                virtual void TearDown()
149                {
150                    IdentifierManager::getInstance().destroyClassHierarchy();
151                }
152        };
153    }
154
155    void test(int function, int b, int c)
156    {
157        EXPECT_EQ(function, functionNr);
158        EXPECT_EQ(b, valueB);
159        EXPECT_EQ(c, valueC);
160    }
161
162    TEST_F(CommandTest, ModuleTest)
163    {
164        test(0, 0, 0);
165        CommandExecutor::execute("test 0", false);
166        test(1, 0, 0);
167        CommandExecutor::execute("test 1", false);
168        test(1, 0, 0);
169        CommandExecutor::execute("test 0", false);
170        test(2, 10, 0);
171        CommandExecutor::execute("test 0 123", false);
172        test(2, 123, 0);
173        CommandExecutor::execute("test 1 1234", false);
174        test(2, 1234, 0);
175        CommandExecutor::execute("test 1", false);
176        test(3, 20, 0);
177        CommandExecutor::execute("test 0", false);
178        test(4, 30, 40);
179        CommandExecutor::execute("test 1 100 200", false);
180        test(4, 100, 200);
181        CommandExecutor::execute("test 0", false);
182        test(5, 11, 0);
183        CommandExecutor::execute("test 1", false);
184        test(5, 11, 0);
185        CommandExecutor::execute("test 2", false);
186        test(6, 11, 0);
187        CommandExecutor::execute("test 0", false);
188        test(6, 22, 0);
189        CommandExecutor::execute("test -1", false);
190        test(6, 22, 0);
191        CommandExecutor::execute("test -1", false);
192        test(5, 11, 0);
193        CommandExecutor::execute("test 0", false);
194        test(4, 30, 40);
195    }
196}
Note: See TracBrowser for help on using the repository browser.