CreatureLib/tests/BattleTests/ScriptTests/ScriptSourceTest.cpp

106 lines
3.0 KiB
C++

#ifdef TESTS_BUILD
#include <utility>
#include "../../../extern/catch.hpp"
#include "../../../src/Battling/ScriptHandling/ScriptSource.hpp"
#include "../../../src/Battling/ScriptHandling/ScriptAggregator.hpp"
using namespace CreatureLib;
using namespace CreatureLib::Battling;
class TestScript : public Script{
public:
explicit TestScript(std::string name) : Script(std::move(name)){};
void TestMethod(int& runCount) {
runCount++;
}
};
class ScriptSourceWithScriptPtr : public ScriptSource{
public:
Script* ScriptPtr = nullptr;
protected:
void GetActiveScripts(std::vector<ScriptWrapper> &scripts) override {
scripts.emplace_back(&ScriptPtr);
}
};
class ScriptSourceWithScriptSet : public ScriptSource{
public:
ScriptSet Set;
protected:
void GetActiveScripts(std::vector<ScriptWrapper> &scripts) override {
scripts.emplace_back(&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 Script("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 Script("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 Script("foobar");
source.Set.Add(s);
auto scripts = source.GetScriptIterator();
auto first = scripts.GetNext();
CHECK(first != nullptr);
CHECK (first->GetName() == "foobar");
delete s;
}
TEST_CASE( "Script source with multiple item script set.", "[Battling, Scripting]" ) {
auto source = ScriptSourceWithScriptSet();
auto s = new Script("foobar");
auto s2 = new Script("foobar2");
source.Set.Add(s);
source.Set.Add(s2);
auto scripts = source.GetScriptIterator();
auto first = scripts.GetNext();
CHECK(first != nullptr);
CHECK (first->GetName() == "foobar");
auto second = scripts.GetNext();
CHECK(second != nullptr);
CHECK (second->GetName() == "foobar2");
delete s;
delete s2;
}
#endif