Browse Source

Add SectionPairRowSingleValue member to SectionPairRow class

Patrick-Christopher Mattulat 2 years ago
parent
commit
c3ab956fbe

+ 7 - 3
include/ls-std/io/section-pair/SectionPairRow.hpp

@@ -11,10 +11,11 @@
 #define LS_STD_SECTION_PAIR_ROW_HPP
 
 #include "SectionPairRowEnumType.hpp"
+#include "SectionPairRowValue.hpp"
 #include "SectionPairTypes.hpp"
-#include <list>
 #include <ls-std/core/Class.hpp>
 #include <ls-std/os/dynamic-goal.hpp>
+#include <memory>
 
 namespace ls::std::io
 {
@@ -22,7 +23,7 @@ namespace ls::std::io
   {
     public:
 
-      explicit SectionPairRow(const ls::std::io::section_pair_identifier &_key);
+      explicit SectionPairRow(const ls::std::io::section_pair_identifier &_key, const ls::std::io::SectionPairRowEnumType &_type);
       ~SectionPairRow() override;
 
       [[nodiscard]] ls::std::io::section_pair_identifier getKey();
@@ -32,7 +33,10 @@ namespace ls::std::io
     private:
 
       ls::std::io::section_pair_identifier key{};
-      ls::std::io::SectionPairRowEnumType type{};
+      ::std::shared_ptr<ls::std::io::SectionPairRowValue> value{};
+
+      void _initValue(const ls::std::io::SectionPairRowEnumType &_type);
+      void _setKey(const ls::std::io::section_pair_identifier &_key);
   };
 }
 

+ 3 - 0
include/ls-std/io/section-pair/SectionPairRowSingleValue.hpp

@@ -26,10 +26,13 @@ namespace ls::std::io
 
       [[nodiscard]] ls::std::io::section_pair_row_value get();
       [[nodiscard]] ls::std::io::SectionPairRowEnumType getType() override;
+      void set(const ls::std::io::section_pair_row_value &_value);
 
     private:
 
       ls::std::io::section_pair_row_value value{};
+
+      void _set(const ls::std::io::section_pair_row_value &_value);
   };
 }
 

+ 34 - 6
source/ls-std/io/section-pair/SectionPairRow.cpp

@@ -8,14 +8,15 @@
 * */
 
 #include <ls-std/core/evaluator/EmptyStringArgumentEvaluator.hpp>
+#include <ls-std/core/exception/IllegalArgumentException.hpp>
 #include <ls-std/io/section-pair/SectionPairIdentifierArgumentEvaluator.hpp>
 #include <ls-std/io/section-pair/SectionPairRow.hpp>
+#include <ls-std/io/section-pair/SectionPairRowSingleValue.hpp>
 
-ls::std::io::SectionPairRow::SectionPairRow(const ls::std::io::section_pair_identifier &_key) : ls::std::core::Class("SectionPairRow")
+ls::std::io::SectionPairRow::SectionPairRow(const ls::std::io::section_pair_identifier &_key, const ls::std::io::SectionPairRowEnumType &_type) : ls::std::core::Class("SectionPairRow")
 {
-  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;
+  this->_setKey(_key);
+  this->_initValue(_type);
 }
 
 ls::std::io::SectionPairRow::~SectionPairRow() = default;
@@ -27,10 +28,37 @@ ls::std::io::section_pair_row_value ls::std::io::SectionPairRow::getKey()
 
 bool ls::std::io::SectionPairRow::isList()
 {
-  return this->type == ls::std::io::SectionPairRowEnumType::SECTION_PAIR_ROW_LIST;
+  return this->value->getType() == ls::std::io::SectionPairRowEnumType::SECTION_PAIR_ROW_LIST;
 }
 
 bool ls::std::io::SectionPairRow::isSingleValue()
 {
-  return this->type == ls::std::io::SectionPairRowEnumType::SECTION_PAIR_ROW_SINGLE_VALUE;
+  return this->value->getType() == ls::std::io::SectionPairRowEnumType::SECTION_PAIR_ROW_SINGLE_VALUE;
+}
+
+void ls::std::io::SectionPairRow::_initValue(const ls::std::io::SectionPairRowEnumType &_type)
+{
+  switch (_type)
+  {
+    case SECTION_PAIR_ROW_NOT_IMPLEMENTED:
+    {
+      throw ls::std::core::IllegalArgumentException{"default row enum type can not be set!"};
+    }
+    case SECTION_PAIR_ROW_LIST:
+    {
+    }
+    break;
+    case SECTION_PAIR_ROW_SINGLE_VALUE:
+    {
+      this->value = ::std::make_shared<ls::std::io::SectionPairRowSingleValue>("empty");
+    }
+    break;
+  }
+}
+
+void ls::std::io::SectionPairRow::_setKey(const ls::std::io::section_pair_identifier &_key)
+{
+  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;
 }

+ 13 - 3
source/ls-std/io/section-pair/SectionPairRowSingleValue.cpp

@@ -13,9 +13,7 @@
 
 ls::std::io::SectionPairRowSingleValue::SectionPairRowSingleValue(const ls::std::io::section_pair_row_value &_value) : ls::std::core::Class("SectionPairRowSingleValue"), ls::std::io::SectionPairRowValue(ls::std::io::SECTION_PAIR_ROW_SINGLE_VALUE)
 {
-  ls::std::core::EmptyStringArgumentEvaluator{_value}.evaluate();
-  ls::std::io::SectionPairRowValueArgumentEvaluator(_value, "section pair single value \"" + _value + "\" contains invalid characters!").evaluate();
-  this->value = _value;
+  this->_set(_value);
 }
 
 ls::std::io::SectionPairRowSingleValue::~SectionPairRowSingleValue() = default;
@@ -29,3 +27,15 @@ ls::std::io::SectionPairRowEnumType ls::std::io::SectionPairRowSingleValue::getT
 {
   return this->type;
 }
+
+void ls::std::io::SectionPairRowSingleValue::set(const ls::std::io::section_pair_row_value &_value)
+{
+  this->_set(_value);
+}
+
+void ls::std::io::SectionPairRowSingleValue::_set(const ls::std::io::section_pair_row_value &_value)
+{
+  ls::std::core::EmptyStringArgumentEvaluator{_value}.evaluate();
+  ls::std::io::SectionPairRowValueArgumentEvaluator(_value, "section pair single value \"" + _value + "\" contains invalid characters!").evaluate();
+  this->value = _value;
+}

+ 37 - 1
test/cases/io/section-pair/SectionPairRowSingleValueTest.cpp

@@ -79,4 +79,40 @@ namespace
     SectionPairRowSingleValue value{"blue"};
     ASSERT_EQ(ls::std::io::SECTION_PAIR_ROW_SINGLE_VALUE, value.getType());
   }
-}
+
+  TEST_F(SectionPairRowSingleValueTest, set_empty_value)
+  {
+    SectionPairRowSingleValue value{"empty"};
+
+    EXPECT_THROW(
+        {
+          try
+          {
+            value.set("");
+          }
+          catch (const IllegalArgumentException &_exception)
+          {
+            throw;
+          }
+        },
+        IllegalArgumentException);
+  }
+
+  TEST_F(SectionPairRowSingleValueTest, set_invalid_value)
+  {
+    SectionPairRowSingleValue value{"empty"};
+
+    EXPECT_THROW(
+        {
+          try
+          {
+            value.set("=33");
+          }
+          catch (const IllegalArgumentException &_exception)
+          {
+            throw;
+          }
+        },
+        IllegalArgumentException);
+  }
+}

+ 6 - 6
test/cases/io/section-pair/SectionPairRowTest.cpp

@@ -33,7 +33,7 @@ namespace
 
   TEST_F(SectionPairRowTest, getClassName)
   {
-    ASSERT_STREQ("SectionPairRow", SectionPairRow{"tmp-key"}.getClassName().c_str());
+    ASSERT_STREQ("SectionPairRow", SectionPairRow("tmp-key", SectionPairRowEnumType::SECTION_PAIR_ROW_SINGLE_VALUE).getClassName().c_str());
   }
 
   TEST_F(SectionPairRowTest, constructor_empty_key)
@@ -42,7 +42,7 @@ namespace
         {
           try
           {
-            SectionPairRow row{""};
+            SectionPairRow row("", SectionPairRowEnumType::SECTION_PAIR_ROW_SINGLE_VALUE);
           }
           catch (const IllegalArgumentException &_exception)
           {
@@ -58,7 +58,7 @@ namespace
         {
           try
           {
-            SectionPairRow row{"-tmp-key"};
+            SectionPairRow row("-tmp-key", SectionPairRowEnumType::SECTION_PAIR_ROW_SINGLE_VALUE);
           }
           catch (const IllegalArgumentException &_exception)
           {
@@ -72,17 +72,17 @@ namespace
   {
     ls::std::io::section_pair_identifier key = "tmp-key";
 
-    SectionPairRow row{key};
+    SectionPairRow row(key, SectionPairRowEnumType::SECTION_PAIR_ROW_SINGLE_VALUE);
     ASSERT_STREQ(key.c_str(), row.getKey().c_str());
   }
 
   TEST_F(SectionPairRowTest, isSingleValue)
   {
-    ASSERT_FALSE(SectionPairRow{"tmp-key"}.isSingleValue());
+    ASSERT_TRUE(SectionPairRow("tmp-key", SectionPairRowEnumType::SECTION_PAIR_ROW_SINGLE_VALUE).isSingleValue());
   }
 
   TEST_F(SectionPairRowTest, isList)
   {
-    ASSERT_FALSE(SectionPairRow{"tmp-key"}.isList());
+    ASSERT_FALSE(SectionPairRow("tmp-key", SectionPairRowEnumType::SECTION_PAIR_ROW_SINGLE_VALUE).isList());
   }
 }