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