PkmnLib/tests/ScriptTests/ScriptResolverTests.cpp

129 lines
4.1 KiB
C++

#ifdef TESTS_BUILD
#include "../../extern/catch.hpp"
#include "../../src/ScriptResolving/AngelScript/AngelScripResolver.hpp"
#include "../TestLibrary/TestLibrary.hpp"
static std::unordered_map<const char*, const char*> _scripts =
std::unordered_map<const char*, const char*>{
{"testScript1", R"(
class testScript1 {
int add(int a, int b) {
return a + b;
}
void testFunc2(const Species@ s){
if (s.Name != "testSpecies2"){
throw("err");
}
}
}
)"}
};
static const char* _testLoadFunc(const char* name) { return _scripts[name]; }
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, then delete it") {
auto lib = dynamic_cast<AngelScripResolver*>(PkmnLib::Battling::BattleLibrary::CreateScriptResolver());
lib->Initialize(TestLibrary::GetLibrary());
lib->SetCreateFunction(&_testLoadFunc);
delete lib;
}
TEST_CASE("Get a script resolver, set script load function, load script, then build module") {
auto lib = dynamic_cast<AngelScripResolver*>(PkmnLib::Battling::BattleLibrary::CreateScriptResolver());
lib->Initialize(TestLibrary::GetLibrary());
lib->SetCreateFunction(&_testLoadFunc);
lib->CreateScript("testScript1");
lib->FinalizeModule();
delete lib;
}
TEST_CASE("Build script resolver, then create object") {
auto lib = dynamic_cast<AngelScripResolver*>(PkmnLib::Battling::BattleLibrary::CreateScriptResolver());
lib->Initialize(TestLibrary::GetLibrary());
lib->SetCreateFunction(&_testLoadFunc);
lib->CreateScript("testScript1");
lib->FinalizeModule();
auto obj = lib->LoadScript(AngelScripResolver::ScriptCategory::Creature, "testScript1");
delete obj;
delete lib;
}
TEST_CASE("Build script resolver, create object, invoke addition method") {
auto lib = dynamic_cast<AngelScripResolver*>(PkmnLib::Battling::BattleLibrary::CreateScriptResolver());
lib->Initialize(TestLibrary::GetLibrary());
lib->SetCreateFunction(&_testLoadFunc);
lib->CreateScript("testScript1");
lib->FinalizeModule();
auto obj =
dynamic_cast<AngelScriptScript*>(lib->LoadScript(AngelScripResolver::ScriptCategory::Creature, "testScript1"));
auto ctxPool = obj->GetContextPool();
auto ctx = ctxPool->RequestContext();
auto func = obj->PrepareMethod("add", 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("Test whether species object was properly registered.") {
auto lib = dynamic_cast<AngelScripResolver*>(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<AngelScriptScript*>(lib->LoadScript(AngelScripResolver::ScriptCategory::Creature, "testScript1"));
auto ctxPool = obj->GetContextPool();
auto ctx = ctxPool->RequestContext();
obj->PrepareMethod("testFunc2", ctx);
ctx->SetArgObject(0, const_cast<PkmnLib::Library::PokemonSpecies*>(species));
auto result = ctx->Execute();
if (result == asEXECUTION_EXCEPTION){
FAIL(ctx->GetExceptionString());
}
ctx->Unprepare();
species = mainLib->GetSpeciesLibrary()->GetPkmnSpecies("testSpecies");
obj->PrepareMethod("testFunc2", ctx);
ctx->SetArgObject(0, const_cast<PkmnLib::Library::PokemonSpecies*>(species));
result = ctx->Execute();
REQUIRE(result == asEXECUTION_EXCEPTION);
ctxPool->ReturnContextToPool(ctx);
delete obj;
delete lib;
}
#endif