Initial support for item use scripts in angelscript.
Some checks failed
continuous-integration/drone/push Build is failing
Some checks failed
continuous-integration/drone/push Build is failing
This commit is contained in:
88
tests/ScriptTests/ItemUseScriptTests.cpp
Normal file
88
tests/ScriptTests/ItemUseScriptTests.cpp
Normal file
@@ -0,0 +1,88 @@
|
||||
#ifdef TESTS_BUILD
|
||||
#include "../../extern/doctest.hpp"
|
||||
#include "../../src/Battling/Pokemon/CreatePokemon.hpp"
|
||||
#include "../../src/ScriptResolving/AngelScript/AngelScriptResolver.hpp"
|
||||
#include "../TestLibrary/TestLibrary.hpp"
|
||||
|
||||
#define AS_CLASS(name, contents) \
|
||||
{ #name, "namespace Pokemon{ [ItemUse effect=" #name "] class " #name " : ItemUseScript { " contents "}}" }
|
||||
|
||||
static std::unordered_map<const char*, const char*> _scripts = std::unordered_map<const char*, const char*>{
|
||||
AS_CLASS(blankClass, R"(
|
||||
)"),
|
||||
|
||||
AS_CLASS(isItemUsable, R"(
|
||||
bool IsItemUsable() override {
|
||||
return true;
|
||||
}
|
||||
)"),
|
||||
|
||||
AS_CLASS(isPokemonUseItem, R"(
|
||||
bool IsPokemonUseItem() override {
|
||||
return true;
|
||||
}
|
||||
)"),
|
||||
};
|
||||
|
||||
static AngelScriptResolver* _resolverCache = nullptr;
|
||||
static AngelScriptResolver* GetScriptResolver(PkmnLib::Battling::BattleLibrary* mainLib) {
|
||||
_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 AngelScriptItemUseScript* GetScript(PkmnLib::Battling::BattleLibrary* mainLib, const ArbUt::StringView& name) {
|
||||
auto lib = GetScriptResolver(mainLib);
|
||||
auto item = CreatureLib::Library::Item(name, CreatureLib::Library::ItemCategory::MiscItem,
|
||||
CreatureLib::Library::BattleItemCategory::None, 0,
|
||||
new CreatureLib::Library::SecondaryEffect(100, name, {}), {});
|
||||
|
||||
auto s = lib->LoadItemScript(&item);
|
||||
auto script = dynamic_cast<AngelScriptItemUseScript*>(s);
|
||||
REQUIRE(script != nullptr);
|
||||
return script;
|
||||
}
|
||||
|
||||
TEST_CASE("Invoke isItemUsable item use script function on empty class") {
|
||||
auto mainLib = TestLibrary::GetLibrary();
|
||||
|
||||
auto script = GetScript(mainLib, "blankClass"_cnc);
|
||||
REQUIRE(script != nullptr);
|
||||
REQUIRE_FALSE(script->IsItemUsable());
|
||||
|
||||
delete script;
|
||||
}
|
||||
TEST_CASE("Invoke isItemUsable item use script function") {
|
||||
auto mainLib = TestLibrary::GetLibrary();
|
||||
|
||||
auto script = GetScript(mainLib, "isItemUsable"_cnc);
|
||||
REQUIRE(script != nullptr);
|
||||
REQUIRE(script->IsItemUsable());
|
||||
|
||||
delete script;
|
||||
}
|
||||
|
||||
TEST_CASE("Invoke isPokemonUseItem item use script function on empty class") {
|
||||
auto mainLib = TestLibrary::GetLibrary();
|
||||
|
||||
auto script = GetScript(mainLib, "blankClass"_cnc);
|
||||
REQUIRE(script != nullptr);
|
||||
REQUIRE_FALSE(script->IsCreatureUseItem());
|
||||
|
||||
delete script;
|
||||
}
|
||||
TEST_CASE("Invoke isPokemonUseItem item use script function") {
|
||||
auto mainLib = TestLibrary::GetLibrary();
|
||||
|
||||
auto script = GetScript(mainLib, "isPokemonUseItem"_cnc);
|
||||
REQUIRE(script != nullptr);
|
||||
REQUIRE(script->IsCreatureUseItem());
|
||||
|
||||
delete script;
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -8,7 +8,7 @@ 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 {
|
||||
class testScript1 : PkmnScript {
|
||||
bool testSpecies(Pokemon@ p, const Species@ species){ return p.Species is species; }
|
||||
bool testForme(Pokemon@ p, const Forme@ forme){ return p.Forme is forme; }
|
||||
bool testLevel(Pokemon@ p, uint8 level){ return p.Level == level; }
|
||||
|
||||
@@ -7,7 +7,7 @@ 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 {
|
||||
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; }
|
||||
|
||||
@@ -7,7 +7,7 @@ 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 {
|
||||
class testScript1 : PkmnScript {
|
||||
bool testName(const Item@ i, const constString &in name){ return i.Name == name; }
|
||||
bool testCategory(const Item@ i, ItemCategory category){ return i.Category == category; }
|
||||
bool testBattleCategory(const Item@ i, BattleItemCategory category){ return i.BattleCategory == category; }
|
||||
|
||||
@@ -7,7 +7,7 @@ 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 {
|
||||
class testScript1 : PkmnScript {
|
||||
bool testName(const MoveData@ s, const constString &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; }
|
||||
|
||||
@@ -7,7 +7,7 @@ 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 {
|
||||
class testScript1 : PkmnScript {
|
||||
bool testName(const Species@ s, const constString &in name){ return s.Name == name; }
|
||||
bool testId(const Species@ s, uint16 id){ return s.Id == id; }
|
||||
bool testGenderRate(const Species@ s, float rate){ return s.GenderRate == rate; }
|
||||
|
||||
@@ -7,7 +7,7 @@ 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 {
|
||||
class testScript1 : PkmnScript {
|
||||
bool testMaximumLevel(const StaticLibrary@ s, uint8 level){ return s.Settings.MaximalLevel == level; }
|
||||
bool testMaximumMoves(const StaticLibrary@ s, uint8 moveCount){ return s.Settings.MaximalMoves == moveCount; }
|
||||
bool testSpeciesLibrary(const StaticLibrary@ s, const SpeciesLibrary@ speciesLib){ return s.SpeciesLibrary is speciesLib; }
|
||||
|
||||
@@ -55,6 +55,6 @@ PkmnLib::Library::ItemLibrary* TestLibrary::BuildItemLibrary() {
|
||||
auto lib = new PkmnLib::Library::ItemLibrary();
|
||||
lib->Insert("testItem"_cnc.GetHash(),
|
||||
new PkmnLib::Library::Item("testItem"_cnc, CreatureLib::Library::ItemCategory::MiscItem,
|
||||
CreatureLib::Library::BattleItemCategory::None, 0, {}, 0));
|
||||
CreatureLib::Library::BattleItemCategory::None, 0, nullptr, {}, 0));
|
||||
return lib;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user