Browse Source

Add evaluators to constructor of SectionPairRow class

Patrick-Christopher Mattulat 2 years ago
parent
commit
8c4ce5a5ed

+ 5 - 1
include/ls-std/io/section-pair/SectionPairRow.hpp

@@ -3,7 +3,7 @@
 * Company:         Lynar Studios
 * E-Mail:          webmaster@lynarstudios.com
 * Created:         2023-02-08
-* Changed:         2023-02-08
+* Changed:         2023-02-09
 *
 * */
 
@@ -24,6 +24,7 @@ namespace ls::std::io
       explicit SectionPairRow(const ::std::string &_key);
       ~SectionPairRow() override;
 
+      [[nodiscard]] ::std::string getKey();
       [[nodiscard]] ::std::string getValue();
       [[nodiscard]] bool isKeyValue();
       [[nodiscard]] bool isList();
@@ -34,6 +35,9 @@ namespace ls::std::io
       ::std::string key{};
       ls::std::io::SectionPairRowEnumType type{};
       ::std::list<::std::string> values{};
+
+      void _setType(const ls::std::io::SectionPairRowEnumType& _type);
+      void _setValue(const ::std::string &_value);
   };
 }
 

+ 26 - 3
source/ls-std/io/section-pair/SectionPairRow.cpp

@@ -3,20 +3,28 @@
 * Company:         Lynar Studios
 * E-Mail:          webmaster@lynarstudios.com
 * Created:         2023-02-08
-* Changed:         2023-02-08
+* Changed:         2023-02-09
 *
 * */
 
+#include <ls-std/core/evaluator/EmptyStringArgumentEvaluator.hpp>
+#include <ls-std/io/section-pair/SectionPairIdentifierArgumentEvaluator.hpp>
 #include <ls-std/io/section-pair/SectionPairRow.hpp>
 
 ls::std::io::SectionPairRow::SectionPairRow(const ::std::string &_key) : ls::std::core::Class("SectionPairRow")
 {
-  //ls::std::io::SectionPairRowKeyArgumentEvaluator{_key}.evaluate();
+  ls::std::core::EmptyStringArgumentEvaluator{_key, "passed key identifier for section pair row is empty!"}.evaluate();
+  ls::std::io::SectionPairIdentifierArgumentEvaluator(_key, "section pair key identifier \"" + _key + "\" contains invalid characters!").evaluate();
   this->key = _key;
 }
 
 ls::std::io::SectionPairRow::~SectionPairRow() = default;
 
+::std::string ls::std::io::SectionPairRow::getKey()
+{
+  return this->key;
+}
+
 ::std::string ls::std::io::SectionPairRow::getValue()
 {
   return this->values.empty() ? "" : this->values.front();
@@ -34,7 +42,22 @@ bool ls::std::io::SectionPairRow::isList()
 
 void ls::std::io::SectionPairRow::setValue(const ::std::string &_value)
 {
-  //ls::std::core::EmptyStringArgumentEvaluator{_value}.evaluate();
+  ls::std::core::EmptyStringArgumentEvaluator{_value, "passed value for section pair row is empty!"}.evaluate();
+  //TODO: add SectionPairRowValueArgumentEvaluator
+  this->_setType(ls::std::io::SectionPairRowEnumType::SECTION_PAIR_ROW_KEY_VALUE);
+  this->_setValue(_value);
+}
+
+void ls::std::io::SectionPairRow::_setType(const ls::std::io::SectionPairRowEnumType &_type)
+{
+  if (_type == ls::std::io::SectionPairRowEnumType::SECTION_PAIR_ROW_NOT_IMPLEMENTED)
+  {
+    this->type = _type;
+  }
+}
+
+void ls::std::io::SectionPairRow::_setValue(const ::std::string &_value)
+{
   if (!this->values.empty())
   {
     this->values.pop_front();

+ 63 - 5
test/cases/io/section-pair/SectionPairRowTest.cpp

@@ -3,7 +3,7 @@
 * Company:         Lynar Studios
 * E-Mail:          webmaster@lynarstudios.com
 * Created:         2023-02-08
-* Changed:         2023-02-08
+* Changed:         2023-02-09
 *
 * */
 
@@ -33,23 +33,63 @@ namespace
 
   TEST_F(SectionPairRowTest, getClassName)
   {
-    ASSERT_STREQ("SectionPairRow", SectionPairRow{"TMP_KEY"}.getClassName().c_str());
+    ASSERT_STREQ("SectionPairRow", SectionPairRow{"tmp-key"}.getClassName().c_str());
+  }
+
+  TEST_F(SectionPairRowTest, constructor_empty_key)
+  {
+    EXPECT_THROW(
+        {
+          try
+          {
+            SectionPairRow row{""};
+          }
+          catch (const IllegalArgumentException &_exception)
+          {
+            throw;
+          }
+        },
+        IllegalArgumentException);
+  }
+
+  TEST_F(SectionPairRowTest, constructor_invalid_key)
+  {
+    EXPECT_THROW(
+        {
+          try
+          {
+            SectionPairRow row{"-tmp-key"};
+          }
+          catch (const IllegalArgumentException &_exception)
+          {
+            throw;
+          }
+        },
+        IllegalArgumentException);
+  }
+
+  TEST_F(SectionPairRowTest, getKey)
+  {
+    ::std::string key = "tmp-key";
+
+    SectionPairRow row{key};
+    ASSERT_STREQ(key.c_str(), row.getKey().c_str());
   }
 
   TEST_F(SectionPairRowTest, getValue)
   {
-    SectionPairRow row{"TMP_KEY"};
+    SectionPairRow row{"tmp-key"};
     ASSERT_TRUE(row.getValue().empty());
   }
 
   TEST_F(SectionPairRowTest, isKeyValue)
   {
-    ASSERT_FALSE(SectionPairRow{"TMP_KEY"}.isKeyValue());
+    ASSERT_FALSE(SectionPairRow{"tmp-key"}.isKeyValue());
   }
 
   TEST_F(SectionPairRowTest, isList)
   {
-    ASSERT_FALSE(SectionPairRow{"TMP_KEY"}.isList());
+    ASSERT_FALSE(SectionPairRow{"tmp-key"}.isList());
   }
 
   TEST_F(SectionPairRowTest, setValue_key_value)
@@ -59,4 +99,22 @@ namespace
 
     ASSERT_STREQ("blue", row.getValue().c_str());
   }
+
+  TEST_F(SectionPairRowTest, setValue_key_value_empty_value)
+  {
+    SectionPairRow row{"color"};
+
+    EXPECT_THROW(
+        {
+          try
+          {
+            row.setValue("");
+          }
+          catch (const IllegalArgumentException &_exception)
+          {
+            throw;
+          }
+        },
+        IllegalArgumentException);
+  }
 }