From 07700008f7ff2369bcde7e2d73f0388d97f11a3d Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Mon, 3 Feb 2020 13:05:46 +0100 Subject: [PATCH] Slight tweaks for CreatureException, add tests to see if what() works. --- src/Core/Exceptions/CreatureException.hpp | 5 +++-- tests/ExceptionTests.cpp | 15 +++++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) create mode 100644 tests/ExceptionTests.cpp diff --git a/src/Core/Exceptions/CreatureException.hpp b/src/Core/Exceptions/CreatureException.hpp index d316d33..a66dc51 100644 --- a/src/Core/Exceptions/CreatureException.hpp +++ b/src/Core/Exceptions/CreatureException.hpp @@ -8,9 +8,10 @@ class CreatureException : std::exception { std::string _error; public: - CreatureException(std::string error) : _error(error) {} + explicit CreatureException(std::string error) : _error(error) {} + const char* what() const throw() override { return _error.c_str(); } - const char* what() const noexcept override { return _error.c_str(); } + virtual ~CreatureException() = default; }; #endif // CREATURELIB_CREATUREEXCEPTION_HPP diff --git a/tests/ExceptionTests.cpp b/tests/ExceptionTests.cpp new file mode 100644 index 0000000..1c34155 --- /dev/null +++ b/tests/ExceptionTests.cpp @@ -0,0 +1,15 @@ +#ifdef TESTS_BUILD +#include +#include "../extern/catch.hpp" +#include "../src/Core/Exceptions/CreatureException.hpp" + +TEST_CASE("When throwing exception, what() is readable", "[Utilities]") { + try { + throw CreatureException("foobar"); + } catch (const CreatureException& e) { + INFO(e.what()); + REQUIRE(strcmp(e.what(), "foobar") == 0); + } +} + +#endif \ No newline at end of file