2019-11-12 17:38:09 +00:00
|
|
|
#ifdef TESTS_BUILD
|
|
|
|
|
2019-11-28 11:55:22 +00:00
|
|
|
#include "../../../src/Battling/ScriptHandling/ScriptSource.hpp"
|
2020-09-25 10:43:08 +00:00
|
|
|
#include "../../../extern/doctest.hpp"
|
2019-11-12 17:38:09 +00:00
|
|
|
|
|
|
|
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-12 17:38:09 +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-12 17:38:09 +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-12 17:38:09 +00:00
|
|
|
};
|
|
|
|
|
2019-11-28 11:55:22 +00:00
|
|
|
class ScriptSourceWithScriptPtr : public ScriptSource {
|
2019-11-12 17:38:09 +00:00
|
|
|
public:
|
2021-03-07 09:26:41 +00:00
|
|
|
std::unique_ptr<BattleScript> ScriptPtr = nullptr;
|
2019-11-28 11:55:22 +00:00
|
|
|
|
2019-11-12 17:38:09 +00:00
|
|
|
protected:
|
2020-04-25 09:33:25 +00:00
|
|
|
size_t ScriptCount() const override { return 1; }
|
2020-05-26 16:31:06 +00:00
|
|
|
void GetActiveScripts(ArbUt::List<ScriptWrapper>& scripts) override {
|
2020-04-23 21:23:58 +00:00
|
|
|
scripts.Append(ScriptWrapper::FromScript(&ScriptPtr));
|
|
|
|
}
|
2019-11-12 17:38:09 +00:00
|
|
|
};
|
|
|
|
|
2019-11-28 11:55:22 +00:00
|
|
|
class ScriptSourceWithScriptSet : public ScriptSource {
|
2019-11-12 17:38:09 +00:00
|
|
|
public:
|
|
|
|
ScriptSet Set;
|
2019-11-28 11:55:22 +00:00
|
|
|
|
2019-11-12 17:38:09 +00:00
|
|
|
protected:
|
2020-04-25 09:33:25 +00:00
|
|
|
size_t ScriptCount() const override { return 1; }
|
2020-05-26 16:31:06 +00:00
|
|
|
void GetActiveScripts(ArbUt::List<ScriptWrapper>& scripts) override {
|
2020-04-23 21:23:58 +00:00
|
|
|
scripts.Append(ScriptWrapper::FromSet(&Set));
|
|
|
|
}
|
2019-11-12 17:38:09 +00:00
|
|
|
};
|
|
|
|
|
2020-09-25 10:43:08 +00:00
|
|
|
TEST_CASE("Script source with unset script ptr.") {
|
2019-11-12 17:38:09 +00:00
|
|
|
auto source = ScriptSourceWithScriptPtr();
|
|
|
|
auto scripts = source.GetScriptIterator();
|
2021-04-18 10:50:48 +00:00
|
|
|
ArbUt::BorrowedPtr<CreatureLib::Battling::BattleScript> next = (CreatureLib::Battling::BattleScript*)1;
|
|
|
|
REQUIRE_FALSE(scripts.GetNext(next));
|
2019-11-12 17:38:09 +00:00
|
|
|
}
|
|
|
|
|
2020-09-25 10:43:08 +00:00
|
|
|
TEST_CASE("Script source with script ptr being set.") {
|
2019-11-12 17:38:09 +00:00
|
|
|
auto source = ScriptSourceWithScriptPtr();
|
2020-06-02 11:43:44 +00:00
|
|
|
source.ScriptPtr = std::make_unique<TestScript>("foobar");
|
2019-11-12 17:38:09 +00:00
|
|
|
auto scripts = source.GetScriptIterator();
|
2021-04-18 10:50:48 +00:00
|
|
|
ArbUt::BorrowedPtr<CreatureLib::Battling::BattleScript> next = (CreatureLib::Battling::BattleScript*)1;
|
|
|
|
REQUIRE(scripts.GetNext(next));
|
2019-11-12 17:38:09 +00:00
|
|
|
}
|
|
|
|
|
2020-09-25 10:43:08 +00:00
|
|
|
TEST_CASE("Script source with script ptr being set after first iteration.") {
|
2019-11-12 17:38:09 +00:00
|
|
|
auto source = ScriptSourceWithScriptPtr();
|
|
|
|
auto scripts = source.GetScriptIterator();
|
2021-04-18 10:50:48 +00:00
|
|
|
ArbUt::BorrowedPtr<CreatureLib::Battling::BattleScript> next = (CreatureLib::Battling::BattleScript*)1;
|
|
|
|
REQUIRE_FALSE(scripts.GetNext(next));
|
2020-06-02 11:43:44 +00:00
|
|
|
source.ScriptPtr = std::make_unique<TestScript>("foobar");
|
2019-11-12 17:38:09 +00:00
|
|
|
scripts = source.GetScriptIterator();
|
2021-04-18 10:50:48 +00:00
|
|
|
REQUIRE(scripts.GetNext(next));
|
2019-11-12 17:38:09 +00:00
|
|
|
}
|
|
|
|
|
2020-09-25 10:43:08 +00:00
|
|
|
TEST_CASE("Script source with empty script set.") {
|
2019-11-12 17:38:09 +00:00
|
|
|
auto source = ScriptSourceWithScriptSet();
|
|
|
|
auto scripts = source.GetScriptIterator();
|
2020-06-05 16:06:45 +00:00
|
|
|
scripts.Reset();
|
2021-04-18 10:50:48 +00:00
|
|
|
ArbUt::BorrowedPtr<CreatureLib::Battling::BattleScript> next = (CreatureLib::Battling::BattleScript*)1;
|
|
|
|
REQUIRE_FALSE(scripts.GetNext(next));
|
2019-11-12 17:38:09 +00:00
|
|
|
}
|
|
|
|
|
2020-09-25 10:43:08 +00:00
|
|
|
TEST_CASE("Script source with single item script set.") {
|
2019-11-12 17:38:09 +00:00
|
|
|
auto source = ScriptSourceWithScriptSet();
|
2020-02-23 11:58:13 +00:00
|
|
|
auto s = new TestScript("foobar");
|
2019-11-12 17:38:09 +00:00
|
|
|
source.Set.Add(s);
|
|
|
|
auto scripts = source.GetScriptIterator();
|
2021-04-18 10:50:48 +00:00
|
|
|
ArbUt::BorrowedPtr<CreatureLib::Battling::BattleScript> next = (CreatureLib::Battling::BattleScript*)1;
|
|
|
|
REQUIRE(scripts.GetNext(next));
|
|
|
|
CHECK(next->GetName() == "foobar");
|
2019-11-12 17:38:09 +00:00
|
|
|
}
|
|
|
|
|
2020-09-25 10:43:08 +00:00
|
|
|
TEST_CASE("Script source with multiple item script set.") {
|
2019-11-12 17:38:09 +00:00
|
|
|
auto source = ScriptSourceWithScriptSet();
|
2020-02-23 11:58:13 +00:00
|
|
|
auto s = new TestScript("foobar");
|
|
|
|
auto s2 = new TestScript("foobar2");
|
2019-11-12 17:38:09 +00:00
|
|
|
source.Set.Add(s);
|
|
|
|
source.Set.Add(s2);
|
|
|
|
auto scripts = source.GetScriptIterator();
|
2021-04-18 10:50:48 +00:00
|
|
|
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");
|
2019-11-12 17:38:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|