PkmnLib/tests/ScriptTests/Angelscript/ScriptResolverTests.cpp

128 lines
4.8 KiB
C++
Raw Permalink Normal View History

#if TESTS_BUILD && ANGELSCRIPT
2022-02-11 11:17:27 +00:00
#include <doctest.h>
2022-05-14 14:50:20 +00:00
#include "../../TestLibrary/TestLibrary.hpp"
#include "../../src/ScriptResolving/AngelScript/AngelScriptResolver.hpp"
#include "../../src/ScriptResolving/AngelScript/ContextPool.hpp"
static std::unordered_map<const char*, const char*> _scripts =
std::unordered_map<const char*, const char*>{{"testScript1", R"(
namespace Pokemon{
[Pokemon effect=testScript1]
2021-01-22 13:11:03 +00:00
class testScript1 : PkmnScript {
int add(int a, int b) {
return a + b;
}
void testFunc2(const Species@ s){
if (s.Name != "testSpecies2"){
throw("err");
}
}
2021-01-22 13:11:03 +00:00
void OnInitialize(const BattleLibrary@ library, const narray<EffectParameter@>@ parameters) override { }
}
}
)"}};
TEST_CASE("Get a script resolver, initialize it, then delete it") {
auto lib = PkmnLib::Battling::BattleLibrary::CreateScriptResolver();
lib->Initialize(TestLibrary::GetLibrary());
delete lib;
}
TEST_CASE("Get a script resolver, set script load function, load script, then build module") {
auto lib = dynamic_cast<AngelScriptResolver*>(PkmnLib::Battling::BattleLibrary::CreateScriptResolver());
lib->Initialize(TestLibrary::GetLibrary());
lib->CreateScript("testScript1", _scripts["testScript1"]);
lib->FinalizeModule();
delete lib;
}
TEST_CASE("Build script resolver, then create object") {
auto lib = dynamic_cast<AngelScriptResolver*>(PkmnLib::Battling::BattleLibrary::CreateScriptResolver());
lib->Initialize(TestLibrary::GetLibrary());
lib->CreateScript("testScript1", _scripts["testScript1"]);
lib->FinalizeModule();
2022-05-14 14:50:20 +00:00
auto obj = lib->LoadScript(nullptr, ScriptCategory::Creature, "testScript1"_cnc).TakeOwnership();
delete obj;
delete lib;
}
TEST_CASE("Build script resolver, create object, invoke addition method") {
auto lib = dynamic_cast<AngelScriptResolver*>(PkmnLib::Battling::BattleLibrary::CreateScriptResolver());
lib->Initialize(TestLibrary::GetLibrary());
lib->CreateScript("testScript1", _scripts["testScript1"]);
lib->FinalizeModule();
2022-05-14 14:50:20 +00:00
auto obj = dynamic_cast<AngelScriptScript*>(
lib->LoadScript(nullptr, ScriptCategory::Creature, "testScript1"_cnc).TakeOwnership());
REQUIRE(obj != nullptr);
auto ctxPool = obj->GetContextPool();
auto ctx = ctxPool->RequestContext();
auto func = obj->PrepareMethod("add"_cnc, ctx);
REQUIRE(func != nullptr);
ctx->SetArgDWord(0, 5);
ctx->SetArgDWord(1, 100);
auto result = ctx->Execute();
if (result == asEXECUTION_EXCEPTION) {
FAIL(ctx->GetExceptionString());
}
CHECK(result == asEXECUTION_FINISHED);
auto returnValue = ctx->GetReturnDWord();
REQUIRE(returnValue == 105);
ctxPool->ReturnContextToPool(ctx);
delete obj;
delete lib;
}
TEST_CASE("Get a script resolver, save the byte code to memory, create new script resolver from it") {
auto originLib = dynamic_cast<AngelScriptResolver*>(PkmnLib::Battling::BattleLibrary::CreateScriptResolver());
originLib->Initialize(TestLibrary::GetLibrary());
originLib->CreateScript("testScript1", _scripts["testScript1"]);
originLib->FinalizeModule();
2022-05-14 14:50:20 +00:00
auto obj = dynamic_cast<AngelScriptScript*>(
originLib->LoadScript(nullptr, ScriptCategory::Creature, "testScript1"_cnc).TakeOwnership());
2021-01-22 13:11:03 +00:00
REQUIRE(obj != nullptr);
REQUIRE(obj->GetType()->GetOnInitialize().Exists);
delete obj;
size_t size;
auto byteCode = originLib->WriteByteCodeToMemory(size);
auto newLib = dynamic_cast<AngelScriptResolver*>(PkmnLib::Battling::BattleLibrary::CreateScriptResolver());
newLib->Initialize(TestLibrary::GetLibrary());
newLib->LoadByteCodeFromMemory(byteCode, size);
2022-05-14 14:50:20 +00:00
obj = dynamic_cast<AngelScriptScript*>(
newLib->LoadScript(nullptr, ScriptCategory::Creature, "testScript1"_cnc).TakeOwnership());
REQUIRE(obj != nullptr);
2021-01-22 13:11:03 +00:00
REQUIRE(obj->GetType()->GetOnInitialize().Exists);
delete obj;
delete originLib;
delete newLib;
free(byteCode);
}
TEST_CASE("Get a script resolver, save the byte code to file, create new script resolver from it") {
auto originLib = dynamic_cast<AngelScriptResolver*>(PkmnLib::Battling::BattleLibrary::CreateScriptResolver());
originLib->Initialize(TestLibrary::GetLibrary());
originLib->CreateScript("testScript1", _scripts["testScript1"]);
originLib->FinalizeModule();
originLib->WriteByteCodeToFile("foo.bin");
auto newLib = dynamic_cast<AngelScriptResolver*>(PkmnLib::Battling::BattleLibrary::CreateScriptResolver());
newLib->Initialize(TestLibrary::GetLibrary());
newLib->LoadByteCodeFromFile("foo.bin");
2022-05-14 14:50:20 +00:00
auto obj = dynamic_cast<AngelScriptScript*>(
newLib->LoadScript(nullptr, ScriptCategory::Creature, "testScript1"_cnc).TakeOwnership());
REQUIRE(obj != nullptr);
delete obj;
delete originLib;
delete newLib;
2020-05-02 09:55:04 +00:00
remove("foo.bin");
}
#endif