Browse Source

Add raw pointer handling to NullPointerArgumentEvaluator class

Patrick-Christopher Mattulat 1 year ago
parent
commit
56d4d2e5c5

+ 1 - 1
README.md

@@ -41,7 +41,7 @@ A __Date__ class comes with this submodule, which you can use to represent a dat
 - made test constructors public and reduced test setup overhead
 - added missing __nodiscard__ attributes to test package
 - made __Exception__ base class abstract, which prevents it from being instantiated
-- __NullPointerEvaluator__ now excepts raw pointers
+- __NullPointerEvaluator__ & __NullPointerArgumentEvaluator__ now except raw pointers
 
 #### Fixes ####
 

+ 3 - 1
include/ls-std/core/evaluator/NullPointerArgumentEvaluator.hpp

@@ -3,7 +3,7 @@
 * Company:         Lynar Studios
 * E-Mail:          webmaster@lynarstudios.com
 * Created:         2023-02-08
-* Changed:         2023-02-22
+* Changed:         2023-04-07
 *
 * */
 
@@ -21,6 +21,8 @@ namespace ls::std::core
   {
     public:
 
+      explicit NullPointerArgumentEvaluator(const void *_rawArgument);
+      explicit NullPointerArgumentEvaluator(const void *_rawArgument, ::std::string _message);
       explicit NullPointerArgumentEvaluator(const ::std::shared_ptr<void> &_argument);
       explicit NullPointerArgumentEvaluator(const ::std::shared_ptr<void> &_argument, ::std::string _message);
       ~NullPointerArgumentEvaluator() noexcept override;

+ 23 - 3
source/ls-std/core/evaluator/NullPointerArgumentEvaluator.cpp

@@ -3,7 +3,7 @@
 * Company:         Lynar Studios
 * E-Mail:          webmaster@lynarstudios.com
 * Created:         2023-02-08
-* Changed:         2023-02-23
+* Changed:         2023-04-07
 *
 * */
 
@@ -12,15 +12,35 @@
 
 using ls::std::core::IllegalArgumentException;
 using ls::std::core::NullPointerArgumentEvaluator;
+using std::make_shared;
 using std::move;
 using std::shared_ptr;
 using std::string;
 
+NullPointerArgumentEvaluator::NullPointerArgumentEvaluator(const void *_rawArgument)
+{
+  shared_ptr<void> value{};
+
+  if (_rawArgument != nullptr)
+  {
+    value = make_shared<int>();
+  }
+
+  this->argument = value;
+}
+
+NullPointerArgumentEvaluator::NullPointerArgumentEvaluator(const void *_rawArgument, string _message) : NullPointerArgumentEvaluator(_rawArgument)
+{
+  this->message = ::move(_message);
+}
+
 NullPointerArgumentEvaluator::NullPointerArgumentEvaluator(const shared_ptr<void> &_argument) : argument(_argument)
 {}
 
-NullPointerArgumentEvaluator::NullPointerArgumentEvaluator(const shared_ptr<void> &_argument, string _message) : argument(_argument), message(::move(_message))
-{}
+NullPointerArgumentEvaluator::NullPointerArgumentEvaluator(const shared_ptr<void> &_argument, string _message) : NullPointerArgumentEvaluator(_argument)
+{
+  this->message = ::move(_message);
+}
 
 NullPointerArgumentEvaluator::~NullPointerArgumentEvaluator() noexcept = default;
 

+ 49 - 3
test/cases/core/evaluator/NullPointerArgumentEvaluatorTest.cpp

@@ -3,16 +3,18 @@
 * Company:         Lynar Studios
 * E-Mail:          webmaster@lynarstudios.com
 * Created:         2023-02-08
-* Changed:         2023-03-25
+* Changed:         2023-04-07
 *
 * */
 
 #include <gtest/gtest.h>
 #include <ls-std/ls-std-core.hpp>
+#include <memory>
 #include <string>
 
 using ls::std::core::IllegalArgumentException;
 using ls::std::core::NullPointerArgumentEvaluator;
+using std::shared_ptr;
 using std::string;
 using testing::Test;
 
@@ -26,7 +28,7 @@ namespace
       ~NullPointerArgumentEvaluatorTest() override = default;
   };
 
-  TEST_F(NullPointerArgumentEvaluatorTest, evaluate)
+  TEST_F(NullPointerArgumentEvaluatorTest, evaluate_raw_pointer)
   {
     EXPECT_THROW(
         {
@@ -46,7 +48,7 @@ namespace
         IllegalArgumentException);
   }
 
-  TEST_F(NullPointerArgumentEvaluatorTest, evaluate_dedicated_message)
+  TEST_F(NullPointerArgumentEvaluatorTest, evaluate_raw_pointer_with_dedicated_message)
   {
     EXPECT_THROW(
         {
@@ -65,4 +67,48 @@ namespace
         },
         IllegalArgumentException);
   }
+
+  TEST_F(NullPointerArgumentEvaluatorTest, evaluate)
+  {
+    shared_ptr<void> value{};
+
+    EXPECT_THROW(
+        {
+          try
+          {
+            NullPointerArgumentEvaluator{value}.evaluate();
+          }
+          catch (const IllegalArgumentException &_exception)
+          {
+            string actual = _exception.what();
+            string expected = _exception.getName() + " thrown - passed argument is null!";
+
+            ASSERT_STREQ(expected.c_str(), actual.c_str());
+            throw;
+          }
+        },
+        IllegalArgumentException);
+  }
+
+  TEST_F(NullPointerArgumentEvaluatorTest, evaluate_dedicated_message)
+  {
+    shared_ptr<void> value{};
+
+    EXPECT_THROW(
+        {
+          try
+          {
+            NullPointerArgumentEvaluator(value, "this reference is null!").evaluate();
+          }
+          catch (const IllegalArgumentException &_exception)
+          {
+            string actual = _exception.what();
+            string expected = _exception.getName() + " thrown - this reference is null!";
+
+            ASSERT_STREQ(expected.c_str(), actual.c_str());
+            throw;
+          }
+        },
+        IllegalArgumentException);
+  }
 }