[9605] | 1 | #include <gtest/gtest.h> |
---|
| 2 | #include <gmock/gmock.h> |
---|
| 3 | |
---|
| 4 | #include "core/object/Iterator.h" |
---|
| 5 | #include "core/class/OrxonoxClass.h" |
---|
| 6 | #include "core/class/OrxonoxInterface.h" |
---|
| 7 | #include "core/CoreIncludes.h" |
---|
[10624] | 8 | #include "core/module/ModuleInstance.h" |
---|
[9605] | 9 | |
---|
| 10 | namespace orxonox |
---|
| 11 | { |
---|
| 12 | namespace |
---|
| 13 | { |
---|
[9607] | 14 | class TestInterface : virtual public OrxonoxInterface |
---|
[9605] | 15 | { |
---|
| 16 | public: |
---|
[11071] | 17 | TestInterface() { RegisterObject(TestInterface); } |
---|
[9605] | 18 | }; |
---|
| 19 | |
---|
| 20 | class TestClass : public OrxonoxClass, public TestInterface |
---|
| 21 | { |
---|
| 22 | public: |
---|
[9659] | 23 | TestClass() { RegisterObject(TestClass); } |
---|
[9605] | 24 | MOCK_METHOD0(test, void()); |
---|
| 25 | }; |
---|
[9649] | 26 | |
---|
[10624] | 27 | RegisterClassNoArgs(TestInterface); |
---|
| 28 | RegisterClassNoArgs(TestClass); |
---|
| 29 | |
---|
[9649] | 30 | // Fixture |
---|
| 31 | class IteratorTest : public ::testing::Test |
---|
| 32 | { |
---|
| 33 | public: |
---|
[11071] | 34 | virtual void SetUp() override |
---|
[9649] | 35 | { |
---|
[10624] | 36 | new IdentifierManager(); |
---|
| 37 | ModuleInstance::getCurrentModuleInstance()->loadAllStaticallyInitializedInstances(StaticInitialization::IDENTIFIER); |
---|
[11071] | 38 | Context::setRootContext(new Context(nullptr)); |
---|
[9649] | 39 | } |
---|
| 40 | |
---|
[11071] | 41 | virtual void TearDown() override |
---|
[9649] | 42 | { |
---|
[10624] | 43 | Context::destroyRootContext(); |
---|
| 44 | ModuleInstance::getCurrentModuleInstance()->unloadAllStaticallyInitializedInstances(StaticInitialization::IDENTIFIER); |
---|
| 45 | delete &IdentifierManager::getInstance(); |
---|
[9649] | 46 | } |
---|
| 47 | }; |
---|
[9605] | 48 | } |
---|
| 49 | |
---|
[9649] | 50 | TEST_F(IteratorTest, CanCreateIterator) |
---|
[9605] | 51 | { |
---|
| 52 | Iterator<TestInterface> it; |
---|
| 53 | } |
---|
| 54 | |
---|
[9649] | 55 | TEST_F(IteratorTest, CanAssignIterator) |
---|
[9605] | 56 | { |
---|
[11071] | 57 | ObjectList<TestInterface> list; |
---|
| 58 | Iterator<TestInterface> it = list.begin(); |
---|
[9605] | 59 | } |
---|
| 60 | |
---|
[9649] | 61 | TEST_F(IteratorTest, CanIterateOverEmptyList) |
---|
[9605] | 62 | { |
---|
| 63 | size_t i = 0; |
---|
[11071] | 64 | ObjectList<TestInterface> list; |
---|
| 65 | for (Iterator<TestInterface> it = list.begin(); it != list.end(); ++it) |
---|
[9605] | 66 | ++i; |
---|
| 67 | EXPECT_EQ(0u, i); |
---|
| 68 | } |
---|
| 69 | |
---|
[9649] | 70 | TEST_F(IteratorTest, CanCallObjects) |
---|
[9605] | 71 | { |
---|
| 72 | TestClass test1; |
---|
| 73 | TestClass test2; |
---|
| 74 | TestClass test3; |
---|
| 75 | |
---|
| 76 | EXPECT_CALL(test1, test()); |
---|
| 77 | EXPECT_CALL(test2, test()); |
---|
| 78 | EXPECT_CALL(test3, test()); |
---|
| 79 | |
---|
| 80 | // iterate over interfaces but use a TestClass iterator - now we can call TestClass::test() |
---|
[11071] | 81 | ObjectList<TestInterface> list; |
---|
| 82 | for (Iterator<TestClass> it = list.begin(); it != list.end(); ++it) |
---|
[9605] | 83 | it->test(); |
---|
| 84 | } |
---|
[11071] | 85 | |
---|
| 86 | TEST_F(IteratorTest, CanIterateOverInterfaceListWithInterfaceIterator) |
---|
| 87 | { |
---|
| 88 | TestClass testClass; |
---|
| 89 | TestInterface testInterface; |
---|
| 90 | |
---|
| 91 | size_t i = 0; |
---|
| 92 | ObjectList<TestInterface> list; |
---|
| 93 | for (Iterator<TestInterface> it = list.begin(); it != list.end(); ++it) |
---|
| 94 | { |
---|
| 95 | ++i; |
---|
| 96 | if (i == 1u) EXPECT_EQ(&testClass, *it); |
---|
| 97 | if (i == 2u) EXPECT_EQ(&testInterface, *it); |
---|
| 98 | } |
---|
| 99 | EXPECT_EQ(2u, i); |
---|
| 100 | } |
---|
| 101 | |
---|
| 102 | TEST_F(IteratorTest, CanIterateOverClassListWithClassIterator) |
---|
| 103 | { |
---|
| 104 | TestClass testClass; |
---|
| 105 | TestInterface testInterface; |
---|
| 106 | |
---|
| 107 | size_t i = 0; |
---|
| 108 | ObjectList<TestClass> list; |
---|
| 109 | for (Iterator<TestClass> it = list.begin(); it != list.end(); ++it) |
---|
| 110 | { |
---|
| 111 | ++i; |
---|
| 112 | if (i == 1u) EXPECT_EQ(&testClass, *it); |
---|
| 113 | } |
---|
| 114 | EXPECT_EQ(1u, i); |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | TEST_F(IteratorTest, CanIterateOverInterfaceListWithClassIterator) |
---|
| 118 | { |
---|
| 119 | TestClass testClass; |
---|
| 120 | TestInterface testInterface; |
---|
| 121 | |
---|
| 122 | size_t i = 0; |
---|
| 123 | ObjectList<TestInterface> list; |
---|
| 124 | for (Iterator<TestClass> it = list.begin(); it != list.end(); ++it) |
---|
| 125 | { |
---|
| 126 | ++i; |
---|
| 127 | if (i == 1u) EXPECT_EQ(&testClass, *it); |
---|
| 128 | if (i == 2u) EXPECT_EQ(nullptr, *it); |
---|
| 129 | } |
---|
| 130 | EXPECT_EQ(2u, i); |
---|
| 131 | } |
---|
| 132 | |
---|
| 133 | TEST_F(IteratorTest, CanIterateOverClassListWithInterfaceIterator) |
---|
| 134 | { |
---|
| 135 | TestClass testClass; |
---|
| 136 | TestInterface testInterface; |
---|
| 137 | |
---|
| 138 | size_t i = 0; |
---|
| 139 | ObjectList<TestClass> list; |
---|
| 140 | for (Iterator<TestInterface> it = list.begin(); it != list.end(); ++it) |
---|
| 141 | { |
---|
| 142 | ++i; |
---|
| 143 | if (i == 1u) EXPECT_EQ(&testClass, *it); |
---|
| 144 | } |
---|
| 145 | EXPECT_EQ(1u, i); |
---|
| 146 | } |
---|
[9605] | 147 | } |
---|