diff --git a/src/Battling/ScriptHandling/ScriptAggregator.hpp b/src/Battling/ScriptHandling/ScriptAggregator.hpp index 0e0654f..b9a47fe 100644 --- a/src/Battling/ScriptHandling/ScriptAggregator.hpp +++ b/src/Battling/ScriptHandling/ScriptAggregator.hpp @@ -22,7 +22,7 @@ namespace CreatureLib::Battling{ }; bool HasNext(){ - return _selfIterator != _selfEnd || _isSetSet; + return _selfIterator != _selfEnd || (_isSetSet && _setIterator != _setEnd); } Script* GetNext(){ @@ -42,6 +42,7 @@ namespace CreatureLib::Battling{ if (_selfIterator == _selfEnd) return nullptr; auto next = *_selfIterator; + _selfIterator++; if (!next.IsSet()){ auto scriptPtr = next.GetScript(); if (scriptPtr == nullptr) diff --git a/tests/BattleTests/ScriptTests/ScriptAggregatorTests.cpp b/tests/BattleTests/ScriptTests/ScriptAggregatorTests.cpp new file mode 100644 index 0000000..ee60961 --- /dev/null +++ b/tests/BattleTests/ScriptTests/ScriptAggregatorTests.cpp @@ -0,0 +1,169 @@ +#ifdef TESTS_BUILD + +#include + +#include "../../../extern/catch.hpp" +#include "../../../src/Battling/ScriptHandling/ScriptAggregator.hpp" + + + +using namespace CreatureLib; +using namespace CreatureLib::Battling; + +class TestScript : public Script{ +public: + explicit TestScript(std::string name) : Script(std::move(name)){}; + + void TestMethod(int& runCount) { + runCount++; + } + +}; + +TEST_CASE( "Script Aggregator properly iterates containing script.", "[Battling, Scripting]" ) { + Script* script = new TestScript("test"); + auto ran = 0; + auto vec = std::vector{&script}; + auto aggr = ScriptAggregator(vec); + while (aggr.HasNext()){ + auto next = aggr.GetNext(); + REQUIRE(next != nullptr); + dynamic_cast(next)->TestMethod(ran); + } + CHECK(ran == 1); + delete script; +} + +TEST_CASE( "Script Aggregator properly iterates multiple scripts.", "[Battling, Scripting]" ) { + Script* script = new TestScript("test"); + Script* script2 = new TestScript("test2"); + Script* script3 = new TestScript("test3"); + auto ran = 0; + auto vec = std::vector{&script, &script2, &script3}; + auto aggr = ScriptAggregator(vec); + while (aggr.HasNext()){ + auto next = aggr.GetNext(); + REQUIRE(next != nullptr); + dynamic_cast(next)->TestMethod(ran); + } + CHECK(ran == 3); + delete script; + delete script2; + delete script3; +} + +TEST_CASE( "Script Aggregator properly iterates Script Set.", "[Battling, Scripting]" ) { + Script* script = new TestScript("test"); + Script* script2 = new TestScript("test2"); + Script* script3 = new TestScript("test3"); + auto ran = 0; + auto set = ScriptSet(); + set.Add(script); + set.Add(script2); + set.Add(script3); + auto vec = std::vector{&set}; + auto aggr = ScriptAggregator(vec); + while (aggr.HasNext()){ + auto next = aggr.GetNext(); + REQUIRE(next != nullptr); + dynamic_cast(next)->TestMethod(ran); + } + CHECK(ran == 3); + delete script; + delete script2; + delete script3; +} + +TEST_CASE( "Script Aggregator properly iterates data of Script Set and Script.", "[Battling, Scripting]" ) { + Script* script = new TestScript("test"); + Script* script2 = new TestScript("test2"); + Script* script3 = new TestScript("test3"); + auto ran = 0; + auto set = ScriptSet(); + set.Add(script2); + set.Add(script3); + auto vec = std::vector{&set, &script}; + auto aggr = ScriptAggregator(vec); + while (aggr.HasNext()){ + auto next = aggr.GetNext(); + REQUIRE(next != nullptr); + dynamic_cast(next)->TestMethod(ran); + } + CHECK(ran == 3); + delete script; + delete script2; + delete script3; +} + +TEST_CASE( "Script Aggregator properly iterates data of Script and Script Set.", "[Battling, Scripting]" ) { + Script* script = new TestScript("test"); + Script* script2 = new TestScript("test2"); + Script* script3 = new TestScript("test3"); + auto ran = 0; + auto set = ScriptSet(); + set.Add(script2); + set.Add(script3); + auto vec = std::vector{&script, &set}; + auto aggr = ScriptAggregator(vec); + while (aggr.HasNext()){ + auto next = aggr.GetNext(); + REQUIRE(next != nullptr); + dynamic_cast(next)->TestMethod(ran); + } + CHECK(ran == 3); + delete script; + delete script2; + delete script3; +} + +TEST_CASE( "Script Aggregator properly iterates data of Script, Script Set and Script.", "[Battling, Scripting]" ) { + Script* script = new TestScript("test"); + Script* script2 = new TestScript("test2"); + Script* script3 = new TestScript("test3"); + Script* script4 = new TestScript("test4"); + auto ran = 0; + auto set = ScriptSet(); + set.Add(script2); + set.Add(script3); + auto vec = std::vector{&script, &set, &script4}; + auto aggr = ScriptAggregator(vec); + while (aggr.HasNext()){ + auto next = aggr.GetNext(); + REQUIRE(next != nullptr); + dynamic_cast(next)->TestMethod(ran); + } + CHECK(ran == 4); + delete script; + delete script2; + delete script3; + delete script4; +} + +TEST_CASE( "Script Aggregator properly iterates when empty.", "[Battling, Scripting]" ) { + auto ran = 0; + auto vec = std::vector{}; + auto aggr = ScriptAggregator(vec); + while (aggr.HasNext()){ + throw CreatureException("Aggregator returned a script, but should have been empty."); + } + CHECK(ran == 0); +} + +TEST_CASE( "Script Aggregator properly iterates empty Script Set.", "[Battling, Scripting]" ) { + auto ran = 0; + auto set = ScriptSet(); + auto vec = std::vector{&set}; + auto aggr = ScriptAggregator(vec); + while (aggr.HasNext()){ + auto next = aggr.GetNext(); + REQUIRE(next == nullptr); + ran++; + } + CHECK(ran == 1); +} + + + + + +#endif \ No newline at end of file