2020-02-04 18:34:30 +00:00
|
|
|
#ifdef TESTS_BUILD
|
2022-02-11 11:17:27 +00:00
|
|
|
#include <doctest.h>
|
2020-02-04 18:34:30 +00:00
|
|
|
#include "../../src/Battling/Pokemon/CreatePokemon.hpp"
|
2020-04-23 22:05:43 +00:00
|
|
|
#include "../../src/ScriptResolving/AngelScript/AngelScriptResolver.hpp"
|
2021-09-22 17:02:01 +00:00
|
|
|
#include "../../src/ScriptResolving/AngelScript/ContextPool.hpp"
|
2020-02-04 18:34:30 +00:00
|
|
|
#include "../TestLibrary/TestLibrary.hpp"
|
|
|
|
|
2020-02-05 16:06:15 +00:00
|
|
|
#define AS_CLASS(name, contents) \
|
2022-03-26 15:23:24 +00:00
|
|
|
{ \
|
|
|
|
#name, "namespace Pokemon{ [Pokemon effect=" #name "] shared class " #name " : PkmnScript { " contents "}}" \
|
|
|
|
}
|
2020-02-04 18:34:30 +00:00
|
|
|
|
2020-02-05 16:06:15 +00:00
|
|
|
static std::unordered_map<const char*, const char*> _scripts = std::unordered_map<const char*, const char*>{
|
|
|
|
AS_CLASS(blankScript, ),
|
2020-04-10 18:39:11 +00:00
|
|
|
AS_CLASS(initializeScript, R"(
|
2021-09-25 16:33:12 +00:00
|
|
|
uint64 length = 0;
|
2020-04-10 18:39:11 +00:00
|
|
|
bool boolValue = false;
|
|
|
|
int64 intValue = 0;
|
2020-04-16 12:25:20 +00:00
|
|
|
constString stringValue;
|
2022-02-12 16:27:39 +00:00
|
|
|
void OnInitialize(const BattleLibrary@ library, const narray<EffectParameter@>@ parameters) override {
|
2021-09-25 16:33:12 +00:00
|
|
|
length = parameters.Length;
|
2020-04-10 18:39:11 +00:00
|
|
|
boolValue = parameters[0].AsBool();
|
|
|
|
intValue = parameters[1].AsInt();
|
|
|
|
stringValue = parameters[2].AsString();
|
|
|
|
}
|
2021-09-25 16:33:12 +00:00
|
|
|
uint64 GetLength() { return length; }
|
2020-04-10 18:39:11 +00:00
|
|
|
bool GetBoolValue() { return boolValue; }
|
|
|
|
int64 GetIntValue() { return intValue; }
|
2020-04-16 12:25:20 +00:00
|
|
|
constString GetStringValue() { return stringValue; }
|
2020-04-10 18:39:11 +00:00
|
|
|
)"),
|
|
|
|
|
2020-02-05 16:06:15 +00:00
|
|
|
AS_CLASS(stackScript, "int value = 0; void Stack() override { value++; } int GetValue() { return value; }"),
|
2020-02-06 15:25:55 +00:00
|
|
|
AS_CLASS(onRemoveScript, "int value = 0; void OnRemove() override { value++; } int GetValue() { return value; }"),
|
2020-02-05 16:06:15 +00:00
|
|
|
{"doubleInheritanceScript", R"(
|
2020-02-04 18:34:30 +00:00
|
|
|
class doubleInheritanceScriptBase : PkmnScript {
|
|
|
|
int value = 0;
|
|
|
|
void Stack() override{
|
|
|
|
value++;
|
|
|
|
}
|
|
|
|
|
|
|
|
int GetValue(){ return value; }
|
|
|
|
}
|
2020-04-10 12:57:20 +00:00
|
|
|
[Pokemon effect=doubleInheritanceScript]
|
2020-02-04 18:34:30 +00:00
|
|
|
class doubleInheritanceScript : doubleInheritanceScriptBase {}
|
|
|
|
)"},
|
2020-02-05 16:06:15 +00:00
|
|
|
AS_CLASS(preventAttackScript,
|
|
|
|
R"(void PreventAttack(ExecutingMove@ attack, bool& result) override{ result = !result; })"),
|
|
|
|
AS_CLASS(stopBeforeAttackScript, R"(
|
2020-02-04 18:34:30 +00:00
|
|
|
void StopBeforeAttack(ExecutingMove@ attack, bool& result) override{
|
|
|
|
result = !result;
|
|
|
|
})"),
|
2020-02-05 16:06:15 +00:00
|
|
|
AS_CLASS(OnBeforeAttackScript, "int value = 0; void OnBeforeAttack(ExecutingMove@ attack) override { value++; } "
|
|
|
|
"int GetValue() { return value; }"),
|
|
|
|
AS_CLASS(
|
|
|
|
FailIncomingAttackScript,
|
|
|
|
R"(void FailIncomingAttack(ExecutingMove@ attack, Pokemon@ target, bool& result) override{ result = !result; })"),
|
|
|
|
AS_CLASS(
|
|
|
|
IsInvulnerableScript,
|
|
|
|
R"(void IsInvulnerable(ExecutingMove@ attack, Pokemon@ target, bool& result) override{ result = !result; })"),
|
|
|
|
|
|
|
|
AS_CLASS(OnAttackMissScript,
|
2021-03-27 21:19:18 +00:00
|
|
|
R"(
|
|
|
|
int value = 0;
|
|
|
|
void OnAttackMiss(ExecutingMove@ attack, Pokemon@ target) override { value++; }
|
|
|
|
int GetValue() { return value; }
|
|
|
|
)"),
|
2020-02-05 16:06:15 +00:00
|
|
|
AS_CLASS(
|
|
|
|
ChangeAttackTypeScript,
|
|
|
|
R"(void ChangeAttackType(ExecutingMove@ attack, Pokemon@ target, uint8 hit, uint8& outType) override{outType = 1; };)"),
|
2020-04-19 11:41:41 +00:00
|
|
|
AS_CLASS(
|
|
|
|
ChangeEffectivenessScript,
|
|
|
|
R"(void ChangeEffectiveness(ExecutingMove@ attack, Pokemon@ target, uint8 hit, float& eff) override{eff = 0.75; };)"),
|
2020-02-05 16:06:15 +00:00
|
|
|
AS_CLASS(
|
|
|
|
PreventSecondaryEffectsScript,
|
|
|
|
R"(void PreventSecondaryEffects(ExecutingMove@ attack, Pokemon@ target, uint8 hit, bool& result) override{ result = !result; })"),
|
2021-03-27 21:19:18 +00:00
|
|
|
AS_CLASS(OnSecondaryEffectScript, R"(
|
|
|
|
int value = 0;
|
|
|
|
void OnSecondaryEffect(ExecutingMove@ attack, Pokemon@ target, uint8 hit) override {
|
|
|
|
value++;
|
|
|
|
}
|
|
|
|
int GetValue() { return value; })"),
|
2020-02-05 16:06:15 +00:00
|
|
|
AS_CLASS(OnAfterHitsScript,
|
|
|
|
"int value = 0; void OnAfterHits(ExecutingMove@ attack, Pokemon@ target) override { value++; } "
|
|
|
|
"int GetValue() { return value; }"),
|
2020-02-16 10:44:50 +00:00
|
|
|
AS_CLASS(throwScript,
|
|
|
|
R"(void PreventAttack(ExecutingMove@ attack, bool& result) override{ throw("test exception"); })"),
|
|
|
|
|
2021-03-27 21:19:18 +00:00
|
|
|
AS_CLASS(AddVolatileModMain,
|
|
|
|
R"(
|
|
|
|
void OnSecondaryEffect(ExecutingMove@ attack, Pokemon@ target, uint8 hit) override {
|
|
|
|
auto script = cast<AddVolatileModSecondary>(target.AddVolatile("AddVolatileModSecondary"));
|
|
|
|
script.value++;
|
|
|
|
};
|
|
|
|
)"),
|
|
|
|
AS_CLASS(AddVolatileModSecondary,
|
|
|
|
R"(
|
|
|
|
int value = 0;
|
|
|
|
int GetValue() { return value; }
|
|
|
|
)"),
|
|
|
|
|
2020-02-04 18:34:30 +00:00
|
|
|
};
|
|
|
|
|
2020-04-23 22:05:43 +00:00
|
|
|
static AngelScriptResolver* GetScriptResolver(PkmnLib::Battling::BattleLibrary* mainLib) {
|
2021-10-30 09:44:16 +00:00
|
|
|
auto res = dynamic_cast<AngelScriptResolver*>(mainLib->GetScriptResolver().get());
|
|
|
|
res->Reset(mainLib);
|
|
|
|
for (auto kv : _scripts) {
|
|
|
|
res->CreateScript(kv.first, kv.second);
|
2020-02-04 18:34:30 +00:00
|
|
|
}
|
2021-10-30 09:44:16 +00:00
|
|
|
res->FinalizeModule();
|
|
|
|
return res;
|
2020-02-04 18:34:30 +00:00
|
|
|
}
|
|
|
|
|
2020-07-04 13:50:30 +00:00
|
|
|
static AngelScriptScript* GetScript(PkmnLib::Battling::BattleLibrary* mainLib, const ArbUt::StringView& scriptName) {
|
2020-02-04 18:34:30 +00:00
|
|
|
auto lib = GetScriptResolver(mainLib);
|
2021-10-29 21:54:44 +00:00
|
|
|
auto s = lib->LoadScript(nullptr, ScriptCategory::Creature, scriptName);
|
2020-02-04 18:34:30 +00:00
|
|
|
auto script = dynamic_cast<AngelScriptScript*>(s);
|
2020-04-10 12:57:20 +00:00
|
|
|
REQUIRE(script != nullptr);
|
|
|
|
|
2020-02-04 18:34:30 +00:00
|
|
|
return script;
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("Invoke non-implemented script function") {
|
|
|
|
auto mainLib = TestLibrary::GetLibrary();
|
2020-02-28 18:51:50 +00:00
|
|
|
auto script = GetScript(mainLib, "blankScript"_cnc);
|
2020-04-10 12:57:20 +00:00
|
|
|
REQUIRE(script != nullptr);
|
|
|
|
|
2020-02-04 18:34:30 +00:00
|
|
|
script->Stack();
|
|
|
|
delete script;
|
|
|
|
}
|
|
|
|
|
2020-04-10 18:39:11 +00:00
|
|
|
TEST_CASE("Invoke OnInitialize script function") {
|
|
|
|
auto mainLib = TestLibrary::GetLibrary();
|
|
|
|
auto script = GetScript(mainLib, "initializeScript"_cnc);
|
|
|
|
REQUIRE(script != nullptr);
|
|
|
|
|
2020-04-16 12:25:20 +00:00
|
|
|
auto parameters = {new CreatureLib::Library::EffectParameter(true),
|
|
|
|
new CreatureLib::Library::EffectParameter((int64_t)684),
|
2020-07-04 13:50:30 +00:00
|
|
|
new CreatureLib::Library::EffectParameter(ArbUt::StringView("foobar"))};
|
2020-04-10 18:39:11 +00:00
|
|
|
|
2022-02-12 16:27:39 +00:00
|
|
|
script->OnInitialize(mainLib, parameters);
|
2020-04-10 18:39:11 +00:00
|
|
|
|
|
|
|
auto ctxPool = script->GetContextPool();
|
2021-09-25 16:33:12 +00:00
|
|
|
|
2020-04-10 18:39:11 +00:00
|
|
|
auto ctx = ctxPool->RequestContext();
|
2021-09-25 16:33:12 +00:00
|
|
|
script->PrepareMethod("GetLength"_cnc, ctx);
|
|
|
|
REQUIRE(ctx->Execute() == asEXECUTION_FINISHED);
|
|
|
|
REQUIRE_EQ((uint64_t)ctx->GetReturnQWord(), 3);
|
|
|
|
ctxPool->ReturnContextToPool(ctx);
|
2020-04-10 18:39:11 +00:00
|
|
|
|
2021-09-25 16:33:12 +00:00
|
|
|
ctx = ctxPool->RequestContext();
|
2020-04-10 18:39:11 +00:00
|
|
|
script->PrepareMethod("GetBoolValue"_cnc, ctx);
|
|
|
|
REQUIRE(ctx->Execute() == asEXECUTION_FINISHED);
|
|
|
|
REQUIRE((bool)ctx->GetReturnDWord());
|
|
|
|
ctxPool->ReturnContextToPool(ctx);
|
|
|
|
|
|
|
|
ctx = ctxPool->RequestContext();
|
|
|
|
script->PrepareMethod("GetIntValue"_cnc, ctx);
|
|
|
|
REQUIRE(ctx->Execute() == asEXECUTION_FINISHED);
|
|
|
|
REQUIRE(ctx->GetReturnQWord() == 684);
|
|
|
|
ctxPool->ReturnContextToPool(ctx);
|
|
|
|
|
|
|
|
ctx = ctxPool->RequestContext();
|
|
|
|
script->PrepareMethod("GetStringValue"_cnc, ctx);
|
|
|
|
REQUIRE(ctx->Execute() == asEXECUTION_FINISHED);
|
|
|
|
ctxPool->ReturnContextToPool(ctx);
|
|
|
|
|
2020-04-16 12:25:20 +00:00
|
|
|
for (auto p : parameters) {
|
2020-04-10 18:39:11 +00:00
|
|
|
delete p;
|
|
|
|
}
|
|
|
|
delete script;
|
|
|
|
}
|
|
|
|
|
2020-02-04 18:34:30 +00:00
|
|
|
TEST_CASE("Invoke Stack script function") {
|
|
|
|
auto mainLib = TestLibrary::GetLibrary();
|
2020-02-28 18:51:50 +00:00
|
|
|
auto script = GetScript(mainLib, "stackScript"_cnc);
|
2020-04-10 12:57:20 +00:00
|
|
|
REQUIRE(script != nullptr);
|
|
|
|
|
2020-02-04 18:34:30 +00:00
|
|
|
for (int i = 1; i <= 10; i++) {
|
|
|
|
script->Stack();
|
|
|
|
|
|
|
|
auto ctxPool = script->GetContextPool();
|
|
|
|
auto ctx = ctxPool->RequestContext();
|
2020-04-06 18:03:44 +00:00
|
|
|
script->PrepareMethod("GetValue"_cnc, ctx);
|
2020-02-04 18:34:30 +00:00
|
|
|
REQUIRE(ctx->Execute() == asEXECUTION_FINISHED);
|
|
|
|
REQUIRE(ctx->GetReturnDWord() == i);
|
|
|
|
ctxPool->ReturnContextToPool(ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
delete script;
|
|
|
|
}
|
|
|
|
|
2020-02-06 15:25:55 +00:00
|
|
|
TEST_CASE("Invoke OnRemove script function") {
|
|
|
|
auto mainLib = TestLibrary::GetLibrary();
|
2020-02-28 18:51:50 +00:00
|
|
|
auto script = GetScript(mainLib, "onRemoveScript"_cnc);
|
2020-04-10 12:57:20 +00:00
|
|
|
REQUIRE(script != nullptr);
|
|
|
|
|
2020-02-06 15:25:55 +00:00
|
|
|
script->OnRemove();
|
|
|
|
|
|
|
|
auto ctxPool = script->GetContextPool();
|
|
|
|
auto ctx = ctxPool->RequestContext();
|
2020-04-06 18:03:44 +00:00
|
|
|
script->PrepareMethod("GetValue"_cnc, ctx);
|
2020-02-06 15:25:55 +00:00
|
|
|
REQUIRE(ctx->Execute() == asEXECUTION_FINISHED);
|
|
|
|
REQUIRE(ctx->GetReturnDWord() == 1);
|
|
|
|
ctxPool->ReturnContextToPool(ctx);
|
|
|
|
|
|
|
|
delete script;
|
|
|
|
}
|
|
|
|
|
2020-02-04 18:34:30 +00:00
|
|
|
TEST_CASE("Invoke Stack script function with implementation in base class") {
|
|
|
|
auto mainLib = TestLibrary::GetLibrary();
|
2020-02-28 18:51:50 +00:00
|
|
|
auto script = GetScript(mainLib, "doubleInheritanceScript"_cnc);
|
2020-02-04 18:34:30 +00:00
|
|
|
for (int i = 1; i <= 10; i++) {
|
|
|
|
script->Stack();
|
|
|
|
|
|
|
|
auto ctxPool = script->GetContextPool();
|
|
|
|
auto ctx = ctxPool->RequestContext();
|
2020-04-06 18:03:44 +00:00
|
|
|
script->PrepareMethod("GetValue"_cnc, ctx);
|
2020-02-04 18:34:30 +00:00
|
|
|
REQUIRE(ctx->Execute() == asEXECUTION_FINISHED);
|
|
|
|
REQUIRE(ctx->GetReturnDWord() == i);
|
|
|
|
ctxPool->ReturnContextToPool(ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
delete script;
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("Invoke preventAttackScript script function") {
|
|
|
|
auto mainLib = TestLibrary::GetLibrary();
|
2020-02-28 18:51:50 +00:00
|
|
|
auto script = GetScript(mainLib, "preventAttackScript"_cnc);
|
2020-02-04 18:34:30 +00:00
|
|
|
bool b = false;
|
|
|
|
script->PreventAttack(nullptr, &b);
|
|
|
|
REQUIRE(b);
|
|
|
|
|
|
|
|
delete script;
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("Invoke StopBeforeAttack script function") {
|
|
|
|
auto mainLib = TestLibrary::GetLibrary();
|
2020-02-28 18:51:50 +00:00
|
|
|
auto script = GetScript(mainLib, "stopBeforeAttackScript"_cnc);
|
2020-02-04 18:34:30 +00:00
|
|
|
bool b = false;
|
|
|
|
script->StopBeforeAttack(nullptr, &b);
|
|
|
|
REQUIRE(b);
|
|
|
|
|
|
|
|
delete script;
|
|
|
|
}
|
|
|
|
|
2020-02-05 16:06:15 +00:00
|
|
|
TEST_CASE("Invoke OnBeforeAttack script function") {
|
|
|
|
auto mainLib = TestLibrary::GetLibrary();
|
2020-02-28 18:51:50 +00:00
|
|
|
auto script = GetScript(mainLib, "OnBeforeAttackScript"_cnc);
|
2020-04-10 12:57:20 +00:00
|
|
|
REQUIRE(script != nullptr);
|
2020-02-05 16:06:15 +00:00
|
|
|
|
|
|
|
script->OnBeforeAttack(nullptr);
|
|
|
|
|
|
|
|
auto ctxPool = script->GetContextPool();
|
|
|
|
auto ctx = ctxPool->RequestContext();
|
2020-04-06 18:03:44 +00:00
|
|
|
script->PrepareMethod("GetValue"_cnc, ctx);
|
2020-02-05 16:06:15 +00:00
|
|
|
REQUIRE(ctx->Execute() == asEXECUTION_FINISHED);
|
|
|
|
REQUIRE(ctx->GetReturnDWord() == 1);
|
|
|
|
ctxPool->ReturnContextToPool(ctx);
|
|
|
|
|
|
|
|
delete script;
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("Invoke FailIncomingAttack script function") {
|
|
|
|
auto mainLib = TestLibrary::GetLibrary();
|
2020-02-28 18:51:50 +00:00
|
|
|
auto script = GetScript(mainLib, "FailIncomingAttackScript"_cnc);
|
2020-04-10 12:57:20 +00:00
|
|
|
REQUIRE(script != nullptr);
|
2020-02-05 16:06:15 +00:00
|
|
|
bool b = false;
|
|
|
|
script->FailIncomingAttack(nullptr, nullptr, &b);
|
|
|
|
REQUIRE(b);
|
|
|
|
|
|
|
|
delete script;
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("Invoke OnAttackMiss script function") {
|
|
|
|
auto mainLib = TestLibrary::GetLibrary();
|
2020-02-28 18:51:50 +00:00
|
|
|
auto script = GetScript(mainLib, "OnAttackMissScript"_cnc);
|
2020-04-10 12:57:20 +00:00
|
|
|
REQUIRE(script != nullptr);
|
2020-02-05 16:06:15 +00:00
|
|
|
|
|
|
|
script->OnAttackMiss(nullptr, nullptr);
|
|
|
|
|
|
|
|
auto ctxPool = script->GetContextPool();
|
|
|
|
auto ctx = ctxPool->RequestContext();
|
2020-04-06 18:03:44 +00:00
|
|
|
script->PrepareMethod("GetValue"_cnc, ctx);
|
2020-02-05 16:06:15 +00:00
|
|
|
REQUIRE(ctx->Execute() == asEXECUTION_FINISHED);
|
|
|
|
REQUIRE(ctx->GetReturnDWord() == 1);
|
|
|
|
ctxPool->ReturnContextToPool(ctx);
|
|
|
|
|
|
|
|
delete script;
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("Invoke ChangeAttackType script function") {
|
|
|
|
auto mainLib = TestLibrary::GetLibrary();
|
2020-02-28 18:51:50 +00:00
|
|
|
auto script = GetScript(mainLib, "ChangeAttackTypeScript"_cnc);
|
2020-04-10 12:57:20 +00:00
|
|
|
REQUIRE(script != nullptr);
|
2020-02-05 16:06:15 +00:00
|
|
|
uint8_t b = 0;
|
|
|
|
script->ChangeAttackType(nullptr, nullptr, 0, &b);
|
|
|
|
REQUIRE(b == 1);
|
|
|
|
|
|
|
|
delete script;
|
|
|
|
}
|
|
|
|
|
2020-04-19 11:41:41 +00:00
|
|
|
TEST_CASE("Invoke ChangeEffectiveness script function") {
|
|
|
|
auto mainLib = TestLibrary::GetLibrary();
|
|
|
|
auto script = GetScript(mainLib, "ChangeEffectivenessScript"_cnc);
|
|
|
|
REQUIRE(script != nullptr);
|
|
|
|
float b = 0;
|
|
|
|
script->ChangeEffectiveness(nullptr, nullptr, 0, &b);
|
2020-09-29 16:04:06 +00:00
|
|
|
REQUIRE(b == doctest::Approx(0.75));
|
2020-04-19 11:41:41 +00:00
|
|
|
|
|
|
|
delete script;
|
|
|
|
}
|
|
|
|
|
2020-02-05 16:06:15 +00:00
|
|
|
TEST_CASE("Invoke PreventSecondaryEffects script function") {
|
|
|
|
auto mainLib = TestLibrary::GetLibrary();
|
2020-02-28 18:51:50 +00:00
|
|
|
auto script = GetScript(mainLib, "PreventSecondaryEffectsScript"_cnc);
|
2020-02-05 16:06:15 +00:00
|
|
|
bool b = false;
|
|
|
|
script->PreventSecondaryEffects(nullptr, nullptr, 0, &b);
|
|
|
|
REQUIRE(b);
|
|
|
|
|
|
|
|
delete script;
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("Invoke OnSecondaryEffect script function") {
|
|
|
|
auto mainLib = TestLibrary::GetLibrary();
|
2020-02-28 18:51:50 +00:00
|
|
|
auto script = GetScript(mainLib, "OnSecondaryEffectScript"_cnc);
|
2020-02-05 16:06:15 +00:00
|
|
|
|
|
|
|
script->OnSecondaryEffect(nullptr, nullptr, 0);
|
|
|
|
|
|
|
|
auto ctxPool = script->GetContextPool();
|
|
|
|
auto ctx = ctxPool->RequestContext();
|
2020-04-06 18:03:44 +00:00
|
|
|
script->PrepareMethod("GetValue"_cnc, ctx);
|
2020-02-05 16:06:15 +00:00
|
|
|
REQUIRE(ctx->Execute() == asEXECUTION_FINISHED);
|
|
|
|
REQUIRE(ctx->GetReturnDWord() == 1);
|
|
|
|
ctxPool->ReturnContextToPool(ctx);
|
|
|
|
|
|
|
|
delete script;
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("Invoke OnAfterHits script function") {
|
|
|
|
auto mainLib = TestLibrary::GetLibrary();
|
2020-02-28 18:51:50 +00:00
|
|
|
auto script = GetScript(mainLib, "OnAfterHitsScript"_cnc);
|
2020-04-10 12:57:20 +00:00
|
|
|
REQUIRE(script != nullptr);
|
2020-02-05 16:06:15 +00:00
|
|
|
|
|
|
|
script->OnAfterHits(nullptr, nullptr);
|
|
|
|
|
|
|
|
auto ctxPool = script->GetContextPool();
|
|
|
|
auto ctx = ctxPool->RequestContext();
|
2020-04-06 18:03:44 +00:00
|
|
|
script->PrepareMethod("GetValue"_cnc, ctx);
|
2020-02-05 16:06:15 +00:00
|
|
|
REQUIRE(ctx->Execute() == asEXECUTION_FINISHED);
|
|
|
|
REQUIRE(ctx->GetReturnDWord() == 1);
|
|
|
|
ctxPool->ReturnContextToPool(ctx);
|
|
|
|
|
|
|
|
delete script;
|
|
|
|
}
|
2020-02-04 18:34:30 +00:00
|
|
|
|
2020-04-06 18:03:44 +00:00
|
|
|
void TryException(AngelScriptScript* script) {
|
|
|
|
bool b = false;
|
|
|
|
script->PreventAttack(nullptr, &b);
|
|
|
|
}
|
|
|
|
|
2020-04-10 12:57:20 +00:00
|
|
|
TEST_CASE("Get script name.") {
|
|
|
|
auto mainLib = TestLibrary::GetLibrary();
|
|
|
|
auto script = GetScript(mainLib, "throwScript"_cnc);
|
|
|
|
REQUIRE(script != nullptr);
|
|
|
|
INFO(script->GetName().c_str());
|
|
|
|
INFO(script->GetName().std_str());
|
|
|
|
CHECK(strcmp(script->GetName().c_str(), "throwScript") == 0);
|
|
|
|
delete script;
|
|
|
|
}
|
|
|
|
|
2020-02-16 10:44:50 +00:00
|
|
|
TEST_CASE("Handle script exceptions.") {
|
|
|
|
auto mainLib = TestLibrary::GetLibrary();
|
2020-02-28 18:51:50 +00:00
|
|
|
auto script = GetScript(mainLib, "throwScript"_cnc);
|
2020-04-10 12:57:20 +00:00
|
|
|
REQUIRE(script != nullptr);
|
2020-08-16 09:22:43 +00:00
|
|
|
try {
|
2020-08-15 13:21:02 +00:00
|
|
|
TryException(script);
|
2020-08-16 09:22:43 +00:00
|
|
|
} catch (const ArbUt::Exception& e) {
|
2020-08-15 13:21:02 +00:00
|
|
|
REQUIRE(strcmp(e.what(), "Script exception in script 'throwScript', line 1. Message: 'test exception'.") == 0);
|
|
|
|
delete script;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
throw ArbUt::Exception("Didn't throw");
|
2020-02-16 10:44:50 +00:00
|
|
|
}
|
|
|
|
|
2021-06-27 15:11:37 +00:00
|
|
|
static PkmnLib::Library::TimeOfDay GetTime() { return PkmnLib::Library::TimeOfDay::Morning; }
|
2021-03-27 21:19:18 +00:00
|
|
|
TEST_CASE("Add Volatile with return script function") {
|
|
|
|
auto statCalc = new PkmnLib::Battling::StatCalculator();
|
|
|
|
|
|
|
|
auto resolver = dynamic_cast<AngelScriptResolver*>(PkmnLib::Battling::BattleLibrary::CreateScriptResolver());
|
|
|
|
auto mainLib = new PkmnLib::Battling::BattleLibrary(
|
|
|
|
TestLibrary::BuildStaticLibrary(), statCalc, new PkmnLib::Battling::DamageLibrary(),
|
2022-03-26 15:23:24 +00:00
|
|
|
new PkmnLib::Battling::ExperienceLibrary(), resolver, new PkmnLib::Battling::MiscLibrary(GetTime),
|
|
|
|
new PkmnLib::Battling::CaptureLibrary());
|
2021-03-27 21:19:18 +00:00
|
|
|
resolver->Initialize(mainLib);
|
|
|
|
for (auto kv : _scripts) {
|
|
|
|
resolver->CreateScript(kv.first, kv.second);
|
|
|
|
}
|
|
|
|
resolver->FinalizeModule();
|
|
|
|
|
|
|
|
auto script = GetScript(mainLib, "AddVolatileModMain"_cnc);
|
|
|
|
auto mon = PkmnLib::Battling::CreatePokemon(mainLib, "testSpecies"_cnc, 30).Build();
|
|
|
|
|
|
|
|
script->OnSecondaryEffect(nullptr, mon, 0);
|
|
|
|
|
2021-04-18 10:56:46 +00:00
|
|
|
ArbUt::BorrowedPtr<CreatureLib::Battling::BattleScript> scriptObj = (CreatureLib::Battling::BattleScript*)1;
|
|
|
|
REQUIRE(const_cast<CreatureLib::Battling::ScriptAggregator&>(mon->GetScriptIterator()).GetNext(scriptObj));
|
|
|
|
REQUIRE(scriptObj->GetName() == "AddVolatileModSecondary"_cnc);
|
2021-03-27 21:19:18 +00:00
|
|
|
auto script2 = scriptObj.As<AngelScriptScript>();
|
|
|
|
auto ctxPool = script2->GetContextPool();
|
|
|
|
auto ctx = ctxPool->RequestContext();
|
|
|
|
script2->PrepareMethod("GetValue"_cnc, ctx);
|
|
|
|
REQUIRE(ctx->Execute() == asEXECUTION_FINISHED);
|
|
|
|
REQUIRE(ctx->GetReturnDWord() == 1);
|
|
|
|
ctxPool->ReturnContextToPool(ctx);
|
|
|
|
|
2021-03-27 21:29:28 +00:00
|
|
|
delete mon;
|
2021-03-27 21:19:18 +00:00
|
|
|
delete script;
|
2021-03-27 21:29:28 +00:00
|
|
|
delete mainLib;
|
2021-03-27 21:19:18 +00:00
|
|
|
}
|
|
|
|
|
2020-02-04 18:34:30 +00:00
|
|
|
#endif
|