[9637] | 1 | #include <gtest/gtest.h> |
---|
| 2 | #include "core/CoreIncludes.h" |
---|
| 3 | #include "core/class/Identifiable.h" |
---|
[10624] | 4 | #include "core/module/ModuleInstance.h" |
---|
[9637] | 5 | |
---|
| 6 | namespace orxonox |
---|
| 7 | { |
---|
| 8 | namespace |
---|
| 9 | { |
---|
| 10 | class Interface : virtual public Identifiable |
---|
[9646] | 11 | { |
---|
| 12 | public: |
---|
| 13 | Interface() |
---|
| 14 | { |
---|
[9659] | 15 | RegisterObject(Interface); |
---|
[9646] | 16 | } |
---|
[9637] | 17 | |
---|
[9646] | 18 | virtual void test() = 0; |
---|
| 19 | }; |
---|
| 20 | |
---|
[9637] | 21 | class BaseClass : virtual public Identifiable |
---|
[9646] | 22 | { |
---|
| 23 | public: |
---|
| 24 | BaseClass() |
---|
| 25 | { |
---|
[9659] | 26 | RegisterObject(BaseClass); |
---|
[9646] | 27 | } |
---|
| 28 | }; |
---|
[9637] | 29 | |
---|
| 30 | class RealClass : public BaseClass, public Interface |
---|
[9646] | 31 | { |
---|
| 32 | public: |
---|
| 33 | RealClass() |
---|
| 34 | { |
---|
| 35 | RegisterObject(RealClass); |
---|
| 36 | } |
---|
[9637] | 37 | |
---|
[9646] | 38 | virtual void test() {} |
---|
| 39 | }; |
---|
| 40 | |
---|
[10624] | 41 | RegisterAbstractClass(Interface).inheritsFrom<Identifiable>(); |
---|
| 42 | RegisterClassNoArgs(BaseClass); |
---|
| 43 | RegisterClassNoArgs(RealClass); |
---|
| 44 | |
---|
[9637] | 45 | // Fixture |
---|
| 46 | class IdentifierExternalClassHierarchyTest : public ::testing::Test |
---|
| 47 | { |
---|
| 48 | public: |
---|
| 49 | virtual void SetUp() |
---|
| 50 | { |
---|
[10624] | 51 | new IdentifierManager(); |
---|
| 52 | ModuleInstance::getCurrentModuleInstance()->loadAllStaticallyInitializedInstances(StaticInitialization::IDENTIFIER); |
---|
| 53 | Context::setRootContext(new Context(NULL)); |
---|
| 54 | Identifier::initConfigValues_s = false; // TODO: hack! |
---|
[9640] | 55 | IdentifierManager::getInstance().createClassHierarchy(); |
---|
[9637] | 56 | } |
---|
| 57 | |
---|
| 58 | virtual void TearDown() |
---|
| 59 | { |
---|
[10624] | 60 | IdentifierManager::getInstance().destroyClassHierarchy(); |
---|
| 61 | Context::destroyRootContext(); |
---|
| 62 | ModuleInstance::getCurrentModuleInstance()->unloadAllStaticallyInitializedInstances(StaticInitialization::IDENTIFIER); |
---|
| 63 | delete &IdentifierManager::getInstance(); |
---|
[9637] | 64 | } |
---|
| 65 | }; |
---|
[9646] | 66 | |
---|
[10624] | 67 | bool contains(const std::list<const Identifier*> identifiers, Identifier* identifier) |
---|
[9646] | 68 | { |
---|
[10624] | 69 | return std::find(identifiers.begin(), identifiers.end(), identifier) != identifiers.end(); |
---|
[9646] | 70 | } |
---|
[9637] | 71 | |
---|
[10624] | 72 | bool contains(const std::set<const Identifier*> identifiers, Identifier* identifier) |
---|
[9637] | 73 | { |
---|
[10624] | 74 | return identifiers.find(identifier) != identifiers.end(); |
---|
[9637] | 75 | } |
---|
| 76 | } |
---|
| 77 | |
---|
| 78 | TEST_F(IdentifierExternalClassHierarchyTest, TestInterface) |
---|
| 79 | { |
---|
| 80 | Identifier* identifier = Class(Interface); |
---|
| 81 | |
---|
[9646] | 82 | EXPECT_EQ(1u, identifier->getDirectChildren().size()); |
---|
| 83 | EXPECT_TRUE(contains(identifier->getDirectChildren(), Class(RealClass))); |
---|
| 84 | |
---|
[9637] | 85 | EXPECT_EQ(1u, identifier->getChildren().size()); |
---|
[9646] | 86 | EXPECT_TRUE(contains(identifier->getChildren(), Class(RealClass))); |
---|
| 87 | |
---|
| 88 | EXPECT_EQ(1u, identifier->getDirectParents().size()); |
---|
| 89 | EXPECT_TRUE(contains(identifier->getDirectParents(), Class(Identifiable))); |
---|
| 90 | |
---|
| 91 | EXPECT_EQ(1u, identifier->getParents().size()); |
---|
| 92 | EXPECT_TRUE(contains(identifier->getParents(), Class(Identifiable))); |
---|
[9637] | 93 | } |
---|
| 94 | |
---|
| 95 | TEST_F(IdentifierExternalClassHierarchyTest, BaseClass) |
---|
| 96 | { |
---|
| 97 | Identifier* identifier = Class(BaseClass); |
---|
| 98 | |
---|
[9646] | 99 | EXPECT_EQ(1u, identifier->getDirectChildren().size()); |
---|
| 100 | EXPECT_TRUE(contains(identifier->getDirectChildren(), Class(RealClass))); |
---|
| 101 | |
---|
[9637] | 102 | EXPECT_EQ(1u, identifier->getChildren().size()); |
---|
[9646] | 103 | EXPECT_TRUE(contains(identifier->getChildren(), Class(RealClass))); |
---|
| 104 | |
---|
| 105 | EXPECT_EQ(1u, identifier->getDirectParents().size()); |
---|
| 106 | EXPECT_TRUE(contains(identifier->getDirectParents(), Class(Identifiable))); |
---|
| 107 | |
---|
| 108 | EXPECT_EQ(1u, identifier->getParents().size()); |
---|
| 109 | EXPECT_TRUE(contains(identifier->getParents(), Class(Identifiable))); |
---|
[9637] | 110 | } |
---|
| 111 | |
---|
| 112 | TEST_F(IdentifierExternalClassHierarchyTest, RealClass) |
---|
| 113 | { |
---|
| 114 | Identifier* identifier = Class(RealClass); |
---|
| 115 | |
---|
[9646] | 116 | EXPECT_EQ(0u, identifier->getDirectChildren().size()); |
---|
| 117 | |
---|
[9637] | 118 | EXPECT_EQ(0u, identifier->getChildren().size()); |
---|
[9646] | 119 | |
---|
| 120 | EXPECT_EQ(2u, identifier->getDirectParents().size()); |
---|
| 121 | EXPECT_TRUE(contains(identifier->getDirectParents(), Class(Interface))); |
---|
| 122 | EXPECT_TRUE(contains(identifier->getDirectParents(), Class(BaseClass))); |
---|
| 123 | |
---|
| 124 | EXPECT_EQ(3u, identifier->getParents().size()); |
---|
| 125 | EXPECT_TRUE(contains(identifier->getParents(), Class(Identifiable))); |
---|
| 126 | EXPECT_TRUE(contains(identifier->getParents(), Class(Interface))); |
---|
| 127 | EXPECT_TRUE(contains(identifier->getParents(), Class(BaseClass))); |
---|
[9637] | 128 | } |
---|
| 129 | } |
---|