/
Entity.cpp
112 lines (89 loc) · 3.53 KB
/
Entity.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include "RadiantTest.h"
#include "ieclass.h"
#include "ientity.h"
namespace test
{
using EntityTest = RadiantTest;
namespace
{
// Obtain entity attachments as a simple std::list
std::list<Entity::Attachment> getAttachments(const IEntityNodePtr node)
{
std::list<Entity::Attachment> attachments;
if (node)
{
node->getEntity().forEachAttachment(
[&](const Entity::Attachment& a) { attachments.push_back(a); }
);
}
return attachments;
}
}
TEST_F(EntityTest, LookupEntityClass)
{
// Nonexistent class should return null (but not throw or crash)
auto cls = GlobalEntityClassManager().findClass("notAnEntityClass");
EXPECT_FALSE(cls);
// Real entity class should return a valid pointer
auto lightCls = GlobalEntityClassManager().findClass("light");
EXPECT_TRUE(lightCls);
}
TEST_F(EntityTest, LightEntitiesRecognisedAsLights)
{
// The 'light' class should be recognised as an actual light
auto lightCls = GlobalEntityClassManager().findClass("light");
EXPECT_TRUE(lightCls->isLight());
// Things which are not lights should also be correctly identified
auto notLightCls = GlobalEntityClassManager().findClass("dr:entity_using_modeldef");
EXPECT_TRUE(notLightCls);
EXPECT_FALSE(notLightCls->isLight());
// Anything deriving from the light class should also be a light
auto derived1 = GlobalEntityClassManager().findClass("atdm:light_base");
EXPECT_TRUE(derived1->isLight());
// Second level derivations too
auto derived2 = GlobalEntityClassManager().findClass("light_extinguishable");
EXPECT_TRUE(derived2->isLight());
// torch_brazier is not a light itself, but has a light attached, so it
// should not have isLight() == true
auto brazier = GlobalEntityClassManager().findClass("atdm:torch_brazier");
EXPECT_FALSE(brazier->isLight());
}
TEST_F(EntityTest, CannotCreateEntityWithoutClass)
{
// Creating with a null entity class should throw an exception
EXPECT_THROW(GlobalEntityModule().createEntity({}), std::runtime_error);
}
TEST_F(EntityTest, CreateBasicLightEntity)
{
// Create a basic light
auto lightCls = GlobalEntityClassManager().findClass("light");
auto light = GlobalEntityModule().createEntity(lightCls);
// Light has a sensible autogenerated name
EXPECT_EQ(light->name(), "light_1");
// Entity should have a "classname" key matching the actual entity class we
// created
auto clsName = light->getEntity().getKeyValue("classname");
EXPECT_EQ(clsName, "light");
// Entity should have an IEntityClass pointer which matches the one we
// looked up
EXPECT_EQ(light->getEntity().getEntityClass().get(), lightCls.get());
// This basic light entity should have no attachments
auto attachments = getAttachments(light);
EXPECT_EQ(attachments.size(), 0);
}
TEST_F(EntityTest, CreateAttachedLightEntity)
{
// Create the torch entity which has an attached light
auto torchCls = GlobalEntityClassManager().findClass("atdm:torch_brazier");
auto torch = GlobalEntityModule().createEntity(torchCls);
EXPECT_TRUE(torch);
// Check that the attachment spawnargs are present
const Entity& spawnArgs = torch->getEntity();
EXPECT_EQ(spawnArgs.getKeyValue("def_attach"), "light_cageflame_small");
EXPECT_EQ(spawnArgs.getKeyValue("pos_attach"), "flame");
EXPECT_EQ(spawnArgs.getKeyValue("name_attach"), "flame");
// Spawnargs should be parsed into a single attachment
auto attachments = getAttachments(torch);
EXPECT_EQ(attachments.size(), 1);
}
}