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 | |
---|
7 | namespace orxonox |
---|
8 | { |
---|
9 | namespace |
---|
10 | { |
---|
11 | class Interface : public OrxonoxInterface |
---|
12 | { |
---|
13 | public: |
---|
14 | Interface() |
---|
15 | { |
---|
16 | RegisterObject(Interface); |
---|
17 | } |
---|
18 | |
---|
19 | virtual void test() = 0; |
---|
20 | }; |
---|
21 | |
---|
22 | class BaseClass : public OrxonoxClass |
---|
23 | { |
---|
24 | public: |
---|
25 | BaseClass() |
---|
26 | { |
---|
27 | RegisterObject(BaseClass); |
---|
28 | } |
---|
29 | }; |
---|
30 | |
---|
31 | class RealClass : public BaseClass, public Interface |
---|
32 | { |
---|
33 | public: |
---|
34 | RealClass() |
---|
35 | { |
---|
36 | RegisterObject(RealClass); |
---|
37 | } |
---|
38 | |
---|
39 | virtual void test() {} |
---|
40 | }; |
---|
41 | |
---|
42 | // Fixture |
---|
43 | class IdentifierSimpleClassHierarchyTest : public ::testing::Test |
---|
44 | { |
---|
45 | public: |
---|
46 | virtual void SetUp() |
---|
47 | { |
---|
48 | registerClass("Context", new ClassFactoryWithContext<Context>()); |
---|
49 | registerClass("Listable", new ClassFactoryWithContext<Listable>()); |
---|
50 | registerClass("Configurable", new ClassFactoryNoArgs<Configurable>()); |
---|
51 | registerClass("OrxonoxInterface", new ClassFactoryNoArgs<OrxonoxInterface>()); |
---|
52 | registerClass("OrxonoxClass", new ClassFactoryNoArgs<OrxonoxClass>()); |
---|
53 | registerClass("Interface", static_cast<ClassFactory<Interface>*>(NULL), false)->inheritsFrom(Class(OrxonoxInterface)); |
---|
54 | registerClass("BaseClass", new ClassFactoryNoArgs<BaseClass>()); |
---|
55 | registerClass("RealClass", new ClassFactoryNoArgs<RealClass>()); |
---|
56 | |
---|
57 | IdentifierManager::getInstance().createClassHierarchy(); |
---|
58 | } |
---|
59 | |
---|
60 | virtual void TearDown() |
---|
61 | { |
---|
62 | IdentifierManager::getInstance().destroyAllIdentifiers(); |
---|
63 | } |
---|
64 | }; |
---|
65 | |
---|
66 | bool contains(const std::list<const Identifier*> identifiers, Identifier* identifier) |
---|
67 | { |
---|
68 | return std::find(identifiers.begin(), identifiers.end(), identifier) != identifiers.end(); |
---|
69 | } |
---|
70 | |
---|
71 | bool contains(const std::set<const Identifier*> identifiers, Identifier* identifier) |
---|
72 | { |
---|
73 | return identifiers.find(identifier) != identifiers.end(); |
---|
74 | } |
---|
75 | } |
---|
76 | |
---|
77 | TEST(IdentifierSimpleClassHierarchyTest_NoFixture, NoInitialization) |
---|
78 | { |
---|
79 | { |
---|
80 | Identifier* identifier = Class(Interface); |
---|
81 | EXPECT_EQ(0u, identifier->getChildren().size()); |
---|
82 | EXPECT_EQ(0u, identifier->getParents().size()); |
---|
83 | } |
---|
84 | { |
---|
85 | Identifier* identifier = Class(BaseClass); |
---|
86 | EXPECT_EQ(0u, identifier->getChildren().size()); |
---|
87 | EXPECT_EQ(0u, identifier->getParents().size()); |
---|
88 | } |
---|
89 | { |
---|
90 | Identifier* identifier = Class(RealClass); |
---|
91 | EXPECT_EQ(0u, identifier->getChildren().size()); |
---|
92 | EXPECT_EQ(0u, identifier->getParents().size()); |
---|
93 | } |
---|
94 | } |
---|
95 | |
---|
96 | TEST_F(IdentifierSimpleClassHierarchyTest, TestInterface) |
---|
97 | { |
---|
98 | Identifier* identifier = Class(Interface); |
---|
99 | |
---|
100 | EXPECT_EQ(1u, identifier->getDirectChildren().size()); |
---|
101 | EXPECT_TRUE(contains(identifier->getDirectChildren(), Class(RealClass))); |
---|
102 | |
---|
103 | EXPECT_EQ(1u, identifier->getChildren().size()); |
---|
104 | EXPECT_TRUE(contains(identifier->getChildren(), Class(RealClass))); |
---|
105 | |
---|
106 | EXPECT_EQ(1u, identifier->getDirectParents().size()); |
---|
107 | EXPECT_TRUE(contains(identifier->getDirectParents(), Class(OrxonoxInterface))); |
---|
108 | |
---|
109 | EXPECT_EQ(4u, identifier->getParents().size()); |
---|
110 | EXPECT_TRUE(contains(identifier->getParents(), Class(Identifiable))); |
---|
111 | EXPECT_TRUE(contains(identifier->getParents(), Class(Listable))); |
---|
112 | EXPECT_TRUE(contains(identifier->getParents(), Class(Configurable))); |
---|
113 | EXPECT_TRUE(contains(identifier->getParents(), Class(OrxonoxInterface))); |
---|
114 | } |
---|
115 | |
---|
116 | TEST_F(IdentifierSimpleClassHierarchyTest, BaseClass) |
---|
117 | { |
---|
118 | Identifier* identifier = Class(BaseClass); |
---|
119 | |
---|
120 | EXPECT_EQ(1u, identifier->getDirectChildren().size()); |
---|
121 | EXPECT_TRUE(contains(identifier->getDirectChildren(), Class(RealClass))); |
---|
122 | |
---|
123 | EXPECT_EQ(1u, identifier->getChildren().size()); |
---|
124 | EXPECT_TRUE(contains(identifier->getChildren(), Class(RealClass))); |
---|
125 | |
---|
126 | EXPECT_EQ(1u, identifier->getDirectParents().size()); |
---|
127 | EXPECT_TRUE(contains(identifier->getDirectParents(), Class(OrxonoxClass))); |
---|
128 | |
---|
129 | EXPECT_EQ(4u, identifier->getParents().size()); |
---|
130 | EXPECT_TRUE(contains(identifier->getParents(), Class(Identifiable))); |
---|
131 | EXPECT_TRUE(contains(identifier->getParents(), Class(Listable))); |
---|
132 | EXPECT_TRUE(contains(identifier->getParents(), Class(Configurable))); |
---|
133 | EXPECT_TRUE(contains(identifier->getParents(), Class(OrxonoxClass))); |
---|
134 | } |
---|
135 | |
---|
136 | TEST_F(IdentifierSimpleClassHierarchyTest, RealClass) |
---|
137 | { |
---|
138 | Identifier* identifier = Class(RealClass); |
---|
139 | |
---|
140 | EXPECT_EQ(0u, identifier->getDirectChildren().size()); |
---|
141 | |
---|
142 | EXPECT_EQ(0u, identifier->getChildren().size()); |
---|
143 | |
---|
144 | EXPECT_EQ(2u, identifier->getDirectParents().size()); |
---|
145 | EXPECT_TRUE(contains(identifier->getDirectParents(), Class(Interface))); |
---|
146 | EXPECT_TRUE(contains(identifier->getDirectParents(), Class(BaseClass))); |
---|
147 | |
---|
148 | EXPECT_EQ(7u, identifier->getParents().size()); |
---|
149 | EXPECT_TRUE(contains(identifier->getParents(), Class(Identifiable))); |
---|
150 | EXPECT_TRUE(contains(identifier->getParents(), Class(Listable))); |
---|
151 | EXPECT_TRUE(contains(identifier->getParents(), Class(Configurable))); |
---|
152 | EXPECT_TRUE(contains(identifier->getParents(), Class(OrxonoxInterface))); |
---|
153 | EXPECT_TRUE(contains(identifier->getParents(), Class(OrxonoxClass))); |
---|
154 | EXPECT_TRUE(contains(identifier->getParents(), Class(Interface))); |
---|
155 | EXPECT_TRUE(contains(identifier->getParents(), Class(BaseClass))); |
---|
156 | } |
---|
157 | } |
---|