Parcourir la source

Make SectionPairRowListValue serializable

Patrick-Christopher Mattulat il y a 2 ans
Parent
commit
34188ef289

+ 4 - 0
include/ls-std/io/section-pair/model/SectionPairRowListValue.hpp

@@ -29,9 +29,13 @@ namespace ls::std::io
       [[nodiscard]] ::std::list<ls::std::io::section_pair_row_value> getList();
       [[nodiscard]] size_t getSize();
       [[nodiscard]] ls::std::io::SectionPairRowEnumType getType() override;
+      [[nodiscard]] ls::std::core::type::byte_field marshal() override;
+      void setSerializable(const ::std::shared_ptr<ls::std::core::interface_type::ISerializable>& _serializable);
+      void unmarshal(const ls::std::core::type::byte_field &_data) override;
 
     private:
 
+      ::std::shared_ptr<ls::std::core::interface_type::ISerializable> serializable{};
       ::std::list<ls::std::io::section_pair_row_value> values{};
   };
 }

+ 23 - 0
source/ls-std/io/section-pair/model/SectionPairRowListValue.cpp

@@ -9,8 +9,11 @@
 
 #include <ls-std/core/evaluator/EmptyStringArgumentEvaluator.hpp>
 #include <ls-std/core/evaluator/IndexOutOfBoundsEvaluator.hpp>
+#include <ls-std/core/evaluator/NullPointerArgumentEvaluator.hpp>
+#include <ls-std/core/evaluator/NullPointerEvaluator.hpp>
 #include <ls-std/io/section-pair/evaluator/SectionPairRowValueArgumentEvaluator.hpp>
 #include <ls-std/io/section-pair/model/SectionPairRowListValue.hpp>
+#include <string>
 
 ls::std::io::SectionPairRowListValue::SectionPairRowListValue() : ls::std::core::Class("SectionPairRowListValue"), ls::std::io::SectionPairRowValue(ls::std::io::SectionPairRowEnumType::SECTION_PAIR_ROW_LIST_VALUE)
 {}
@@ -58,3 +61,23 @@ ls::std::io::SectionPairRowEnumType ls::std::io::SectionPairRowListValue::getTyp
 {
   return this->type;
 }
+
+ls::std::core::type::byte_field ls::std::io::SectionPairRowListValue::marshal()
+{
+  ::std::string message = "member \"serializable\" for marshal attempt is null!";
+  ls::std::core::NullPointerEvaluator{::std::reinterpret_pointer_cast<void>(this->serializable)}.evaluate();
+  return this->serializable->marshal();
+}
+
+void ls::std::io::SectionPairRowListValue::setSerializable(const ::std::shared_ptr<ls::std::core::interface_type::ISerializable> &_serializable)
+{
+  ls::std::core::NullPointerArgumentEvaluator{::std::reinterpret_pointer_cast<void>(_serializable)}.evaluate();
+  this->serializable = _serializable;
+}
+
+void ls::std::io::SectionPairRowListValue::unmarshal(const ls::std::core::type::byte_field &_data)
+{
+  ::std::string message = "member \"serializable\" for unmarshal attempt is null!";
+  ls::std::core::NullPointerEvaluator{::std::reinterpret_pointer_cast<void>(this->serializable)}.evaluate();
+  this->serializable->unmarshal(_data);
+}

+ 81 - 0
test/cases/io/section-pair/model/SectionPairRowListValueTest.cpp

@@ -12,6 +12,7 @@
 #include <ls-std/ls-std-io.hpp>
 
 using namespace ls::std::core;
+using namespace ls::std::core::type;
 using namespace ls::std::io;
 using namespace ::std;
 
@@ -116,4 +117,84 @@ namespace
     SectionPairRowListValue list{};
     ASSERT_EQ(ls::std::io::SECTION_PAIR_ROW_LIST_VALUE, list.getType());
   }
+
+  TEST_F(SectionPairRowListValueTest, marshal)
+  {
+    shared_ptr<SectionPairRowListValue> value = make_shared<SectionPairRowListValue>();
+    value->setSerializable(make_shared<SerializableSectionPairRowListValue>(value));
+    value->add("Drumming");
+    value->add("Reading");
+    value->add("Coding");
+
+    ::std::string expected = "  Drumming" + NewLine::get() + "  Reading" + NewLine::get() + "  Coding" + NewLine::get();
+
+    ASSERT_STREQ(expected.c_str(), value->marshal().c_str());
+  }
+
+  TEST_F(SectionPairRowListValueTest, marshal_no_reference)
+  {
+    SectionPairRowListValue list{};
+
+    EXPECT_THROW(
+        {
+          try
+          {
+            byte_field data = list.marshal();
+          }
+          catch (const NullPointerException &_exception)
+          {
+            throw;
+          }
+        },
+        NullPointerException);
+  }
+
+  TEST_F(SectionPairRowListValueTest, setSerializable_no_reference)
+  {
+    SectionPairRowListValue list{};
+
+    EXPECT_THROW(
+        {
+          try
+          {
+            list.setSerializable(nullptr);
+          }
+          catch (const IllegalArgumentException &_exception)
+          {
+            throw;
+          }
+        },
+        IllegalArgumentException);
+  }
+
+  TEST_F(SectionPairRowListValueTest, unmarshal)
+  {
+    shared_ptr<SectionPairRowListValue> value = make_shared<SectionPairRowListValue>();
+    value->setSerializable(make_shared<SerializableSectionPairRowListValue>(value));
+    ::std::string serializedListValue = "  Drumming" + NewLine::get() + "  Reading" + NewLine::get() + "  Coding" + NewLine::get();
+    value->unmarshal(serializedListValue);
+
+    ASSERT_EQ(3, value->getSize());
+    ASSERT_STREQ("Drumming", value->get(0).c_str());
+    ASSERT_STREQ("Reading", value->get(1).c_str());
+    ASSERT_STREQ("Coding", value->get(2).c_str());
+  }
+
+  TEST_F(SectionPairRowListValueTest, unmarshal_no_reference)
+  {
+    SectionPairRowListValue list{};
+
+    EXPECT_THROW(
+        {
+          try
+          {
+            list.unmarshal("  Blue\nYellow\n");
+          }
+          catch (const NullPointerException &_exception)
+          {
+            throw;
+          }
+        },
+        NullPointerException);
+  }
 }