From a606fbbc7bc06681e91476eba7db070fb94699fc Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Sun, 23 Feb 2020 12:58:13 +0100 Subject: [PATCH] Make basic Script class abstract, require implementation of GetName function. --- src/Battling/ScriptHandling/Script.hpp | 6 ++--- .../ScriptTests/ScriptAggregatorTests.cpp | 6 ++++- .../ScriptTests/ScriptSetTests.cpp | 27 ++++++++++++------- .../ScriptTests/ScriptSourceTest.cpp | 16 ++++++----- 4 files changed, 35 insertions(+), 20 deletions(-) diff --git a/src/Battling/ScriptHandling/Script.hpp b/src/Battling/ScriptHandling/Script.hpp index edf4d20..2687606 100644 --- a/src/Battling/ScriptHandling/Script.hpp +++ b/src/Battling/ScriptHandling/Script.hpp @@ -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){}; diff --git a/tests/BattleTests/ScriptTests/ScriptAggregatorTests.cpp b/tests/BattleTests/ScriptTests/ScriptAggregatorTests.cpp index 61562b2..bcb088c 100644 --- a/tests/BattleTests/ScriptTests/ScriptAggregatorTests.cpp +++ b/tests/BattleTests/ScriptTests/ScriptAggregatorTests.cpp @@ -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++; } }; diff --git a/tests/BattleTests/ScriptTests/ScriptSetTests.cpp b/tests/BattleTests/ScriptTests/ScriptSetTests.cpp index e85944b..8efec0a 100644 --- a/tests/BattleTests/ScriptTests/ScriptSetTests.cpp +++ b/tests/BattleTests/ScriptTests/ScriptSetTests.cpp @@ -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); diff --git a/tests/BattleTests/ScriptTests/ScriptSourceTest.cpp b/tests/BattleTests/ScriptTests/ScriptSourceTest.cpp index f4038f0..1ed4f0a 100644 --- a/tests/BattleTests/ScriptTests/ScriptSourceTest.cpp +++ b/tests/BattleTests/ScriptTests/ScriptSourceTest.cpp @@ -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();