From d2320fde3c0b18abbf21538a2efa8160369e7618 Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Fri, 10 Apr 2020 20:09:15 +0200 Subject: [PATCH] Unit tests for EffectParameters. --- src/Library/EffectParameter.hpp | 7 +++--- .../CreatureTests.cpp | 2 +- tests/LibraryTests/EffectParameterTests.cpp | 23 +++++++++++++++++++ 3 files changed, 28 insertions(+), 4 deletions(-) rename tests/{LibraryTests => BattleTests}/CreatureTests.cpp (97%) create mode 100644 tests/LibraryTests/EffectParameterTests.cpp diff --git a/src/Library/EffectParameter.hpp b/src/Library/EffectParameter.hpp index 41ce087..b514cba 100644 --- a/src/Library/EffectParameter.hpp +++ b/src/Library/EffectParameter.hpp @@ -14,9 +14,10 @@ namespace CreatureLib::Library { public: EffectParameter() : _type(EffectParameterType::None), _val(nullptr){}; - EffectParameter(bool b) : _type(EffectParameterType::Bool), _val((void*)b){}; - EffectParameter(int64_t i) : _type(EffectParameterType::Int), _val((void*)i){}; - EffectParameter(const std::string& s) : _type(EffectParameterType::String), _val(new char[s.size() + 1]) { + explicit EffectParameter(bool b) : _type(EffectParameterType::Bool), _val((void*)b){}; + explicit EffectParameter(int64_t i) : _type(EffectParameterType::Int), _val((void*)i){}; + explicit EffectParameter(const std::string& s) + : _type(EffectParameterType::String), _val(new char[s.size() + 1]) { strncpy((char*)_val, s.c_str(), s.size() + 1); }; EffectParameter(const EffectParameter& other) = delete; diff --git a/tests/LibraryTests/CreatureTests.cpp b/tests/BattleTests/CreatureTests.cpp similarity index 97% rename from tests/LibraryTests/CreatureTests.cpp rename to tests/BattleTests/CreatureTests.cpp index e1e4e66..3ba17c8 100644 --- a/tests/LibraryTests/CreatureTests.cpp +++ b/tests/BattleTests/CreatureTests.cpp @@ -3,7 +3,7 @@ #include "../../src/Battling/Models/CreateCreature.hpp" #include "../TestLibrary/TestLibrary.hpp" -using namespace CreatureLib::Library; +using namespace CreatureLib::Battling; TEST_CASE("Create basic creature", "[Library]") { auto library = TestLibrary::Get(); auto creature = CreateCreature(library, "testSpecies1"_cnc, 1).Create(); diff --git a/tests/LibraryTests/EffectParameterTests.cpp b/tests/LibraryTests/EffectParameterTests.cpp new file mode 100644 index 0000000..843546c --- /dev/null +++ b/tests/LibraryTests/EffectParameterTests.cpp @@ -0,0 +1,23 @@ +#ifdef TESTS_BUILD +#include "../../extern/catch.hpp" +#include "../../src/Library/EffectParameter.hpp" + +using namespace CreatureLib::Library; +TEST_CASE("Bool EffectParameter", "[Library]") { + auto p = EffectParameter(true); + REQUIRE(p.AsBool()); + auto p2 = EffectParameter(false); + REQUIRE_FALSE(p2.AsBool()); +} + +TEST_CASE("Int EffectParameter", "[Library]") { + auto p = EffectParameter((int64_t)684); + REQUIRE(p.AsInt() == 684); +} + +TEST_CASE("String EffectParameter", "[Library]") { + auto p = EffectParameter(std::string("foobar")); + REQUIRE(strcmp(p.AsString(), "foobar") == 0); +} + +#endif \ No newline at end of file