Kaynağa Gözat

Add EmptyStringArgumentEvaluator class

This class evaluates, whether a parameter string is empty and throws an IllegalArgumentException.
Patrick-Christopher Mattulat 1 yıl önce
ebeveyn
işleme
6e32f1f78e

+ 2 - 0
CMakeLists.txt

@@ -127,6 +127,7 @@ set(SOURCE_FILES_BOXING
         ${CMAKE_CURRENT_SOURCE_DIR}/source/ls-std/boxing/String.cpp)
 
 set(SOURCE_FILES_CORE
+        ${CMAKE_CURRENT_SOURCE_DIR}/source/ls-std/core/evaluator/EmptyStringArgumentEvaluator.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
@@ -205,6 +206,7 @@ if (${LS_STD_BUILD_WITH_TESTS})
             ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/boxing/StringTest.cpp)
 
     set(TEST_FILES_CORE
+            ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/core/evaluator/EmptyStringArgumentEvaluatorTest.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

+ 35 - 0
include/ls-std/core/evaluator/EmptyStringArgumentEvaluator.hpp

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

+ 11 - 12
include/ls-std/io/xml/XmlNode.hpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-09-24
- * Changed:         2023-02-06
+ * Changed:         2023-02-08
  *
  * */
 
@@ -25,14 +25,14 @@ namespace ls::std::io
       explicit XmlNode(::std::string _name);
       ~XmlNode() override;
 
-      bool addAttributeAfter(const ::std::shared_ptr<ls::std::io::XmlAttribute> &_attribute, const ::std::string &_name); // nodiscard is optional here
-      bool addAttributeBefore(const ::std::shared_ptr<ls::std::io::XmlAttribute> &_attribute, const ::std::string &_name); // nodiscard is optional here
-      bool addAttributeToBeginning(const ::std::shared_ptr<ls::std::io::XmlAttribute> &_attribute); // nodiscard is optional here
-      bool addAttributeToEnd(const ::std::shared_ptr<ls::std::io::XmlAttribute> &_attribute); // nodiscard is optional here
-      bool addChildAfter(const ::std::shared_ptr<ls::std::io::XmlNode> &_child, const ::std::shared_ptr<ls::std::io::XmlNode> &_search); // nodiscard is optional here
+      bool addAttributeAfter(const ::std::shared_ptr<ls::std::io::XmlAttribute> &_attribute, const ::std::string &_name);                 // nodiscard is optional here
+      bool addAttributeBefore(const ::std::shared_ptr<ls::std::io::XmlAttribute> &_attribute, const ::std::string &_name);                // nodiscard is optional here
+      bool addAttributeToBeginning(const ::std::shared_ptr<ls::std::io::XmlAttribute> &_attribute);                                       // nodiscard is optional here
+      bool addAttributeToEnd(const ::std::shared_ptr<ls::std::io::XmlAttribute> &_attribute);                                             // nodiscard is optional here
+      bool addChildAfter(const ::std::shared_ptr<ls::std::io::XmlNode> &_child, const ::std::shared_ptr<ls::std::io::XmlNode> &_search);  // nodiscard is optional here
       bool addChildBefore(const ::std::shared_ptr<ls::std::io::XmlNode> &_child, const ::std::shared_ptr<ls::std::io::XmlNode> &_search); // nodiscard is optional here
-      bool addChildToBeginning(const ::std::shared_ptr<ls::std::io::XmlNode> &_child); // nodiscard is optional here
-      bool addChildToEnd(const ::std::shared_ptr<ls::std::io::XmlNode> &_child); // nodiscard is optional here
+      bool addChildToBeginning(const ::std::shared_ptr<ls::std::io::XmlNode> &_child);                                                    // nodiscard is optional here
+      bool addChildToEnd(const ::std::shared_ptr<ls::std::io::XmlNode> &_child);                                                          // nodiscard is optional here
       void clearValue();
       [[nodiscard]] ::std::list<::std::shared_ptr<ls::std::io::XmlAttribute>> getAttributes();
       [[nodiscard]] ::std::list<::std::shared_ptr<ls::std::io::XmlNode>> getChildren();
@@ -43,9 +43,9 @@ namespace ls::std::io
       [[nodiscard]] bool hasChild(const ::std::string &_name);
       [[nodiscard]] bool hasChild(const ::std::shared_ptr<ls::std::io::XmlNode> &_child);
       bool removeFirstAttribute(); // nodiscard is optional here
-      bool removeLastAttribute(); // nodiscard is optional here
-      bool removeFirstChild(); // nodiscard is optional here
-      bool removeLastChild(); // nodiscard is optional here
+      bool removeLastAttribute();  // nodiscard is optional here
+      bool removeFirstChild();     // nodiscard is optional here
+      bool removeLastChild();      // nodiscard is optional here
       void setName(const ::std::string &_name);
       void setValue(const ::std::string &_value);
       [[nodiscard]] ::std::string toXml();
@@ -65,7 +65,6 @@ namespace ls::std::io
       void _assignName(const ::std::string &_name);
       void _assignValue(const ::std::string &_value);
       static void _checkIfAttributeReferenceIsValid(const ::std::shared_ptr<ls::std::io::XmlAttribute> &_attribute);
-      static void _checkIfNameIsNotEmpty(const ::std::string &_name);
       static void _checkIfNodeReferenceIsValid(const ::std::shared_ptr<ls::std::io::XmlNode> &_child);
       [[nodiscard]] static ::std::string _getTab(uint8_t _tabSize);
       [[nodiscard]] bool _hasAttribute(const ::std::string &_name);

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

@@ -3,13 +3,15 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2022-05-13
- * Changed:         2023-02-07
+ * Changed:         2023-02-08
  *
  * */
 
 #ifndef LS_STD_LS_STD_CORE_HPP
 #define LS_STD_LS_STD_CORE_HPP
 
+#include <ls-std/core/evaluator/EmptyStringArgumentEvaluator.hpp>
+
 #include <ls-std/core/exception/EventNotHandledException.hpp>
 #include <ls-std/core/exception/EventNotSubscribedException.hpp>
 #include <ls-std/core/exception/ExceptionMessage.hpp>

+ 3 - 7
source/ls-std/core/Class.cpp

@@ -3,12 +3,12 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-08-07
- * Changed:         2023-02-07
+ * Changed:         2023-02-08
  *
  * */
 
 #include <ls-std/core/Class.hpp>
-#include <ls-std/core/exception/IllegalArgumentException.hpp>
+#include <ls-std/core/evaluator/EmptyStringArgumentEvaluator.hpp>
 
 ls::std::core::Class::Class(const ::std::string &_name)
 {
@@ -24,10 +24,6 @@ ls::std::core::Class::~Class() = default;
 
 void ls::std::core::Class::_assignClassName(const ::std::string &_name)
 {
-  if (_name.empty())
-  {
-    throw ls::std::core::IllegalArgumentException{"_name is empty"};
-  }
-
+  ls::std::core::EmptyStringArgumentEvaluator{_name, "class name is empty!"}.evaluate();
   this->name = _name;
 }

+ 34 - 0
source/ls-std/core/evaluator/EmptyStringArgumentEvaluator.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/EmptyStringArgumentEvaluator.hpp>
+#include <ls-std/core/exception/IllegalArgumentException.hpp>
+
+ls::std::core::EmptyStringArgumentEvaluator::EmptyStringArgumentEvaluator(::std::string _argument) : argument(::std::move(_argument))
+{}
+
+ls::std::core::EmptyStringArgumentEvaluator::EmptyStringArgumentEvaluator(::std::string _argument, ::std::string _message) : argument(::std::move(_argument)), message(::std::move(_message))
+{}
+
+ls::std::core::EmptyStringArgumentEvaluator::~EmptyStringArgumentEvaluator() = default;
+
+void ls::std::core::EmptyStringArgumentEvaluator::evaluate()
+{
+  if (this->argument.empty())
+  {
+    if (this->message.empty())
+    {
+      throw ls::std::core::IllegalArgumentException{"passed argument is empty!"};
+    }
+    else
+    {
+      throw ls::std::core::IllegalArgumentException{this->message};
+    }
+  }
+}

+ 3 - 7
source/ls-std/event/Event.cpp

@@ -3,11 +3,11 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-11-26
- * Changed:         2023-02-07
+ * Changed:         2023-02-08
  *
  * */
 
-#include <ls-std/core/exception/IllegalArgumentException.hpp>
+#include <ls-std/core/evaluator/EmptyStringArgumentEvaluator.hpp>
 #include <ls-std/event/Event.hpp>
 
 ls::std::event::Event::Event(const ls::std::core::type::event_id &_id) : ls::std::core::Class("Event")
@@ -56,11 +56,7 @@ void ls::std::event::Event::setId(const ls::std::core::type::event_id &_id)
 
 void ls::std::event::Event::_assignId(const ls::std::core::type::event_id &_id)
 {
-  if (_id.empty())
-  {
-    throw ls::std::core::IllegalArgumentException{"_id is empty"};
-  }
-
+  ls::std::core::EmptyStringArgumentEvaluator{_id, "event id is empty!"}.evaluate();
   this->id = _id;
 }
 

+ 3 - 7
source/ls-std/event/EventHandler.cpp

@@ -3,11 +3,11 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-11-27
- * Changed:         2023-02-07
+ * Changed:         2023-02-08
  *
  * */
 
-#include <ls-std/core/exception/IllegalArgumentException.hpp>
+#include <ls-std/core/evaluator/EmptyStringArgumentEvaluator.hpp>
 #include <ls-std/event/EventHandler.hpp>
 
 ls::std::event::EventHandler::EventHandler(const ls::std::core::type::event_id &_id) : ls::std::event::Narrator()
@@ -24,10 +24,6 @@ ls::std::core::type::event_id ls::std::event::EventHandler::getId()
 
 void ls::std::event::EventHandler::_assignId(const ls::std::core::type::event_id &_id)
 {
-  if (_id.empty())
-  {
-    throw ls::std::core::IllegalArgumentException{"_id is empty"};
-  }
-
+  ls::std::core::EmptyStringArgumentEvaluator{_id, "event manager id is empty!"}.evaluate();
   this->id = _id;
 }

+ 5 - 14
source/ls-std/event/EventManager.cpp

@@ -3,10 +3,11 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-11-27
- * Changed:         2023-02-07
+ * Changed:         2023-02-08
  *
  * */
 
+#include <ls-std/core/evaluator/EmptyStringArgumentEvaluator.hpp>
 #include <ls-std/core/exception/EventNotHandledException.hpp>
 #include <ls-std/core/exception/EventNotSubscribedException.hpp>
 #include <ls-std/core/exception/IllegalArgumentException.hpp>
@@ -19,10 +20,7 @@ ls::std::event::EventManager::~EventManager() = default;
 
 void ls::std::event::EventManager::subscribe(const ls::std::core::type::event_id &_id, const ::std::shared_ptr<ls::std::core::interface_type::IListener> &_listener)
 {
-  if (_id.empty())
-  {
-    throw ls::std::core::IllegalArgumentException{"_id is empty"};
-  }
+  ls::std::core::EmptyStringArgumentEvaluator{_id, "event id is empty and can not be subscribed!"}.evaluate();
 
   if (_listener == nullptr)
   {
@@ -41,10 +39,7 @@ void ls::std::event::EventManager::subscribe(const ls::std::core::type::event_id
 
 void ls::std::event::EventManager::unsubscribe(const ls::std::core::type::event_id &_id, const ::std::shared_ptr<ls::std::core::interface_type::IListener> &_listener)
 {
-  if (_id.empty())
-  {
-    throw ls::std::core::IllegalArgumentException{"_id is empty"};
-  }
+  ls::std::core::EmptyStringArgumentEvaluator{_id, "event id is empty and can not be unsubscribed!"}.evaluate();
 
   if (_listener == nullptr)
   {
@@ -89,11 +84,7 @@ void ls::std::event::EventManager::fire(ls::std::event::Event _event)
 
 bool ls::std::event::EventManager::hasEventHandler(const ls::std::core::type::event_id &_id)
 {
-  if (_id.empty())
-  {
-    throw ls::std::core::IllegalArgumentException{"_id is empty"};
-  }
-
+  ls::std::core::EmptyStringArgumentEvaluator{_id, "event id is empty and can not be passed!"}.evaluate();
   return this->_hasEventHandler(_id);
 }
 

+ 3 - 7
source/ls-std/io/kv/KvPair.cpp

@@ -3,11 +3,11 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-12-25
- * Changed:         2023-02-07
+ * Changed:         2023-02-08
  *
  * */
 
-#include <ls-std/core/exception/IllegalArgumentException.hpp>
+#include <ls-std/core/evaluator/EmptyStringArgumentEvaluator.hpp>
 #include <ls-std/io/kv/KvPair.hpp>
 
 ls::std::io::KvPair::KvPair(const ls::std::core::type::kv_key &_key, ls::std::core::type::kv_value _value) : ls::std::core::Class("KvPair"), value(::std::move(_value))
@@ -34,10 +34,6 @@ void ls::std::io::KvPair::setValue(const ls::std::core::type::kv_value &_value)
 
 void ls::std::io::KvPair::_assignKey(const ls::std::core::type::kv_key &_key)
 {
-  if (_key.empty())
-  {
-    throw ls::std::core::IllegalArgumentException{"_key is empty"};
-  }
-
+  ls::std::core::EmptyStringArgumentEvaluator{_key, "key is empty!"}.evaluate();
   this->key = _key;
 }

+ 4 - 12
source/ls-std/io/xml/XmlAttribute.cpp

@@ -3,11 +3,11 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-09-23
- * Changed:         2023-02-07
+ * Changed:         2023-02-08
  *
  * */
 
-#include <ls-std/core/exception/IllegalArgumentException.hpp>
+#include <ls-std/core/evaluator/EmptyStringArgumentEvaluator.hpp>
 #include <ls-std/io/xml/XmlAttribute.hpp>
 
 ls::std::io::XmlAttribute::XmlAttribute(const ::std::string &_name) : ls::std::core::Class("XmlAttribute")
@@ -44,20 +44,12 @@ void ls::std::io::XmlAttribute::setValue(const ::std::string &_value)
 
 void ls::std::io::XmlAttribute::_assignName(const ::std::string &_name)
 {
-  if (_name.empty())
-  {
-    throw ls::std::core::IllegalArgumentException{"_name is empty"};
-  }
-
+  ls::std::core::EmptyStringArgumentEvaluator{_name, "xml attribute name is empty!"}.evaluate();
   this->name = _name;
 }
 
 void ls::std::io::XmlAttribute::_assignValue(const ::std::string &_value)
 {
-  if (_value.empty())
-  {
-    throw ls::std::core::IllegalArgumentException{"_value is empty"};
-  }
-
+  ls::std::core::EmptyStringArgumentEvaluator{_value, "xml attribute value is empty!"}.evaluate();
   this->value = _value;
 }

+ 16 - 43
source/ls-std/io/xml/XmlNode.cpp

@@ -3,11 +3,12 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-09-24
- * Changed:         2023-02-07
+ * Changed:         2023-02-08
  *
  * */
 
 #include <algorithm>
+#include <ls-std/core/evaluator/EmptyStringArgumentEvaluator.hpp>
 #include <ls-std/core/exception/IllegalArgumentException.hpp>
 #include <ls-std/io/xml/XmlNode.hpp>
 
@@ -21,7 +22,7 @@ bool ls::std::io::XmlNode::addAttributeAfter(const ::std::shared_ptr<ls::std::io
   bool added{};
   auto iterator = this->attributes.begin();
   _checkIfAttributeReferenceIsValid(_attribute);
-  _checkIfNameIsNotEmpty(_name);
+  ls::std::core::EmptyStringArgumentEvaluator{_name, "xml node name is empty!"}.evaluate();
 
   if (!this->_hasAttribute(_attribute->getName()))
   {
@@ -47,7 +48,7 @@ bool ls::std::io::XmlNode::addAttributeBefore(const ::std::shared_ptr<ls::std::i
   bool added{};
   auto iterator = this->attributes.begin();
   _checkIfAttributeReferenceIsValid(_attribute);
-  _checkIfNameIsNotEmpty(_name);
+  ls::std::core::EmptyStringArgumentEvaluator{_name, "xml node name is empty!"}.evaluate();
 
   if (!this->_hasAttribute(_attribute->getName()))
   {
@@ -310,21 +311,13 @@ std::string ls::std::io::XmlNode::_toXml_(uint8_t _tabSize)
 
 void ls::std::io::XmlNode::_assignName(const ::std::string &_name)
 {
-  if (_name.empty())
-  {
-    throw ls::std::core::IllegalArgumentException{"_name is empty"};
-  }
-
+  ls::std::core::EmptyStringArgumentEvaluator{_name, "xml node name is empty!"}.evaluate();
   this->name = _name;
 }
 
 void ls::std::io::XmlNode::_assignValue(const ::std::string &_value)
 {
-  if (_value.empty())
-  {
-    throw ls::std::core::IllegalArgumentException{"_value is empty"};
-  }
-
+  ls::std::core::EmptyStringArgumentEvaluator{_value, "xml node value is empty!"}.evaluate();
   this->value = _value;
 }
 
@@ -336,14 +329,6 @@ void ls::std::io::XmlNode::_checkIfAttributeReferenceIsValid(const ::std::shared
   }
 }
 
-void ls::std::io::XmlNode::_checkIfNameIsNotEmpty(const ::std::string &_name)
-{
-  if (_name.empty())
-  {
-    throw ls::std::core::IllegalArgumentException{"_name is empty"};
-  }
-}
-
 void ls::std::io::XmlNode::_checkIfNodeReferenceIsValid(const ::std::shared_ptr<ls::std::io::XmlNode> &_child)
 {
   if (_child == nullptr)
@@ -367,20 +352,14 @@ std::string ls::std::io::XmlNode::_getTab(uint8_t _tabSize)
 bool ls::std::io::XmlNode::_hasAttribute(const ::std::string &_name)
 {
   bool exists{};
+  ls::std::core::EmptyStringArgumentEvaluator{_name, "xml attribute name is empty!"}.evaluate();
 
-  if (_name.empty())
+  for (const auto &attribute : this->attributes)
   {
-    throw ls::std::core::IllegalArgumentException{"_name is empty"};
-  }
-  else
-  {
-    for (const auto &attribute : this->attributes)
+    if (attribute->getName() == _name)
     {
-      if (attribute->getName() == _name)
-      {
-        exists = true;
-        break;
-      }
+      exists = true;
+      break;
     }
   }
 
@@ -396,20 +375,14 @@ bool ls::std::io::XmlNode::_hasChild(const ::std::shared_ptr<ls::std::io::XmlNod
 bool ls::std::io::XmlNode::_hasChild(const ::std::string &_name)
 {
   bool exists{};
+  ls::std::core::EmptyStringArgumentEvaluator{_name, "xml child node name is empty!"}.evaluate();
 
-  if (_name.empty())
+  for (const auto &attribute : this->children)
   {
-    throw ls::std::core::IllegalArgumentException{"_name is empty"};
-  }
-  else
-  {
-    for (const auto &attribute : this->children)
+    if (attribute->getName() == _name)
     {
-      if (attribute->getName() == _name)
-      {
-        exists = true;
-        break;
-      }
+      exists = true;
+      break;
     }
   }
 

+ 68 - 0
test/cases/core/evaluator/EmptyStringArgumentEvaluatorTest.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 EmptyStringArgumentEvaluatorTest : public ::testing::Test
+  {
+    protected:
+
+      EmptyStringArgumentEvaluatorTest() = default;
+      ~EmptyStringArgumentEvaluatorTest() override = default;
+
+      void SetUp() override
+      {}
+
+      void TearDown() override
+      {}
+  };
+
+  TEST_F(EmptyStringArgumentEvaluatorTest, evaluate)
+  {
+    EXPECT_THROW(
+        {
+          try
+          {
+            EmptyStringArgumentEvaluator{""}.evaluate();
+          }
+          catch (const IllegalArgumentException &_exception)
+          {
+            string message = _exception.what();
+            ASSERT_STREQ("IllegalArgumentException thrown - passed argument is empty!", message.c_str());
+            throw;
+          }
+        },
+        IllegalArgumentException);
+  }
+
+  TEST_F(EmptyStringArgumentEvaluatorTest, evaluate_dedicated_message)
+  {
+    EXPECT_THROW(
+        {
+          try
+          {
+            EmptyStringArgumentEvaluator("", "this id is empty!").evaluate();
+          }
+          catch (const IllegalArgumentException &_exception)
+          {
+            string message = _exception.what();
+            ASSERT_STREQ("IllegalArgumentException thrown - this id is empty!", message.c_str());
+            throw;
+          }
+        },
+        IllegalArgumentException);
+  }
+}