| 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" | 
|---|
| 8 | #include "core/module/ModuleInstance.h" | 
|---|
| 9 |  | 
|---|
| 10 | namespace orxonox | 
|---|
| 11 | { | 
|---|
| 12 | namespace | 
|---|
| 13 | { | 
|---|
| 14 | class TestInterface : virtual public OrxonoxInterface | 
|---|
| 15 | { | 
|---|
| 16 | public: | 
|---|
| 17 | TestInterface() { RegisterObject(TestInterface); } | 
|---|
| 18 | }; | 
|---|
| 19 |  | 
|---|
| 20 | class TestClass : public OrxonoxClass, public TestInterface | 
|---|
| 21 | { | 
|---|
| 22 | public: | 
|---|
| 23 | TestClass() { RegisterObject(TestClass); } | 
|---|
| 24 | MOCK_METHOD0(test, void()); | 
|---|
| 25 | }; | 
|---|
| 26 |  | 
|---|
| 27 | RegisterClassNoArgs(TestInterface); | 
|---|
| 28 | RegisterClassNoArgs(TestClass); | 
|---|
| 29 |  | 
|---|
| 30 | // Fixture | 
|---|
| 31 | class IteratorTest : public ::testing::Test | 
|---|
| 32 | { | 
|---|
| 33 | public: | 
|---|
| 34 | virtual void SetUp() | 
|---|
| 35 | { | 
|---|
| 36 | new IdentifierManager(); | 
|---|
| 37 | ModuleInstance::getCurrentModuleInstance()->loadAllStaticallyInitializedInstances(StaticInitialization::IDENTIFIER); | 
|---|
| 38 | Context::setRootContext(new Context(NULL)); | 
|---|
| 39 | } | 
|---|
| 40 |  | 
|---|
| 41 | virtual void TearDown() | 
|---|
| 42 | { | 
|---|
| 43 | Context::destroyRootContext(); | 
|---|
| 44 | ModuleInstance::getCurrentModuleInstance()->unloadAllStaticallyInitializedInstances(StaticInitialization::IDENTIFIER); | 
|---|
| 45 | delete &IdentifierManager::getInstance(); | 
|---|
| 46 | } | 
|---|
| 47 | }; | 
|---|
| 48 | } | 
|---|
| 49 |  | 
|---|
| 50 | TEST_F(IteratorTest, CanCreateIterator) | 
|---|
| 51 | { | 
|---|
| 52 | Iterator<TestInterface> it; | 
|---|
| 53 | } | 
|---|
| 54 |  | 
|---|
| 55 | TEST_F(IteratorTest, CanAssignIterator) | 
|---|
| 56 | { | 
|---|
| 57 | Iterator<TestInterface> it = ObjectList<TestInterface>::begin(); | 
|---|
| 58 | } | 
|---|
| 59 |  | 
|---|
| 60 | TEST_F(IteratorTest, CanIterateOverEmptyList) | 
|---|
| 61 | { | 
|---|
| 62 | size_t i = 0; | 
|---|
| 63 | for (Iterator<TestInterface> it = ObjectList<TestInterface>::begin(); it != ObjectList<TestInterface>::end(); ++it) | 
|---|
| 64 | ++i; | 
|---|
| 65 | EXPECT_EQ(0u, i); | 
|---|
| 66 | } | 
|---|
| 67 |  | 
|---|
| 68 | TEST_F(IteratorTest, CanCallObjects) | 
|---|
| 69 | { | 
|---|
| 70 | TestClass test1; | 
|---|
| 71 | TestClass test2; | 
|---|
| 72 | TestClass test3; | 
|---|
| 73 |  | 
|---|
| 74 | EXPECT_CALL(test1, test()); | 
|---|
| 75 | EXPECT_CALL(test2, test()); | 
|---|
| 76 | EXPECT_CALL(test3, test()); | 
|---|
| 77 |  | 
|---|
| 78 | // iterate over interfaces but use a TestClass iterator - now we can call TestClass::test() | 
|---|
| 79 | for (Iterator<TestClass> it = ObjectList<TestInterface>::begin(); it != ObjectList<TestInterface>::end(); ++it) | 
|---|
| 80 | it->test(); | 
|---|
| 81 | } | 
|---|
| 82 | } | 
|---|