From a80c70ae4237a6723c9c7bcd0902bad2066dc582 Mon Sep 17 00:00:00 2001 From: Segfault <5221072+Segfaultd@users.noreply.github.com> Date: Fri, 2 Feb 2024 23:43:25 +0100 Subject: [PATCH 1/2] Make sure we have an initialized engine. Added UT --- code/framework/src/scripting/module.cpp | 6 ++++++ code/tests/modules/scripting_engine_ut.h | 6 ++++++ 2 files changed, 12 insertions(+) diff --git a/code/framework/src/scripting/module.cpp b/code/framework/src/scripting/module.cpp index a70f324cd..94c1a1b61 100644 --- a/code/framework/src/scripting/module.cpp +++ b/code/framework/src/scripting/module.cpp @@ -31,6 +31,12 @@ namespace Framework::Scripting { default: break; } + + // Make sure we got a valid pointer + if (!_engine) { + return ModuleError::MODULE_ENGINE_NULL; + } + _engine->SetModName(_modName); _engineType = engineType; diff --git a/code/tests/modules/scripting_engine_ut.h b/code/tests/modules/scripting_engine_ut.h index 165bd48e4..b16f06bba 100644 --- a/code/tests/modules/scripting_engine_ut.h +++ b/code/tests/modules/scripting_engine_ut.h @@ -14,6 +14,12 @@ MODULE(scripting_engine, { using namespace Framework::Scripting; + IT("should not allow to initialize the engine with an invalid type", { + Module *pEngine = new Module; + EQUALS(pEngine->Init(static_cast(-1), NULL), ModuleError::MODULE_ENGINE_NULL); + delete pEngine; + }); + IT("can allocate and deallocate a valid scripting engine instance", { Module *pEngine = new Module; From 4cdd897bec579d6aa62ada1d46bf0cab0d961c40 Mon Sep 17 00:00:00 2001 From: Segfault <5221072+Segfaultd@users.noreply.github.com> Date: Fri, 2 Feb 2024 23:47:22 +0100 Subject: [PATCH 2/2] Improve scripting module UT --- code/framework/src/scripting/module.cpp | 12 +++++++++++ code/tests/framework_ut.cpp | 4 ++-- ...ting_engine_ut.h => scripting_module_ut.h} | 20 ++++++++++++++++++- 3 files changed, 33 insertions(+), 3 deletions(-) rename code/tests/modules/{scripting_engine_ut.h => scripting_module_ut.h} (69%) diff --git a/code/framework/src/scripting/module.cpp b/code/framework/src/scripting/module.cpp index 94c1a1b61..2ecea226a 100644 --- a/code/framework/src/scripting/module.cpp +++ b/code/framework/src/scripting/module.cpp @@ -79,6 +79,12 @@ namespace Framework::Scripting { } bool Module::LoadGamemode() const { + // Make sure there is an engine + if (!_engine) { + return false; + } + + // Load the gamemode const cppfs::FileHandle dir = cppfs::fs::open("gamemode"); if (!dir.exists() || !dir.isDirectory()) { Logging::GetLogger(FRAMEWORK_INNER_SCRIPTING)->error("Failed to find the gamemode directory"); @@ -89,6 +95,12 @@ namespace Framework::Scripting { } bool Module::UnloadGamemode() const { + // Make sure there is an engine + if (!_engine) { + return false; + } + + // Unload the gamemode return _engine->UnloadGamemode("gamemode"); } } // namespace Framework::Scripting diff --git a/code/tests/framework_ut.cpp b/code/tests/framework_ut.cpp index df5896eff..89530bb4d 100644 --- a/code/tests/framework_ut.cpp +++ b/code/tests/framework_ut.cpp @@ -12,7 +12,7 @@ /* TEST CATEGORIES */ #include "modules/interpolator_ut.h" -#include "modules/scripting_engine_ut.h" +#include "modules/scripting_module_ut.h" int main() { UNIT_CREATE("FrameworkTests"); @@ -20,7 +20,7 @@ int main() { Framework::Logging::GetInstance()->PauseLogging(true); UNIT_MODULE(interpolator); - UNIT_MODULE(scripting_engine); + UNIT_MODULE(scripting_module); return UNIT_RUN(); } diff --git a/code/tests/modules/scripting_engine_ut.h b/code/tests/modules/scripting_module_ut.h similarity index 69% rename from code/tests/modules/scripting_engine_ut.h rename to code/tests/modules/scripting_module_ut.h index b16f06bba..c6d4c9f2d 100644 --- a/code/tests/modules/scripting_engine_ut.h +++ b/code/tests/modules/scripting_module_ut.h @@ -11,7 +11,7 @@ #include "scripting/engines/node/engine.h" #include "scripting/module.h" -MODULE(scripting_engine, { +MODULE(scripting_module, { using namespace Framework::Scripting; IT("should not allow to initialize the engine with an invalid type", { @@ -20,6 +20,24 @@ MODULE(scripting_engine, { delete pEngine; }); + IT("should not allow to shutdown if the engine is not initialized", { + Module *pEngine = new Module; + EQUALS(pEngine->Shutdown(), ModuleError::MODULE_ENGINE_NULL); + delete pEngine; + }); + + IT("should not allow to load gamemode if the engine is not initialized", { + Module *pEngine = new Module; + EQUALS(pEngine->LoadGamemode(), false); + delete pEngine; + }); + + IT("should not allow to unload gamemode if the engine is not initialized", { + Module *pEngine = new Module; + EQUALS(pEngine->UnloadGamemode(), false); + delete pEngine; + }); + IT("can allocate and deallocate a valid scripting engine instance", { Module *pEngine = new Module;