2020-01-11 21:30:23 +00:00
|
|
|
#ifdef TESTS_BUILD
|
|
|
|
#include "../../extern/catch.hpp"
|
2020-04-23 22:05:43 +00:00
|
|
|
#include "../../src/ScriptResolving/AngelScript/AngelScriptResolver.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]
|
2020-01-11 21:30:23 +00:00
|
|
|
class testScript1 {
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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();
|
|
|
|
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);
|
2020-04-12 08:17:15 +00:00
|
|
|
auto obj = dynamic_cast<AngelScriptScript*>(newLib->LoadScript(ScriptCategory::Creature, "testScript1"_cnc));
|
2020-04-11 12:42:49 +00:00
|
|
|
REQUIRE(obj != nullptr);
|
|
|
|
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;
|
|
|
|
// remove("foo.bin");
|
|
|
|
}
|
|
|
|
|
2020-01-11 21:30:23 +00:00
|
|
|
#endif
|