CreatureLib/tests/BattleTests/ScriptTests/ScriptAggregatorTests.cpp

201 lines
7.4 KiB
C++

#ifdef TESTS_BUILD
#include <doctest.h>
#include "../../../src/Battling/ScriptHandling/ScriptAggregator.hpp"
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); }
};
TEST_CASE("Script Aggregator properly iterates containing script.") {
auto script = std::make_unique<TestScript>("test");
auto ran = 0;
auto vec = ArbUt::List<ScriptWrapper>{
ScriptWrapper::FromScript(reinterpret_cast<std::unique_ptr<BattleScript>*>(&script))};
auto aggr = ScriptAggregator(vec);
ArbUt::BorrowedPtr<CreatureLib::Battling::BattleScript> next = (CreatureLib::Battling::BattleScript*)1;
while (aggr.GetNext(next)) {
next.As<TestScript>()->TestMethod(ran);
}
CHECK(ran == 1);
}
TEST_CASE("Script Aggregator properly iterates multiple scripts.") {
auto script = std::make_unique<TestScript>("test");
auto script2 = std::make_unique<TestScript>("test2");
auto script3 = std::make_unique<TestScript>("test3");
auto ran = 0;
auto vec = ArbUt::List<ScriptWrapper>{
ScriptWrapper::FromScript(reinterpret_cast<std::unique_ptr<BattleScript>*>(&script)),
ScriptWrapper::FromScript(reinterpret_cast<std::unique_ptr<BattleScript>*>(&script2)),
ScriptWrapper::FromScript(reinterpret_cast<std::unique_ptr<BattleScript>*>(&script3))};
auto aggr = ScriptAggregator(vec);
ArbUt::BorrowedPtr<CreatureLib::Battling::BattleScript> next = (CreatureLib::Battling::BattleScript*)1;
while (aggr.GetNext(next)) {
next.As<TestScript>()->TestMethod(ran);
}
CHECK(ran == 3);
}
TEST_CASE("Script Aggregator properly iterates Script Set.") {
BattleScript* script = new TestScript("test");
BattleScript* script2 = new TestScript("test2");
BattleScript* script3 = new TestScript("test3");
auto ran = 0;
auto set = ScriptSet();
set.Add(script);
set.Add(script2);
set.Add(script3);
auto vec = ArbUt::List<ScriptWrapper>{ScriptWrapper::FromSet(&set)};
auto aggr = ScriptAggregator(vec);
ArbUt::BorrowedPtr<CreatureLib::Battling::BattleScript> next = (CreatureLib::Battling::BattleScript*)1;
while (aggr.GetNext(next)) {
next.As<TestScript>()->TestMethod(ran);
}
CHECK(ran == 3);
}
TEST_CASE("Script Aggregator properly iterates Script Set when removing from it.") {
BattleScript* script = new TestScript("test");
BattleScript* script2 = new TestScript("test2");
BattleScript* script3 = new TestScript("test3");
auto ran = 0;
auto set = ScriptSet();
set.Add(script);
set.Add(script2);
set.Add(script3);
auto vec = ArbUt::List<ScriptWrapper>{ScriptWrapper::FromSet(&set)};
auto aggr = ScriptAggregator(vec);
ArbUt::BorrowedPtr<CreatureLib::Battling::BattleScript> next = (CreatureLib::Battling::BattleScript*)1;
while (ran <= 2) {
aggr.GetNext(next);
next.As<TestScript>()->TestMethod(ran);
}
set.Remove("test3"_cnc);
REQUIRE_FALSE(aggr.GetNext(next));
}
TEST_CASE("Script Aggregator properly iterates data of Script Set and Script.") {
auto script = std::make_unique<TestScript>("test");
BattleScript* script2 = new TestScript("test2");
BattleScript* script3 = new TestScript("test3");
auto ran = 0;
auto set = ScriptSet();
set.Add(script2);
set.Add(script3);
auto vec = ArbUt::List<ScriptWrapper>{
ScriptWrapper::FromSet(&set),
ScriptWrapper::FromScript(reinterpret_cast<std::unique_ptr<BattleScript>*>(&script))};
auto aggr = ScriptAggregator(vec);
ArbUt::BorrowedPtr<CreatureLib::Battling::BattleScript> next = (CreatureLib::Battling::BattleScript*)1;
while (aggr.GetNext(next)) {
next.As<TestScript>()->TestMethod(ran);
}
CHECK(ran == 3);
}
TEST_CASE("Script Aggregator properly iterates data of Script and Script Set.") {
auto script = std::make_unique<TestScript>("test");
BattleScript* script2 = new TestScript("test2");
BattleScript* script3 = new TestScript("test3");
auto ran = 0;
auto set = ScriptSet();
set.Add(script2);
set.Add(script3);
auto vec =
ArbUt::List<ScriptWrapper>{ScriptWrapper::FromScript(reinterpret_cast<std::unique_ptr<BattleScript>*>(&script)),
ScriptWrapper::FromSet(&set)};
auto aggr = ScriptAggregator(vec);
ArbUt::BorrowedPtr<CreatureLib::Battling::BattleScript> next = (CreatureLib::Battling::BattleScript*)1;
while (aggr.GetNext(next)) {
next.As<TestScript>()->TestMethod(ran);
}
CHECK(ran == 3);
}
TEST_CASE("Script Aggregator properly iterates data of Script, Script Set and Script.") {
auto script = std::make_unique<TestScript>("test");
BattleScript* script2 = new TestScript("test2");
BattleScript* script3 = new TestScript("test3");
auto script4 = std::make_unique<TestScript>("test4");
auto ran = 0;
auto set = ScriptSet();
set.Add(script2);
set.Add(script3);
auto vec = ArbUt::List<ScriptWrapper>{
ScriptWrapper::FromScript(reinterpret_cast<std::unique_ptr<BattleScript>*>(&script)),
ScriptWrapper::FromSet(&set),
ScriptWrapper::FromScript(reinterpret_cast<std::unique_ptr<BattleScript>*>(&script4))};
auto aggr = ScriptAggregator(vec);
ArbUt::BorrowedPtr<CreatureLib::Battling::BattleScript> next = (CreatureLib::Battling::BattleScript*)1;
while (aggr.GetNext(next)) {
next.As<TestScript>()->TestMethod(ran);
}
CHECK(ran == 4);
}
TEST_CASE("Script Aggregator properly iterates multiple script sets.") {
BattleScript* script1 = new TestScript("test");
BattleScript* script2 = new TestScript("test2");
BattleScript* script3 = new TestScript("test3");
auto set1 = ScriptSet();
set1.Add(script2);
set1.Add(script3);
auto set2 = ScriptSet();
set2.Add(script1);
auto vec = ArbUt::List<ScriptWrapper>{
ScriptWrapper::FromSet(&set1),
ScriptWrapper::FromSet(&set2),
};
auto aggr = ScriptAggregator(vec);
auto ran = 0;
ArbUt::BorrowedPtr<CreatureLib::Battling::BattleScript> next = (CreatureLib::Battling::BattleScript*)1;
while (aggr.GetNext(next)) {
next.As<TestScript>()->TestMethod(ran);
}
CHECK(ran == 3);
aggr.Reset();
ran = 0;
while (aggr.GetNext(next)) {
next.As<TestScript>()->TestMethod(ran);
}
CHECK(ran == 3);
}
TEST_CASE("Script Aggregator properly iterates when empty.") {
auto ran = 0;
auto vec = ArbUt::List<ScriptWrapper>{};
auto aggr = ScriptAggregator(vec);
ArbUt::BorrowedPtr<CreatureLib::Battling::BattleScript> next = (CreatureLib::Battling::BattleScript*)1;
REQUIRE_FALSE(aggr.GetNext(next));
CHECK(ran == 0);
}
TEST_CASE("Script Aggregator properly iterates empty Script Set.") {
auto ran = 0;
auto set = ScriptSet();
auto vec = ArbUt::List<ScriptWrapper>{ScriptWrapper::FromSet(&set)};
auto aggr = ScriptAggregator(vec);
aggr.Reset();
ArbUt::BorrowedPtr<CreatureLib::Battling::BattleScript> next = (CreatureLib::Battling::BattleScript*)1;
while (aggr.GetNext(next)) {
ran++;
}
CHECK(ran == 0);
}
#endif