diff --git a/tests/ScriptTests/ScriptTypeTests/SpeciesTests.cpp b/tests/ScriptTests/ScriptTypeTests/SpeciesTests.cpp new file mode 100644 index 0000000..b445187 --- /dev/null +++ b/tests/ScriptTests/ScriptTypeTests/SpeciesTests.cpp @@ -0,0 +1,144 @@ +#ifdef TESTS_BUILD +#include "../../../extern/catch.hpp" +#include "../../../src/AngelScript/AngelScripResolver.hpp" +#include "../../TestLibrary/TestLibrary.hpp" + +static std::unordered_map _scripts = + std::unordered_map{{"testScript1", R"( +class testScript1 { + bool testName(const Species@ s, const string &in name){ return s.Name == name; } + bool testId(const Species@ s, uint16 id){ return s.Id == id; } + bool testGenderRate(const Species@ s, float rate){ return s.GenderRate == rate; } + bool testCaptureRate(const Species@ s, uint8 rate){ return s.CaptureRate == rate; } +} +)"}}; + +static const char* _testLoadFunc(const char* name) { return _scripts[name]; } + +TEST_CASE("Validate Species Name in Script") { + auto lib = dynamic_cast(PkmnLib::Battling::BattleLibrary::CreateScriptResolver()); + auto mainLib = TestLibrary::GetLibrary(); + lib->Initialize(mainLib); + lib->SetCreateFunction(&_testLoadFunc); + lib->CreateScript("testScript1"); + lib->FinalizeModule(); + auto species = mainLib->GetSpeciesLibrary()->GetPkmnSpecies("testSpecies2"); + + auto obj = + dynamic_cast(lib->LoadScript(AngelScripResolver::ScriptCategory::Creature, "testScript1")); + auto ctxPool = obj->GetContextPool(); + auto ctx = ctxPool->RequestContext(); + + auto func = obj->PrepareMethod("testName", ctx); + REQUIRE(func != nullptr); + + ctx->SetArgObject( + 0, const_cast(species)); + auto name = species->GetName(); + ctx->SetArgAddress(1, &name); + + auto result = ctx->Execute(); + REQUIRE(result == asEXECUTION_FINISHED); + auto v = (bool)ctx->GetReturnWord(); + REQUIRE(v); + + ctxPool->ReturnContextToPool(ctx); + delete obj; + delete lib; +} + +TEST_CASE("Validate Species Id in Script") { + auto lib = dynamic_cast(PkmnLib::Battling::BattleLibrary::CreateScriptResolver()); + auto mainLib = TestLibrary::GetLibrary(); + lib->Initialize(mainLib); + lib->SetCreateFunction(&_testLoadFunc); + lib->CreateScript("testScript1"); + lib->FinalizeModule(); + auto species = mainLib->GetSpeciesLibrary()->GetPkmnSpecies("testSpecies2"); + + auto obj = + dynamic_cast(lib->LoadScript(AngelScripResolver::ScriptCategory::Creature, "testScript1")); + auto ctxPool = obj->GetContextPool(); + auto ctx = ctxPool->RequestContext(); + + auto func = obj->PrepareMethod("testId", ctx); + REQUIRE(func != nullptr); + + ctx->SetArgObject( + 0, const_cast(species)); + ctx->SetArgWord(1, 2); + + auto result = ctx->Execute(); + REQUIRE(result == asEXECUTION_FINISHED); + auto v = (bool)ctx->GetReturnWord(); + REQUIRE(v); + + ctxPool->ReturnContextToPool(ctx); + delete obj; + delete lib; +} + +TEST_CASE("Validate Species Gender Rate in Script") { + auto lib = dynamic_cast(PkmnLib::Battling::BattleLibrary::CreateScriptResolver()); + auto mainLib = TestLibrary::GetLibrary(); + lib->Initialize(mainLib); + lib->SetCreateFunction(&_testLoadFunc); + lib->CreateScript("testScript1"); + lib->FinalizeModule(); + auto species = mainLib->GetSpeciesLibrary()->GetPkmnSpecies("testSpecies2"); + + auto obj = + dynamic_cast(lib->LoadScript(AngelScripResolver::ScriptCategory::Creature, "testScript1")); + auto ctxPool = obj->GetContextPool(); + auto ctx = ctxPool->RequestContext(); + + auto func = obj->PrepareMethod("testGenderRate", ctx); + REQUIRE(func != nullptr); + + ctx->SetArgObject( + 0, const_cast(species)); + ctx->SetArgFloat(1, 0.5); + + auto result = ctx->Execute(); + REQUIRE(result == asEXECUTION_FINISHED); + auto v = (bool)ctx->GetReturnWord(); + REQUIRE(v); + + ctxPool->ReturnContextToPool(ctx); + delete obj; + delete lib; +} + +TEST_CASE("Validate Species Capture Rate in Script") { + auto lib = dynamic_cast(PkmnLib::Battling::BattleLibrary::CreateScriptResolver()); + auto mainLib = TestLibrary::GetLibrary(); + lib->Initialize(mainLib); + lib->SetCreateFunction(&_testLoadFunc); + lib->CreateScript("testScript1"); + lib->FinalizeModule(); + auto species = mainLib->GetSpeciesLibrary()->GetPkmnSpecies("testSpecies2"); + + auto obj = + dynamic_cast(lib->LoadScript(AngelScripResolver::ScriptCategory::Creature, "testScript1")); + auto ctxPool = obj->GetContextPool(); + auto ctx = ctxPool->RequestContext(); + + auto func = obj->PrepareMethod("testCaptureRate", ctx); + REQUIRE(func != nullptr); + + ctx->SetArgObject( + 0, const_cast(species)); + ctx->SetArgByte(1, species->GetCaptureRate()); + + auto result = ctx->Execute(); + REQUIRE(result == asEXECUTION_FINISHED); + auto v = (bool)ctx->GetReturnWord(); + REQUIRE(v); + + ctxPool->ReturnContextToPool(ctx); + delete obj; + delete lib; +} + + +#endif \ No newline at end of file