2020-01-11 21:30:23 +00:00
|
|
|
#ifdef TESTS_BUILD
|
|
|
|
#include "../../extern/catch.hpp"
|
2020-02-02 11:23:50 +00:00
|
|
|
#include "../../src/ScriptResolving/AngelScript/AngelScripResolver.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-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") {
|
|
|
|
auto lib = dynamic_cast<AngelScripResolver*>(PkmnLib::Battling::BattleLibrary::CreateScriptResolver());
|
|
|
|
lib->Initialize(TestLibrary::GetLibrary());
|
2020-02-16 13:38:05 +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") {
|
|
|
|
auto lib = dynamic_cast<AngelScripResolver*>(PkmnLib::Battling::BattleLibrary::CreateScriptResolver());
|
|
|
|
lib->Initialize(TestLibrary::GetLibrary());
|
2020-02-16 13:38:05 +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") {
|
|
|
|
auto lib = dynamic_cast<AngelScripResolver*>(PkmnLib::Battling::BattleLibrary::CreateScriptResolver());
|
|
|
|
lib->Initialize(TestLibrary::GetLibrary());
|
2020-02-16 13:38:05 +00:00
|
|
|
lib->CreateScript("testScript1" , _scripts["testScript1"]);
|
2020-01-11 21:30:23 +00:00
|
|
|
lib->FinalizeModule();
|
|
|
|
|
|
|
|
auto obj =
|
2020-02-28 18:51:50 +00:00
|
|
|
dynamic_cast<AngelScriptScript*>(lib->LoadScript(ScriptCategory::Creature, "testScript1"_cnc));
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|