[9601] | 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" |
---|
[10624] | 7 | #include "core/module/ModuleInstance.h" |
---|
[9601] | 8 | |
---|
| 9 | namespace orxonox |
---|
| 10 | { |
---|
| 11 | namespace |
---|
| 12 | { |
---|
| 13 | class TestClass : public BaseObject |
---|
| 14 | { |
---|
| 15 | public: |
---|
[11071] | 16 | TestClass(Context* context = nullptr) : BaseObject(context), changedNameBase_(false), xmlPortBase_(false), modeBase_(XMLPort::NOP) |
---|
[9601] | 17 | { |
---|
[9659] | 18 | RegisterObject(TestClass); |
---|
[9601] | 19 | } |
---|
| 20 | |
---|
[11071] | 21 | virtual void changedName() override |
---|
[9601] | 22 | { |
---|
| 23 | this->changedNameBase_ = true; |
---|
| 24 | } |
---|
| 25 | |
---|
[11071] | 26 | virtual void XMLPort(Element& xmlelement, XMLPort::Mode mode) override |
---|
[9601] | 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: |
---|
[11071] | 40 | TestSubclass(Context* context = nullptr) : TestClass(context), changedNameSubclass_(false), xmlPortSubclass_(false), modeSubclass_(XMLPort::NOP) |
---|
[9601] | 41 | { |
---|
| 42 | RegisterObject(TestSubclass); |
---|
| 43 | } |
---|
| 44 | |
---|
[11071] | 45 | virtual void changedName() override |
---|
[9601] | 46 | { |
---|
| 47 | this->changedNameSubclass_ = true; |
---|
| 48 | |
---|
| 49 | SUPER(TestSubclass, changedName); |
---|
| 50 | } |
---|
| 51 | |
---|
[11071] | 52 | virtual void XMLPort(Element& xmlelement, XMLPort::Mode mode) override |
---|
[9601] | 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 | |
---|
[10624] | 65 | RegisterClass(TestClass); |
---|
| 66 | RegisterClass(TestSubclass); |
---|
| 67 | |
---|
| 68 | // Fixture |
---|
[9647] | 69 | class SuperTest : public ::testing::Test |
---|
| 70 | { |
---|
| 71 | public: |
---|
[11071] | 72 | virtual void SetUp() override |
---|
[9647] | 73 | { |
---|
[10624] | 74 | new IdentifierManager(); |
---|
| 75 | ModuleInstance::getCurrentModuleInstance()->loadAllStaticallyInitializedInstances(StaticInitialization::IDENTIFIER); |
---|
[11071] | 76 | Context::setRootContext(new Context(nullptr)); |
---|
[10624] | 77 | Identifier::initConfigValues_s = false; // TODO: hack! |
---|
[9647] | 78 | IdentifierManager::getInstance().createClassHierarchy(); |
---|
| 79 | } |
---|
| 80 | |
---|
[11071] | 81 | virtual void TearDown() override |
---|
[9647] | 82 | { |
---|
[10624] | 83 | IdentifierManager::getInstance().destroyClassHierarchy(); |
---|
| 84 | Context::destroyRootContext(); |
---|
| 85 | ModuleInstance::getCurrentModuleInstance()->unloadAllStaticallyInitializedInstances(StaticInitialization::IDENTIFIER); |
---|
| 86 | delete &IdentifierManager::getInstance(); |
---|
[9647] | 87 | } |
---|
| 88 | }; |
---|
[9601] | 89 | } |
---|
| 90 | |
---|
[9648] | 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()); |
---|
[10624] | 99 | EXPECT_TRUE(std::find(identifier->getDirectParents().begin(), identifier->getDirectParents().end(), Class(TestClass)) != identifier->getDirectParents().end()); |
---|
[9648] | 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()); |
---|
[10624] | 108 | EXPECT_TRUE(std::find(identifier->getDirectParents().begin(), identifier->getDirectParents().end(), Class(BaseObject)) != identifier->getDirectParents().end()); |
---|
[9648] | 109 | } |
---|
| 110 | } |
---|
| 111 | |
---|
[9647] | 112 | TEST_F(SuperTest, SuperCallWithoutArguments) |
---|
[9601] | 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 | |
---|
[9647] | 125 | TEST_F(SuperTest, SuperCallWithArguments) |
---|
[9601] | 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 | |
---|
[11071] | 134 | Element* element = nullptr; |
---|
[9601] | 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 | } |
---|