2019-11-10 18:55:01 +00:00
|
|
|
#ifdef TESTS_BUILD
|
|
|
|
|
|
|
|
#include <utility>
|
|
|
|
#include "../../../extern/catch.hpp"
|
|
|
|
#include "../../../src/Battling/ScriptHandling/ScriptAggregator.hpp"
|
|
|
|
|
|
|
|
using namespace CreatureLib;
|
|
|
|
using namespace CreatureLib::Battling;
|
|
|
|
|
2019-11-28 11:55:22 +00:00
|
|
|
class TestScript : public Script {
|
2020-02-23 11:58:13 +00:00
|
|
|
private:
|
2020-02-28 18:23:24 +00:00
|
|
|
ConstString _name;
|
2020-02-23 11:58:13 +00:00
|
|
|
|
2019-11-10 18:55:01 +00:00
|
|
|
public:
|
2020-02-23 11:58:13 +00:00
|
|
|
explicit TestScript(std::string name) : _name(std::move(name)){};
|
2020-02-28 18:23:24 +00:00
|
|
|
const ConstString& GetName() const override { return _name; }
|
2019-11-10 18:55:01 +00:00
|
|
|
|
2019-11-28 11:55:22 +00:00
|
|
|
void TestMethod(int& runCount) { runCount++; }
|
2019-11-10 18:55:01 +00:00
|
|
|
};
|
|
|
|
|
2019-11-28 11:55:22 +00:00
|
|
|
TEST_CASE("Script Aggregator properly iterates containing script.", "[Battling, Scripting]") {
|
2019-11-10 18:55:01 +00:00
|
|
|
Script* script = new TestScript("test");
|
|
|
|
auto ran = 0;
|
2020-03-22 18:21:40 +00:00
|
|
|
auto vec = Arbutils::Collections::List<ScriptWrapper>{&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();
|
|
|
|
REQUIRE(next != nullptr);
|
|
|
|
dynamic_cast<TestScript*>(next)->TestMethod(ran);
|
|
|
|
}
|
|
|
|
CHECK(ran == 1);
|
|
|
|
delete script;
|
|
|
|
}
|
|
|
|
|
2019-11-28 11:55:22 +00:00
|
|
|
TEST_CASE("Script Aggregator properly iterates multiple scripts.", "[Battling, Scripting]") {
|
2019-11-10 18:55:01 +00:00
|
|
|
Script* script = new TestScript("test");
|
|
|
|
Script* script2 = new TestScript("test2");
|
|
|
|
Script* script3 = new TestScript("test3");
|
|
|
|
auto ran = 0;
|
2020-03-22 18:21:40 +00:00
|
|
|
auto vec = Arbutils::Collections::List<ScriptWrapper>{&script, &script2, &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();
|
|
|
|
REQUIRE(next != nullptr);
|
|
|
|
dynamic_cast<TestScript*>(next)->TestMethod(ran);
|
|
|
|
}
|
|
|
|
CHECK(ran == 3);
|
|
|
|
delete script;
|
|
|
|
delete script2;
|
|
|
|
delete script3;
|
|
|
|
}
|
|
|
|
|
2019-11-28 11:55:22 +00:00
|
|
|
TEST_CASE("Script Aggregator properly iterates Script Set.", "[Battling, Scripting]") {
|
2019-11-10 18:55:01 +00:00
|
|
|
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);
|
2020-03-22 18:21:40 +00:00
|
|
|
auto vec = Arbutils::Collections::List<ScriptWrapper>{&set};
|
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();
|
|
|
|
REQUIRE(next != nullptr);
|
|
|
|
dynamic_cast<TestScript*>(next)->TestMethod(ran);
|
|
|
|
}
|
|
|
|
CHECK(ran == 3);
|
|
|
|
}
|
|
|
|
|
2019-11-28 11:55:22 +00:00
|
|
|
TEST_CASE("Script Aggregator properly iterates data of Script Set and Script.", "[Battling, Scripting]") {
|
2019-11-10 18:55:01 +00:00
|
|
|
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);
|
2020-03-22 18:21:40 +00:00
|
|
|
auto vec = Arbutils::Collections::List<ScriptWrapper>{&set, &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();
|
|
|
|
REQUIRE(next != nullptr);
|
|
|
|
dynamic_cast<TestScript*>(next)->TestMethod(ran);
|
|
|
|
}
|
|
|
|
CHECK(ran == 3);
|
|
|
|
delete script;
|
|
|
|
}
|
|
|
|
|
2019-11-28 11:55:22 +00:00
|
|
|
TEST_CASE("Script Aggregator properly iterates data of Script and Script Set.", "[Battling, Scripting]") {
|
2019-11-10 18:55:01 +00:00
|
|
|
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);
|
2020-03-22 18:21:40 +00:00
|
|
|
auto vec = Arbutils::Collections::List<ScriptWrapper>{&script, &set};
|
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();
|
|
|
|
REQUIRE(next != nullptr);
|
|
|
|
dynamic_cast<TestScript*>(next)->TestMethod(ran);
|
|
|
|
}
|
|
|
|
CHECK(ran == 3);
|
|
|
|
delete script;
|
|
|
|
}
|
|
|
|
|
2019-11-28 11:55:22 +00:00
|
|
|
TEST_CASE("Script Aggregator properly iterates data of Script, Script Set and Script.", "[Battling, Scripting]") {
|
2019-11-10 18:55:01 +00:00
|
|
|
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);
|
2020-03-22 18:21:40 +00:00
|
|
|
auto vec = Arbutils::Collections::List<ScriptWrapper>{&script, &set, &script4};
|
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();
|
|
|
|
REQUIRE(next != nullptr);
|
|
|
|
dynamic_cast<TestScript*>(next)->TestMethod(ran);
|
|
|
|
}
|
|
|
|
CHECK(ran == 4);
|
|
|
|
delete script;
|
|
|
|
delete script4;
|
|
|
|
}
|
|
|
|
|
2019-11-28 11:55:22 +00:00
|
|
|
TEST_CASE("Script Aggregator properly iterates when empty.", "[Battling, Scripting]") {
|
2019-11-10 18:55:01 +00:00
|
|
|
auto ran = 0;
|
2020-03-22 18:21:40 +00:00
|
|
|
auto vec = Arbutils::Collections::List<ScriptWrapper>{};
|
2019-11-28 11:55:22 +00:00
|
|
|
auto aggr = ScriptAggregator(vec);
|
|
|
|
while (aggr.HasNext()) {
|
2019-11-10 18:55:01 +00:00
|
|
|
throw CreatureException("Aggregator returned a script, but should have been empty.");
|
|
|
|
}
|
|
|
|
CHECK(ran == 0);
|
|
|
|
}
|
|
|
|
|
2019-11-28 11:55:22 +00:00
|
|
|
TEST_CASE("Script Aggregator properly iterates empty Script Set.", "[Battling, Scripting]") {
|
2019-11-10 18:55:01 +00:00
|
|
|
auto ran = 0;
|
|
|
|
auto set = ScriptSet();
|
2020-03-22 18:21:40 +00:00
|
|
|
auto vec = Arbutils::Collections::List<ScriptWrapper>{&set};
|
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();
|
|
|
|
REQUIRE(next == nullptr);
|
|
|
|
ran++;
|
|
|
|
}
|
|
|
|
CHECK(ran == 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|