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-06-26 15:08:23 +00:00
|
|
|
ArbUt::StringView _name;
|
2020-02-23 11:58:13 +00:00
|
|
|
|
2019-11-12 17:38:09 +00:00
|
|
|
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; }
|
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:
|
2020-06-02 11:43:44 +00:00
|
|
|
std::unique_ptr<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();
|
2020-06-05 16:10:58 +00:00
|
|
|
REQUIRE_FALSE(scripts.HasNext());
|
2019-11-12 17:38:09 +00:00
|
|
|
}
|
|
|
|
|
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-06-02 11:43:44 +00:00
|
|
|
source.ScriptPtr = std::make_unique<TestScript>("foobar");
|
2019-11-12 17:38:09 +00:00
|
|
|
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 after first iteration.", "[Battling, Scripting]") {
|
2019-11-12 17:38:09 +00:00
|
|
|
auto source = ScriptSourceWithScriptPtr();
|
|
|
|
auto scripts = source.GetScriptIterator();
|
2020-06-05 16:06:45 +00:00
|
|
|
REQUIRE_FALSE(scripts.HasNext());
|
2020-06-02 11:43:44 +00:00
|
|
|
source.ScriptPtr = std::make_unique<TestScript>("foobar");
|
2019-11-12 17:38:09 +00:00
|
|
|
scripts = source.GetScriptIterator();
|
2020-06-05 16:06:45 +00:00
|
|
|
auto first = scripts.GetNext();
|
2019-11-12 17:38:09 +00:00
|
|
|
CHECK(first != nullptr);
|
|
|
|
}
|
|
|
|
|
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();
|
2020-06-05 16:06:45 +00:00
|
|
|
scripts.Reset();
|
|
|
|
REQUIRE_FALSE(scripts.HasNext());
|
2019-11-12 17:38:09 +00:00
|
|
|
}
|
|
|
|
|
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
|