Răsfoiți Sursa

Add serializable for SectionPairRowSingleValue class

Patrick-Christopher Mattulat 2 ani în urmă
părinte
comite
db23dba74f

+ 2 - 0
CMakeLists.txt

@@ -182,6 +182,7 @@ set(SOURCE_FILES_IO
         ${CMAKE_CURRENT_SOURCE_DIR}/source/ls-std/io/section-pair/SectionPairRowValue.cpp
         ${CMAKE_CURRENT_SOURCE_DIR}/source/ls-std/io/section-pair/SectionPairRowValueArgumentEvaluator.cpp
         ${CMAKE_CURRENT_SOURCE_DIR}/source/ls-std/io/section-pair/SectionPairRowValueValidator.cpp
+        ${CMAKE_CURRENT_SOURCE_DIR}/source/ls-std/io/section-pair/SerializableSectionPairRowSingleValue.cpp
         ${CMAKE_CURRENT_SOURCE_DIR}/source/ls-std/io/xml/XmlAttribute.cpp
         ${CMAKE_CURRENT_SOURCE_DIR}/source/ls-std/io/xml/XmlDeclaration.cpp
         ${CMAKE_CURRENT_SOURCE_DIR}/source/ls-std/io/xml/XmlDocument.cpp
@@ -273,6 +274,7 @@ if (${LS_STD_BUILD_WITH_TESTS})
             ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/io/section-pair/SectionPairRowTest.cpp
             ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/io/section-pair/SectionPairRowValueArgumentEvaluatorTest.cpp
             ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/io/section-pair/SectionPairRowValueValidatorTest.cpp
+            ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/io/section-pair/SerializableSectionPairRowSingleValueTest.cpp
             ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/io/xml/XmlAttributeTest.cpp
             ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/io/xml/XmlDeclarationTest.cpp
             ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/io/xml/XmlDocumentTest.cpp

+ 2 - 2
include/ls-std/core/exception/WrongCallException.hpp

@@ -3,7 +3,7 @@
 * Company:         Lynar Studios
 * E-Mail:          webmaster@lynarstudios.com
 * Created:         2023-02-10
-* Changed:         2023-02-10
+* Changed:         2023-02-11
 *
 * */
 
@@ -16,7 +16,7 @@
 
 namespace ls::std::core
 {
-  class LS_STD_DYNAMIC_GOAL WrongCallException : public ::std::exception
+  class LS_STD_DYNAMIC_GOAL WrongCallException : public ::std::exception // TODO: remove, really makes no sense :D
   {
     public:
 

+ 39 - 0
include/ls-std/io/section-pair/SerializableSectionPairRowSingleValue.hpp

@@ -0,0 +1,39 @@
+/*
+* Author:          Patrick-Christopher Mattulat
+* Company:         Lynar Studios
+* E-Mail:          webmaster@lynarstudios.com
+* Created:         2023-02-11
+* Changed:         2023-02-11
+*
+* */
+
+#ifndef LS_STD_SERIALIZABLE_SECTION_PAIR_ROW_SINGLE_VALUE_HPP
+#define LS_STD_SERIALIZABLE_SECTION_PAIR_ROW_SINGLE_VALUE_HPP
+
+#include "SectionPairRowSingleValue.hpp"
+#include <ls-std/core/interface/ISerializable.hpp>
+#include <ls-std/os/dynamic-goal.hpp>
+#include <memory>
+
+namespace ls::std::io
+{
+  class LS_STD_DYNAMIC_GOAL SerializableSectionPairRowSingleValue : public ls::std::core::interface_type::ISerializable
+  {
+    public:
+
+      explicit SerializableSectionPairRowSingleValue(const ::std::shared_ptr<ls::std::io::SectionPairRowSingleValue>& _value);
+      ~SerializableSectionPairRowSingleValue() override;
+
+      [[nodiscard]] ::std::shared_ptr<ls::std::io::SectionPairRowSingleValue> getValue();
+      [[nodiscard]] ls::std::core::type::byte_field marshal() override;
+      void unmarshal(const ls::std::core::type::byte_field &_data) override;
+
+    private:
+
+      ::std::shared_ptr<ls::std::io::SectionPairRowSingleValue> value{};
+
+      void _setValue(const ::std::shared_ptr<ls::std::io::SectionPairRowSingleValue>& _value);
+  };
+}
+
+#endif

+ 2 - 1
include/ls-std/ls-std-io.hpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2022-05-14
- * Changed:         2023-02-10
+ * Changed:         2023-02-11
  *
  * */
 
@@ -30,6 +30,7 @@
 #include <ls-std/io/section-pair/SectionPairRowValueArgumentEvaluator.hpp>
 #include <ls-std/io/section-pair/SectionPairRowValueValidator.hpp>
 #include <ls-std/io/section-pair/SectionPairTypes.hpp>
+#include <ls-std/io/section-pair/SerializableSectionPairRowSingleValue.hpp>
 
 #include <ls-std/io/xml/XmlAttribute.hpp>
 #include <ls-std/io/xml/XmlDeclaration.hpp>

+ 41 - 0
source/ls-std/io/section-pair/SerializableSectionPairRowSingleValue.cpp

@@ -0,0 +1,41 @@
+/*
+* Author:          Patrick-Christopher Mattulat
+* Company:         Lynar Studios
+* E-Mail:          webmaster@lynarstudios.com
+* Created:         2023-02-11
+* Changed:         2023-02-11
+*
+* */
+
+#include <ls-std/core/evaluator/EmptyStringArgumentEvaluator.hpp>
+#include <ls-std/core/evaluator/NullPointerArgumentEvaluator.hpp>
+#include <ls-std/io/section-pair/SerializableSectionPairRowSingleValue.hpp>
+
+ls::std::io::SerializableSectionPairRowSingleValue::SerializableSectionPairRowSingleValue(const ::std::shared_ptr<ls::std::io::SectionPairRowSingleValue> &_value)
+{
+  this->_setValue(_value);
+}
+
+ls::std::io::SerializableSectionPairRowSingleValue::~SerializableSectionPairRowSingleValue() = default;
+
+::std::shared_ptr<ls::std::io::SectionPairRowSingleValue> ls::std::io::SerializableSectionPairRowSingleValue::getValue()
+{
+  return this->value;
+}
+
+ls::std::core::type::byte_field ls::std::io::SerializableSectionPairRowSingleValue::marshal()
+{
+  return this->value->get();
+}
+
+void ls::std::io::SerializableSectionPairRowSingleValue::unmarshal(const ls::std::core::type::byte_field &_data)
+{
+  ls::std::core::EmptyStringArgumentEvaluator{_data}.evaluate();
+  this->value->set(_data);
+}
+
+void ls::std::io::SerializableSectionPairRowSingleValue::_setValue(const ::std::shared_ptr<ls::std::io::SectionPairRowSingleValue> &_value)
+{
+  ls::std::core::NullPointerArgumentEvaluator{_value, "model reference for SerializableSectionPairRowSingleValue is null!"}.evaluate();
+  this->value = _value;
+}

+ 91 - 0
test/cases/io/section-pair/SerializableSectionPairRowSingleValueTest.cpp

@@ -0,0 +1,91 @@
+/*
+* Author:          Patrick-Christopher Mattulat
+* Company:         Lynar Studios
+* E-Mail:          webmaster@lynarstudios.com
+* Created:         2023-02-11
+* Changed:         2023-02-11
+*
+* */
+
+#include <gtest/gtest.h>
+#include <ls-std/ls-std-core.hpp>
+#include <ls-std/ls-std-io.hpp>
+#include <memory>
+
+using namespace ls::std::core;
+using namespace ls::std::io;
+using namespace ::std;
+
+namespace
+{
+  class SerializableSectionPairRowSingleValueTest : public ::testing::Test
+  {
+    protected:
+
+      SerializableSectionPairRowSingleValueTest() = default;
+      ~SerializableSectionPairRowSingleValueTest() override = default;
+
+      void SetUp() override
+      {}
+
+      void TearDown() override
+      {}
+  };
+
+  TEST_F(SerializableSectionPairRowSingleValueTest, constructor_no_reference)
+  {
+    EXPECT_THROW(
+        {
+          try
+          {
+            SerializableSectionPairRowSingleValue serializable(nullptr);
+          }
+          catch (const IllegalArgumentException &_exception)
+          {
+            throw;
+          }
+        },
+        IllegalArgumentException);
+  }
+
+  TEST_F(SerializableSectionPairRowSingleValueTest, getValue)
+  {
+    shared_ptr<SectionPairRowSingleValue> value = make_shared<SectionPairRowSingleValue>("empty");
+    SerializableSectionPairRowSingleValue serializable(value);
+
+    ASSERT_TRUE(value == serializable.getValue());
+  }
+
+  TEST_F(SerializableSectionPairRowSingleValueTest, marshal)
+  {
+    SerializableSectionPairRowSingleValue serializable(make_shared<SectionPairRowSingleValue>("empty"));
+    ASSERT_STREQ("empty", serializable.marshal().c_str());
+  }
+
+  TEST_F(SerializableSectionPairRowSingleValueTest, unmarshal)
+  {
+    shared_ptr<SectionPairRowSingleValue> value = make_shared<SectionPairRowSingleValue>("empty");
+    SerializableSectionPairRowSingleValue serializable(value);
+    serializable.unmarshal("blue");
+
+    ASSERT_STREQ("blue", value->get().c_str());
+  }
+
+  TEST_F(SerializableSectionPairRowSingleValueTest, unmarshal_empty_string)
+  {
+    SerializableSectionPairRowSingleValue serializable(make_shared<SectionPairRowSingleValue>("empty"));
+
+    EXPECT_THROW(
+        {
+          try
+          {
+            serializable.unmarshal("");
+          }
+          catch (const IllegalArgumentException &_exception)
+          {
+            throw;
+          }
+        },
+        IllegalArgumentException);
+  }
+}