1 | #include <gtest/gtest.h> |
---|
2 | #include "core/command/ConsoleCommandIncludes.h" |
---|
3 | #include "core/command/CommandExecutor.h" |
---|
4 | #include "core/object/Destroyable.h" |
---|
5 | #include "core/module/ModuleInstance.h" |
---|
6 | |
---|
7 | namespace orxonox |
---|
8 | { |
---|
9 | namespace |
---|
10 | { |
---|
11 | int functionNr = 0; |
---|
12 | int valueA = 0; |
---|
13 | int valueB = 0; |
---|
14 | int valueC = 0; |
---|
15 | |
---|
16 | void testfunction1(int a); |
---|
17 | void testfunction2(int a, int b); |
---|
18 | void testfunction3(int a, int b); |
---|
19 | void testfunction4(int a, int b, int c); |
---|
20 | |
---|
21 | class Testclass : public Destroyable |
---|
22 | { |
---|
23 | public: |
---|
24 | void testfunction5(int a); |
---|
25 | void testfunction6(int a); |
---|
26 | |
---|
27 | Testclass* other_; |
---|
28 | int b_; |
---|
29 | }; |
---|
30 | |
---|
31 | SetConsoleCommand("test", &testfunction1); |
---|
32 | |
---|
33 | Testclass* class1 = new Testclass(); |
---|
34 | Testclass* class2 = new Testclass(); |
---|
35 | |
---|
36 | void testfunction1(int a) |
---|
37 | { |
---|
38 | functionNr = 1; |
---|
39 | valueA = a; |
---|
40 | valueB = 0; |
---|
41 | valueC = 0; |
---|
42 | |
---|
43 | if (a == 1) |
---|
44 | ModifyConsoleCommand("test").pushFunction().defaultValue(1, 10).setFunction(&testfunction2); |
---|
45 | else if (a == -1) |
---|
46 | ModifyConsoleCommand("test").popFunction(); |
---|
47 | } |
---|
48 | |
---|
49 | void testfunction2(int a, int b) |
---|
50 | { |
---|
51 | functionNr = 2; |
---|
52 | valueA = a; |
---|
53 | valueB = b; |
---|
54 | valueC = 0; |
---|
55 | |
---|
56 | if (a == 1) |
---|
57 | ModifyConsoleCommand("test").pushFunction().defaultValue(1, 20).setFunction(&testfunction3); |
---|
58 | else if (a == -1) |
---|
59 | ModifyConsoleCommand("test").popFunction(); |
---|
60 | } |
---|
61 | |
---|
62 | void testfunction3(int a, int b) |
---|
63 | { |
---|
64 | functionNr = 3; |
---|
65 | valueA = a; |
---|
66 | valueB = b; |
---|
67 | valueC = 0; |
---|
68 | |
---|
69 | if (a == 1) |
---|
70 | ModifyConsoleCommand("test").pushFunction().defaultValue(1, 30).defaultValue(2, 40).setFunction(&testfunction4); |
---|
71 | else if (a == -1) |
---|
72 | ModifyConsoleCommand("test").popFunction(); |
---|
73 | } |
---|
74 | |
---|
75 | void testfunction4(int a, int b, int c) |
---|
76 | { |
---|
77 | functionNr = 4; |
---|
78 | valueA = a; |
---|
79 | valueB = b; |
---|
80 | valueC = c; |
---|
81 | |
---|
82 | if (a == 1) |
---|
83 | ModifyConsoleCommand("test").pushFunction().setFunction(&Testclass::testfunction5).setObject(class1); |
---|
84 | else if (a == -1) |
---|
85 | ModifyConsoleCommand("test").popFunction(); |
---|
86 | } |
---|
87 | |
---|
88 | void Testclass::testfunction5(int a) |
---|
89 | { |
---|
90 | class1->b_ = 11; |
---|
91 | class2->b_ = 22; |
---|
92 | |
---|
93 | class1->other_ = class2; |
---|
94 | class2->other_ = class1; |
---|
95 | |
---|
96 | functionNr = 5; |
---|
97 | valueA = a; |
---|
98 | valueB = b_; |
---|
99 | valueC = 0; |
---|
100 | |
---|
101 | if (a == 2) |
---|
102 | ModifyConsoleCommand("test").pushObject(this->other_); |
---|
103 | else if (a == -2) |
---|
104 | ModifyConsoleCommand("test").popObject(); |
---|
105 | else if (a == 1) |
---|
106 | ModifyConsoleCommand("test").pushFunction(&Testclass::testfunction6, this); |
---|
107 | else if (a == -1) |
---|
108 | ModifyConsoleCommand("test").popFunction(); |
---|
109 | } |
---|
110 | |
---|
111 | void Testclass::testfunction6(int a) |
---|
112 | { |
---|
113 | class1->b_ = 11; |
---|
114 | class2->b_ = 22; |
---|
115 | |
---|
116 | class1->other_ = class2; |
---|
117 | class2->other_ = class1; |
---|
118 | |
---|
119 | functionNr = 6; |
---|
120 | valueA = a; |
---|
121 | valueB = b_; |
---|
122 | valueC = 0; |
---|
123 | |
---|
124 | if (a == 2) |
---|
125 | ModifyConsoleCommand("test").pushObject(this->other_); |
---|
126 | else if (a == -2) |
---|
127 | ModifyConsoleCommand("test").popObject(); |
---|
128 | else if (a == 1) |
---|
129 | ModifyConsoleCommand("test").setFunction(FunctorPtr(0)); |
---|
130 | else if (a == -1) |
---|
131 | ModifyConsoleCommand("test").popFunction(); |
---|
132 | } |
---|
133 | } |
---|
134 | |
---|
135 | void test(int function, int b, int c) |
---|
136 | { |
---|
137 | EXPECT_EQ(function, functionNr); |
---|
138 | EXPECT_EQ(b, valueB); |
---|
139 | EXPECT_EQ(c, valueC); |
---|
140 | } |
---|
141 | |
---|
142 | TEST(CommandTest, ModuleTest) |
---|
143 | { |
---|
144 | ModuleInstance::getCurrentModuleInstance()->loadAllStaticallyInitializedInstances(); |
---|
145 | |
---|
146 | test(0, 0, 0); |
---|
147 | CommandExecutor::execute("test 0", false); |
---|
148 | test(1, 0, 0); |
---|
149 | CommandExecutor::execute("test 1", false); |
---|
150 | test(1, 0, 0); |
---|
151 | CommandExecutor::execute("test 0", false); |
---|
152 | test(2, 10, 0); |
---|
153 | CommandExecutor::execute("test 0 123", false); |
---|
154 | test(2, 123, 0); |
---|
155 | CommandExecutor::execute("test 1 1234", false); |
---|
156 | test(2, 1234, 0); |
---|
157 | CommandExecutor::execute("test 1", false); |
---|
158 | test(3, 20, 0); |
---|
159 | CommandExecutor::execute("test 0", false); |
---|
160 | test(4, 30, 40); |
---|
161 | CommandExecutor::execute("test 1 100 200", false); |
---|
162 | test(4, 100, 200); |
---|
163 | CommandExecutor::execute("test 0", false); |
---|
164 | test(5, 11, 0); |
---|
165 | CommandExecutor::execute("test 1", false); |
---|
166 | test(5, 11, 0); |
---|
167 | CommandExecutor::execute("test 2", false); |
---|
168 | test(6, 11, 0); |
---|
169 | CommandExecutor::execute("test 0", false); |
---|
170 | test(6, 22, 0); |
---|
171 | CommandExecutor::execute("test -1", false); |
---|
172 | test(6, 22, 0); |
---|
173 | CommandExecutor::execute("test -1", false); |
---|
174 | test(5, 11, 0); |
---|
175 | CommandExecutor::execute("test 0", false); |
---|
176 | test(4, 30, 40); |
---|
177 | } |
---|
178 | } |
---|