Changeset View
Changeset View
Standalone View
Standalone View
source/simulation2/tests/test_ComponentManager.h
Show All 18 Lines | |||||
#include "simulation2/system/ComponentManager.h" | #include "simulation2/system/ComponentManager.h" | ||||
#include "simulation2/MessageTypes.h" | #include "simulation2/MessageTypes.h" | ||||
#include "simulation2/system/ParamNode.h" | #include "simulation2/system/ParamNode.h" | ||||
#include "simulation2/system/SimContext.h" | #include "simulation2/system/SimContext.h" | ||||
#include "simulation2/serialization/ISerializer.h" | #include "simulation2/serialization/ISerializer.h" | ||||
#include "simulation2/components/ICmpTest.h" | #include "simulation2/components/ICmpTest.h" | ||||
#include "simulation2/components/ICmpTemplateManager.h" | #include "simulation2/components/CCmpTemplateManager.h" | ||||
#include "ps/CLogger.h" | #include "ps/CLogger.h" | ||||
#include "ps/Filesystem.h" | #include "ps/Filesystem.h" | ||||
#include "ps/XML/Xeromyces.h" | #include "ps/XML/Xeromyces.h" | ||||
#define TS_ASSERT_STREAM(stream, len, buffer) \ | #define TS_ASSERT_STREAM(stream, len, buffer) \ | ||||
TS_ASSERT_EQUALS(stream.str().length(), (size_t)len); \ | TS_ASSERT_EQUALS(stream.str().length(), (size_t)len); \ | ||||
TS_ASSERT_SAME_DATA(stream.str().data(), buffer, len) | TS_ASSERT_SAME_DATA(stream.str().data(), buffer, len) | ||||
▲ Show 20 Lines • Show All 378 Lines • ▼ Show 20 Lines | void test_script_QueryInterface() | ||||
TS_ASSERT_EQUALS(static_cast<ICmpTest1*> (man.QueryInterface(ent1, IID_Test1))->GetX(), 400); | TS_ASSERT_EQUALS(static_cast<ICmpTest1*> (man.QueryInterface(ent1, IID_Test1))->GetX(), 400); | ||||
TS_ASSERT_EQUALS(static_cast<ICmpTest1*> (man.QueryInterface(ent2, IID_Test1))->GetX(), 21000); | TS_ASSERT_EQUALS(static_cast<ICmpTest1*> (man.QueryInterface(ent2, IID_Test1))->GetX(), 21000); | ||||
} | } | ||||
void test_script_AddEntity() | void test_script_AddEntity() | ||||
{ | { | ||||
CSimContext context; | CSimContext context; | ||||
CCmpTemplateManager templateMan; | |||||
CComponentManager man(context, g_ScriptContext); | CComponentManager man(context, g_ScriptContext); | ||||
man.LoadComponentTypes(); | man.LoadComponentTypes(); | ||||
TS_ASSERT(man.LoadScript(L"simulation/components/test-addentity.js")); | TS_ASSERT(man.LoadScript(L"simulation/components/test-addentity.js")); | ||||
TS_ASSERT(man.LoadScript(L"simulation/components/addentity/test-addentity.js")); | TS_ASSERT(man.LoadScript(L"simulation/components/addentity/test-addentity.js")); | ||||
man.InitSystemEntity(); | man.InitSystemEntity(); | ||||
entity_id_t ent1 = man.AllocateNewEntity(); | entity_id_t ent1 = man.AllocateNewEntity(); | ||||
entity_id_t ent2 = ent1 + 2; | entity_id_t ent2 = ent1 + 2; | ||||
CEntityHandle hnd1 = man.AllocateEntityHandle(ent1); | CEntityHandle hnd1 = man.AllocateEntityHandle(ent1); | ||||
CParamNode noParam; | CParamNode noParam; | ||||
TS_ASSERT(man.AddComponent(man.GetSystemEntity(), CID_TemplateManager, noParam)); | man.AddConstructedComponent(man.GetSystemEntity(), templateMan, noParam); | ||||
TS_ASSERT(man.AddComponent(hnd1, man.LookupCID("TestScript1_AddEntity"), noParam)); | TS_ASSERT(man.AddComponent(hnd1, man.LookupCID("TestScript1_AddEntity"), noParam)); | ||||
TS_ASSERT(man.QueryInterface(ent2, IID_Test1) == NULL); | TS_ASSERT(man.QueryInterface(ent2, IID_Test1) == NULL); | ||||
TS_ASSERT(man.QueryInterface(ent2, IID_Test2) == NULL); | TS_ASSERT(man.QueryInterface(ent2, IID_Test2) == NULL); | ||||
{ | { | ||||
TestLogger logger; // ignore bogus-template warnings | TestLogger logger; // ignore bogus-template warnings | ||||
TS_ASSERT_EQUALS(static_cast<ICmpTest1*> (man.QueryInterface(ent1, IID_Test1))->GetX(), (int)ent2); | TS_ASSERT_EQUALS(static_cast<ICmpTest1*> (man.QueryInterface(ent1, IID_Test1))->GetX(), (int)ent2); | ||||
} | } | ||||
TS_ASSERT(man.QueryInterface(ent2, IID_Test1) != NULL); | TS_ASSERT(man.QueryInterface(ent2, IID_Test1) != NULL); | ||||
TS_ASSERT(man.QueryInterface(ent2, IID_Test2) != NULL); | TS_ASSERT(man.QueryInterface(ent2, IID_Test2) != NULL); | ||||
TS_ASSERT_EQUALS(static_cast<ICmpTest1*> (man.QueryInterface(ent2, IID_Test1))->GetX(), 999); | TS_ASSERT_EQUALS(static_cast<ICmpTest1*> (man.QueryInterface(ent2, IID_Test1))->GetX(), 999); | ||||
TS_ASSERT_EQUALS(static_cast<ICmpTest2*> (man.QueryInterface(ent2, IID_Test2))->GetX(), 12345); | TS_ASSERT_EQUALS(static_cast<ICmpTest2*> (man.QueryInterface(ent2, IID_Test2))->GetX(), 12345); | ||||
} | } | ||||
void test_script_AddLocalEntity() | void test_script_AddLocalEntity() | ||||
{ | { | ||||
CSimContext context; | CSimContext context; | ||||
CCmpTemplateManager templateMan; | |||||
CComponentManager man(context, g_ScriptContext); | CComponentManager man(context, g_ScriptContext); | ||||
man.LoadComponentTypes(); | man.LoadComponentTypes(); | ||||
TS_ASSERT(man.LoadScript(L"simulation/components/test-addentity.js")); | TS_ASSERT(man.LoadScript(L"simulation/components/test-addentity.js")); | ||||
TS_ASSERT(man.LoadScript(L"simulation/components/addentity/test-addentity.js")); | TS_ASSERT(man.LoadScript(L"simulation/components/addentity/test-addentity.js")); | ||||
man.InitSystemEntity(); | man.InitSystemEntity(); | ||||
entity_id_t ent1 = man.AllocateNewEntity(); | entity_id_t ent1 = man.AllocateNewEntity(); | ||||
entity_id_t ent2 = man.AllocateNewLocalEntity() + 2; | entity_id_t ent2 = man.AllocateNewLocalEntity() + 2; | ||||
CEntityHandle hnd1 = man.AllocateEntityHandle(ent1); | CEntityHandle hnd1 = man.AllocateEntityHandle(ent1); | ||||
CParamNode noParam; | CParamNode noParam; | ||||
TS_ASSERT(man.AddComponent(man.GetSystemEntity(), CID_TemplateManager, noParam)); | man.AddConstructedComponent(man.GetSystemEntity(), templateMan, noParam); | ||||
TS_ASSERT(man.AddComponent(hnd1, man.LookupCID("TestScript1_AddLocalEntity"), noParam)); | TS_ASSERT(man.AddComponent(hnd1, man.LookupCID("TestScript1_AddLocalEntity"), noParam)); | ||||
TS_ASSERT(man.QueryInterface(ent2, IID_Test1) == NULL); | TS_ASSERT(man.QueryInterface(ent2, IID_Test1) == NULL); | ||||
TS_ASSERT(man.QueryInterface(ent2, IID_Test2) == NULL); | TS_ASSERT(man.QueryInterface(ent2, IID_Test2) == NULL); | ||||
{ | { | ||||
TestLogger logger; // ignore bogus-template warnings | TestLogger logger; // ignore bogus-template warnings | ||||
▲ Show 20 Lines • Show All 366 Lines • ▼ Show 20 Lines | void test_script_serialization_errors() | ||||
std::stringstream stateStream; | std::stringstream stateStream; | ||||
TS_ASSERT_THROWS_PSERROR(man.SerializeState(stateStream), PSERROR_Serialize_ScriptError, "Cannot serialize property getters"); | TS_ASSERT_THROWS_PSERROR(man.SerializeState(stateStream), PSERROR_Serialize_ScriptError, "Cannot serialize property getters"); | ||||
// (The script will die if the getter is executed) | // (The script will die if the getter is executed) | ||||
} | } | ||||
void test_script_serialization_template() | void test_script_serialization_template() | ||||
{ | { | ||||
CSimContext context; | CSimContext context; | ||||
CCmpTemplateManager templateMan; | |||||
CComponentManager man(context, g_ScriptContext); | CComponentManager man(context, g_ScriptContext); | ||||
man.LoadComponentTypes(); | man.LoadComponentTypes(); | ||||
TS_ASSERT(man.LoadScript(L"simulation/components/test-serialize.js")); | TS_ASSERT(man.LoadScript(L"simulation/components/test-serialize.js")); | ||||
man.InitSystemEntity(); | man.InitSystemEntity(); | ||||
entity_id_t ent2 = 2; | entity_id_t ent2 = 2; | ||||
CEntityHandle hnd2 = man.AllocateEntityHandle(ent2); | CEntityHandle hnd2 = man.AllocateEntityHandle(ent2); | ||||
CParamNode noParam; | CParamNode noParam; | ||||
// The template manager takes care of reloading templates on deserialization, | // The template manager takes care of reloading templates on deserialization, | ||||
// so we need to use it here | // so we need to use it here | ||||
TS_ASSERT(man.AddComponent(man.GetSystemEntity(), CID_TemplateManager, noParam)); | man.AddConstructedComponent(man.GetSystemEntity(), templateMan, noParam); | ||||
ICmpTemplateManager* tempMan = static_cast<ICmpTemplateManager*> (man.QueryInterface(SYSTEM_ENTITY, IID_TemplateManager)); | ICmpTemplateManager* tempMan = static_cast<ICmpTemplateManager*> (man.QueryInterface(SYSTEM_ENTITY, IID_TemplateManager)); | ||||
const CParamNode* testParam = tempMan->LoadTemplate(ent2, "template-serialize"); | const CParamNode* testParam = tempMan->LoadTemplate(ent2, "template-serialize"); | ||||
man.AddComponent(hnd2, man.LookupCID("TestScript1_consts"), testParam->GetChild("TestScript1_consts")); | man.AddComponent(hnd2, man.LookupCID("TestScript1_consts"), testParam->GetChild("TestScript1_consts")); | ||||
TS_ASSERT_EQUALS(static_cast<ICmpTest1*> (man.QueryInterface(ent2, IID_Test1))->GetX(), 12347); | TS_ASSERT_EQUALS(static_cast<ICmpTest1*> (man.QueryInterface(ent2, IID_Test1))->GetX(), 12347); | ||||
Show All 34 Lines |
Wildfire Games · Phabricator