Pārlūkot izejas kodu

Add evaluator to SerializableSectionPairSection class

Patrick-Christopher Mattulat 1 gadu atpakaļ
vecāks
revīzija
f39845fb40

+ 2 - 0
CMakeLists.txt

@@ -174,6 +174,7 @@ set(SOURCE_FILES_IO
         ${CMAKE_CURRENT_SOURCE_DIR}/source/ls-std/io/section-pair/evaluator/SectionPairRowArgumentEvaluator.cpp
         ${CMAKE_CURRENT_SOURCE_DIR}/source/ls-std/io/section-pair/evaluator/SectionPairRowListValueArgumentEvaluator.cpp
         ${CMAKE_CURRENT_SOURCE_DIR}/source/ls-std/io/section-pair/evaluator/SectionPairRowSingleValueArgumentEvaluator.cpp
+        ${CMAKE_CURRENT_SOURCE_DIR}/source/ls-std/io/section-pair/evaluator/SectionPairSectionArgumentEvaluator.cpp
         ${CMAKE_CURRENT_SOURCE_DIR}/source/ls-std/io/section-pair/evaluator/SectionPairValueArgumentEvaluator.cpp
         ${CMAKE_CURRENT_SOURCE_DIR}/source/ls-std/io/section-pair/model/SectionPairDocument.cpp
         ${CMAKE_CURRENT_SOURCE_DIR}/source/ls-std/io/section-pair/model/SectionPairRow.cpp
@@ -279,6 +280,7 @@ if (${LS_STD_BUILD_WITH_TESTS})
             ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/io/section-pair/evaluator/SectionPairRowArgumentEvaluatorTest.cpp
             ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/io/section-pair/evaluator/SectionPairRowListValueArgumentEvaluatorTest.cpp
             ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/io/section-pair/evaluator/SectionPairRowSingleValueArgumentEvaluatorTest.cpp
+            ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/io/section-pair/evaluator/SectionPairSectionArgumentEvaluatorTest.cpp
             ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/io/section-pair/evaluator/SectionPairValueArgumentEvaluatorTest.cpp
             ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/io/section-pair/validator/SectionPairIdentifierValidatorTest.cpp
             ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/io/section-pair/validator/SectionPairRowListValueValidatorTest.cpp

+ 35 - 0
include/ls-std/io/section-pair/evaluator/SectionPairSectionArgumentEvaluator.hpp

@@ -0,0 +1,35 @@
+/*
+* Author:          Patrick-Christopher Mattulat
+* Company:         Lynar Studios
+* E-Mail:          webmaster@lynarstudios.com
+* Created:         2023-02-20
+* Changed:         2023-02-20
+*
+* */
+
+#ifndef LS_STD_SECTION_PAIR_SECTION_ARGUMENT_EVALUATOR_HPP
+#define LS_STD_SECTION_PAIR_SECTION_ARGUMENT_EVALUATOR_HPP
+
+#include <ls-std/core/Class.hpp>
+#include <ls-std/core/interface/IEvaluator.hpp>
+#include <ls-std/os/dynamic-goal.hpp>
+#include <string>
+
+namespace ls::std::io
+{
+  class LS_STD_DYNAMIC_GOAL SectionPairSectionArgumentEvaluator : public ls::std::core::Class, public ls::std::core::interface_type::IEvaluator
+  {
+    public:
+
+      explicit SectionPairSectionArgumentEvaluator(::std::string _section);
+      ~SectionPairSectionArgumentEvaluator() override;
+
+      void evaluate() override;
+
+    private:
+
+      ::std::string section{};
+  };
+}
+
+#endif

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

@@ -20,6 +20,7 @@
 #include <ls-std/io/section-pair/evaluator/SectionPairRowArgumentEvaluator.hpp>
 #include <ls-std/io/section-pair/evaluator/SectionPairRowListValueArgumentEvaluator.hpp>
 #include <ls-std/io/section-pair/evaluator/SectionPairRowSingleValueArgumentEvaluator.hpp>
+#include <ls-std/io/section-pair/evaluator/SectionPairSectionArgumentEvaluator.hpp>
 #include <ls-std/io/section-pair/evaluator/SectionPairValueArgumentEvaluator.hpp>
 #include <ls-std/io/section-pair/model/SectionPairDocument.hpp>
 #include <ls-std/io/section-pair/model/SectionPairRow.hpp>

+ 26 - 0
source/ls-std/io/section-pair/evaluator/SectionPairSectionArgumentEvaluator.cpp

@@ -0,0 +1,26 @@
+/*
+* Author:          Patrick-Christopher Mattulat
+* Company:         Lynar Studios
+* E-Mail:          webmaster@lynarstudios.com
+* Created:         2023-02-20
+* Changed:         2023-02-20
+*
+* */
+
+#include <ls-std/core/exception/IllegalArgumentException.hpp>
+#include <ls-std/io/section-pair/evaluator/SectionPairSectionArgumentEvaluator.hpp>
+#include <ls-std/io/section-pair/validator/SectionPairSectionValidator.hpp>
+
+ls::std::io::SectionPairSectionArgumentEvaluator::SectionPairSectionArgumentEvaluator(::std::string _section) : ls::std::core::Class("SectionPairSectionArgumentEvaluator"), section(::std::move(_section))
+{}
+
+ls::std::io::SectionPairSectionArgumentEvaluator::~SectionPairSectionArgumentEvaluator() = default;
+
+void ls::std::io::SectionPairSectionArgumentEvaluator::evaluate()
+{
+  if (!ls::std::io::SectionPairSectionValidator{this->section}.isValid())
+  {
+    ::std::string message = "\"" + this->section + "\" is not a valid section!";
+    throw ls::std::core::IllegalArgumentException{message};
+  }
+}

+ 3 - 1
source/ls-std/io/section-pair/serialization/SerializableSectionPairSection.cpp

@@ -3,12 +3,13 @@
 * Company:         Lynar Studios
 * E-Mail:          webmaster@lynarstudios.com
 * Created:         2023-02-14
-* Changed:         2023-02-17
+* Changed:         2023-02-20
 *
 * */
 
 #include <ls-std/core/evaluator/NullPointerArgumentEvaluator.hpp>
 #include <ls-std/core/exception/IllegalArgumentException.hpp>
+#include <ls-std/io/section-pair/evaluator/SectionPairSectionArgumentEvaluator.hpp>
 #include <ls-std/io/section-pair/model/SectionPairRow.hpp>
 #include <ls-std/io/section-pair/model/SectionPairSection.hpp>
 #include <ls-std/io/section-pair/serialization/SerializableSectionPairSection.hpp>
@@ -40,6 +41,7 @@ ls::std::core::type::byte_field ls::std::io::SerializableSectionPairSection::mar
 
 void ls::std::io::SerializableSectionPairSection::unmarshal(const ls::std::core::type::byte_field &_data)
 {
+  ls::std::io::SectionPairSectionArgumentEvaluator{_data}.evaluate();
   size_t sectionHeaderSize = this->_unmarshalSectionHeader(_data);
   this->_unmarshalRows(_data.substr(sectionHeaderSize));
 }

+ 70 - 0
test/cases/io/section-pair/evaluator/SectionPairSectionArgumentEvaluatorTest.cpp

@@ -0,0 +1,70 @@
+/*
+* Author:          Patrick-Christopher Mattulat
+* Company:         Lynar Studios
+* E-Mail:          webmaster@lynarstudios.com
+* Created:         2023-02-20
+* Changed:         2023-02-20
+*
+* */
+
+#include <gtest/gtest.h>
+#include <ls-std/ls-std-core.hpp>
+#include <ls-std/ls-std-io.hpp>
+#include <string>
+
+using namespace ls::std::core;
+using namespace ls::std::io;
+using namespace ::std;
+
+namespace
+{
+  class SectionPairSectionArgumentEvaluatorTest : public ::testing::Test
+  {
+    protected:
+
+      SectionPairSectionArgumentEvaluatorTest() = default;
+      ~SectionPairSectionArgumentEvaluatorTest() override = default;
+
+      void SetUp() override
+      {}
+
+      void TearDown() override
+      {}
+  };
+
+  class SectionPairSectionArgumentEvaluatorTest_InvalidArgumentTest : public ::testing::TestWithParam<string>
+  {
+    protected:
+
+      SectionPairSectionArgumentEvaluatorTest_InvalidArgumentTest() = default;
+      ~SectionPairSectionArgumentEvaluatorTest_InvalidArgumentTest() override = default;
+  };
+
+  TEST_F(SectionPairSectionArgumentEvaluatorTest, getClassName)
+  {
+    shared_ptr<SectionPairSectionArgumentEvaluator> evaluator = make_shared<SectionPairSectionArgumentEvaluator>("=33");
+    ASSERT_STREQ("SectionPairSectionArgumentEvaluator", evaluator->getClassName().c_str());
+  }
+
+  TEST_P(SectionPairSectionArgumentEvaluatorTest_InvalidArgumentTest, evaluate)
+  {
+    EXPECT_THROW(
+        {
+          try
+          {
+            SectionPairSectionArgumentEvaluator(GetParam()).evaluate();
+          }
+          catch (const IllegalArgumentException &_exception)
+          {
+            ::std::string actual = _exception.what();
+            ::std::string expected = "IllegalArgumentException thrown - \"" + GetParam() + "\" is not a valid section!";
+
+            ASSERT_STREQ(expected.c_str(), actual.c_str());
+            throw;
+          }
+        },
+        IllegalArgumentException);
+  }
+
+  INSTANTIATE_TEST_SUITE_P(InvalidArgumentTest, SectionPairSectionArgumentEvaluatorTest_InvalidArgumentTest, ::testing::Values("\n[general]\n\n", "\n[section]\n\ncolors:"));
+}

+ 20 - 0
test/cases/io/section-pair/serialization/SerializableSectionPairSectionTest.cpp

@@ -153,5 +153,25 @@ namespace
     ASSERT_STREQ("33", dynamic_pointer_cast<SectionPairRowSingleValue>(section->get(2)->getValue())->get().c_str());
   }
 
+  TEST_F(SerializableSectionPairSectionTest, unmarshal_invalid_input)
+  {
+    SerializableSectionPairParameter parameter{};
+    parameter.setValue(make_shared<SectionPairSection>("tmp-key"));
+    SerializableSectionPairSection serializable(parameter);
+
+    EXPECT_THROW(
+        {
+          try
+          {
+            serializable.unmarshal(R"(\n[general]\n\n)");
+          }
+          catch (const IllegalArgumentException &_exception)
+          {
+            throw;
+          }
+        },
+        IllegalArgumentException);
+  }
+
   INSTANTIATE_TEST_SUITE_P(LineBreakTest, SerializableSectionPairSectionTest_LineBreakTest, ::testing::Values(NewLine::getUnixNewLine(), NewLine::getWindowsNewLine()));
 }