Actual implementation of Angelscript hooks into battle library.
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:
119
tests/ScriptTests/BaseScriptClassTests.cpp
Normal file
119
tests/ScriptTests/BaseScriptClassTests.cpp
Normal file
@@ -0,0 +1,119 @@
|
||||
#ifdef TESTS_BUILD
|
||||
#include "../../extern/catch.hpp"
|
||||
#include "../../src/Battling/Pokemon/CreatePokemon.hpp"
|
||||
#include "../../src/ScriptResolving/AngelScript/AngelScripResolver.hpp"
|
||||
#include "../TestLibrary/TestLibrary.hpp"
|
||||
|
||||
#define AS_CLASS(name, contents) { #name, "class " #name " : PkmnScript { " contents "}" }
|
||||
|
||||
static std::unordered_map<const char*, const char*> _scripts = std::unordered_map<const char*, const char*> {
|
||||
AS_CLASS(blankScript, ),
|
||||
AS_CLASS( stackScript, "int value = 0; void Stack() override { value++; } int GetValue() { return value; }"),
|
||||
{"doubleInheritanceScript", R"(
|
||||
class doubleInheritanceScriptBase : PkmnScript {
|
||||
int value = 0;
|
||||
void Stack() override{
|
||||
value++;
|
||||
}
|
||||
|
||||
int GetValue(){ return value; }
|
||||
}
|
||||
class doubleInheritanceScript : doubleInheritanceScriptBase {}
|
||||
)"},
|
||||
AS_CLASS( preventAttackScript, R"(
|
||||
void PreventAttack(ExecutingMove@ attack, bool& result) override{
|
||||
result = !result;
|
||||
})"),
|
||||
AS_CLASS( stopBeforeAttackScript, R"(
|
||||
void StopBeforeAttack(ExecutingMove@ attack, bool& result) override{
|
||||
result = !result;
|
||||
})"),
|
||||
|
||||
};
|
||||
|
||||
static const char* _testLoadFunc(const char* name) { return _scripts[name]; }
|
||||
|
||||
static AngelScripResolver* _resolverCache = nullptr;
|
||||
static AngelScripResolver* GetScriptResolver(PkmnLib::Battling::BattleLibrary* mainLib) {
|
||||
if (_resolverCache == nullptr) {
|
||||
_resolverCache = dynamic_cast<AngelScripResolver*>(PkmnLib::Battling::BattleLibrary::CreateScriptResolver());
|
||||
_resolverCache->Initialize(mainLib);
|
||||
_resolverCache->SetCreateFunction(&_testLoadFunc);
|
||||
for (auto kv : _scripts) {
|
||||
_resolverCache->CreateScript(kv.first);
|
||||
}
|
||||
_resolverCache->FinalizeModule();
|
||||
}
|
||||
return _resolverCache;
|
||||
}
|
||||
|
||||
static AngelScriptScript* GetScript(PkmnLib::Battling::BattleLibrary* mainLib, const char* scriptName) {
|
||||
auto lib = GetScriptResolver(mainLib);
|
||||
auto s = lib->LoadScript(AngelScripResolver::ScriptCategory::Creature, scriptName);
|
||||
auto script = dynamic_cast<AngelScriptScript*>(s);
|
||||
return script;
|
||||
}
|
||||
|
||||
TEST_CASE("Invoke non-implemented script function") {
|
||||
auto mainLib = TestLibrary::GetLibrary();
|
||||
auto script = GetScript(mainLib, "blankScript");
|
||||
script->Stack();
|
||||
delete script;
|
||||
}
|
||||
|
||||
TEST_CASE("Invoke Stack script function") {
|
||||
auto mainLib = TestLibrary::GetLibrary();
|
||||
auto script = GetScript(mainLib, "stackScript");
|
||||
for (int i = 1; i <= 10; i++) {
|
||||
script->Stack();
|
||||
|
||||
auto ctxPool = script->GetContextPool();
|
||||
auto ctx = ctxPool->RequestContext();
|
||||
script->PrepareMethod("GetValue", ctx);
|
||||
REQUIRE(ctx->Execute() == asEXECUTION_FINISHED);
|
||||
REQUIRE(ctx->GetReturnDWord() == i);
|
||||
ctxPool->ReturnContextToPool(ctx);
|
||||
}
|
||||
|
||||
delete script;
|
||||
}
|
||||
|
||||
TEST_CASE("Invoke Stack script function with implementation in base class") {
|
||||
auto mainLib = TestLibrary::GetLibrary();
|
||||
auto script = GetScript(mainLib, "doubleInheritanceScript");
|
||||
for (int i = 1; i <= 10; i++) {
|
||||
script->Stack();
|
||||
|
||||
auto ctxPool = script->GetContextPool();
|
||||
auto ctx = ctxPool->RequestContext();
|
||||
script->PrepareMethod("GetValue", ctx);
|
||||
REQUIRE(ctx->Execute() == asEXECUTION_FINISHED);
|
||||
REQUIRE(ctx->GetReturnDWord() == i);
|
||||
ctxPool->ReturnContextToPool(ctx);
|
||||
}
|
||||
|
||||
delete script;
|
||||
}
|
||||
|
||||
TEST_CASE("Invoke preventAttackScript script function") {
|
||||
auto mainLib = TestLibrary::GetLibrary();
|
||||
auto script = GetScript(mainLib, "preventAttackScript");
|
||||
bool b = false;
|
||||
script->PreventAttack(nullptr, &b);
|
||||
REQUIRE(b);
|
||||
|
||||
delete script;
|
||||
}
|
||||
|
||||
TEST_CASE("Invoke StopBeforeAttack script function") {
|
||||
auto mainLib = TestLibrary::GetLibrary();
|
||||
auto script = GetScript(mainLib, "stopBeforeAttackScript");
|
||||
bool b = false;
|
||||
script->StopBeforeAttack(nullptr, &b);
|
||||
REQUIRE(b);
|
||||
|
||||
delete script;
|
||||
}
|
||||
|
||||
|
||||
#endif
|
||||
@@ -89,41 +89,4 @@ TEST_CASE("Build script resolver, create object, invoke addition method") {
|
||||
delete lib;
|
||||
}
|
||||
|
||||
|
||||
TEST_CASE("Test whether species object was properly registered.") {
|
||||
auto lib = dynamic_cast<AngelScripResolver*>(PkmnLib::Battling::BattleLibrary::CreateScriptResolver());
|
||||
auto mainLib = TestLibrary::GetLibrary();
|
||||
lib->Initialize(mainLib);
|
||||
lib->SetCreateFunction(&_testLoadFunc);
|
||||
lib->CreateScript("testScript1");
|
||||
lib->FinalizeModule();
|
||||
auto species = mainLib->GetSpeciesLibrary()->GetPkmnSpecies("testSpecies2");
|
||||
|
||||
auto obj =
|
||||
dynamic_cast<AngelScriptScript*>(lib->LoadScript(AngelScripResolver::ScriptCategory::Creature, "testScript1"));
|
||||
|
||||
auto ctxPool = obj->GetContextPool();
|
||||
auto ctx = ctxPool->RequestContext();
|
||||
|
||||
obj->PrepareMethod("testFunc2", ctx);
|
||||
ctx->SetArgObject(0, const_cast<PkmnLib::Library::PokemonSpecies*>(species));
|
||||
auto result = ctx->Execute();
|
||||
if (result == asEXECUTION_EXCEPTION){
|
||||
FAIL(ctx->GetExceptionString());
|
||||
}
|
||||
ctx->Unprepare();
|
||||
|
||||
species = mainLib->GetSpeciesLibrary()->GetPkmnSpecies("testSpecies");
|
||||
|
||||
obj->PrepareMethod("testFunc2", ctx);
|
||||
ctx->SetArgObject(0, const_cast<PkmnLib::Library::PokemonSpecies*>(species));
|
||||
result = ctx->Execute();
|
||||
REQUIRE(result == asEXECUTION_EXCEPTION);
|
||||
|
||||
ctxPool->ReturnContextToPool(ctx);
|
||||
delete obj;
|
||||
delete lib;
|
||||
}
|
||||
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user