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