176 lines
7.1 KiB
C++
176 lines
7.1 KiB
C++
#if TESTS_BUILD and ANGELSCRIPT
|
|
#include <doctest.h>
|
|
#include "../../../../TestLibrary/TestLibrary.hpp"
|
|
#include "../../../../src/ScriptResolving/AngelScript/AngelScriptResolver.hpp"
|
|
#include "../../../../src/ScriptResolving/AngelScript/ContextPool.hpp"
|
|
|
|
static std::unordered_map<const char*, const char*> _scripts =
|
|
std::unordered_map<const char*, const char*>{{"testScript1", R"(
|
|
namespace Pokemon{
|
|
[Pokemon effect=testScript1]
|
|
class testScript1 : PkmnScript {
|
|
bool testName(const Forme@ s, const constString &in name){ return s.Name == name; }
|
|
bool testWeight(const Forme@ s, float weight){ return s.Weight == weight; }
|
|
bool testHeight(const Forme@ s, float height){ return s.Height == height; }
|
|
bool testBaseExperience(const Forme@ s, uint baseExperience){ return s.BaseExperience == baseExperience; }
|
|
bool testTypeCount(const Forme@ s, int typeCount){ return s.TypeCount == typeCount; }
|
|
bool testGetType(const Forme@ s, uint8 type){ return s.GetType(0) == type; }
|
|
bool testGetStatistic(const Forme@ s, Statistic stat, uint value){ return s.GetStatistic(stat) == value; }
|
|
bool testGetAbility(const Forme@ s, const constString &in ability){ return s.GetAbility(false, 0) == ability; }
|
|
}}
|
|
)"}};
|
|
|
|
struct ScriptData {
|
|
AngelScriptScript* Script = nullptr;
|
|
AngelScriptResolver* Resolver = nullptr;
|
|
asIScriptFunction* Func = nullptr;
|
|
asIScriptContext* Context = nullptr;
|
|
|
|
~ScriptData() {
|
|
Script->GetContextPool()->ReturnContextToPool(Context);
|
|
delete Script;
|
|
}
|
|
};
|
|
|
|
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);
|
|
_resolverCache->CreateScript("testScript1", _scripts["testScript1"]);
|
|
_resolverCache->FinalizeModule();
|
|
}
|
|
return _resolverCache;
|
|
}
|
|
|
|
static ScriptData GetScript(PkmnLib::Battling::BattleLibrary* mainLib, const ArbUt::StringView& funcName) {
|
|
auto lib = GetScriptResolver(mainLib);
|
|
auto s = lib->LoadScript(nullptr, ScriptCategory::Creature, "testScript1"_cnc).TakeOwnership();
|
|
auto script = dynamic_cast<AngelScriptScript*>(s);
|
|
REQUIRE(script != nullptr);
|
|
|
|
auto ctxPool = script->GetContextPool();
|
|
auto ctx = ctxPool->RequestContext();
|
|
|
|
auto func = script->PrepareMethod(funcName, ctx);
|
|
REQUIRE(func != nullptr);
|
|
|
|
return {.Script = script, .Resolver = lib, .Func = func, .Context = ctx};
|
|
}
|
|
|
|
TEST_CASE("Validate Forme Name in Script") {
|
|
auto mainLib = TestLibrary::GetLibrary();
|
|
auto data = GetScript(mainLib, "testName"_cnc);
|
|
|
|
auto forme = mainLib->GetSpeciesLibrary()->Get("testSpecies2"_cnc)->GetDefaultForme();
|
|
data.Context->SetArgObject(0, (void*)forme.GetRaw());
|
|
auto name = forme->GetName();
|
|
data.Context->SetArgAddress(1, &name);
|
|
|
|
auto result = data.Context->Execute();
|
|
REQUIRE(result == asEXECUTION_FINISHED);
|
|
auto v = (bool)data.Context->GetReturnWord();
|
|
REQUIRE(v);
|
|
}
|
|
|
|
TEST_CASE("Validate Forme Weight in Script") {
|
|
auto mainLib = TestLibrary::GetLibrary();
|
|
auto data = GetScript(mainLib, "testWeight"_cnc);
|
|
|
|
auto forme = mainLib->GetSpeciesLibrary()->Get("testSpecies2"_cnc)->GetDefaultForme();
|
|
data.Context->SetArgObject(0, (void*)forme.GetRaw());
|
|
data.Context->SetArgFloat(1, forme->GetWeight());
|
|
|
|
auto result = data.Context->Execute();
|
|
REQUIRE(result == asEXECUTION_FINISHED);
|
|
auto v = (bool)data.Context->GetReturnWord();
|
|
REQUIRE(v);
|
|
}
|
|
|
|
TEST_CASE("Validate Forme Height in Script") {
|
|
auto mainLib = TestLibrary::GetLibrary();
|
|
auto data = GetScript(mainLib, "testHeight"_cnc);
|
|
|
|
auto forme = mainLib->GetSpeciesLibrary()->Get("testSpecies2"_cnc)->GetDefaultForme();
|
|
data.Context->SetArgObject(0, (void*)forme.GetRaw());
|
|
data.Context->SetArgFloat(1, forme->GetHeight());
|
|
|
|
auto result = data.Context->Execute();
|
|
REQUIRE(result == asEXECUTION_FINISHED);
|
|
auto v = (bool)data.Context->GetReturnWord();
|
|
REQUIRE(v);
|
|
}
|
|
|
|
TEST_CASE("Validate Forme Base Experience in Script") {
|
|
auto mainLib = TestLibrary::GetLibrary();
|
|
auto data = GetScript(mainLib, "testBaseExperience"_cnc);
|
|
|
|
auto forme = mainLib->GetSpeciesLibrary()->Get("testSpecies2"_cnc)->GetDefaultForme();
|
|
data.Context->SetArgObject(0, (void*)forme.GetRaw());
|
|
data.Context->SetArgDWord(1, forme->GetBaseExperience());
|
|
|
|
auto result = data.Context->Execute();
|
|
REQUIRE(result == asEXECUTION_FINISHED);
|
|
auto v = (bool)data.Context->GetReturnWord();
|
|
REQUIRE(v);
|
|
}
|
|
|
|
TEST_CASE("Validate Forme Type Count in Script") {
|
|
auto mainLib = TestLibrary::GetLibrary();
|
|
auto data = GetScript(mainLib, "testTypeCount"_cnc);
|
|
|
|
auto forme = mainLib->GetSpeciesLibrary()->Get("testSpecies2"_cnc)->GetDefaultForme();
|
|
data.Context->SetArgObject(0, (void*)forme.GetRaw());
|
|
data.Context->SetArgDWord(1, forme->GetTypeCount());
|
|
|
|
auto result = data.Context->Execute();
|
|
REQUIRE(result == asEXECUTION_FINISHED);
|
|
auto v = (bool)data.Context->GetReturnWord();
|
|
REQUIRE(v);
|
|
}
|
|
|
|
TEST_CASE("Validate Forme GetType in Script") {
|
|
auto mainLib = TestLibrary::GetLibrary();
|
|
auto data = GetScript(mainLib, "testGetType"_cnc);
|
|
|
|
auto forme = mainLib->GetSpeciesLibrary()->Get("testSpecies2"_cnc)->GetDefaultForme();
|
|
data.Context->SetArgObject(0, (void*)forme.GetRaw());
|
|
data.Context->SetArgByte(1, forme->GetType(0));
|
|
|
|
auto result = data.Context->Execute();
|
|
REQUIRE(result == asEXECUTION_FINISHED);
|
|
auto v = (bool)data.Context->GetReturnWord();
|
|
REQUIRE(v);
|
|
}
|
|
|
|
TEST_CASE("Validate Forme GetStatistic in Script") {
|
|
auto mainLib = TestLibrary::GetLibrary();
|
|
|
|
// Iterate over each stat, ensure they return the expected value.
|
|
for (uint8_t statInt = static_cast<uint8_t>(CreatureLib::Library::Statistic::Health);
|
|
statInt != static_cast<uint8_t>(CreatureLib::Library::Statistic::Speed); statInt++) {
|
|
auto stat = static_cast<CreatureLib::Library::Statistic>(statInt);
|
|
|
|
auto data = GetScript(mainLib, "testGetStatistic"_cnc);
|
|
auto forme = mainLib->GetSpeciesLibrary()->Get("testSpecies2"_cnc)->GetDefaultForme();
|
|
data.Context->SetArgObject(0, (void*)forme.GetRaw());
|
|
data.Context->SetArgDWord(1, static_cast<asDWORD>(stat));
|
|
data.Context->SetArgDWord(2, forme->GetStatistic(stat));
|
|
REQUIRE(data.Context->Execute() == asEXECUTION_FINISHED);
|
|
REQUIRE((bool)data.Context->GetReturnWord());
|
|
}
|
|
}
|
|
|
|
TEST_CASE("Validate Forme GetAbility in Script") {
|
|
auto mainLib = TestLibrary::GetLibrary();
|
|
|
|
auto data = GetScript(mainLib, "testGetAbility"_cnc);
|
|
auto forme = mainLib->GetSpeciesLibrary()->Get("testSpecies2"_cnc)->GetDefaultForme();
|
|
data.Context->SetArgObject(0, (void*)forme.GetRaw());
|
|
auto ability = forme->GetAbility(CreatureLib::Library::TalentIndex(false, 0))->GetName();
|
|
data.Context->SetArgAddress(1, &ability);
|
|
REQUIRE(data.Context->Execute() == asEXECUTION_FINISHED);
|
|
REQUIRE((bool)data.Context->GetReturnWord());
|
|
}
|
|
|
|
#endif |