PkmnLib/tests/ScriptTests/Angelscript/ScriptOwnerTest.cpp

102 lines
3.5 KiB
C++
Raw Normal View History

#if TESTS_BUILD and ANGELSCRIPT
2022-02-11 11:17:27 +00:00
#include <doctest.h>
2021-10-30 09:44:16 +00:00
#include "../../src/Battling/Pokemon/CreatePokemon.hpp"
#include "../../src/ScriptResolving/AngelScript/AngelScriptResolver.hpp"
#include "../../src/ScriptResolving/AngelScript/ContextPool.hpp"
2022-05-14 09:59:54 +00:00
#include "../../TestLibrary/TestLibrary.hpp"
2021-10-30 09:44:16 +00:00
static std::unordered_map<const char*, const char*> _scripts =
std::unordered_map<const char*, const char*>{{"basic_ownership_test",
R"(namespace Pokemon {
[Pokemon effect="basic_ownership_test"]
shared class basic_ownership_test : PkmnScript {
void Test(){
auto mon = cast<Pokemon@>(GetOwner());
if (mon is null){
print("mon was null");
2021-10-30 09:44:16 +00:00
throw("Owner was null!");
}
}
}
})"},
{"volatile_test",
R"(namespace Pokemon {
[Pokemon effect="volatile_test"]
shared class volatile_test : PkmnScript {
void Test(){
auto mon = cast<Pokemon@>(GetOwner());
if (mon is null){
throw("Owner was null!");
}
}
}
2021-10-30 09:44:16 +00:00
})"}};
void MessageCallback(const asSMessageInfo* msg, void*) {
const char* type = "ERR ";
if (msg->type == asMSGTYPE_WARNING)
type = "WARN";
else if (msg->type == asMSGTYPE_INFORMATION)
type = "INFO";
printf("%s (%d, %d) : %s : %s\n", msg->section, msg->row, msg->col, type, msg->message);
}
2021-10-30 09:44:16 +00:00
static AngelScriptResolver* GetScriptResolver(PkmnLib::Battling::BattleLibrary* mainLib) {
auto res = dynamic_cast<AngelScriptResolver*>(mainLib->GetScriptResolver().get());
res->Reset(mainLib);
res->GetEngine()->SetMessageCallback(asFUNCTION(MessageCallback), nullptr, asCALL_CDECL);
for (auto kv : _scripts) {
res->CreateScript(kv.first, kv.second);
2021-10-30 09:44:16 +00:00
}
res->FinalizeModule();
return res;
2021-10-30 09:44:16 +00:00
}
static AngelScriptScript* GetScript(PkmnLib::Battling::BattleLibrary* mainLib, const ArbUt::StringView& scriptName,
PkmnLib::Battling::Pokemon* owner) {
auto* lib = GetScriptResolver(mainLib);
auto* s = lib->LoadScript(owner, ScriptCategory::Creature, scriptName);
auto* script = dynamic_cast<AngelScriptScript*>(s);
2021-10-30 09:44:16 +00:00
REQUIRE(script != nullptr);
return script;
}
TEST_CASE("Basic script owner tests.") {
auto lib = TestLibrary::GetLibrary();
auto mon = PkmnLib::Battling::CreatePokemon(lib, "testSpecies"_cnc, 1).Build();
auto script = GetScript(lib, "basic_ownership_test"_cnc, mon);
REQUIRE(script != nullptr);
script->OnRemove();
auto ctxPool = script->GetContextPool();
auto ctx = ctxPool->RequestContext();
script->PrepareMethod("Test"_cnc, ctx);
REQUIRE(ctx->Execute() == asEXECUTION_FINISHED);
ctxPool->ReturnContextToPool(ctx);
delete script;
delete mon;
}
TEST_CASE("Add volatile and get owner.") {
auto lib = TestLibrary::GetLibrary();
auto mon = PkmnLib::Battling::CreatePokemon(lib, "testSpecies"_cnc, 1).Build();
auto res = dynamic_cast<AngelScriptResolver*>(lib->GetScriptResolver().get());
res->Reset(lib);
for (auto kv : _scripts) {
res->CreateScript(kv.first, kv.second);
}
res->FinalizeModule();
auto script = (AngelScriptScript*)mon->AddVolatileScript("volatile_test"_cnc);
REQUIRE(script != nullptr);
auto owner = script->GetAngelscriptOwner();
REQUIRE_EQ(owner->GetType(), res->GetEngine()->GetTypeInfoByName("Pokemon"));
delete mon;
}
2021-10-30 09:44:16 +00:00
#endif