From 9372ba4b27f773dcfa500f9582dc67c663ce9349 Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Sat, 18 Jan 2020 14:35:08 +0100 Subject: [PATCH] Adds tests for MoveData type in AngelScript. --- conanfile.py | 2 +- .../Library/RegisterMoveTypes.cpp | 2 +- src/Battling/Library/BattleLibrary.hpp | 12 +- src/Library/Moves/MoveData.cpp | 2 +- src/Library/Moves/MoveData.hpp | 2 +- .../ScriptTests/ScriptTypeTests/MoveTests.cpp | 150 ++++++++++++++++++ tests/TestLibrary/TestLibrary.cpp | 6 + tests/TestLibrary/TestLibrary.hpp | 5 +- 8 files changed, 170 insertions(+), 11 deletions(-) create mode 100644 tests/ScriptTests/ScriptTypeTests/MoveTests.cpp diff --git a/conanfile.py b/conanfile.py index eecd1c0..8302b06 100644 --- a/conanfile.py +++ b/conanfile.py @@ -40,7 +40,7 @@ class PkmnLibConan(ConanFile): self.options["AngelScript"].link_std_statically = True def requirements(self): - self.requires("CreatureLib/b02577554f4dfe2141318a440fc99c11de97daca@creaturelib/master") + self.requires("CreatureLib/cc46cd46a78d998d6cd6ebb2f76b5b54aa240893@creaturelib/master") if self.options.script_handler == "angelscript": self.requires("AngelScript/2.34@AngelScript/Deukhoofd") else: diff --git a/src/AngelScript/TypeRegistry/Library/RegisterMoveTypes.cpp b/src/AngelScript/TypeRegistry/Library/RegisterMoveTypes.cpp index b86a9d1..7072774 100644 --- a/src/AngelScript/TypeRegistry/Library/RegisterMoveTypes.cpp +++ b/src/AngelScript/TypeRegistry/Library/RegisterMoveTypes.cpp @@ -45,7 +45,7 @@ void RegisterMoveTypes::RegisterMoveType(asIScriptEngine* engine) { r = engine->RegisterObjectMethod("MoveData", "const string& get_Name() const property", asMETHOD(PkmnLib::Library::MoveData, GetName), asCALL_THISCALL); 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); assert(r >= 0); r = engine->RegisterObjectMethod("MoveData", "MoveCategory get_Category() const property", diff --git a/src/Battling/Library/BattleLibrary.hpp b/src/Battling/Library/BattleLibrary.hpp index 612c133..4150232 100644 --- a/src/Battling/Library/BattleLibrary.hpp +++ b/src/Battling/Library/BattleLibrary.hpp @@ -17,16 +17,22 @@ namespace PkmnLib::Battling { scriptResolver, miscLibrary) {} const Library::PokemonLibrary* GetStaticLib() const{ - return reinterpret_cast(CreatureLib::Battling::BattleLibrary::_staticLib); + return dynamic_cast(CreatureLib::Battling::BattleLibrary::_staticLib); } const Library::SpeciesLibrary* GetSpeciesLibrary() const { - return reinterpret_cast( + return dynamic_cast( CreatureLib::Battling::BattleLibrary::GetSpeciesLibrary()); } + const Library::MoveLibrary* GetMoveLibrary() const { + return dynamic_cast( + CreatureLib::Battling::BattleLibrary::GetAttackLibrary()); + } + + const Library::ItemLibrary* GetItemLibrary() const { - return reinterpret_cast( + return dynamic_cast( CreatureLib::Battling::BattleLibrary::GetItemLibrary()); } diff --git a/src/Library/Moves/MoveData.cpp b/src/Library/Moves/MoveData.cpp index 740ecbb..bf26813 100644 --- a/src/Library/Moves/MoveData.cpp +++ b/src/Library/Moves/MoveData.cpp @@ -1,6 +1,6 @@ #include "MoveData.hpp" #include -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, uint8_t baseUsage, CreatureLib::Library::AttackTarget target, uint8_t priority, std::unordered_set flags) diff --git a/src/Library/Moves/MoveData.hpp b/src/Library/Moves/MoveData.hpp index 9852194..0703e47 100644 --- a/src/Library/Moves/MoveData.hpp +++ b/src/Library/Moves/MoveData.hpp @@ -9,7 +9,7 @@ namespace PkmnLib::Library { private: 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, std::unordered_set flags); diff --git a/tests/ScriptTests/ScriptTypeTests/MoveTests.cpp b/tests/ScriptTests/ScriptTypeTests/MoveTests.cpp new file mode 100644 index 0000000..6653879 --- /dev/null +++ b/tests/ScriptTests/ScriptTypeTests/MoveTests.cpp @@ -0,0 +1,150 @@ +#ifdef TESTS_BUILD +#include "../../../extern/catch.hpp" +#include "../../../src/AngelScript/AngelScripResolver.hpp" +#include "../../TestLibrary/TestLibrary.hpp" + +static std::unordered_map _scripts = + std::unordered_map{{"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(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(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(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(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(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(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(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(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(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(move)); + data.Context->SetArgByte(1, move->GetPriority()); + + REQUIRE(data.Context->Execute() == asEXECUTION_FINISHED); + REQUIRE((bool)data.Context->GetReturnWord()); +} + + +#endif \ No newline at end of file diff --git a/tests/TestLibrary/TestLibrary.cpp b/tests/TestLibrary/TestLibrary.cpp index add83eb..955b2c9 100644 --- a/tests/TestLibrary/TestLibrary.cpp +++ b/tests/TestLibrary/TestLibrary.cpp @@ -31,3 +31,9 @@ PkmnLib::Library::SpeciesLibrary* TestLibrary::BuildSpeciesLibrary() { 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; +} diff --git a/tests/TestLibrary/TestLibrary.hpp b/tests/TestLibrary/TestLibrary.hpp index fc8a8f1..15913cd 100644 --- a/tests/TestLibrary/TestLibrary.hpp +++ b/tests/TestLibrary/TestLibrary.hpp @@ -38,10 +38,7 @@ public: static PkmnLib::Library::SpeciesLibrary* BuildSpeciesLibrary(); - static PkmnLib::Library::MoveLibrary* BuildMoveLibrary() { - auto lib = new PkmnLib::Library::MoveLibrary(); - return lib; - } + static PkmnLib::Library::MoveLibrary* BuildMoveLibrary(); static PkmnLib::Library::ItemLibrary* BuildItemLibrary() { auto lib = new PkmnLib::Library::ItemLibrary();