Jelajahi Sumber

Add WrongCallException class

This exception can be thrown in case a function or method is called, although it is prohibited.
Patrick-Christopher Mattulat 2 tahun lalu
induk
melakukan
2f35fc5640

+ 2 - 0
CMakeLists.txt

@@ -139,6 +139,7 @@ set(SOURCE_FILES_CORE
         ${CMAKE_CURRENT_SOURCE_DIR}/source/ls-std/core/exception/IllegalArithmeticOperationException.cpp
         ${CMAKE_CURRENT_SOURCE_DIR}/source/ls-std/core/exception/IncompleteJsonException.cpp
         ${CMAKE_CURRENT_SOURCE_DIR}/source/ls-std/core/exception/NullPointerException.cpp
+        ${CMAKE_CURRENT_SOURCE_DIR}/source/ls-std/core/exception/WrongCallException.cpp
         ${CMAKE_CURRENT_SOURCE_DIR}/source/ls-std/core/interface/IBoxing.cpp
         ${CMAKE_CURRENT_SOURCE_DIR}/source/ls-std/core/interface/IEncoding.cpp
         ${CMAKE_CURRENT_SOURCE_DIR}/source/ls-std/core/interface/IEvaluator.cpp
@@ -226,6 +227,7 @@ if (${LS_STD_BUILD_WITH_TESTS})
             ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/core/exception/IllegalArithmeticOperationExceptionTest.cpp
             ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/core/exception/IncompleteJsonExceptionTest.cpp
             ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/core/exception/NullPointerExceptionTest.cpp
+            ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/core/exception/WrongCallExceptionTest.cpp
             ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/core/ClassTest.cpp
             ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/core/LibraryVersionTest.cpp
             ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/core/VersionTest.cpp

+ 35 - 0
include/ls-std/core/exception/WrongCallException.hpp

@@ -0,0 +1,35 @@
+/*
+* Author:          Patrick-Christopher Mattulat
+* Company:         Lynar Studios
+* E-Mail:          webmaster@lynarstudios.com
+* Created:         2023-02-10
+* Changed:         2023-02-10
+*
+* */
+
+#ifndef LS_STD_WRONG_CALL_EXCEPTION_HPP
+#define LS_STD_WRONG_CALL_EXCEPTION_HPP
+
+#include <exception>
+#include <ls-std/os/dynamic-goal.hpp>
+#include <string>
+
+namespace ls::std::core
+{
+  class LS_STD_DYNAMIC_GOAL WrongCallException : public ::std::exception
+  {
+    public:
+
+      WrongCallException();
+      explicit WrongCallException(::std::string _message);
+      ~WrongCallException() override;
+
+      [[nodiscard]] const char *what() const noexcept override;
+
+    private:
+
+      ::std::string message{};
+  };
+}
+
+#endif

+ 2 - 1
include/ls-std/ls-std-core.hpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2022-05-13
- * Changed:         2023-02-08
+ * Changed:         2023-02-10
  *
  * */
 
@@ -23,6 +23,7 @@
 #include <ls-std/core/exception/IllegalArithmeticOperationException.hpp>
 #include <ls-std/core/exception/IncompleteJsonException.hpp>
 #include <ls-std/core/exception/NullPointerException.hpp>
+#include <ls-std/core/exception/WrongCallException.hpp>
 
 #include <ls-std/core/interface/IBoxing.hpp>
 #include <ls-std/core/interface/IEncoding.hpp>

+ 34 - 0
source/ls-std/core/exception/WrongCallException.cpp

@@ -0,0 +1,34 @@
+/*
+* Author:          Patrick-Christopher Mattulat
+* Company:         Lynar Studios
+* E-Mail:          webmaster@lynarstudios.com
+* Created:         2023-02-10
+* Changed:         2023-02-10
+*
+* */
+
+#include <ls-std/core/exception/WrongCallException.hpp>
+#include <ls-std/core/exception/ExceptionMessage.hpp>
+
+ls::std::core::WrongCallException::WrongCallException() = default;
+
+ls::std::core::WrongCallException::WrongCallException(::std::string _message) : message(::std::move(_message))
+{}
+
+ls::std::core::WrongCallException::~WrongCallException() = default;
+
+const char *ls::std::core::WrongCallException::what() const noexcept
+{
+  ::std::string concatenatedMessage = "WrongCallException thrown - ";
+
+  if (this->message.empty())
+  {
+    concatenatedMessage = concatenatedMessage + "this function / method call is invalid!";
+  }
+  else
+  {
+    concatenatedMessage = concatenatedMessage + this->message;
+  }
+
+  return ExceptionMessage{concatenatedMessage}.toCharacterPointer();
+}

+ 67 - 0
test/cases/core/exception/WrongCallExceptionTest.cpp

@@ -0,0 +1,67 @@
+/*
+* Author:          Patrick-Christopher Mattulat
+* Company:         Lynar Studios
+* E-Mail:          webmaster@lynarstudios.com
+* Created:         2023-02-10
+* Changed:         2023-02-10
+*
+* */
+
+#include <gtest/gtest.h>
+#include <ls-std/ls-std-core.hpp>
+#include <string>
+
+using namespace ls::std::core;
+
+namespace
+{
+ class WrongCallExceptionTest : public ::testing::Test
+ {
+   protected:
+
+     WrongCallExceptionTest() = default;
+     ~WrongCallExceptionTest() override = default;
+
+     void SetUp() override
+     {}
+
+     void TearDown() override
+     {}
+ };
+
+ TEST_F(WrongCallExceptionTest, constructor)
+ {
+   EXPECT_THROW(
+       {
+         try
+         {
+           throw WrongCallException{};
+         }
+         catch (const WrongCallException &_exception)
+         {
+           ::std::string message = _exception.what();
+           EXPECT_STREQ("WrongCallException thrown - this function / method call is invalid!", message.c_str());
+           throw;
+         }
+       },
+       WrongCallException);
+ }
+
+ TEST_F(WrongCallExceptionTest, constructor_dedicated_message)
+ {
+   EXPECT_THROW(
+       {
+         try
+         {
+           throw WrongCallException{"method \"getValue()\" was called, although this is forbidden!"};
+         }
+         catch (const WrongCallException &_exception)
+         {
+           ::std::string message = _exception.what();
+           EXPECT_STREQ("WrongCallException thrown - method \"getValue()\" was called, although this is forbidden!", message.c_str());
+           throw;
+         }
+       },
+       WrongCallException);
+ }
+}