Bläddra i källkod

Add NullPointerEvaluator class

This class evaluates, whether a reference in use is null and throws a NullPointerException.
Patrick-Christopher Mattulat 1 år sedan
förälder
incheckning
fa2f8ce0d8

+ 2 - 0
CMakeLists.txt

@@ -129,6 +129,7 @@ set(SOURCE_FILES_BOXING
 set(SOURCE_FILES_CORE
         ${CMAKE_CURRENT_SOURCE_DIR}/source/ls-std/core/evaluator/EmptyStringArgumentEvaluator.cpp
         ${CMAKE_CURRENT_SOURCE_DIR}/source/ls-std/core/evaluator/NullPointerArgumentEvaluator.cpp
+        ${CMAKE_CURRENT_SOURCE_DIR}/source/ls-std/core/evaluator/NullPointerEvaluator.cpp
         ${CMAKE_CURRENT_SOURCE_DIR}/source/ls-std/core/exception/EventNotHandledException.cpp
         ${CMAKE_CURRENT_SOURCE_DIR}/source/ls-std/core/exception/EventNotSubscribedException.cpp
         ${CMAKE_CURRENT_SOURCE_DIR}/source/ls-std/core/exception/ExceptionMessage.cpp
@@ -209,6 +210,7 @@ if (${LS_STD_BUILD_WITH_TESTS})
     set(TEST_FILES_CORE
             ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/core/evaluator/EmptyStringArgumentEvaluatorTest.cpp
             ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/core/evaluator/NullPointerArgumentEvaluatorTest.cpp
+            ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/core/evaluator/NullPointerEvaluatorTest.cpp
             ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/core/exception/EventNotHandledExceptionTest.cpp
             ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/core/exception/EventNotSubscribedExceptionTest.cpp
             ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/core/exception/ExceptionMessageTest.cpp

+ 36 - 0
include/ls-std/core/evaluator/NullPointerEvaluator.hpp

@@ -0,0 +1,36 @@
+/*
+* Author:          Patrick-Christopher Mattulat
+* Company:         Lynar Studios
+* E-Mail:          webmaster@lynarstudios.com
+* Created:         2023-02-08
+* Changed:         2023-02-08
+*
+* */
+
+#ifndef LS_STD_NULL_POINTER_EVALUATOR_HPP
+#define LS_STD_NULL_POINTER_EVALUATOR_HPP
+
+#include <ls-std/core/interface/IEvaluator.hpp>
+#include <memory>
+#include <string>
+
+namespace ls::std::core
+{
+  class NullPointerEvaluator : public ls::std::core::interface_type::IEvaluator
+  {
+    public:
+
+      explicit NullPointerEvaluator(const ::std::shared_ptr<void> &_argument);
+      explicit NullPointerEvaluator(const ::std::shared_ptr<void> &_argument, ::std::string _message);
+      ~NullPointerEvaluator() override;
+
+      void evaluate() override;
+
+    private:
+
+      ::std::shared_ptr<void> argument{};
+      ::std::string message{};
+  };
+}
+
+#endif

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

@@ -12,6 +12,7 @@
 
 #include <ls-std/core/evaluator/EmptyStringArgumentEvaluator.hpp>
 #include <ls-std/core/evaluator/NullPointerArgumentEvaluator.hpp>
+#include <ls-std/core/evaluator/NullPointerEvaluator.hpp>
 
 #include <ls-std/core/exception/EventNotHandledException.hpp>
 #include <ls-std/core/exception/EventNotSubscribedException.hpp>

+ 34 - 0
source/ls-std/core/evaluator/NullPointerEvaluator.cpp

@@ -0,0 +1,34 @@
+/*
+* Author:          Patrick-Christopher Mattulat
+* Company:         Lynar Studios
+* E-Mail:          webmaster@lynarstudios.com
+* Created:         2023-02-08
+* Changed:         2023-02-08
+*
+* */
+
+#include <ls-std/core/evaluator/NullPointerEvaluator.hpp>
+#include <ls-std/core/exception/NullPointerException.hpp>
+
+ls::std::core::NullPointerEvaluator::NullPointerEvaluator(const ::std::shared_ptr<void> &_argument) : argument(_argument)
+{}
+
+ls::std::core::NullPointerEvaluator::NullPointerEvaluator(const ::std::shared_ptr<void> &_argument, ::std::string _message) : argument(_argument), message(::std::move(_message))
+{}
+
+ls::std::core::NullPointerEvaluator::~NullPointerEvaluator() = default;
+
+void ls::std::core::NullPointerEvaluator::evaluate()
+{
+  if (this->argument == nullptr)
+  {
+    if (this->message.empty())
+    {
+      throw ls::std::core::NullPointerException{"reference in use is null!"};
+    }
+    else
+    {
+      throw ls::std::core::NullPointerException{this->message};
+    }
+  }
+}

+ 68 - 0
test/cases/core/evaluator/NullPointerEvaluatorTest.cpp

@@ -0,0 +1,68 @@
+/*
+* Author:          Patrick-Christopher Mattulat
+* Company:         Lynar Studios
+* E-Mail:          webmaster@lynarstudios.com
+* Created:         2023-02-08
+* Changed:         2023-02-08
+*
+* */
+
+#include <gtest/gtest.h>
+#include <ls-std/ls-std-core.hpp>
+#include <string>
+
+using namespace ls::std::core;
+using namespace ::std;
+
+namespace
+{
+  class NullPointerArgumentTest : public ::testing::Test
+  {
+    protected:
+
+      NullPointerArgumentTest() = default;
+      ~NullPointerArgumentTest() override = default;
+
+      void SetUp() override
+      {}
+
+      void TearDown() override
+      {}
+  };
+
+  TEST_F(NullPointerArgumentTest, evaluate)
+  {
+    EXPECT_THROW(
+        {
+          try
+          {
+            NullPointerEvaluator{nullptr}.evaluate();
+          }
+          catch (const NullPointerException &_exception)
+          {
+            string message = _exception.what();
+            ASSERT_STREQ("NullPointerException thrown - reference in use is null!", message.c_str());
+            throw;
+          }
+        },
+        NullPointerException);
+  }
+
+  TEST_F(NullPointerArgumentTest, evaluate_dedicated_message)
+  {
+    EXPECT_THROW(
+        {
+          try
+          {
+            NullPointerEvaluator(nullptr, "this reference is not set and causes this exception!").evaluate();
+          }
+          catch (const NullPointerException &_exception)
+          {
+            string message = _exception.what();
+            ASSERT_STREQ("NullPointerException thrown - this reference is not set and causes this exception!", message.c_str());
+            throw;
+          }
+        },
+        NullPointerException);
+  }
+}