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