Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/core6/test/core/class/SuperTest.cc @ 9649

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

added ability to set the root-context explicitly (and also to destroy it before the class-hierarchy is destroyed).
currently it's not possible to set the root context explicitly during startup of the game because it is already used by static initialization

  • Property svn:eol-style set to native
File size: 4.5 KB
Line 
1#include <gtest/gtest.h>
2#include <tinyxml/tinyxml.h>
3
4#include "core/CoreIncludes.h"
5#include "core/BaseObject.h"
6#include "core/class/Super.h"
7
8namespace orxonox
9{
10    namespace
11    {
12        class TestClass : public BaseObject
13        {
14            public:
15                TestClass(Context* context = NULL) : BaseObject(context), changedNameBase_(false), xmlPortBase_(false), modeBase_(XMLPort::NOP)
16                {
17                    RegisterRootObject(TestClass);
18                }
19
20                virtual void changedName()
21                {
22                    this->changedNameBase_ = true;
23                }
24
25                virtual void XMLPort(Element& xmlelement, XMLPort::Mode mode)
26                {
27                    this->xmlPortBase_ = true;
28                    this->modeBase_ = mode;
29                }
30
31                bool changedNameBase_;
32                bool xmlPortBase_;
33                XMLPort::Mode modeBase_;
34        };
35
36        class TestSubclass : public TestClass
37        {
38            public:
39                TestSubclass(Context* context = NULL) : TestClass(context), changedNameSubclass_(false), xmlPortSubclass_(false), modeSubclass_(XMLPort::NOP)
40                {
41                    RegisterObject(TestSubclass);
42                }
43
44                virtual void changedName()
45                {
46                    this->changedNameSubclass_ = true;
47
48                    SUPER(TestSubclass, changedName);
49                }
50
51                virtual void XMLPort(Element& xmlelement, XMLPort::Mode mode)
52                {
53                    this->xmlPortSubclass_ = true;
54                    this->modeSubclass_ = mode;
55
56                    SUPER(TestSubclass, XMLPort, xmlelement, mode);
57                }
58
59                bool changedNameSubclass_;
60                bool xmlPortSubclass_;
61                XMLPort::Mode modeSubclass_;
62        };
63
64       // Fixture
65        class SuperTest : public ::testing::Test
66        {
67            public:
68                virtual void SetUp()
69                {
70                    IdentifierManager::getInstance().destroyAllIdentifiers();
71
72                    registerClass("OrxonoxClass", new ClassFactoryNoArgs<OrxonoxClass>());
73                    registerClass("BaseObject", new ClassFactoryWithContext<BaseObject>());
74                    registerClass("TestClass", new ClassFactoryWithContext<TestClass>());
75                    registerClass("TestSubclass", new ClassFactoryWithContext<TestSubclass>());
76
77                    IdentifierManager::getInstance().createClassHierarchy();
78
79                    Context::setRootContext(new Context(NULL));
80                }
81
82                virtual void TearDown()
83                {
84                    Context::setRootContext(NULL);
85
86                    IdentifierManager::getInstance().destroyAllIdentifiers();
87                }
88        };
89    }
90
91    TEST_F(SuperTest, ClassHierarchyIsCorrect)
92    {
93        {
94            Identifier* identifier = Class(TestSubclass);
95
96            EXPECT_EQ(0u, identifier->getDirectChildren().size());
97
98            EXPECT_EQ(1u, identifier->getDirectParents().size());
99            EXPECT_TRUE(identifier->getDirectParents().find(Class(TestClass)) != identifier->getDirectParents().end());
100        }
101        {
102            Identifier* identifier = Class(TestClass);
103
104            EXPECT_EQ(1u, identifier->getDirectChildren().size());
105            EXPECT_TRUE(identifier->getDirectChildren().find(Class(TestSubclass)) != identifier->getDirectChildren().end());
106
107            EXPECT_EQ(1u, identifier->getDirectParents().size());
108            EXPECT_TRUE(identifier->getDirectParents().find(Class(BaseObject)) != identifier->getDirectParents().end());
109        }
110    }
111
112    TEST_F(SuperTest, SuperCallWithoutArguments)
113    {
114        TestSubclass test;
115
116        EXPECT_FALSE(test.changedNameBase_);
117        EXPECT_FALSE(test.changedNameSubclass_);
118
119        test.changedName();
120
121        EXPECT_TRUE(test.changedNameBase_);
122        EXPECT_TRUE(test.changedNameSubclass_);
123    }
124
125    TEST_F(SuperTest, SuperCallWithArguments)
126    {
127        TestSubclass test;
128
129        EXPECT_FALSE(test.xmlPortBase_);
130        EXPECT_FALSE(test.xmlPortSubclass_);
131        EXPECT_EQ(XMLPort::NOP, test.modeBase_);
132        EXPECT_EQ(XMLPort::NOP, test.modeSubclass_);
133
134        Element* element = NULL;
135        test.XMLPort(*element, XMLPort::SaveObject);
136
137        EXPECT_TRUE(test.xmlPortBase_);
138        EXPECT_TRUE(test.xmlPortSubclass_);
139        EXPECT_EQ(XMLPort::SaveObject, test.modeBase_);
140        EXPECT_EQ(XMLPort::SaveObject, test.modeSubclass_);
141    }
142}
Note: See TracBrowser for help on using the repository browser.