Make basic Script class abstract, require implementation of GetName function.
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Deukhoofd 2020-02-23 12:58:13 +01:00
parent 5f891cd4ed
commit a606fbbc7b
Signed by: Deukhoofd
GPG Key ID: ADF2E9256009EDCE
4 changed files with 35 additions and 20 deletions

View File

@ -15,16 +15,14 @@ namespace CreatureLib::Battling {
class Creature;
class Script {
const std::string _name;
public:
explicit Script(std::string name) : _name(std::move(name)) {}
explicit Script() {}
virtual ~Script() = default;
virtual void Stack(){};
virtual void OnRemove(){};
const std::string& GetName() { return _name; }
virtual const std::string& GetName() const = 0;
virtual void OnBeforeTurn(const BaseTurnChoice* choice){};

View File

@ -8,8 +8,12 @@ using namespace CreatureLib;
using namespace CreatureLib::Battling;
class TestScript : public Script {
private:
std::string _name;
public:
explicit TestScript(std::string name) : Script(std::move(name)){};
explicit TestScript(std::string name) : _name(std::move(name)){};
const std::string& GetName() const override { return _name; }
void TestMethod(int& runCount) { runCount++; }
};

View File

@ -7,6 +7,15 @@
using namespace CreatureLib;
using namespace CreatureLib::Battling;
class TestScript : public Script {
private:
std::string _name;
public:
explicit TestScript(std::string name) : _name(std::move(name)){};
const std::string& GetName() const override { return _name; }
};
TEST_CASE("Empty script set count == 0", "[Battling, Scripting]") {
auto set = ScriptSet();
REQUIRE(set.Count() == 0);
@ -14,14 +23,14 @@ TEST_CASE("Empty script set count == 0", "[Battling, Scripting]") {
TEST_CASE("Add script to script set", "[Battling, Scripting]") {
auto set = ScriptSet();
auto s = new Script("foobar");
auto s = new TestScript("foobar");
set.Add(s);
REQUIRE(set.Count() == 1);
}
TEST_CASE("Add script to script set, then retrieve it", "[Battling, Scripting]") {
auto set = ScriptSet();
auto s = new Script("foobar");
auto s = new TestScript("foobar");
set.Add(s);
REQUIRE(set.Count() == 1);
auto get = set.GetIterator()->at(0);
@ -30,8 +39,8 @@ TEST_CASE("Add script to script set, then retrieve it", "[Battling, Scripting]")
TEST_CASE("Add two scripts to script set", "[Battling, Scripting]") {
auto set = ScriptSet();
auto s = new Script("foobar");
auto s2 = new Script("foobar2");
auto s = new TestScript("foobar");
auto s2 = new TestScript("foobar2");
set.Add(s);
set.Add(s2);
REQUIRE(set.Count() == 2);
@ -39,8 +48,8 @@ TEST_CASE("Add two scripts to script set", "[Battling, Scripting]") {
TEST_CASE("Add two scripts to script set, then retrieve them", "[Battling, Scripting]") {
auto set = ScriptSet();
auto s = new Script("foobar");
auto s2 = new Script("foobar2");
auto s = new TestScript("foobar");
auto s2 = new TestScript("foobar2");
set.Add(s);
set.Add(s2);
REQUIRE(set.Count() == 2);
@ -52,7 +61,7 @@ TEST_CASE("Add two scripts to script set, then retrieve them", "[Battling, Scrip
TEST_CASE("Add script to script set, then remove it", "[Battling, Scripting]") {
auto set = ScriptSet();
auto s = new Script("foobar");
auto s = new TestScript("foobar");
set.Add(s);
REQUIRE(set.Count() == 1);
set.Remove("foobar");
@ -63,8 +72,8 @@ TEST_CASE("Add script to script set, then remove it", "[Battling, Scripting]") {
TEST_CASE("Add two scripts to script set, then remove them", "[Battling, Scripting]") {
auto set = ScriptSet();
auto s = new Script("foobar");
auto s2 = new Script("foobar2");
auto s = new TestScript("foobar");
auto s2 = new TestScript("foobar2");
set.Add(s);
set.Add(s2);
REQUIRE(set.Count() == 2);

View File

@ -9,8 +9,12 @@ using namespace CreatureLib;
using namespace CreatureLib::Battling;
class TestScript : public Script {
private:
std::string _name;
public:
explicit TestScript(std::string name) : Script(std::move(name)){};
explicit TestScript(std::string name) : _name(std::move(name)){};
const std::string& GetName() const override { return _name; }
void TestMethod(int& runCount) { runCount++; }
};
@ -40,7 +44,7 @@ TEST_CASE("Script source with unset script ptr.", "[Battling, Scripting]") {
TEST_CASE("Script source with script ptr being set.", "[Battling, Scripting]") {
auto source = ScriptSourceWithScriptPtr();
source.ScriptPtr = new Script("foobar");
source.ScriptPtr = new TestScript("foobar");
auto scripts = source.GetScriptIterator();
auto first = scripts.GetNext();
CHECK(first != nullptr);
@ -52,7 +56,7 @@ TEST_CASE("Script source with script ptr being set after first iteration.", "[Ba
auto scripts = source.GetScriptIterator();
auto first = scripts.GetNext();
CHECK(first == nullptr);
source.ScriptPtr = new Script("foobar");
source.ScriptPtr = new TestScript("foobar");
scripts = source.GetScriptIterator();
first = scripts.GetNext();
CHECK(first != nullptr);
@ -68,7 +72,7 @@ TEST_CASE("Script source with empty script set.", "[Battling, Scripting]") {
TEST_CASE("Script source with single item script set.", "[Battling, Scripting]") {
auto source = ScriptSourceWithScriptSet();
auto s = new Script("foobar");
auto s = new TestScript("foobar");
source.Set.Add(s);
auto scripts = source.GetScriptIterator();
auto first = scripts.GetNext();
@ -78,8 +82,8 @@ TEST_CASE("Script source with single item script set.", "[Battling, Scripting]")
TEST_CASE("Script source with multiple item script set.", "[Battling, Scripting]") {
auto source = ScriptSourceWithScriptSet();
auto s = new Script("foobar");
auto s2 = new Script("foobar2");
auto s = new TestScript("foobar");
auto s2 = new TestScript("foobar2");
source.Set.Add(s);
source.Set.Add(s2);
auto scripts = source.GetScriptIterator();