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() override {} |
---|
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() override |
---|
50 | { |
---|
51 | new IdentifierManager(); |
---|
52 | ModuleInstance::getCurrentModuleInstance()->loadAllStaticallyInitializedInstances(StaticInitialization::IDENTIFIER); |
---|
53 | Context::setRootContext(new Context(nullptr)); |
---|
54 | Identifier::initConfigValues_s = false; // TODO: hack! |
---|
55 | IdentifierManager::getInstance().createClassHierarchy(); |
---|
56 | } |
---|
57 | |
---|
58 | virtual void TearDown() override |
---|
59 | { |
---|
60 | IdentifierManager::getInstance().destroyClassHierarchy(); |
---|
61 | Context::destroyRootContext(); |
---|
62 | ModuleInstance::getCurrentModuleInstance()->unloadAllStaticallyInitializedInstances(StaticInitialization::IDENTIFIER); |
---|
63 | delete &IdentifierManager::getInstance(); |
---|
64 | } |
---|
65 | }; |
---|
66 | |
---|
67 | bool contains(const std::list<const Identifier*> identifiers, Identifier* identifier) |
---|
68 | { |
---|
69 | return std::find(identifiers.begin(), identifiers.end(), identifier) != identifiers.end(); |
---|
70 | } |
---|
71 | |
---|
72 | bool contains(const std::set<const Identifier*> identifiers, Identifier* identifier) |
---|
73 | { |
---|
74 | return identifiers.find(identifier) != identifiers.end(); |
---|
75 | } |
---|
76 | } |
---|
77 | |
---|
78 | TEST_F(IdentifierExternalClassHierarchyTest, TestInterface) |
---|
79 | { |
---|
80 | Identifier* identifier = Class(Interface); |
---|
81 | |
---|
82 | EXPECT_EQ(1u, identifier->getDirectChildren().size()); |
---|
83 | EXPECT_TRUE(contains(identifier->getDirectChildren(), Class(RealClass))); |
---|
84 | |
---|
85 | EXPECT_EQ(1u, identifier->getChildren().size()); |
---|
86 | EXPECT_TRUE(contains(identifier->getChildren(), Class(RealClass))); |
---|
87 | |
---|
88 | EXPECT_EQ(1u, identifier->getDirectParents().size()); |
---|
89 | EXPECT_TRUE(contains(identifier->getDirectParents(), Class(Identifiable))); |
---|
90 | |
---|
91 | EXPECT_EQ(1u, identifier->getParents().size()); |
---|
92 | EXPECT_TRUE(contains(identifier->getParents(), Class(Identifiable))); |
---|
93 | } |
---|
94 | |
---|
95 | TEST_F(IdentifierExternalClassHierarchyTest, BaseClass) |
---|
96 | { |
---|
97 | Identifier* identifier = Class(BaseClass); |
---|
98 | |
---|
99 | EXPECT_EQ(1u, identifier->getDirectChildren().size()); |
---|
100 | EXPECT_TRUE(contains(identifier->getDirectChildren(), Class(RealClass))); |
---|
101 | |
---|
102 | EXPECT_EQ(1u, identifier->getChildren().size()); |
---|
103 | EXPECT_TRUE(contains(identifier->getChildren(), Class(RealClass))); |
---|
104 | |
---|
105 | EXPECT_EQ(1u, identifier->getDirectParents().size()); |
---|
106 | EXPECT_TRUE(contains(identifier->getDirectParents(), Class(Identifiable))); |
---|
107 | |
---|
108 | EXPECT_EQ(1u, identifier->getParents().size()); |
---|
109 | EXPECT_TRUE(contains(identifier->getParents(), Class(Identifiable))); |
---|
110 | } |
---|
111 | |
---|
112 | TEST_F(IdentifierExternalClassHierarchyTest, RealClass) |
---|
113 | { |
---|
114 | Identifier* identifier = Class(RealClass); |
---|
115 | |
---|
116 | EXPECT_EQ(0u, identifier->getDirectChildren().size()); |
---|
117 | |
---|
118 | EXPECT_EQ(0u, identifier->getChildren().size()); |
---|
119 | |
---|
120 | EXPECT_EQ(2u, identifier->getDirectParents().size()); |
---|
121 | EXPECT_TRUE(contains(identifier->getDirectParents(), Class(Interface))); |
---|
122 | EXPECT_TRUE(contains(identifier->getDirectParents(), Class(BaseClass))); |
---|
123 | |
---|
124 | EXPECT_EQ(3u, identifier->getParents().size()); |
---|
125 | EXPECT_TRUE(contains(identifier->getParents(), Class(Identifiable))); |
---|
126 | EXPECT_TRUE(contains(identifier->getParents(), Class(Interface))); |
---|
127 | EXPECT_TRUE(contains(identifier->getParents(), Class(BaseClass))); |
---|
128 | } |
---|
129 | } |
---|