PkmnLib/tests/ScriptTests/WASM/ScriptResolverTests.cpp

63 lines
2.3 KiB
C++

#if TESTS_BUILD && WASM
#include <doctest.h>
#include "../../TestLibrary/TestLibrary.hpp"
#include "../../src/ScriptResolving/WASM/WebAssemblyFunctionCall.hpp"
#include "../../src/ScriptResolving/WASM/WebAssemblyScriptResolver.hpp"
TEST_CASE("Get a script resolver, initialize it, then delete it") {
auto lib = PkmnLib::Battling::BattleLibrary::CreateScriptResolver();
lib->Initialize(TestLibrary::GetLibrary());
delete lib;
}
TEST_CASE("Get a script resolver, load simple wat, run function") {
auto lib = dynamic_cast<WebAssemblyScriptResolver*>(PkmnLib::Battling::BattleLibrary::CreateScriptResolver());
lib->Initialize(TestLibrary::GetLibrary());
auto res = lib->LoadWatFromString(R"(
(module
(type $add_one_t (func (param i32) (result i32)))
(func $add_one_f (type $add_one_t) (param $value i32) (result i32)
local.get $value
i32.const 1
i32.add)
(export "add_one" (func $add_one_f))
)
)");
REQUIRE_EQ(0, res);
lib->Finalize();
auto func_opt = lib->GetFunction<1, 1>("add_one");
REQUIRE(func_opt.has_value());
auto& func = func_opt.value();
func.Loadi32(0, 684);
func.Call();
REQUIRE_EQ(func.GetResultAsi32(), 685);
delete lib;
}
TEST_CASE("Get a script resolver, load a real wasm script") {
auto lib = dynamic_cast<WebAssemblyScriptResolver*>(PkmnLib::Battling::BattleLibrary::CreateScriptResolver());
lib->Initialize(TestLibrary::GetLibrary());
std::string file_path = __FILE__;
std::string dir_path = file_path.substr(0, file_path.rfind("/"));
auto res = lib->LoadWasmFromFile(dir_path + "/gen7_scripts_rs.wasm");
REQUIRE_EQ(0, res);
lib->Finalize();
delete lib;
}
TEST_CASE("Get a script resolver, load a real wasm script, load test script") {
ArbUt::ScopedPtr<WebAssemblyScriptResolver> lib =
dynamic_cast<WebAssemblyScriptResolver*>(PkmnLib::Battling::BattleLibrary::CreateScriptResolver());
lib->Initialize(TestLibrary::GetLibrary());
std::string file_path = __FILE__;
std::string dir_path = file_path.substr(0, file_path.rfind("/"));
auto res = lib->LoadWasmFromFile(dir_path + "/gen7_scripts_rs.wasm");
REQUIRE_EQ(0, res);
lib->Finalize();
auto script = lib->LoadScript(nullptr, ScriptCategory::Attack, "test"_cnc);
EnsureNotNull(script);
script.GetValue()->OnInitialize(TestLibrary::GetLibrary(), {});
}
#endif