101 lines
3.7 KiB
C++
101 lines
3.7 KiB
C++
#ifdef TESTS_BUILD
|
|
|
|
#include "../../../src/Battling/ScriptHandling/ScriptSource.hpp"
|
|
#include <doctest.h>
|
|
|
|
using namespace CreatureLib;
|
|
using namespace CreatureLib::Battling;
|
|
|
|
class TestScript : public BattleScript {
|
|
private:
|
|
ArbUt::StringView _name;
|
|
|
|
public:
|
|
explicit TestScript(const ArbUt::StringView& name) : BattleScript(nullptr), _name(name){};
|
|
const ArbUt::StringView& GetName() const noexcept override { return _name; }
|
|
|
|
void TestMethod(int& runCount) { runCount++; }
|
|
BattleScript* Clone(const ArbUt::OptionalBorrowedPtr<void>&) override { return new TestScript(_name); }
|
|
};
|
|
|
|
class ScriptSourceWithScriptPtr : public ScriptSource {
|
|
public:
|
|
std::unique_ptr<BattleScript> ScriptPtr = nullptr;
|
|
|
|
protected:
|
|
size_t ScriptCount() const override { return 1; }
|
|
void GetActiveScripts(ArbUt::List<ScriptWrapper>& scripts) override { GetOwnScripts(scripts); }
|
|
void GetOwnScripts(ArbUt::List<ScriptWrapper>& scripts) override {
|
|
scripts.Append(ScriptWrapper::FromScript(&ScriptPtr));
|
|
}
|
|
};
|
|
|
|
class ScriptSourceWithScriptSet : public ScriptSource {
|
|
public:
|
|
ScriptSet Set;
|
|
|
|
protected:
|
|
size_t ScriptCount() const override { return 1; }
|
|
void GetActiveScripts(ArbUt::List<ScriptWrapper>& scripts) override { GetOwnScripts(scripts); }
|
|
void GetOwnScripts(ArbUt::List<ScriptWrapper>& scripts) override { scripts.Append(ScriptWrapper::FromSet(&Set)); }
|
|
};
|
|
|
|
TEST_CASE("Script source with unset script ptr.") {
|
|
auto source = ScriptSourceWithScriptPtr();
|
|
auto scripts = source.GetScriptIterator();
|
|
ArbUt::BorrowedPtr<CreatureLib::Battling::BattleScript> next = (CreatureLib::Battling::BattleScript*)1;
|
|
REQUIRE_FALSE(scripts.GetNext(next));
|
|
}
|
|
|
|
TEST_CASE("Script source with script ptr being set.") {
|
|
auto source = ScriptSourceWithScriptPtr();
|
|
source.ScriptPtr = std::make_unique<TestScript>("foobar");
|
|
auto scripts = source.GetScriptIterator();
|
|
ArbUt::BorrowedPtr<CreatureLib::Battling::BattleScript> next = (CreatureLib::Battling::BattleScript*)1;
|
|
REQUIRE(scripts.GetNext(next));
|
|
}
|
|
|
|
TEST_CASE("Script source with script ptr being set after first iteration.") {
|
|
auto source = ScriptSourceWithScriptPtr();
|
|
auto scripts = source.GetScriptIterator();
|
|
ArbUt::BorrowedPtr<CreatureLib::Battling::BattleScript> next = (CreatureLib::Battling::BattleScript*)1;
|
|
REQUIRE_FALSE(scripts.GetNext(next));
|
|
source.ScriptPtr = std::make_unique<TestScript>("foobar");
|
|
scripts = source.GetScriptIterator();
|
|
REQUIRE(scripts.GetNext(next));
|
|
}
|
|
|
|
TEST_CASE("Script source with empty script set.") {
|
|
auto source = ScriptSourceWithScriptSet();
|
|
auto scripts = source.GetScriptIterator();
|
|
scripts.Reset();
|
|
ArbUt::BorrowedPtr<CreatureLib::Battling::BattleScript> next = (CreatureLib::Battling::BattleScript*)1;
|
|
REQUIRE_FALSE(scripts.GetNext(next));
|
|
}
|
|
|
|
TEST_CASE("Script source with single item script set.") {
|
|
auto source = ScriptSourceWithScriptSet();
|
|
auto s = new TestScript("foobar");
|
|
source.Set.Add(s);
|
|
auto scripts = source.GetScriptIterator();
|
|
ArbUt::BorrowedPtr<CreatureLib::Battling::BattleScript> next = (CreatureLib::Battling::BattleScript*)1;
|
|
REQUIRE(scripts.GetNext(next));
|
|
CHECK(next->GetName() == "foobar");
|
|
}
|
|
|
|
TEST_CASE("Script source with multiple item script set.") {
|
|
auto source = ScriptSourceWithScriptSet();
|
|
auto s = new TestScript("foobar");
|
|
auto s2 = new TestScript("foobar2");
|
|
source.Set.Add(s);
|
|
source.Set.Add(s2);
|
|
auto scripts = source.GetScriptIterator();
|
|
ArbUt::BorrowedPtr<CreatureLib::Battling::BattleScript> next = (CreatureLib::Battling::BattleScript*)1;
|
|
REQUIRE(scripts.GetNext(next));
|
|
CHECK(next->GetName() == "foobar");
|
|
REQUIRE(scripts.GetNext(next));
|
|
CHECK(next->GetName() == "foobar2");
|
|
}
|
|
|
|
#endif
|