#ifdef TESTS_BUILD #include #include "../../../extern/doctest.hpp" #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) : _name(name){}; const ArbUt::StringView& GetName() const noexcept override { return _name; } void TestMethod(int& runCount) { runCount++; } BattleScript* Clone() override { return new TestScript(_name); } }; TEST_CASE("Script Aggregator properly iterates containing script.") { auto script = std::make_unique("test"); auto ran = 0; auto vec = ArbUt::List{ ScriptWrapper::FromScript(reinterpret_cast*>(&script))}; auto aggr = ScriptAggregator(vec); while (aggr.HasNext()) { auto next = aggr.GetNext(); next.As()->TestMethod(ran); } CHECK(ran == 1); } TEST_CASE("Script Aggregator properly iterates multiple scripts.") { auto script = std::make_unique("test"); auto script2 = std::make_unique("test2"); auto script3 = std::make_unique("test3"); auto ran = 0; auto vec = ArbUt::List{ ScriptWrapper::FromScript(reinterpret_cast*>(&script)), ScriptWrapper::FromScript(reinterpret_cast*>(&script2)), ScriptWrapper::FromScript(reinterpret_cast*>(&script3))}; auto aggr = ScriptAggregator(vec); while (aggr.HasNext()) { auto next = aggr.GetNext(); next.As()->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::FromSet(&set)}; auto aggr = ScriptAggregator(vec); while (aggr.HasNext()) { auto next = aggr.GetNextNotNull(); next.value().As()->TestMethod(ran); } CHECK(ran == 3); } TEST_CASE("Script Aggregator properly iterates data of Script Set and Script.") { auto script = std::make_unique("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::FromSet(&set), ScriptWrapper::FromScript(reinterpret_cast*>(&script))}; auto aggr = ScriptAggregator(vec); while (aggr.HasNext()) { auto next = aggr.GetNextNotNull(); next.value().As()->TestMethod(ran); } CHECK(ran == 3); } TEST_CASE("Script Aggregator properly iterates data of Script and Script Set.") { auto script = std::make_unique("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::FromScript(reinterpret_cast*>(&script)), ScriptWrapper::FromSet(&set)}; auto aggr = ScriptAggregator(vec); while (aggr.HasNext()) { auto next = aggr.GetNextNotNull(); next.value().As()->TestMethod(ran); } CHECK(ran == 3); } TEST_CASE("Script Aggregator properly iterates data of Script, Script Set and Script.") { auto script = std::make_unique("test"); BattleScript* script2 = new TestScript("test2"); BattleScript* script3 = new TestScript("test3"); auto script4 = std::make_unique("test4"); auto ran = 0; auto set = ScriptSet(); set.Add(script2); set.Add(script3); auto vec = ArbUt::List{ ScriptWrapper::FromScript(reinterpret_cast*>(&script)), ScriptWrapper::FromSet(&set), ScriptWrapper::FromScript(reinterpret_cast*>(&script4))}; auto aggr = ScriptAggregator(vec); while (aggr.HasNext()) { auto next = aggr.GetNextNotNull(); next.value().As()->TestMethod(ran); } CHECK(ran == 4); } TEST_CASE("Script Aggregator properly iterates when empty.") { auto ran = 0; auto vec = ArbUt::List{}; auto aggr = ScriptAggregator(vec); while (aggr.HasNext()) { THROW("Aggregator returned a script, but should have been empty."); } CHECK(ran == 0); } TEST_CASE("Script Aggregator properly iterates empty Script Set.") { auto ran = 0; auto set = ScriptSet(); auto vec = ArbUt::List{ScriptWrapper::FromSet(&set)}; auto aggr = ScriptAggregator(vec); aggr.Reset(); while (aggr.HasNext()) { ran++; } CHECK(ran == 0); } #endif