CreatureLib/tests/BattleTests/ScriptTests/ScriptSourceTest.cpp

105 lines
3.3 KiB
C++
Raw Normal View History

#ifdef TESTS_BUILD
#include "../../../src/Battling/ScriptHandling/ScriptSource.hpp"
#include <utility>
#include "../../../extern/catch.hpp"
#include "../../../src/Battling/ScriptHandling/ScriptAggregator.hpp"
using namespace CreatureLib;
using namespace CreatureLib::Battling;
class TestScript : public Script {
private:
2020-05-26 16:31:06 +00:00
ArbUt::CaseInsensitiveConstString _name;
public:
explicit TestScript(std::string name) : _name(std::move(name)){};
2020-05-26 16:31:06 +00:00
const ArbUt::CaseInsensitiveConstString& GetName() const noexcept override { return _name; }
void TestMethod(int& runCount) { runCount++; }
};
class ScriptSourceWithScriptPtr : public ScriptSource {
public:
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.", "[Battling, Scripting]") {
auto source = ScriptSourceWithScriptPtr();
auto scripts = source.GetScriptIterator();
auto first = scripts.GetNext();
CHECK(first == nullptr);
}
TEST_CASE("Script source with script ptr being set.", "[Battling, Scripting]") {
auto source = ScriptSourceWithScriptPtr();
source.ScriptPtr = new TestScript("foobar");
auto scripts = source.GetScriptIterator();
auto first = scripts.GetNext();
CHECK(first != nullptr);
delete source.ScriptPtr;
}
TEST_CASE("Script source with script ptr being set after first iteration.", "[Battling, Scripting]") {
auto source = ScriptSourceWithScriptPtr();
auto scripts = source.GetScriptIterator();
auto first = scripts.GetNext();
CHECK(first == nullptr);
source.ScriptPtr = new TestScript("foobar");
scripts = source.GetScriptIterator();
first = scripts.GetNext();
CHECK(first != nullptr);
delete source.ScriptPtr;
}
TEST_CASE("Script source with empty script set.", "[Battling, Scripting]") {
auto source = ScriptSourceWithScriptSet();
auto scripts = source.GetScriptIterator();
auto first = scripts.GetNext();
CHECK(first == nullptr);
}
TEST_CASE("Script source with single item script set.", "[Battling, Scripting]") {
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();
CHECK(first != nullptr);
CHECK(first->GetName() == "foobar");
}
TEST_CASE("Script source with multiple item script set.", "[Battling, Scripting]") {
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 != nullptr);
CHECK(first->GetName() == "foobar");
2020-04-23 21:23:58 +00:00
auto second = scripts.GetNextNotNull();
REQUIRE(second != nullptr);
CHECK(second->GetName() == "foobar2");
}
#endif