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"
|
2019-11-12 17:38:09 +00:00
|
|
|
#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 {
|
2019-11-12 17:38:09 +00:00
|
|
|
public:
|
|
|
|
explicit TestScript(std::string name) : Script(std::move(name)){};
|
|
|
|
|
2019-11-28 11:55:22 +00:00
|
|
|
void TestMethod(int& runCount) { runCount++; }
|
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:
|
|
|
|
Script* ScriptPtr = nullptr;
|
2019-11-28 11:55:22 +00:00
|
|
|
|
2019-11-12 17:38:09 +00:00
|
|
|
protected:
|
2019-11-28 11:55:22 +00:00
|
|
|
void GetActiveScripts(std::vector<ScriptWrapper>& scripts) override { scripts.emplace_back(&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:
|
2019-11-28 11:55:22 +00:00
|
|
|
void GetActiveScripts(std::vector<ScriptWrapper>& scripts) override { scripts.emplace_back(&Set); }
|
2019-11-12 17:38:09 +00:00
|
|
|
};
|
|
|
|
|
2019-11-28 11:55:22 +00:00
|
|
|
TEST_CASE("Script source with unset script ptr.", "[Battling, Scripting]") {
|
2019-11-12 17:38:09 +00:00
|
|
|
auto source = ScriptSourceWithScriptPtr();
|
|
|
|
auto scripts = source.GetScriptIterator();
|
|
|
|
auto first = scripts.GetNext();
|
|
|
|
CHECK(first == nullptr);
|
|
|
|
}
|
|
|
|
|
2019-11-28 11:55:22 +00:00
|
|
|
TEST_CASE("Script source with script ptr being set.", "[Battling, Scripting]") {
|
2019-11-12 17:38:09 +00:00
|
|
|
auto source = ScriptSourceWithScriptPtr();
|
|
|
|
source.ScriptPtr = new Script("foobar");
|
|
|
|
auto scripts = source.GetScriptIterator();
|
|
|
|
auto first = scripts.GetNext();
|
|
|
|
CHECK(first != nullptr);
|
|
|
|
delete source.ScriptPtr;
|
|
|
|
}
|
|
|
|
|
2019-11-28 11:55:22 +00:00
|
|
|
TEST_CASE("Script source with script ptr being set after first iteration.", "[Battling, Scripting]") {
|
2019-11-12 17:38:09 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-11-28 11:55:22 +00:00
|
|
|
TEST_CASE("Script source with empty script set.", "[Battling, Scripting]") {
|
2019-11-12 17:38:09 +00:00
|
|
|
auto source = ScriptSourceWithScriptSet();
|
|
|
|
auto scripts = source.GetScriptIterator();
|
|
|
|
auto first = scripts.GetNext();
|
|
|
|
CHECK(first == nullptr);
|
|
|
|
}
|
|
|
|
|
2019-11-28 11:55:22 +00:00
|
|
|
TEST_CASE("Script source with single item script set.", "[Battling, Scripting]") {
|
2019-11-12 17:38:09 +00:00
|
|
|
auto source = ScriptSourceWithScriptSet();
|
|
|
|
auto s = new Script("foobar");
|
|
|
|
source.Set.Add(s);
|
|
|
|
auto scripts = source.GetScriptIterator();
|
|
|
|
auto first = scripts.GetNext();
|
|
|
|
CHECK(first != nullptr);
|
2019-11-28 11:55:22 +00:00
|
|
|
CHECK(first->GetName() == "foobar");
|
2019-11-12 17:38:09 +00:00
|
|
|
delete s;
|
|
|
|
}
|
|
|
|
|
2019-11-28 11:55:22 +00:00
|
|
|
TEST_CASE("Script source with multiple item script set.", "[Battling, Scripting]") {
|
2019-11-12 17:38:09 +00:00
|
|
|
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);
|
2019-11-28 11:55:22 +00:00
|
|
|
CHECK(first->GetName() == "foobar");
|
2019-11-12 17:38:09 +00:00
|
|
|
auto second = scripts.GetNext();
|
|
|
|
CHECK(second != nullptr);
|
2019-11-28 11:55:22 +00:00
|
|
|
CHECK(second->GetName() == "foobar2");
|
2019-11-12 17:38:09 +00:00
|
|
|
delete s;
|
2019-11-17 09:42:09 +00:00
|
|
|
delete s2;
|
2019-11-12 17:38:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|