2020-01-11 21:30:23 +00:00
|
|
|
#ifdef TESTS_BUILD
|
2020-09-29 16:04:06 +00:00
|
|
|
#include "../../extern/doctest.hpp"
|
2020-04-23 22:05:43 +00:00
|
|
|
#include "../../src/ScriptResolving/AngelScript/AngelScriptResolver.hpp"
|
2021-09-22 17:02:01 +00:00
|
|
|
#include "../../src/ScriptResolving/AngelScript/ContextPool.hpp"
|
2020-01-11 21:30:23 +00:00
|
|
|
#include "../TestLibrary/TestLibrary.hpp"
|
|
|
|
|
|
|
|
static std::unordered_map<const char*, const char*> _scripts =
|
2020-02-13 12:59:07 +00:00
|
|
|
std::unordered_map<const char*, const char*>{{"testScript1", R"(
|
2020-02-16 13:38:05 +00:00
|
|
|
namespace Pokemon{
|
2020-04-10 12:57:20 +00:00
|
|
|
[Pokemon effect=testScript1]
|
2021-01-22 13:11:03 +00:00
|
|
|
class testScript1 : PkmnScript {
|
2020-01-11 21:30:23 +00:00
|
|
|
int add(int a, int b) {
|
|
|
|
return a + b;
|
|
|
|
}
|
|
|
|
|
2020-01-12 18:30:44 +00:00
|
|
|
void testFunc2(const Species@ s){
|
2020-01-11 21:30:23 +00:00
|
|
|
if (s.Name != "testSpecies2"){
|
|
|
|
throw("err");
|
|
|
|
}
|
|
|
|
}
|
2021-01-22 13:11:03 +00:00
|
|
|
|
2021-09-25 15:56:31 +00:00
|
|
|
void OnInitialize(const narray<EffectParameter@>@ parameters) override{ }
|
2020-01-11 21:30:23 +00:00
|
|
|
}
|
2020-02-13 12:59:07 +00:00
|
|
|
}
|
2020-02-16 13:38:05 +00:00
|
|
|
)"}};
|
2020-01-11 21:30:23 +00:00
|
|
|
|
|
|
|
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") {
|
2020-04-23 22:05:43 +00:00
|
|
|
auto lib = dynamic_cast<AngelScriptResolver*>(PkmnLib::Battling::BattleLibrary::CreateScriptResolver());
|
2020-01-11 21:30:23 +00:00
|
|
|
lib->Initialize(TestLibrary::GetLibrary());
|
2020-04-12 08:17:15 +00:00
|
|
|
lib->CreateScript("testScript1", _scripts["testScript1"]);
|
2020-01-11 21:30:23 +00:00
|
|
|
lib->FinalizeModule();
|
|
|
|
delete lib;
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("Build script resolver, then create object") {
|
2020-04-23 22:05:43 +00:00
|
|
|
auto lib = dynamic_cast<AngelScriptResolver*>(PkmnLib::Battling::BattleLibrary::CreateScriptResolver());
|
2020-01-11 21:30:23 +00:00
|
|
|
lib->Initialize(TestLibrary::GetLibrary());
|
2020-04-12 08:17:15 +00:00
|
|
|
lib->CreateScript("testScript1", _scripts["testScript1"]);
|
2020-01-11 21:30:23 +00:00
|
|
|
lib->FinalizeModule();
|
|
|
|
|
2020-02-28 18:51:50 +00:00
|
|
|
auto obj = lib->LoadScript(ScriptCategory::Creature, "testScript1"_cnc);
|
2020-01-11 21:30:23 +00:00
|
|
|
|
|
|
|
delete obj;
|
|
|
|
delete lib;
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("Build script resolver, create object, invoke addition method") {
|
2020-04-23 22:05:43 +00:00
|
|
|
auto lib = dynamic_cast<AngelScriptResolver*>(PkmnLib::Battling::BattleLibrary::CreateScriptResolver());
|
2020-01-11 21:30:23 +00:00
|
|
|
lib->Initialize(TestLibrary::GetLibrary());
|
2020-04-12 08:17:15 +00:00
|
|
|
lib->CreateScript("testScript1", _scripts["testScript1"]);
|
2020-01-11 21:30:23 +00:00
|
|
|
lib->FinalizeModule();
|
|
|
|
|
2020-04-12 08:17:15 +00:00
|
|
|
auto obj = dynamic_cast<AngelScriptScript*>(lib->LoadScript(ScriptCategory::Creature, "testScript1"_cnc));
|
2020-04-10 12:57:20 +00:00
|
|
|
REQUIRE(obj != nullptr);
|
2020-01-11 21:30:23 +00:00
|
|
|
auto ctxPool = obj->GetContextPool();
|
|
|
|
auto ctx = ctxPool->RequestContext();
|
|
|
|
|
2020-04-06 18:03:44 +00:00
|
|
|
auto func = obj->PrepareMethod("add"_cnc, ctx);
|
2020-01-11 21:30:23 +00:00
|
|
|
REQUIRE(func != nullptr);
|
|
|
|
|
|
|
|
ctx->SetArgDWord(0, 5);
|
|
|
|
ctx->SetArgDWord(1, 100);
|
|
|
|
|
|
|
|
auto result = ctx->Execute();
|
2020-02-13 12:59:07 +00:00
|
|
|
if (result == asEXECUTION_EXCEPTION) {
|
2020-01-11 21:30:23 +00:00
|
|
|
FAIL(ctx->GetExceptionString());
|
|
|
|
}
|
|
|
|
CHECK(result == asEXECUTION_FINISHED);
|
|
|
|
|
|
|
|
auto returnValue = ctx->GetReturnDWord();
|
|
|
|
REQUIRE(returnValue == 105);
|
|
|
|
|
|
|
|
ctxPool->ReturnContextToPool(ctx);
|
|
|
|
delete obj;
|
|
|
|
delete lib;
|
|
|
|
}
|
|
|
|
|
2020-04-11 12:42:49 +00:00
|
|
|
TEST_CASE("Get a script resolver, save the byte code to memory, create new script resolver from it") {
|
2020-04-23 22:05:43 +00:00
|
|
|
auto originLib = dynamic_cast<AngelScriptResolver*>(PkmnLib::Battling::BattleLibrary::CreateScriptResolver());
|
2020-04-11 12:42:49 +00:00
|
|
|
originLib->Initialize(TestLibrary::GetLibrary());
|
2020-04-12 08:17:15 +00:00
|
|
|
originLib->CreateScript("testScript1", _scripts["testScript1"]);
|
2020-04-11 12:42:49 +00:00
|
|
|
originLib->FinalizeModule();
|
2021-01-22 13:11:03 +00:00
|
|
|
auto obj = dynamic_cast<AngelScriptScript*>(originLib->LoadScript(ScriptCategory::Creature, "testScript1"_cnc));
|
|
|
|
REQUIRE(obj != nullptr);
|
|
|
|
REQUIRE(obj->GetType()->GetOnInitialize().Exists);
|
|
|
|
delete obj;
|
2020-04-11 12:42:49 +00:00
|
|
|
size_t size;
|
|
|
|
auto byteCode = originLib->WriteByteCodeToMemory(size);
|
|
|
|
|
2020-04-23 22:05:43 +00:00
|
|
|
auto newLib = dynamic_cast<AngelScriptResolver*>(PkmnLib::Battling::BattleLibrary::CreateScriptResolver());
|
2020-04-11 12:42:49 +00:00
|
|
|
newLib->Initialize(TestLibrary::GetLibrary());
|
2020-05-02 09:13:04 +00:00
|
|
|
newLib->LoadByteCodeFromMemory(byteCode, size);
|
2021-01-22 13:11:03 +00:00
|
|
|
obj = dynamic_cast<AngelScriptScript*>(newLib->LoadScript(ScriptCategory::Creature, "testScript1"_cnc));
|
2020-04-11 12:42:49 +00:00
|
|
|
REQUIRE(obj != nullptr);
|
2021-01-22 13:11:03 +00:00
|
|
|
REQUIRE(obj->GetType()->GetOnInitialize().Exists);
|
2020-04-11 12:42:49 +00:00
|
|
|
delete obj;
|
|
|
|
delete originLib;
|
|
|
|
delete newLib;
|
|
|
|
free(byteCode);
|
|
|
|
}
|
2020-05-02 09:13:04 +00:00
|
|
|
|
|
|
|
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");
|
|
|
|
auto obj = dynamic_cast<AngelScriptScript*>(newLib->LoadScript(ScriptCategory::Creature, "testScript1"_cnc));
|
|
|
|
REQUIRE(obj != nullptr);
|
|
|
|
delete obj;
|
|
|
|
delete originLib;
|
|
|
|
delete newLib;
|
2020-05-02 09:55:04 +00:00
|
|
|
remove("foo.bin");
|
2020-05-02 09:13:04 +00:00
|
|
|
}
|
|
|
|
|
2020-01-11 21:30:23 +00:00
|
|
|
#endif
|