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 | RegisterObject(Interface); |
---|
15 | } |
---|
16 | |
---|
17 | virtual void test() = 0; |
---|
18 | }; |
---|
19 | |
---|
20 | class BaseClass : virtual public Identifiable |
---|
21 | { |
---|
22 | public: |
---|
23 | BaseClass() |
---|
24 | { |
---|
25 | RegisterObject(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("Context", new ClassFactoryWithContext<Context>()); |
---|
47 | registerClass("Listable", new ClassFactoryWithContext<Listable>()); |
---|
48 | registerClass("Interface", static_cast<ClassFactory<Interface>*>(NULL), false).inheritsFrom(Class(Identifiable)); |
---|
49 | registerClass("BaseClass", new ClassFactoryNoArgs<BaseClass>()); |
---|
50 | registerClass("RealClass", new ClassFactoryNoArgs<RealClass>()); |
---|
51 | |
---|
52 | IdentifierManager::getInstance().createClassHierarchy(); |
---|
53 | } |
---|
54 | |
---|
55 | virtual void TearDown() |
---|
56 | { |
---|
57 | IdentifierManager::getInstance().destroyAllIdentifiers(); |
---|
58 | } |
---|
59 | }; |
---|
60 | |
---|
61 | bool contains(const std::set<const Identifier*> identifiers, Identifier* identifier) |
---|
62 | { |
---|
63 | return identifiers.find(identifier) != identifiers.end(); |
---|
64 | } |
---|
65 | } |
---|
66 | |
---|
67 | TEST(IdentifierExternalClassHierarchyTest_NoFixture, NoInitialization) |
---|
68 | { |
---|
69 | { |
---|
70 | Identifier* identifier = Class(Interface); |
---|
71 | EXPECT_EQ(0u, identifier->getChildren().size()); |
---|
72 | EXPECT_EQ(0u, identifier->getParents().size()); |
---|
73 | } |
---|
74 | { |
---|
75 | Identifier* identifier = Class(BaseClass); |
---|
76 | EXPECT_EQ(0u, identifier->getChildren().size()); |
---|
77 | EXPECT_EQ(0u, identifier->getParents().size()); |
---|
78 | } |
---|
79 | { |
---|
80 | Identifier* identifier = Class(RealClass); |
---|
81 | EXPECT_EQ(0u, identifier->getChildren().size()); |
---|
82 | EXPECT_EQ(0u, identifier->getParents().size()); |
---|
83 | } |
---|
84 | } |
---|
85 | |
---|
86 | TEST_F(IdentifierExternalClassHierarchyTest, TestInterface) |
---|
87 | { |
---|
88 | Identifier* identifier = Class(Interface); |
---|
89 | |
---|
90 | EXPECT_EQ(1u, identifier->getDirectChildren().size()); |
---|
91 | EXPECT_TRUE(contains(identifier->getDirectChildren(), Class(RealClass))); |
---|
92 | |
---|
93 | EXPECT_EQ(1u, identifier->getChildren().size()); |
---|
94 | EXPECT_TRUE(contains(identifier->getChildren(), Class(RealClass))); |
---|
95 | |
---|
96 | EXPECT_EQ(1u, identifier->getDirectParents().size()); |
---|
97 | EXPECT_TRUE(contains(identifier->getDirectParents(), Class(Identifiable))); |
---|
98 | |
---|
99 | EXPECT_EQ(1u, identifier->getParents().size()); |
---|
100 | EXPECT_TRUE(contains(identifier->getParents(), Class(Identifiable))); |
---|
101 | } |
---|
102 | |
---|
103 | TEST_F(IdentifierExternalClassHierarchyTest, BaseClass) |
---|
104 | { |
---|
105 | Identifier* identifier = Class(BaseClass); |
---|
106 | |
---|
107 | EXPECT_EQ(1u, identifier->getDirectChildren().size()); |
---|
108 | EXPECT_TRUE(contains(identifier->getDirectChildren(), Class(RealClass))); |
---|
109 | |
---|
110 | EXPECT_EQ(1u, identifier->getChildren().size()); |
---|
111 | EXPECT_TRUE(contains(identifier->getChildren(), Class(RealClass))); |
---|
112 | |
---|
113 | EXPECT_EQ(1u, identifier->getDirectParents().size()); |
---|
114 | EXPECT_TRUE(contains(identifier->getDirectParents(), Class(Identifiable))); |
---|
115 | |
---|
116 | EXPECT_EQ(1u, identifier->getParents().size()); |
---|
117 | EXPECT_TRUE(contains(identifier->getParents(), Class(Identifiable))); |
---|
118 | } |
---|
119 | |
---|
120 | TEST_F(IdentifierExternalClassHierarchyTest, 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(3u, identifier->getParents().size()); |
---|
133 | EXPECT_TRUE(contains(identifier->getParents(), Class(Identifiable))); |
---|
134 | EXPECT_TRUE(contains(identifier->getParents(), Class(Interface))); |
---|
135 | EXPECT_TRUE(contains(identifier->getParents(), Class(BaseClass))); |
---|
136 | } |
---|
137 | } |
---|