PkmnLib/tests/ScriptTests/ScriptOwnerTest.cpp

63 lines
2.2 KiB
C++

#ifdef TESTS_BUILD
#include "../../extern/doctest.hpp"
#include "../../src/Battling/Pokemon/CreatePokemon.hpp"
#include "../../src/ScriptResolving/AngelScript/AngelScriptResolver.hpp"
#include "../../src/ScriptResolving/AngelScript/ContextPool.hpp"
#include "../TestLibrary/TestLibrary.hpp"
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){
throw("Owner was null!");
}
}
}
})"}};
static AngelScriptResolver* _resolverCache = nullptr;
static AngelScriptResolver* GetScriptResolver(PkmnLib::Battling::BattleLibrary* mainLib) {
if (_resolverCache == nullptr) {
_resolverCache = dynamic_cast<AngelScriptResolver*>(PkmnLib::Battling::BattleLibrary::CreateScriptResolver());
_resolverCache->Initialize(mainLib);
for (auto kv : _scripts) {
_resolverCache->CreateScript(kv.first, kv.second);
}
_resolverCache->FinalizeModule();
}
return _resolverCache;
}
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);
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;
}
#endif