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