2019-11-10 18:55:01 +00:00
|
|
|
#ifdef TESTS_BUILD
|
|
|
|
|
|
|
|
#include <utility>
|
2020-09-25 10:43:08 +00:00
|
|
|
#include "../../../extern/doctest.hpp"
|
2019-11-10 18:55:01 +00:00
|
|
|
#include "../../../src/Battling/ScriptHandling/ScriptAggregator.hpp"
|
|
|
|
|
|
|
|
using namespace CreatureLib;
|
|
|
|
using namespace CreatureLib::Battling;
|
|
|
|
|
2021-03-07 09:26:41 +00:00
|
|
|
class TestScript : public BattleScript {
|
2020-02-23 11:58:13 +00:00
|
|
|
private:
|
2020-06-26 15:08:23 +00:00
|
|
|
ArbUt::StringView _name;
|
2020-02-23 11:58:13 +00:00
|
|
|
|
2019-11-10 18:55:01 +00:00
|
|
|
public:
|
2021-04-11 13:20:50 +00:00
|
|
|
explicit TestScript(const ArbUt::StringView& name) : _name(name){};
|
2020-06-26 15:08:23 +00:00
|
|
|
const ArbUt::StringView& GetName() const noexcept override { return _name; }
|
2019-11-10 18:55:01 +00:00
|
|
|
|
2019-11-28 11:55:22 +00:00
|
|
|
void TestMethod(int& runCount) { runCount++; }
|
2021-04-11 13:20:50 +00:00
|
|
|
BattleScript* Clone() override { return new TestScript(_name); }
|
2019-11-10 18:55:01 +00:00
|
|
|
};
|
|
|
|
|
2020-09-25 10:43:08 +00:00
|
|
|
TEST_CASE("Script Aggregator properly iterates containing script.") {
|
2020-06-02 11:43:44 +00:00
|
|
|
auto script = std::make_unique<TestScript>("test");
|
2019-11-10 18:55:01 +00:00
|
|
|
auto ran = 0;
|
2021-03-07 09:26:41 +00:00
|
|
|
auto vec = ArbUt::List<ScriptWrapper>{
|
|
|
|
ScriptWrapper::FromScript(reinterpret_cast<std::unique_ptr<BattleScript>*>(&script))};
|
2019-11-28 11:55:22 +00:00
|
|
|
auto aggr = ScriptAggregator(vec);
|
|
|
|
while (aggr.HasNext()) {
|
2019-11-10 18:55:01 +00:00
|
|
|
auto next = aggr.GetNext();
|
2020-06-05 16:06:45 +00:00
|
|
|
next.As<TestScript>()->TestMethod(ran);
|
2019-11-10 18:55:01 +00:00
|
|
|
}
|
|
|
|
CHECK(ran == 1);
|
|
|
|
}
|
|
|
|
|
2020-09-25 10:43:08 +00:00
|
|
|
TEST_CASE("Script Aggregator properly iterates multiple scripts.") {
|
2020-06-02 11:43:44 +00:00
|
|
|
auto script = std::make_unique<TestScript>("test");
|
|
|
|
auto script2 = std::make_unique<TestScript>("test2");
|
|
|
|
auto script3 = std::make_unique<TestScript>("test3");
|
2019-11-10 18:55:01 +00:00
|
|
|
auto ran = 0;
|
2021-03-07 09:26:41 +00:00
|
|
|
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))};
|
2019-11-28 11:55:22 +00:00
|
|
|
auto aggr = ScriptAggregator(vec);
|
|
|
|
while (aggr.HasNext()) {
|
2019-11-10 18:55:01 +00:00
|
|
|
auto next = aggr.GetNext();
|
2020-06-05 16:06:45 +00:00
|
|
|
next.As<TestScript>()->TestMethod(ran);
|
2019-11-10 18:55:01 +00:00
|
|
|
}
|
|
|
|
CHECK(ran == 3);
|
|
|
|
}
|
|
|
|
|
2020-09-25 10:43:08 +00:00
|
|
|
TEST_CASE("Script Aggregator properly iterates Script Set.") {
|
2021-03-07 09:26:41 +00:00
|
|
|
BattleScript* script = new TestScript("test");
|
|
|
|
BattleScript* script2 = new TestScript("test2");
|
|
|
|
BattleScript* script3 = new TestScript("test3");
|
2019-11-10 18:55:01 +00:00
|
|
|
auto ran = 0;
|
|
|
|
auto set = ScriptSet();
|
|
|
|
set.Add(script);
|
|
|
|
set.Add(script2);
|
|
|
|
set.Add(script3);
|
2020-05-26 16:31:06 +00:00
|
|
|
auto vec = ArbUt::List<ScriptWrapper>{ScriptWrapper::FromSet(&set)};
|
2019-11-28 11:55:22 +00:00
|
|
|
auto aggr = ScriptAggregator(vec);
|
|
|
|
while (aggr.HasNext()) {
|
2020-04-23 21:23:58 +00:00
|
|
|
auto next = aggr.GetNextNotNull();
|
2020-12-12 11:22:48 +00:00
|
|
|
next.value().As<TestScript>()->TestMethod(ran);
|
2019-11-10 18:55:01 +00:00
|
|
|
}
|
|
|
|
CHECK(ran == 3);
|
|
|
|
}
|
|
|
|
|
2020-09-25 10:43:08 +00:00
|
|
|
TEST_CASE("Script Aggregator properly iterates data of Script Set and Script.") {
|
2020-06-02 11:43:44 +00:00
|
|
|
auto script = std::make_unique<TestScript>("test");
|
2021-03-07 09:26:41 +00:00
|
|
|
BattleScript* script2 = new TestScript("test2");
|
|
|
|
BattleScript* script3 = new TestScript("test3");
|
2019-11-10 18:55:01 +00:00
|
|
|
auto ran = 0;
|
|
|
|
auto set = ScriptSet();
|
|
|
|
set.Add(script2);
|
|
|
|
set.Add(script3);
|
2020-06-02 11:43:44 +00:00
|
|
|
auto vec = ArbUt::List<ScriptWrapper>{
|
2021-03-07 09:26:41 +00:00
|
|
|
ScriptWrapper::FromSet(&set),
|
|
|
|
ScriptWrapper::FromScript(reinterpret_cast<std::unique_ptr<BattleScript>*>(&script))};
|
2019-11-28 11:55:22 +00:00
|
|
|
auto aggr = ScriptAggregator(vec);
|
|
|
|
while (aggr.HasNext()) {
|
2020-04-23 21:23:58 +00:00
|
|
|
auto next = aggr.GetNextNotNull();
|
2020-12-12 11:22:48 +00:00
|
|
|
next.value().As<TestScript>()->TestMethod(ran);
|
2019-11-10 18:55:01 +00:00
|
|
|
}
|
|
|
|
CHECK(ran == 3);
|
|
|
|
}
|
|
|
|
|
2020-09-25 10:43:08 +00:00
|
|
|
TEST_CASE("Script Aggregator properly iterates data of Script and Script Set.") {
|
2020-06-02 11:43:44 +00:00
|
|
|
auto script = std::make_unique<TestScript>("test");
|
2021-03-07 09:26:41 +00:00
|
|
|
BattleScript* script2 = new TestScript("test2");
|
|
|
|
BattleScript* script3 = new TestScript("test3");
|
2019-11-10 18:55:01 +00:00
|
|
|
auto ran = 0;
|
|
|
|
auto set = ScriptSet();
|
|
|
|
set.Add(script2);
|
|
|
|
set.Add(script3);
|
2021-03-07 09:26:41 +00:00
|
|
|
auto vec =
|
|
|
|
ArbUt::List<ScriptWrapper>{ScriptWrapper::FromScript(reinterpret_cast<std::unique_ptr<BattleScript>*>(&script)),
|
|
|
|
ScriptWrapper::FromSet(&set)};
|
2019-11-28 11:55:22 +00:00
|
|
|
auto aggr = ScriptAggregator(vec);
|
|
|
|
while (aggr.HasNext()) {
|
2020-04-23 21:23:58 +00:00
|
|
|
auto next = aggr.GetNextNotNull();
|
2020-12-12 11:22:48 +00:00
|
|
|
next.value().As<TestScript>()->TestMethod(ran);
|
2019-11-10 18:55:01 +00:00
|
|
|
}
|
|
|
|
CHECK(ran == 3);
|
|
|
|
}
|
|
|
|
|
2020-09-25 10:43:08 +00:00
|
|
|
TEST_CASE("Script Aggregator properly iterates data of Script, Script Set and Script.") {
|
2020-06-02 11:43:44 +00:00
|
|
|
auto script = std::make_unique<TestScript>("test");
|
2021-03-07 09:26:41 +00:00
|
|
|
BattleScript* script2 = new TestScript("test2");
|
|
|
|
BattleScript* script3 = new TestScript("test3");
|
2020-06-02 11:43:44 +00:00
|
|
|
auto script4 = std::make_unique<TestScript>("test4");
|
2019-11-10 18:55:01 +00:00
|
|
|
auto ran = 0;
|
|
|
|
auto set = ScriptSet();
|
|
|
|
set.Add(script2);
|
|
|
|
set.Add(script3);
|
2020-06-02 11:43:44 +00:00
|
|
|
auto vec = ArbUt::List<ScriptWrapper>{
|
2021-03-07 09:26:41 +00:00
|
|
|
ScriptWrapper::FromScript(reinterpret_cast<std::unique_ptr<BattleScript>*>(&script)),
|
|
|
|
ScriptWrapper::FromSet(&set),
|
|
|
|
ScriptWrapper::FromScript(reinterpret_cast<std::unique_ptr<BattleScript>*>(&script4))};
|
2019-11-28 11:55:22 +00:00
|
|
|
auto aggr = ScriptAggregator(vec);
|
|
|
|
while (aggr.HasNext()) {
|
2020-04-23 21:23:58 +00:00
|
|
|
auto next = aggr.GetNextNotNull();
|
2020-12-12 11:22:48 +00:00
|
|
|
next.value().As<TestScript>()->TestMethod(ran);
|
2019-11-10 18:55:01 +00:00
|
|
|
}
|
|
|
|
CHECK(ran == 4);
|
|
|
|
}
|
|
|
|
|
2020-09-25 10:43:08 +00:00
|
|
|
TEST_CASE("Script Aggregator properly iterates when empty.") {
|
2019-11-10 18:55:01 +00:00
|
|
|
auto ran = 0;
|
2020-05-26 16:31:06 +00:00
|
|
|
auto vec = ArbUt::List<ScriptWrapper>{};
|
2019-11-28 11:55:22 +00:00
|
|
|
auto aggr = ScriptAggregator(vec);
|
|
|
|
while (aggr.HasNext()) {
|
2020-08-17 10:18:01 +00:00
|
|
|
THROW("Aggregator returned a script, but should have been empty.");
|
2019-11-10 18:55:01 +00:00
|
|
|
}
|
|
|
|
CHECK(ran == 0);
|
|
|
|
}
|
|
|
|
|
2020-09-25 10:43:08 +00:00
|
|
|
TEST_CASE("Script Aggregator properly iterates empty Script Set.") {
|
2019-11-10 18:55:01 +00:00
|
|
|
auto ran = 0;
|
|
|
|
auto set = ScriptSet();
|
2020-05-26 16:31:06 +00:00
|
|
|
auto vec = ArbUt::List<ScriptWrapper>{ScriptWrapper::FromSet(&set)};
|
2019-11-28 11:55:22 +00:00
|
|
|
auto aggr = ScriptAggregator(vec);
|
2020-06-05 16:06:45 +00:00
|
|
|
aggr.Reset();
|
2019-11-28 11:55:22 +00:00
|
|
|
while (aggr.HasNext()) {
|
2019-11-10 18:55:01 +00:00
|
|
|
ran++;
|
|
|
|
}
|
2020-06-05 16:06:45 +00:00
|
|
|
CHECK(ran == 0);
|
2019-11-10 18:55:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|