0ad/source/simulation2/tests/test_Simulation2.h
bb 157c6af18e Make the space in 0 A.D. non-breaking throughout the codebase.
Avoid cases of filenames
Update years in terms and other legal(ish) documents
Don't update years in license headers, since change is not meaningful

Will add linter rule in seperate commit

Happy recompiling everyone!

Original Patch By: Nescio
Comment By: Gallaecio
Differential Revision: D2620
This was SVN commit r27786.
2023-07-27 20:54:46 +00:00

171 lines
6.5 KiB
C++
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/* Copyright (C) 2021 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* 0 A.D. is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
*/
#include "lib/self_test.h"
#include "scriptinterface/ScriptInterface.h"
#include "simulation2/Simulation2.h"
#include "simulation2/MessageTypes.h"
#include "simulation2/components/ICmpTest.h"
#include "graphics/Terrain.h"
#include "ps/CLogger.h"
#include "ps/Filesystem.h"
#include "ps/XML/Xeromyces.h"
class TestSimulation2 : public CxxTest::TestSuite
{
void copyFile(const VfsPath& src, const VfsPath& dst)
{
std::shared_ptr<u8> data;
size_t size = 0;
TS_ASSERT_OK(g_VFS->LoadFile(src, data, size));
TS_ASSERT_OK(g_VFS->CreateFile(dst, data, size));
}
CTerrain m_Terrain;
public:
void setUp()
{
g_VFS = CreateVfs();
TS_ASSERT_OK(g_VFS->Mount(L"", DataDir() / "mods" / "_test.sim" / "", VFS_MOUNT_MUST_EXIST));
TS_ASSERT_OK(g_VFS->Mount(L"cache", DataDir() / "_testcache" / "", 0, VFS_MAX_PRIORITY));
CXeromyces::Startup();
}
void tearDown()
{
CXeromyces::Terminate();
g_VFS.reset();
DeleteDirectory(DataDir()/"_testcache");
}
void test_AddEntity()
{
CSimulation2 sim(NULL, g_ScriptContext, &m_Terrain);
TS_ASSERT(sim.LoadScripts(L"simulation/components/addentity/"));
sim.ResetState(true, true);
entity_id_t ent1 = sim.AddEntity(L"test1");
TS_ASSERT_EQUALS(ent1, (u32)2);
TS_ASSERT_EQUALS(static_cast<ICmpTest1*> (sim.QueryInterface(ent1, IID_Test1))->GetX(), 999);
TS_ASSERT_EQUALS(static_cast<ICmpTest2*> (sim.QueryInterface(ent1, IID_Test2))->GetX(), 12345);
entity_id_t ent2 = sim.AddEntity(L"test1-inherit");
TS_ASSERT_EQUALS(ent2, (u32)3);
TS_ASSERT_EQUALS(static_cast<ICmpTest1*> (sim.QueryInterface(ent2, IID_Test1))->GetX(), 1234);
TS_ASSERT_EQUALS(static_cast<ICmpTest2*> (sim.QueryInterface(ent2, IID_Test2))->GetX(), 12345);
}
void test_DestroyEntity()
{
CSimulation2 sim(NULL, g_ScriptContext, &m_Terrain);
TS_ASSERT(sim.LoadScripts(L"simulation/components/addentity/"));
sim.ResetState(true, true);
entity_id_t ent1 = sim.AddEntity(L"test1");
entity_id_t ent2 = sim.AddEntity(L"test1");
entity_id_t ent3 = sim.AddEntity(L"test1");
TS_ASSERT_EQUALS(static_cast<ICmpTest1*> (sim.QueryInterface(ent1, IID_Test1))->GetX(), 999);
TS_ASSERT_EQUALS(static_cast<ICmpTest2*> (sim.QueryInterface(ent1, IID_Test2))->GetX(), 12345);
TS_ASSERT_EQUALS(static_cast<ICmpTest1*> (sim.QueryInterface(ent2, IID_Test1))->GetX(), 999);
TS_ASSERT_EQUALS(static_cast<ICmpTest2*> (sim.QueryInterface(ent2, IID_Test2))->GetX(), 12345);
TS_ASSERT_EQUALS(static_cast<ICmpTest1*> (sim.QueryInterface(ent3, IID_Test1))->GetX(), 999);
TS_ASSERT_EQUALS(static_cast<ICmpTest2*> (sim.QueryInterface(ent3, IID_Test2))->GetX(), 12345);
sim.DestroyEntity(ent2); // mark it for deletion
TS_ASSERT_EQUALS(static_cast<ICmpTest1*> (sim.QueryInterface(ent2, IID_Test1))->GetX(), 999);
TS_ASSERT_EQUALS(static_cast<ICmpTest2*> (sim.QueryInterface(ent2, IID_Test2))->GetX(), 12345);
sim.FlushDestroyedEntities(); // actually delete it
TS_ASSERT(sim.QueryInterface(ent2, IID_Test1) == NULL);
TS_ASSERT(sim.QueryInterface(ent2, IID_Test2) == NULL);
sim.FlushDestroyedEntities(); // nothing in the queue
// Allow destroying INVALID_ENTITY which should do nothing.
sim.DestroyEntity(INVALID_ENTITY);
sim.FlushDestroyedEntities();
sim.DestroyEntity(ent2);
sim.FlushDestroyedEntities(); // already deleted
// Other entities weren't affected
TS_ASSERT_EQUALS(static_cast<ICmpTest1*> (sim.QueryInterface(ent1, IID_Test1))->GetX(), 999);
TS_ASSERT_EQUALS(static_cast<ICmpTest2*> (sim.QueryInterface(ent1, IID_Test2))->GetX(), 12345);
TS_ASSERT_EQUALS(static_cast<ICmpTest1*> (sim.QueryInterface(ent3, IID_Test1))->GetX(), 999);
TS_ASSERT_EQUALS(static_cast<ICmpTest2*> (sim.QueryInterface(ent3, IID_Test2))->GetX(), 12345);
sim.DestroyEntity(ent3); // mark it for deletion twice
sim.DestroyEntity(ent3);
sim.FlushDestroyedEntities();
TS_ASSERT(sim.QueryInterface(ent3, IID_Test1) == NULL);
TS_ASSERT(sim.QueryInterface(ent3, IID_Test2) == NULL);
// Messages mustn't get sent to the destroyed components (else we'll crash)
CMessageTurnStart msg;
sim.BroadcastMessage(msg);
}
void test_hotload_scripts()
{
CSimulation2 sim(NULL, g_ScriptContext, &m_Terrain);
TS_ASSERT_OK(CreateDirectories(DataDir()/"mods"/"_test.sim"/"simulation"/"components"/"hotload"/"", 0700));
copyFile(L"simulation/components/test-hotload1.js", L"simulation/components/hotload/hotload.js");
TS_ASSERT_OK(g_VFS->RemoveFile(L"simulation/components/hotload/hotload.js"));
TS_ASSERT_OK(g_VFS->RepopulateDirectory(L"simulation/components/hotload/"));
TS_ASSERT(sim.LoadScripts(L"simulation/components/hotload/"));
sim.ResetState(true, true);
entity_id_t ent = sim.AddEntity(L"hotload");
TS_ASSERT_EQUALS(static_cast<ICmpTest1*> (sim.QueryInterface(ent, IID_Test1))->GetX(), 100);
copyFile(L"simulation/components/test-hotload2.js", L"simulation/components/hotload/hotload.js");
TS_ASSERT_OK(g_VFS->RemoveFile(L"simulation/components/hotload/hotload.js"));
TS_ASSERT_OK(g_VFS->RepopulateDirectory(L"simulation/components/hotload/"));
TS_ASSERT_OK(sim.ReloadChangedFile(L"art/irrelevant.xml"));
TS_ASSERT_OK(sim.ReloadChangedFile(L"simulation/components/irrelevant.js"));
TS_ASSERT_OK(sim.ReloadChangedFile(L"simulation/components/hotload/nonexistent.js"));
TS_ASSERT_EQUALS(static_cast<ICmpTest1*> (sim.QueryInterface(ent, IID_Test1))->GetX(), 100);
TS_ASSERT_OK(sim.ReloadChangedFile(L"simulation/components/hotload/hotload.js"));
TS_ASSERT_EQUALS(static_cast<ICmpTest1*> (sim.QueryInterface(ent, IID_Test1))->GetX(), 1000);
TS_ASSERT_OK(DeleteDirectory(DataDir()/"mods"/"_test.sim"/"simulation"/"components"/"hotload"/""));
TS_ASSERT_OK(g_VFS->RemoveFile(L"simulation/components/hotload/hotload.js"));
TS_ASSERT_OK(g_VFS->RepopulateDirectory(L"simulation/components/hotload/"));
TS_ASSERT_OK(sim.ReloadChangedFile(L"simulation/components/hotload/hotload.js"));
}
};