| 1 | #include <gtest/gtest.h> | 
|---|
| 2 | #include <tinyxml/tinyxml.h> | 
|---|
| 3 |  | 
|---|
| 4 | #include "core/CoreIncludes.h" | 
|---|
| 5 | #include "core/BaseObject.h" | 
|---|
| 6 | #include "core/class/Super.h" | 
|---|
| 7 | #include "core/module/ModuleInstance.h" | 
|---|
| 8 |  | 
|---|
| 9 | namespace orxonox | 
|---|
| 10 | { | 
|---|
| 11 | namespace | 
|---|
| 12 | { | 
|---|
| 13 | class TestClass : public BaseObject | 
|---|
| 14 | { | 
|---|
| 15 | public: | 
|---|
| 16 | TestClass(Context* context = nullptr) : BaseObject(context), changedNameBase_(false), xmlPortBase_(false), modeBase_(XMLPort::NOP) | 
|---|
| 17 | { | 
|---|
| 18 | RegisterObject(TestClass); | 
|---|
| 19 | } | 
|---|
| 20 |  | 
|---|
| 21 | virtual void changedName() override | 
|---|
| 22 | { | 
|---|
| 23 | this->changedNameBase_ = true; | 
|---|
| 24 | } | 
|---|
| 25 |  | 
|---|
| 26 | virtual void XMLPort(Element& xmlelement, XMLPort::Mode mode) override | 
|---|
| 27 | { | 
|---|
| 28 | this->xmlPortBase_ = true; | 
|---|
| 29 | this->modeBase_ = mode; | 
|---|
| 30 | } | 
|---|
| 31 |  | 
|---|
| 32 | bool changedNameBase_; | 
|---|
| 33 | bool xmlPortBase_; | 
|---|
| 34 | XMLPort::Mode modeBase_; | 
|---|
| 35 | }; | 
|---|
| 36 |  | 
|---|
| 37 | class TestSubclass : public TestClass | 
|---|
| 38 | { | 
|---|
| 39 | public: | 
|---|
| 40 | TestSubclass(Context* context = nullptr) : TestClass(context), changedNameSubclass_(false), xmlPortSubclass_(false), modeSubclass_(XMLPort::NOP) | 
|---|
| 41 | { | 
|---|
| 42 | RegisterObject(TestSubclass); | 
|---|
| 43 | } | 
|---|
| 44 |  | 
|---|
| 45 | virtual void changedName() override | 
|---|
| 46 | { | 
|---|
| 47 | this->changedNameSubclass_ = true; | 
|---|
| 48 |  | 
|---|
| 49 | SUPER(TestSubclass, changedName); | 
|---|
| 50 | } | 
|---|
| 51 |  | 
|---|
| 52 | virtual void XMLPort(Element& xmlelement, XMLPort::Mode mode) override | 
|---|
| 53 | { | 
|---|
| 54 | this->xmlPortSubclass_ = true; | 
|---|
| 55 | this->modeSubclass_ = mode; | 
|---|
| 56 |  | 
|---|
| 57 | SUPER(TestSubclass, XMLPort, xmlelement, mode); | 
|---|
| 58 | } | 
|---|
| 59 |  | 
|---|
| 60 | bool changedNameSubclass_; | 
|---|
| 61 | bool xmlPortSubclass_; | 
|---|
| 62 | XMLPort::Mode modeSubclass_; | 
|---|
| 63 | }; | 
|---|
| 64 |  | 
|---|
| 65 | RegisterClass(TestClass); | 
|---|
| 66 | RegisterClass(TestSubclass); | 
|---|
| 67 |  | 
|---|
| 68 | // Fixture | 
|---|
| 69 | class SuperTest : public ::testing::Test | 
|---|
| 70 | { | 
|---|
| 71 | public: | 
|---|
| 72 | virtual void SetUp() override | 
|---|
| 73 | { | 
|---|
| 74 | new IdentifierManager(); | 
|---|
| 75 | ModuleInstance::getCurrentModuleInstance()->loadAllStaticallyInitializedInstances(StaticInitialization::IDENTIFIER); | 
|---|
| 76 | Context::setRootContext(new Context(nullptr)); | 
|---|
| 77 | Identifier::initConfigValues_s = false; // TODO: hack! | 
|---|
| 78 | IdentifierManager::getInstance().createClassHierarchy(); | 
|---|
| 79 | } | 
|---|
| 80 |  | 
|---|
| 81 | virtual void TearDown() override | 
|---|
| 82 | { | 
|---|
| 83 | IdentifierManager::getInstance().destroyClassHierarchy(); | 
|---|
| 84 | Context::destroyRootContext(); | 
|---|
| 85 | ModuleInstance::getCurrentModuleInstance()->unloadAllStaticallyInitializedInstances(StaticInitialization::IDENTIFIER); | 
|---|
| 86 | delete &IdentifierManager::getInstance(); | 
|---|
| 87 | } | 
|---|
| 88 | }; | 
|---|
| 89 | } | 
|---|
| 90 |  | 
|---|
| 91 | TEST_F(SuperTest, ClassHierarchyIsCorrect) | 
|---|
| 92 | { | 
|---|
| 93 | { | 
|---|
| 94 | Identifier* identifier = Class(TestSubclass); | 
|---|
| 95 |  | 
|---|
| 96 | EXPECT_EQ(0u, identifier->getDirectChildren().size()); | 
|---|
| 97 |  | 
|---|
| 98 | EXPECT_EQ(1u, identifier->getDirectParents().size()); | 
|---|
| 99 | EXPECT_TRUE(std::find(identifier->getDirectParents().begin(), identifier->getDirectParents().end(), Class(TestClass)) != identifier->getDirectParents().end()); | 
|---|
| 100 | } | 
|---|
| 101 | { | 
|---|
| 102 | Identifier* identifier = Class(TestClass); | 
|---|
| 103 |  | 
|---|
| 104 | EXPECT_EQ(1u, identifier->getDirectChildren().size()); | 
|---|
| 105 | EXPECT_TRUE(identifier->getDirectChildren().find(Class(TestSubclass)) != identifier->getDirectChildren().end()); | 
|---|
| 106 |  | 
|---|
| 107 | EXPECT_EQ(1u, identifier->getDirectParents().size()); | 
|---|
| 108 | EXPECT_TRUE(std::find(identifier->getDirectParents().begin(), identifier->getDirectParents().end(), Class(BaseObject)) != identifier->getDirectParents().end()); | 
|---|
| 109 | } | 
|---|
| 110 | } | 
|---|
| 111 |  | 
|---|
| 112 | TEST_F(SuperTest, SuperCallWithoutArguments) | 
|---|
| 113 | { | 
|---|
| 114 | TestSubclass test; | 
|---|
| 115 |  | 
|---|
| 116 | EXPECT_FALSE(test.changedNameBase_); | 
|---|
| 117 | EXPECT_FALSE(test.changedNameSubclass_); | 
|---|
| 118 |  | 
|---|
| 119 | test.changedName(); | 
|---|
| 120 |  | 
|---|
| 121 | EXPECT_TRUE(test.changedNameBase_); | 
|---|
| 122 | EXPECT_TRUE(test.changedNameSubclass_); | 
|---|
| 123 | } | 
|---|
| 124 |  | 
|---|
| 125 | TEST_F(SuperTest, SuperCallWithArguments) | 
|---|
| 126 | { | 
|---|
| 127 | TestSubclass test; | 
|---|
| 128 |  | 
|---|
| 129 | EXPECT_FALSE(test.xmlPortBase_); | 
|---|
| 130 | EXPECT_FALSE(test.xmlPortSubclass_); | 
|---|
| 131 | EXPECT_EQ(XMLPort::NOP, test.modeBase_); | 
|---|
| 132 | EXPECT_EQ(XMLPort::NOP, test.modeSubclass_); | 
|---|
| 133 |  | 
|---|
| 134 | Element* element = nullptr; | 
|---|
| 135 | test.XMLPort(*element, XMLPort::SaveObject); | 
|---|
| 136 |  | 
|---|
| 137 | EXPECT_TRUE(test.xmlPortBase_); | 
|---|
| 138 | EXPECT_TRUE(test.xmlPortSubclass_); | 
|---|
| 139 | EXPECT_EQ(XMLPort::SaveObject, test.modeBase_); | 
|---|
| 140 | EXPECT_EQ(XMLPort::SaveObject, test.modeSubclass_); | 
|---|
| 141 | } | 
|---|
| 142 | } | 
|---|