Tests for forme fields in AngelScript.
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Deukhoofd 2020-01-18 13:07:56 +01:00
parent 2c2039cdb8
commit f32db7a751
Signed by: Deukhoofd
GPG Key ID: ADF2E9256009EDCE
5 changed files with 174 additions and 4 deletions

View File

@ -18,6 +18,7 @@ void AngelScripResolver::Initialize(CreatureLib::Battling::BattleLibrary* librar
_engine->SetEngineProperty(asEP_DISALLOW_VALUE_ASSIGN_FOR_REF_TYPE, true); _engine->SetEngineProperty(asEP_DISALLOW_VALUE_ASSIGN_FOR_REF_TYPE, true);
_engine->SetEngineProperty(asEP_ALWAYS_IMPL_DEFAULT_CONSTRUCT, true); _engine->SetEngineProperty(asEP_ALWAYS_IMPL_DEFAULT_CONSTRUCT, true);
_engine->SetEngineProperty(asEP_AUTO_GARBAGE_COLLECT, false); _engine->SetEngineProperty(asEP_AUTO_GARBAGE_COLLECT, false);
_engine->SetEngineProperty(asEP_REQUIRE_ENUM_SCOPE, true);
int32_t r = _engine->SetMessageCallback(asFUNCTION(MessageCallback), nullptr, asCALL_CDECL); int32_t r = _engine->SetMessageCallback(asFUNCTION(MessageCallback), nullptr, asCALL_CDECL);
if (r < 0) if (r < 0)

View File

@ -85,7 +85,7 @@ void RegisterSpeciesTypes::RegisterFormeType(asIScriptEngine* engine) {
r = engine->RegisterObjectMethod("Forme", "int get_TypeCount() const property", r = engine->RegisterObjectMethod("Forme", "int get_TypeCount() const property",
asMETHOD(PkmnLib::Library::PokemonForme, GetTypeCount), asCALL_THISCALL); asMETHOD(PkmnLib::Library::PokemonForme, GetTypeCount), asCALL_THISCALL);
assert(r >= 0); assert(r >= 0);
r = engine->RegisterObjectMethod("Forme", "float GetType(int index) const", r = engine->RegisterObjectMethod("Forme", "uint8 GetType(int index) const",
asMETHOD(PkmnLib::Library::PokemonForme, GetType), asCALL_THISCALL); asMETHOD(PkmnLib::Library::PokemonForme, GetType), asCALL_THISCALL);
assert(r >= 0); assert(r >= 0);
r = engine->RegisterObjectMethod("Forme", "uint GetStatistic(Statistic stat) const", r = engine->RegisterObjectMethod("Forme", "uint GetStatistic(Statistic stat) const",

View File

@ -12,6 +12,7 @@ namespace PkmnLib::Library {
private: private:
public: public:
inline const std::string& GetAbility(int index) const { return GetTalent(index); }
}; };
} }

View File

@ -0,0 +1,168 @@
#ifdef TESTS_BUILD
#include "../../../extern/catch.hpp"
#include "../../../src/AngelScript/AngelScripResolver.hpp"
#include "../../TestLibrary/TestLibrary.hpp"
static std::unordered_map<const char*, const char*> _scripts =
std::unordered_map<const char*, const char*>{{"testScript1", R"(
class testScript1 {
bool testName(const Forme@ s, const string &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 string &in ability){ return s.GetAbility(0) == ability; }
}
)"}};
static const char* _testLoadFunc(const char* name) { return _scripts[name]; }
struct ScriptData {
AngelScriptScript* Script;
AngelScripResolver* Resolver;
asIScriptFunction* Func;
asIScriptContext* Context;
~ScriptData() {
Script->GetContextPool()->ReturnContextToPool(Context);
delete Script;
delete Resolver;
}
};
static ScriptData GetScript(PkmnLib::Battling::BattleLibrary* mainLib, const char* funcName) {
auto lib = dynamic_cast<AngelScripResolver*>(PkmnLib::Battling::BattleLibrary::CreateScriptResolver());
lib->Initialize(mainLib);
lib->SetCreateFunction(&_testLoadFunc);
lib->CreateScript("testScript1");
lib->FinalizeModule();
auto s = lib->LoadScript(AngelScripResolver::ScriptCategory::Creature, "testScript1");
auto script = dynamic_cast<AngelScriptScript*>(s);
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");
auto forme = mainLib->GetSpeciesLibrary()->GetPkmnSpecies("testSpecies2")->GetDefaultForme();
data.Context->SetArgObject(0, const_cast<PkmnLib::Library::PokemonForme*>(forme));
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");
auto forme = mainLib->GetSpeciesLibrary()->GetPkmnSpecies("testSpecies2")->GetDefaultForme();
data.Context->SetArgObject(0, const_cast<PkmnLib::Library::PokemonForme*>(forme));
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");
auto forme = mainLib->GetSpeciesLibrary()->GetPkmnSpecies("testSpecies2")->GetDefaultForme();
data.Context->SetArgObject(0, const_cast<PkmnLib::Library::PokemonForme*>(forme));
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");
auto forme = mainLib->GetSpeciesLibrary()->GetPkmnSpecies("testSpecies2")->GetDefaultForme();
data.Context->SetArgObject(0, const_cast<PkmnLib::Library::PokemonForme*>(forme));
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");
auto forme = mainLib->GetSpeciesLibrary()->GetPkmnSpecies("testSpecies2")->GetDefaultForme();
data.Context->SetArgObject(0, const_cast<PkmnLib::Library::PokemonForme*>(forme));
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");
auto forme = mainLib->GetSpeciesLibrary()->GetPkmnSpecies("testSpecies2")->GetDefaultForme();
data.Context->SetArgObject(0, const_cast<PkmnLib::Library::PokemonForme*>(forme));
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 = CreatureLib::Core::Statistic::Health; statInt != CreatureLib::Core::Statistic::Speed;
statInt++) {
auto stat = static_cast<CreatureLib::Core::Statistic>(statInt);
auto data = GetScript(mainLib, "testGetStatistic");
auto forme = mainLib->GetSpeciesLibrary()->GetPkmnSpecies("testSpecies2")->GetDefaultForme();
data.Context->SetArgObject(0, const_cast<PkmnLib::Library::PokemonForme*>(forme));
data.Context->SetArgDWord(1, 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");
auto forme = mainLib->GetSpeciesLibrary()->GetPkmnSpecies("testSpecies2")->GetDefaultForme();
data.Context->SetArgObject(0, const_cast<PkmnLib::Library::PokemonForme*>(forme));
auto ability = forme->GetAbility(0);
data.Context->SetArgAddress(1, &ability);
REQUIRE(data.Context->Execute() == asEXECUTION_FINISHED);
REQUIRE((bool)data.Context->GetReturnWord());
}
#endif

View File

@ -30,7 +30,7 @@ struct ScriptData{
} }
}; };
ScriptData GetScript(PkmnLib::Battling::BattleLibrary* mainLib, const char* funcName){ static ScriptData GetScript(PkmnLib::Battling::BattleLibrary* mainLib, const char* funcName){
auto lib = dynamic_cast<AngelScripResolver*>(PkmnLib::Battling::BattleLibrary::CreateScriptResolver()); auto lib = dynamic_cast<AngelScripResolver*>(PkmnLib::Battling::BattleLibrary::CreateScriptResolver());
lib->Initialize(mainLib); lib->Initialize(mainLib);
lib->SetCreateFunction(&_testLoadFunc); lib->SetCreateFunction(&_testLoadFunc);
@ -133,8 +133,7 @@ TEST_CASE("Validate Species Get Default Forme in Script") {
auto data = GetScript(mainLib, "testGetDefaultForme"); auto data = GetScript(mainLib, "testGetDefaultForme");
auto species = mainLib->GetSpeciesLibrary()->GetPkmnSpecies("testSpecies2"); auto species = mainLib->GetSpeciesLibrary()->GetPkmnSpecies("testSpecies2");
data.Context->SetArgObject( data.Context->SetArgObject(0, const_cast<PkmnLib::Library::PokemonSpecies*>(species));
0, const_cast<PkmnLib::Library::PokemonSpecies*>(species));
data.Context->SetArgObject(1, const_cast<PkmnLib::Library::PokemonForme*>(species->GetDefaultForme())); data.Context->SetArgObject(1, const_cast<PkmnLib::Library::PokemonForme*>(species->GetDefaultForme()));
auto result = data.Context->Execute(); auto result = data.Context->Execute();
@ -144,4 +143,5 @@ TEST_CASE("Validate Species Get Default Forme in Script") {
} }
#endif #endif