PkmnLib/tests/ScriptTests/ItemUseScriptTests.cpp

88 lines
2.9 KiB
C++
Raw Normal View History

#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