Reworks script aggregator. Cleans up API and code, and now handles scripts being removed from a set while we're iterating.
All checks were successful
continuous-integration/drone/push Build is passing

Signed-off-by: Deukhoofd <Deukhoofd@gmail.com>
This commit is contained in:
2021-04-18 12:50:48 +02:00
parent 13df99a6cc
commit be10b3515c
4 changed files with 83 additions and 80 deletions

View File

@@ -27,8 +27,8 @@ TEST_CASE("Script Aggregator properly iterates containing script.") {
auto vec = ArbUt::List<ScriptWrapper>{
ScriptWrapper::FromScript(reinterpret_cast<std::unique_ptr<BattleScript>*>(&script))};
auto aggr = ScriptAggregator(vec);
while (aggr.HasNext()) {
auto next = aggr.GetNext();
ArbUt::BorrowedPtr<CreatureLib::Battling::BattleScript> next = (CreatureLib::Battling::BattleScript*)1;
while (aggr.GetNext(next)) {
next.As<TestScript>()->TestMethod(ran);
}
CHECK(ran == 1);
@@ -44,8 +44,8 @@ TEST_CASE("Script Aggregator properly iterates multiple scripts.") {
ScriptWrapper::FromScript(reinterpret_cast<std::unique_ptr<BattleScript>*>(&script2)),
ScriptWrapper::FromScript(reinterpret_cast<std::unique_ptr<BattleScript>*>(&script3))};
auto aggr = ScriptAggregator(vec);
while (aggr.HasNext()) {
auto next = aggr.GetNext();
ArbUt::BorrowedPtr<CreatureLib::Battling::BattleScript> next = (CreatureLib::Battling::BattleScript*)1;
while (aggr.GetNext(next)) {
next.As<TestScript>()->TestMethod(ran);
}
CHECK(ran == 3);
@@ -62,13 +62,33 @@ TEST_CASE("Script Aggregator properly iterates Script Set.") {
set.Add(script3);
auto vec = ArbUt::List<ScriptWrapper>{ScriptWrapper::FromSet(&set)};
auto aggr = ScriptAggregator(vec);
while (aggr.HasNext()) {
auto next = aggr.GetNextNotNull();
next.value().As<TestScript>()->TestMethod(ran);
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");
@@ -81,9 +101,9 @@ TEST_CASE("Script Aggregator properly iterates data of Script Set and Script.")
ScriptWrapper::FromSet(&set),
ScriptWrapper::FromScript(reinterpret_cast<std::unique_ptr<BattleScript>*>(&script))};
auto aggr = ScriptAggregator(vec);
while (aggr.HasNext()) {
auto next = aggr.GetNextNotNull();
next.value().As<TestScript>()->TestMethod(ran);
ArbUt::BorrowedPtr<CreatureLib::Battling::BattleScript> next = (CreatureLib::Battling::BattleScript*)1;
while (aggr.GetNext(next)) {
next.As<TestScript>()->TestMethod(ran);
}
CHECK(ran == 3);
}
@@ -100,9 +120,9 @@ TEST_CASE("Script Aggregator properly iterates data of Script and Script Set.")
ArbUt::List<ScriptWrapper>{ScriptWrapper::FromScript(reinterpret_cast<std::unique_ptr<BattleScript>*>(&script)),
ScriptWrapper::FromSet(&set)};
auto aggr = ScriptAggregator(vec);
while (aggr.HasNext()) {
auto next = aggr.GetNextNotNull();
next.value().As<TestScript>()->TestMethod(ran);
ArbUt::BorrowedPtr<CreatureLib::Battling::BattleScript> next = (CreatureLib::Battling::BattleScript*)1;
while (aggr.GetNext(next)) {
next.As<TestScript>()->TestMethod(ran);
}
CHECK(ran == 3);
}
@@ -121,9 +141,9 @@ TEST_CASE("Script Aggregator properly iterates data of Script, Script Set and Sc
ScriptWrapper::FromSet(&set),
ScriptWrapper::FromScript(reinterpret_cast<std::unique_ptr<BattleScript>*>(&script4))};
auto aggr = ScriptAggregator(vec);
while (aggr.HasNext()) {
auto next = aggr.GetNextNotNull();
next.value().As<TestScript>()->TestMethod(ran);
ArbUt::BorrowedPtr<CreatureLib::Battling::BattleScript> next = (CreatureLib::Battling::BattleScript*)1;
while (aggr.GetNext(next)) {
next.As<TestScript>()->TestMethod(ran);
}
CHECK(ran == 4);
}
@@ -143,17 +163,16 @@ TEST_CASE("Script Aggregator properly iterates multiple script sets.") {
};
auto aggr = ScriptAggregator(vec);
auto ran = 0;
while (aggr.HasNext()) {
auto next = aggr.GetNextNotNull();
next.value().As<TestScript>()->TestMethod(ran);
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.HasNext()) {
auto next = aggr.GetNextNotNull();
next.value().As<TestScript>()->TestMethod(ran);
while (aggr.GetNext(next)) {
next.As<TestScript>()->TestMethod(ran);
}
CHECK(ran == 3);
}
@@ -162,9 +181,8 @@ TEST_CASE("Script Aggregator properly iterates when empty.") {
auto ran = 0;
auto vec = ArbUt::List<ScriptWrapper>{};
auto aggr = ScriptAggregator(vec);
while (aggr.HasNext()) {
THROW("Aggregator returned a script, but should have been empty.");
}
ArbUt::BorrowedPtr<CreatureLib::Battling::BattleScript> next = (CreatureLib::Battling::BattleScript*)1;
REQUIRE_FALSE(aggr.GetNext(next));
CHECK(ran == 0);
}
@@ -174,7 +192,8 @@ TEST_CASE("Script Aggregator properly iterates empty Script Set.") {
auto vec = ArbUt::List<ScriptWrapper>{ScriptWrapper::FromSet(&set)};
auto aggr = ScriptAggregator(vec);
aggr.Reset();
while (aggr.HasNext()) {
ArbUt::BorrowedPtr<CreatureLib::Battling::BattleScript> next = (CreatureLib::Battling::BattleScript*)1;
while (aggr.GetNext(next)) {
ran++;
}
CHECK(ran == 0);

View File

@@ -43,30 +43,34 @@ protected:
TEST_CASE("Script source with unset script ptr.") {
auto source = ScriptSourceWithScriptPtr();
auto scripts = source.GetScriptIterator();
REQUIRE_FALSE(scripts.HasNext());
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();
[[maybe_unused]] auto first = scripts.GetNext();
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();
REQUIRE_FALSE(scripts.HasNext());
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();
[[maybe_unused]] auto first = scripts.GetNext();
REQUIRE(scripts.GetNext(next));
}
TEST_CASE("Script source with empty script set.") {
auto source = ScriptSourceWithScriptSet();
auto scripts = source.GetScriptIterator();
scripts.Reset();
REQUIRE_FALSE(scripts.HasNext());
ArbUt::BorrowedPtr<CreatureLib::Battling::BattleScript> next = (CreatureLib::Battling::BattleScript*)1;
REQUIRE_FALSE(scripts.GetNext(next));
}
TEST_CASE("Script source with single item script set.") {
@@ -74,9 +78,9 @@ TEST_CASE("Script source with single item script set.") {
auto s = new TestScript("foobar");
source.Set.Add(s);
auto scripts = source.GetScriptIterator();
auto first = scripts.GetNextNotNull();
REQUIRE(first.has_value());
CHECK(first.value()->GetName() == "foobar");
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.") {
@@ -86,12 +90,11 @@ TEST_CASE("Script source with multiple item script set.") {
source.Set.Add(s);
source.Set.Add(s2);
auto scripts = source.GetScriptIterator();
auto first = scripts.GetNextNotNull();
REQUIRE(first.has_value());
CHECK(first.value()->GetName() == "foobar");
auto second = scripts.GetNextNotNull();
REQUIRE(second.has_value());
CHECK(second.value()->GetName() == "foobar2");
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