Adds tests for MoveData type in AngelScript.
continuous-integration/drone/push Build is failing Details

This commit is contained in:
Deukhoofd 2020-01-18 14:35:08 +01:00
parent f32db7a751
commit 9372ba4b27
Signed by: Deukhoofd
GPG Key ID: ADF2E9256009EDCE
8 changed files with 170 additions and 11 deletions

View File

@ -40,7 +40,7 @@ class PkmnLibConan(ConanFile):
self.options["AngelScript"].link_std_statically = True self.options["AngelScript"].link_std_statically = True
def requirements(self): def requirements(self):
self.requires("CreatureLib/b02577554f4dfe2141318a440fc99c11de97daca@creaturelib/master") self.requires("CreatureLib/cc46cd46a78d998d6cd6ebb2f76b5b54aa240893@creaturelib/master")
if self.options.script_handler == "angelscript": if self.options.script_handler == "angelscript":
self.requires("AngelScript/2.34@AngelScript/Deukhoofd") self.requires("AngelScript/2.34@AngelScript/Deukhoofd")
else: else:

View File

@ -45,7 +45,7 @@ void RegisterMoveTypes::RegisterMoveType(asIScriptEngine* engine) {
r = engine->RegisterObjectMethod("MoveData", "const string& get_Name() const property", r = engine->RegisterObjectMethod("MoveData", "const string& get_Name() const property",
asMETHOD(PkmnLib::Library::MoveData, GetName), asCALL_THISCALL); asMETHOD(PkmnLib::Library::MoveData, GetName), asCALL_THISCALL);
assert(r >= 0); assert(r >= 0);
r = engine->RegisterObjectMethod("MoveData", "const string& get_Type() const property", r = engine->RegisterObjectMethod("MoveData", "uint8 get_Type() const property",
asMETHOD(PkmnLib::Library::MoveData, GetType), asCALL_THISCALL); asMETHOD(PkmnLib::Library::MoveData, GetType), asCALL_THISCALL);
assert(r >= 0); assert(r >= 0);
r = engine->RegisterObjectMethod("MoveData", "MoveCategory get_Category() const property", r = engine->RegisterObjectMethod("MoveData", "MoveCategory get_Category() const property",

View File

@ -17,16 +17,22 @@ namespace PkmnLib::Battling {
scriptResolver, miscLibrary) {} scriptResolver, miscLibrary) {}
const Library::PokemonLibrary* GetStaticLib() const{ const Library::PokemonLibrary* GetStaticLib() const{
return reinterpret_cast<const Library::PokemonLibrary*>(CreatureLib::Battling::BattleLibrary::_staticLib); return dynamic_cast<const Library::PokemonLibrary*>(CreatureLib::Battling::BattleLibrary::_staticLib);
} }
const Library::SpeciesLibrary* GetSpeciesLibrary() const { const Library::SpeciesLibrary* GetSpeciesLibrary() const {
return reinterpret_cast<const Library::SpeciesLibrary*>( return dynamic_cast<const Library::SpeciesLibrary*>(
CreatureLib::Battling::BattleLibrary::GetSpeciesLibrary()); CreatureLib::Battling::BattleLibrary::GetSpeciesLibrary());
} }
const Library::MoveLibrary* GetMoveLibrary() const {
return dynamic_cast<const Library::MoveLibrary*>(
CreatureLib::Battling::BattleLibrary::GetAttackLibrary());
}
const Library::ItemLibrary* GetItemLibrary() const { const Library::ItemLibrary* GetItemLibrary() const {
return reinterpret_cast<const Library::ItemLibrary*>( return dynamic_cast<const Library::ItemLibrary*>(
CreatureLib::Battling::BattleLibrary::GetItemLibrary()); CreatureLib::Battling::BattleLibrary::GetItemLibrary());
} }

View File

@ -1,6 +1,6 @@
#include "MoveData.hpp" #include "MoveData.hpp"
#include <utility> #include <utility>
PkmnLib::Library::MoveData::MoveData(const std::string& name, const std::string& type, PkmnLib::Library::MoveData::MoveData(const std::string& name, uint8_t type,
PkmnLib::Library::MoveCategory category, uint8_t power, uint8_t accuracy, PkmnLib::Library::MoveCategory category, uint8_t power, uint8_t accuracy,
uint8_t baseUsage, CreatureLib::Library::AttackTarget target, uint8_t priority, uint8_t baseUsage, CreatureLib::Library::AttackTarget target, uint8_t priority,
std::unordered_set<std::string> flags) std::unordered_set<std::string> flags)

View File

@ -9,7 +9,7 @@ namespace PkmnLib::Library {
private: private:
public: public:
MoveData(const std::string& name, const std::string& type, PkmnLib::Library::MoveCategory category, uint8_t power, MoveData(const std::string& name, uint8_t type, PkmnLib::Library::MoveCategory category, uint8_t power,
uint8_t accuracy, uint8_t baseUsage, CreatureLib::Library::AttackTarget target, uint8_t priority, uint8_t accuracy, uint8_t baseUsage, CreatureLib::Library::AttackTarget target, uint8_t priority,
std::unordered_set<std::string> flags); std::unordered_set<std::string> flags);

View File

@ -0,0 +1,150 @@
#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 MoveData@ s, const string &in name){ return s.Name == name; }
bool testType(const MoveData@ s, uint8 type){ return s.Type == type; }
bool testCategory(const MoveData@ s, MoveCategory category){ return s.Category == category; }
bool testBasePower(const MoveData@ s, uint8 basePower){ return s.BasePower == basePower; }
bool testAccuracy(const MoveData@ s, uint8 accuracy){ return s.Accuracy == accuracy; }
bool testBaseUsages(const MoveData@ s, uint8 baseUsages){ return s.BaseUsages == baseUsages; }
bool testTarget(const MoveData@ s, MoveTarget target){ return s.Target == target; }
bool testPriority(const MoveData@ s, int8 priority){ return s.Priority == priority; }
}
)"}};
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 Move Name in Script") {
auto mainLib = TestLibrary::GetLibrary();
auto data = GetScript(mainLib, "testName");
auto move = mainLib->GetMoveLibrary()->GetAttack("testMove");
data.Context->SetArgObject(0, const_cast<PkmnLib::Library::MoveData*>(move));
auto name = move->GetName();
data.Context->SetArgAddress(1, &name);
REQUIRE(data.Context->Execute() == asEXECUTION_FINISHED);
REQUIRE((bool)data.Context->GetReturnWord());
}
TEST_CASE("Validate Move Type in Script") {
auto mainLib = TestLibrary::GetLibrary();
auto data = GetScript(mainLib, "testType");
auto move = mainLib->GetMoveLibrary()->GetAttack("testMove");
data.Context->SetArgObject(0, const_cast<PkmnLib::Library::MoveData*>(move));
data.Context->SetArgByte(1, move->GetType());
REQUIRE(data.Context->Execute() == asEXECUTION_FINISHED);
REQUIRE((bool)data.Context->GetReturnWord());
}
TEST_CASE("Validate Move Category in Script") {
auto mainLib = TestLibrary::GetLibrary();
auto data = GetScript(mainLib, "testCategory");
auto move = mainLib->GetMoveLibrary()->GetAttack("testMove");
data.Context->SetArgObject(0, const_cast<PkmnLib::Library::MoveData*>(move));
data.Context->SetArgDWord(1, (uint32_t)move->GetCategory());
REQUIRE(data.Context->Execute() == asEXECUTION_FINISHED);
REQUIRE((bool)data.Context->GetReturnWord());
}
TEST_CASE("Validate Move BasePower in Script") {
auto mainLib = TestLibrary::GetLibrary();
auto data = GetScript(mainLib, "testBasePower");
auto move = mainLib->GetMoveLibrary()->GetAttack("testMove");
data.Context->SetArgObject(0, const_cast<PkmnLib::Library::MoveData*>(move));
data.Context->SetArgByte(1, move->GetBasePower());
REQUIRE(data.Context->Execute() == asEXECUTION_FINISHED);
REQUIRE((bool)data.Context->GetReturnWord());
}
TEST_CASE("Validate Move Accuracy in Script") {
auto mainLib = TestLibrary::GetLibrary();
auto data = GetScript(mainLib, "testAccuracy");
auto move = mainLib->GetMoveLibrary()->GetAttack("testMove");
data.Context->SetArgObject(0, const_cast<PkmnLib::Library::MoveData*>(move));
data.Context->SetArgByte(1, move->GetAccuracy());
REQUIRE(data.Context->Execute() == asEXECUTION_FINISHED);
REQUIRE((bool)data.Context->GetReturnWord());
}
TEST_CASE("Validate Move BaseUsages in Script") {
auto mainLib = TestLibrary::GetLibrary();
auto data = GetScript(mainLib, "testBaseUsages");
auto move = mainLib->GetMoveLibrary()->GetAttack("testMove");
data.Context->SetArgObject(0, const_cast<PkmnLib::Library::MoveData*>(move));
data.Context->SetArgByte(1, move->GetBaseUsages());
REQUIRE(data.Context->Execute() == asEXECUTION_FINISHED);
REQUIRE((bool)data.Context->GetReturnWord());
}
TEST_CASE("Validate Move Target in Script") {
auto mainLib = TestLibrary::GetLibrary();
auto data = GetScript(mainLib, "testTarget");
auto move = mainLib->GetMoveLibrary()->GetAttack("testMove");
data.Context->SetArgObject(0, const_cast<PkmnLib::Library::MoveData*>(move));
data.Context->SetArgDWord(1, (uint32_t)move->GetTarget());
REQUIRE(data.Context->Execute() == asEXECUTION_FINISHED);
REQUIRE((bool)data.Context->GetReturnWord());
}
TEST_CASE("Validate Move Priority in Script") {
auto mainLib = TestLibrary::GetLibrary();
auto data = GetScript(mainLib, "testPriority");
auto move = mainLib->GetMoveLibrary()->GetAttack("testMove");
data.Context->SetArgObject(0, const_cast<PkmnLib::Library::MoveData*>(move));
data.Context->SetArgByte(1, move->GetPriority());
REQUIRE(data.Context->Execute() == asEXECUTION_FINISHED);
REQUIRE((bool)data.Context->GetReturnWord());
}
#endif

View File

@ -31,3 +31,9 @@ PkmnLib::Library::SpeciesLibrary* TestLibrary::BuildSpeciesLibrary() {
return lib; return lib;
} }
PkmnLib::Library::MoveLibrary* TestLibrary::BuildMoveLibrary() {
auto lib = new PkmnLib::Library::MoveLibrary();
lib->LoadMove("testMove", new PkmnLib::Library::MoveData("testMove", 0, PkmnLib::Library::MoveCategory::Physical,
50, 100, 20, CreatureLib::Library::AttackTarget::Adjacent,0, {}));
return lib;
}

View File

@ -38,10 +38,7 @@ public:
static PkmnLib::Library::SpeciesLibrary* BuildSpeciesLibrary(); static PkmnLib::Library::SpeciesLibrary* BuildSpeciesLibrary();
static PkmnLib::Library::MoveLibrary* BuildMoveLibrary() { static PkmnLib::Library::MoveLibrary* BuildMoveLibrary();
auto lib = new PkmnLib::Library::MoveLibrary();
return lib;
}
static PkmnLib::Library::ItemLibrary* BuildItemLibrary() { static PkmnLib::Library::ItemLibrary* BuildItemLibrary() {
auto lib = new PkmnLib::Library::ItemLibrary(); auto lib = new PkmnLib::Library::ItemLibrary();