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 | |
---|
9 | namespace 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(nullptr)); |
---|
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() override |
---|
141 | { |
---|
142 | new IdentifierManager(); |
---|
143 | new ConsoleCommandManager(); |
---|
144 | ModuleInstance::getCurrentModuleInstance()->loadAllStaticallyInitializedInstances(StaticInitialization::CONSOLE_COMMAND); |
---|
145 | Context::setRootContext(new Context(nullptr)); |
---|
146 | Identifier::initConfigValues_s = false; // TODO: hack! |
---|
147 | IdentifierManager::getInstance().createClassHierarchy(); |
---|
148 | } |
---|
149 | |
---|
150 | virtual void TearDown() override |
---|
151 | { |
---|
152 | IdentifierManager::getInstance().destroyClassHierarchy(); |
---|
153 | Context::destroyRootContext(); |
---|
154 | ModuleInstance::getCurrentModuleInstance()->unloadAllStaticallyInitializedInstances(StaticInitialization::CONSOLE_COMMAND); |
---|
155 | delete &ConsoleCommandManager::getInstance(); |
---|
156 | delete &IdentifierManager::getInstance(); |
---|
157 | } |
---|
158 | }; |
---|
159 | } |
---|
160 | |
---|
161 | void test(int function, int b, int c) |
---|
162 | { |
---|
163 | EXPECT_EQ(function, functionNr); |
---|
164 | EXPECT_EQ(b, valueB); |
---|
165 | EXPECT_EQ(c, valueC); |
---|
166 | } |
---|
167 | |
---|
168 | TEST_F(CommandTest, ModuleTest) |
---|
169 | { |
---|
170 | test(0, 0, 0); |
---|
171 | CommandExecutor::execute("test 0", false); |
---|
172 | test(1, 0, 0); |
---|
173 | CommandExecutor::execute("test 1", false); |
---|
174 | test(1, 0, 0); |
---|
175 | CommandExecutor::execute("test 0", false); |
---|
176 | test(2, 10, 0); |
---|
177 | CommandExecutor::execute("test 0 123", false); |
---|
178 | test(2, 123, 0); |
---|
179 | CommandExecutor::execute("test 1 1234", false); |
---|
180 | test(2, 1234, 0); |
---|
181 | CommandExecutor::execute("test 1", false); |
---|
182 | test(3, 20, 0); |
---|
183 | CommandExecutor::execute("test 0", false); |
---|
184 | test(4, 30, 40); |
---|
185 | CommandExecutor::execute("test 1 100 200", false); |
---|
186 | test(4, 100, 200); |
---|
187 | CommandExecutor::execute("test 0", false); |
---|
188 | test(5, 11, 0); |
---|
189 | CommandExecutor::execute("test 1", false); |
---|
190 | test(5, 11, 0); |
---|
191 | CommandExecutor::execute("test 2", false); |
---|
192 | test(6, 11, 0); |
---|
193 | CommandExecutor::execute("test 0", false); |
---|
194 | test(6, 22, 0); |
---|
195 | CommandExecutor::execute("test -1", false); |
---|
196 | test(6, 22, 0); |
---|
197 | CommandExecutor::execute("test -1", false); |
---|
198 | test(5, 11, 0); |
---|
199 | CommandExecutor::execute("test 0", false); |
---|
200 | test(4, 30, 40); |
---|
201 | } |
---|
202 | } |
---|