From 13df99a6ccfcf4ab54ccd2c0a1f3e36e9f3af408 Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Sat, 17 Apr 2021 16:31:03 +0200 Subject: [PATCH] Try and catch more edge cases where setIndex would be invalid. Signed-off-by: Deukhoofd --- src/Battling/ScriptHandling/ScriptAggregator.hpp | 3 +++ .../ScriptTests/ScriptAggregatorTests.cpp | 14 ++++++++++---- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/Battling/ScriptHandling/ScriptAggregator.hpp b/src/Battling/ScriptHandling/ScriptAggregator.hpp index e5b56ac..e8711d3 100644 --- a/src/Battling/ScriptHandling/ScriptAggregator.hpp +++ b/src/Battling/ScriptHandling/ScriptAggregator.hpp @@ -25,6 +25,9 @@ namespace CreatureLib::Battling { _index++; while (HasNext()) { if (_scripts[_index].HasValue()) { + if (_scripts[_index].IsSet()) { + _setIndex = -1; + } return; } _index++; diff --git a/tests/BattleTests/ScriptTests/ScriptAggregatorTests.cpp b/tests/BattleTests/ScriptTests/ScriptAggregatorTests.cpp index 3cc105c..7b53bc4 100644 --- a/tests/BattleTests/ScriptTests/ScriptAggregatorTests.cpp +++ b/tests/BattleTests/ScriptTests/ScriptAggregatorTests.cpp @@ -132,24 +132,30 @@ TEST_CASE("Script Aggregator properly iterates multiple script sets.") { BattleScript* script1 = new TestScript("test"); BattleScript* script2 = new TestScript("test2"); BattleScript* script3 = new TestScript("test3"); - BattleScript* script4 = new TestScript("test4"); - auto ran = 0; auto set1 = ScriptSet(); set1.Add(script2); set1.Add(script3); auto set2 = ScriptSet(); set2.Add(script1); - set2.Add(script4); auto vec = ArbUt::List{ ScriptWrapper::FromSet(&set1), ScriptWrapper::FromSet(&set2), }; auto aggr = ScriptAggregator(vec); + auto ran = 0; while (aggr.HasNext()) { auto next = aggr.GetNextNotNull(); next.value().As()->TestMethod(ran); } - CHECK(ran == 4); + CHECK(ran == 3); + + aggr.Reset(); + ran = 0; + while (aggr.HasNext()) { + auto next = aggr.GetNextNotNull(); + next.value().As()->TestMethod(ran); + } + CHECK(ran == 3); } TEST_CASE("Script Aggregator properly iterates when empty.") {