CreatureLib/tests/BattleTests/ScriptTests/ScriptSourceTest.cpp

97 lines
3.0 KiB
C++
Raw Normal View History

#ifdef TESTS_BUILD
#include "../../../src/Battling/ScriptHandling/ScriptSource.hpp"
#include "../../../extern/doctest.hpp"
using namespace CreatureLib;
using namespace CreatureLib::Battling;
class TestScript : public Script {
private:
2020-06-26 15:08:23 +00:00
ArbUt::StringView _name;
public:
2020-07-04 13:12:12 +00:00
explicit TestScript(const std::string& name) : _name(name.c_str(), name.length()){};
2020-06-26 15:08:23 +00:00
const ArbUt::StringView& GetName() const noexcept override { return _name; }
void TestMethod(int& runCount) { runCount++; }
};
class ScriptSourceWithScriptPtr : public ScriptSource {
public:
std::unique_ptr<Script> ScriptPtr = nullptr;
protected:
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));
}
};
class ScriptSourceWithScriptSet : public ScriptSource {
public:
ScriptSet Set;
protected:
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));
}
};
TEST_CASE("Script source with unset script ptr.") {
auto source = ScriptSourceWithScriptPtr();
auto scripts = source.GetScriptIterator();
2020-06-05 16:10:58 +00:00
REQUIRE_FALSE(scripts.HasNext());
}
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();
}
TEST_CASE("Script source with script ptr being set after first iteration.") {
auto source = ScriptSourceWithScriptPtr();
auto scripts = source.GetScriptIterator();
REQUIRE_FALSE(scripts.HasNext());
source.ScriptPtr = std::make_unique<TestScript>("foobar");
scripts = source.GetScriptIterator();
[[maybe_unused]] auto first = scripts.GetNext();
}
TEST_CASE("Script source with empty script set.") {
auto source = ScriptSourceWithScriptSet();
auto scripts = source.GetScriptIterator();
scripts.Reset();
REQUIRE_FALSE(scripts.HasNext());
}
TEST_CASE("Script source with single item script set.") {
auto source = ScriptSourceWithScriptSet();
auto s = new TestScript("foobar");
source.Set.Add(s);
auto scripts = source.GetScriptIterator();
2020-04-23 21:23:58 +00:00
auto first = scripts.GetNextNotNull();
REQUIRE(first.has_value());
CHECK(first.value()->GetName() == "foobar");
}
TEST_CASE("Script source with multiple item script set.") {
auto source = ScriptSourceWithScriptSet();
auto s = new TestScript("foobar");
auto s2 = new TestScript("foobar2");
source.Set.Add(s);
source.Set.Add(s2);
auto scripts = source.GetScriptIterator();
2020-04-23 21:23:58 +00:00
auto first = scripts.GetNextNotNull();
REQUIRE(first.has_value());
CHECK(first.value()->GetName() == "foobar");
2020-04-23 21:23:58 +00:00
auto second = scripts.GetNextNotNull();
REQUIRE(second.has_value());
CHECK(second.value()->GetName() == "foobar2");
}
#endif