Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/core6/test/core/class/IdentifierSimpleClassHierarchyTest.cc @ 9647

Last change on this file since 9647 was 9646, checked in by landauf, 11 years ago

implemented new concept to initialize the class hierarchy: each identifier is initialized individually by creating an object of the corresponding class. identifiers of abstract classes must be told explicitly about their direct parents. this allows to build the class tree correctly also for complicated setups like multiple inheritance of abstract classes.

  • Property svn:eol-style set to native
File size: 5.1 KB
Line 
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
7namespace orxonox
8{
9    namespace
10    {
11        class Interface : public OrxonoxInterface
12        {
13            public:
14                Interface()
15                {
16                    RegisterRootObject(Interface);
17                }
18
19                virtual void test() = 0;
20        };
21
22        class BaseClass : public OrxonoxClass
23        {
24            public:
25                BaseClass()
26                {
27                    RegisterRootObject(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("OrxonoxInterface", new ClassFactoryNoArgs<OrxonoxInterface>());
49                    registerClass("OrxonoxClass", new ClassFactoryNoArgs<OrxonoxClass>());
50                    registerClass("Interface", static_cast<ClassFactory<Interface>*>(NULL), false).inheritsFrom(Class(OrxonoxInterface));
51                    registerClass("BaseClass", new ClassFactoryNoArgs<BaseClass>());
52                    registerClass("RealClass", new ClassFactoryNoArgs<RealClass>());
53
54                    IdentifierManager::getInstance().createClassHierarchy();
55                }
56
57                virtual void TearDown()
58                {
59                    IdentifierManager::getInstance().destroyAllIdentifiers();
60                }
61        };
62
63        bool contains(const std::set<const Identifier*> identifiers, Identifier* identifier)
64        {
65            return identifiers.find(identifier) != identifiers.end();
66        }
67    }
68
69    TEST(IdentifierSimpleClassHierarchyTest_NoFixture, NoInitialization)
70    {
71        {
72            Identifier* identifier = Class(Interface);
73            EXPECT_EQ(0u, identifier->getChildren().size());
74            EXPECT_EQ(0u, identifier->getParents().size());
75        }
76        {
77            Identifier* identifier = Class(BaseClass);
78            EXPECT_EQ(0u, identifier->getChildren().size());
79            EXPECT_EQ(0u, identifier->getParents().size());
80        }
81        {
82            Identifier* identifier = Class(RealClass);
83            EXPECT_EQ(0u, identifier->getChildren().size());
84            EXPECT_EQ(0u, identifier->getParents().size());
85        }
86    }
87
88    TEST_F(IdentifierSimpleClassHierarchyTest, TestInterface)
89    {
90        Identifier* identifier = Class(Interface);
91
92        EXPECT_EQ(1u, identifier->getDirectChildren().size());
93        EXPECT_TRUE(contains(identifier->getDirectChildren(), Class(RealClass)));
94
95        EXPECT_EQ(1u, identifier->getChildren().size());
96        EXPECT_TRUE(contains(identifier->getChildren(), Class(RealClass)));
97
98        EXPECT_EQ(1u, identifier->getDirectParents().size());
99        EXPECT_TRUE(contains(identifier->getDirectParents(), Class(OrxonoxInterface)));
100
101        EXPECT_EQ(2u, identifier->getParents().size());
102        EXPECT_TRUE(contains(identifier->getParents(), Class(Identifiable)));
103        EXPECT_TRUE(contains(identifier->getParents(), Class(OrxonoxInterface)));
104    }
105
106    TEST_F(IdentifierSimpleClassHierarchyTest, BaseClass)
107    {
108        Identifier* identifier = Class(BaseClass);
109
110        EXPECT_EQ(1u, identifier->getDirectChildren().size());
111        EXPECT_TRUE(contains(identifier->getDirectChildren(), Class(RealClass)));
112
113        EXPECT_EQ(1u, identifier->getChildren().size());
114        EXPECT_TRUE(contains(identifier->getChildren(), Class(RealClass)));
115
116        EXPECT_EQ(1u, identifier->getDirectParents().size());
117        EXPECT_TRUE(contains(identifier->getDirectParents(), Class(OrxonoxClass)));
118
119        EXPECT_EQ(2u, identifier->getParents().size());
120        EXPECT_TRUE(contains(identifier->getParents(), Class(Identifiable)));
121        EXPECT_TRUE(contains(identifier->getParents(), Class(OrxonoxClass)));
122    }
123
124    TEST_F(IdentifierSimpleClassHierarchyTest, RealClass)
125    {
126        Identifier* identifier = Class(RealClass);
127
128        EXPECT_EQ(0u, identifier->getDirectChildren().size());
129
130        EXPECT_EQ(0u, identifier->getChildren().size());
131
132        EXPECT_EQ(2u, identifier->getDirectParents().size());
133        EXPECT_TRUE(contains(identifier->getDirectParents(), Class(Interface)));
134        EXPECT_TRUE(contains(identifier->getDirectParents(), Class(BaseClass)));
135
136        EXPECT_EQ(5u, identifier->getParents().size());
137        EXPECT_TRUE(contains(identifier->getParents(), Class(Identifiable)));
138        EXPECT_TRUE(contains(identifier->getParents(), Class(OrxonoxInterface)));
139        EXPECT_TRUE(contains(identifier->getParents(), Class(OrxonoxClass)));
140        EXPECT_TRUE(contains(identifier->getParents(), Class(Interface)));
141        EXPECT_TRUE(contains(identifier->getParents(), Class(BaseClass)));
142    }
143}
Note: See TracBrowser for help on using the repository browser.