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 {
|
2020-02-23 11:58:13 +00:00
|
|
|
private:
|
2020-05-26 16:31:06 +00:00
|
|
|
ArbUt::CaseInsensitiveConstString _name;
|
2020-02-23 11:58:13 +00:00
|
|
|
|
2019-11-12 17:38:09 +00:00
|
|
|
public:
|
2020-02-23 11:58:13 +00:00
|
|
|
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; }
|
2019-11-12 17:38:09 +00:00
|
|
|
|
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:
|
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
|
|
|
};
|
|
|
|
|
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();
|
2020-02-23 11:58:13 +00:00
|
|
|
source.ScriptPtr = new TestScript("foobar");
|
2019-11-12 17:38:09 +00:00
|
|
|
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);
|
2020-02-23 11:58:13 +00:00
|
|
|
source.ScriptPtr = new TestScript("foobar");
|
2019-11-12 17:38:09 +00:00
|
|
|
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();
|
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();
|
2020-04-23 21:23:58 +00:00
|
|
|
auto first = scripts.GetNextNotNull();
|
2019-11-12 17:38:09 +00:00
|
|
|
CHECK(first != nullptr);
|
2019-11-28 11:55:22 +00:00
|
|
|
CHECK(first->GetName() == "foobar");
|
2019-11-12 17:38:09 +00:00
|
|
|
}
|
|
|
|
|
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();
|
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();
|
2020-04-23 21:23:58 +00:00
|
|
|
auto first = scripts.GetNextNotNull();
|
|
|
|
REQUIRE(first != nullptr);
|
2019-11-28 11:55:22 +00:00
|
|
|
CHECK(first->GetName() == "foobar");
|
2020-04-23 21:23:58 +00:00
|
|
|
auto second = scripts.GetNextNotNull();
|
|
|
|
REQUIRE(second != nullptr);
|
2019-11-28 11:55:22 +00:00
|
|
|
CHECK(second->GetName() == "foobar2");
|
2019-11-12 17:38:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|