소스 검색

Add validator for SectionPairDocument class

Patrick-Christopher Mattulat 1 년 전
부모
커밋
711b09b70d

+ 2 - 0
CMakeLists.txt

@@ -182,6 +182,7 @@ set(SOURCE_FILES_IO
         ${CMAKE_CURRENT_SOURCE_DIR}/source/ls-std/io/section-pair/model/SectionPairRowSingleValue.cpp
         ${CMAKE_CURRENT_SOURCE_DIR}/source/ls-std/io/section-pair/model/SectionPairRowValue.cpp
         ${CMAKE_CURRENT_SOURCE_DIR}/source/ls-std/io/section-pair/model/SectionPairSection.cpp
+        ${CMAKE_CURRENT_SOURCE_DIR}/source/ls-std/io/section-pair/validator/SectionPairDocumentValidator.cpp
         ${CMAKE_CURRENT_SOURCE_DIR}/source/ls-std/io/section-pair/validator/SectionPairIdentifierValidator.cpp
         ${CMAKE_CURRENT_SOURCE_DIR}/source/ls-std/io/section-pair/validator/SectionPairRowListValueValidator.cpp
         ${CMAKE_CURRENT_SOURCE_DIR}/source/ls-std/io/section-pair/validator/SectionPairRowSingleValueValidator.cpp
@@ -281,6 +282,7 @@ if (${LS_STD_BUILD_WITH_TESTS})
             ${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/SectionPairDocumentValidatorTest.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
             ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/io/section-pair/validator/SectionPairRowSingleValueValidatorTest.cpp

+ 37 - 0
include/ls-std/io/section-pair/validator/SectionPairDocumentValidator.hpp

@@ -0,0 +1,37 @@
+/*
+* Author:          Patrick-Christopher Mattulat
+* Company:         Lynar Studios
+* E-Mail:          webmaster@lynarstudios.com
+* Created:         2023-02-21
+* Changed:         2023-02-21
+*
+* */
+
+#ifndef LS_STD_SECTION_PAIR_DOCUMENT_VALIDATOR_HPP
+#define LS_STD_SECTION_PAIR_DOCUMENT_VALIDATOR_HPP
+
+#include <ls-std/core/Class.hpp>
+#include <ls-std/core/interface/IValidator.hpp>
+#include <ls-std/os/dynamic-goal.hpp>
+#include <string>
+
+namespace ls::std::io
+{
+  class LS_STD_DYNAMIC_GOAL SectionPairDocumentValidator : public ls::std::core::Class, public ls::std::core::interface_type::IValidator
+  {
+    public:
+
+      explicit SectionPairDocumentValidator(::std::string _document);
+      ~SectionPairDocumentValidator() override;
+
+      [[nodiscard]] bool isValid() override;
+
+    private:
+
+      ::std::string document{};
+
+      [[nodiscard]] static ::std::string _getValidationRegex();
+  };
+}
+
+#endif

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

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2023-02-11
- * Changed:         2023-02-20
+ * Changed:         2023-02-21
  *
  * */
 
@@ -36,6 +36,7 @@
 #include <ls-std/io/section-pair/serialization/SerializableSectionPairSection.hpp>
 #include <ls-std/io/section-pair/type/SectionPairDocumentTypes.hpp>
 #include <ls-std/io/section-pair/type/SectionPairSectionTypes.hpp>
+#include <ls-std/io/section-pair/validator/SectionPairDocumentValidator.hpp>
 #include <ls-std/io/section-pair/validator/SectionPairIdentifierValidator.hpp>
 #include <ls-std/io/section-pair/validator/SectionPairRowListValueValidator.hpp>
 #include <ls-std/io/section-pair/validator/SectionPairRowSingleValueValidator.hpp>

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

@@ -3,7 +3,7 @@
 * Company:         Lynar Studios
 * E-Mail:          webmaster@lynarstudios.com
 * Created:         2023-02-16
-* Changed:         2023-02-18
+* Changed:         2023-02-21
 *
 * */
 
@@ -30,7 +30,7 @@ ls::std::core::type::byte_field ls::std::io::SerializableSectionPairDocument::ma
 {
   ::std::shared_ptr<ls::std::io::SectionPairDocument> document = ::std::dynamic_pointer_cast<ls::std::io::SectionPairDocument>(this->parameter.getValue());
   ::std::string newLine = this->parameter.getNewLine();
-  ls::std::core::type::byte_field serializedDocument = document->getHeader() + newLine + newLine;
+  ls::std::core::type::byte_field serializedDocument = document->getHeader() + newLine;
 
   for (const auto &_section : document->getSectionList())
   {
@@ -44,7 +44,7 @@ ls::std::core::type::byte_field ls::std::io::SerializableSectionPairDocument::ma
 void ls::std::io::SerializableSectionPairDocument::unmarshal(const ls::std::core::type::byte_field &_data)
 {
   ls::std::core::type::byte_field serializedDocument = _data;
-  size_t headerSize = ::std::dynamic_pointer_cast<ls::std::io::SectionPairDocument>(this->parameter.getValue())->getHeader().size() + 2 * this->parameter.getNewLine().size();
+  size_t headerSize = ::std::dynamic_pointer_cast<ls::std::io::SectionPairDocument>(this->parameter.getValue())->getHeader().size() + this->parameter.getNewLine().size();
   serializedDocument = serializedDocument.substr(headerSize);
   ls::std::core::type::byte_field serializedSection{};
 

+ 36 - 0
source/ls-std/io/section-pair/validator/SectionPairDocumentValidator.cpp

@@ -0,0 +1,36 @@
+/*
+* Author:          Patrick-Christopher Mattulat
+* Company:         Lynar Studios
+* E-Mail:          webmaster@lynarstudios.com
+* Created:         2023-02-21
+* Changed:         2023-02-21
+*
+* */
+
+#include <ls-std/io/section-pair/validator/SectionPairDocumentValidator.hpp>
+#include <ls-std/io/section-pair/validator/SectionPairSectionValidator.hpp>
+#include <regex>
+
+ls::std::io::SectionPairDocumentValidator::SectionPairDocumentValidator(::std::string _document) : ls::std::core::Class("SectionPairDocumentValidator"), document(::std::move(_document))
+{}
+
+ls::std::io::SectionPairDocumentValidator::~SectionPairDocumentValidator() = default;
+
+bool ls::std::io::SectionPairDocumentValidator::isValid()
+{
+  ::std::string validationRegex = ls::std::io::SectionPairDocumentValidator::_getValidationRegex();
+  static ::std::regex documentRegex = ::std::regex{"^" + validationRegex + "$"};
+
+  return ::std::regex_match(this->document, documentRegex);
+}
+
+::std::string ls::std::io::SectionPairDocumentValidator::_getValidationRegex()
+{
+  ::std::string newLine = R"(((\n)|(\r\n)))";
+  ::std::string documentHeader = R"((# {1}(section\-pair document))" + newLine + R"())";
+  ::std::string section = ls::std::io::SectionPairSectionValidator::getValidationRegex();
+  ::std::string atLeastOneSection = R"(()" + section + R"())";
+  ::std::string optionalSections = R"(()" + section + R"()*)";
+
+  return documentHeader + atLeastOneSection + optionalSections;
+}

+ 84 - 0
test/cases/io/section-pair/validator/SectionPairDocumentValidatorTest.cpp

@@ -0,0 +1,84 @@
+/*
+* Author:          Patrick-Christopher Mattulat
+* Company:         Lynar Studios
+* E-Mail:          webmaster@lynarstudios.com
+* Created:         2023-02-21
+* Changed:         2023-02-21
+*
+* */
+
+#include <gtest/gtest.h>
+#include <ls-std-io-test.hpp>
+#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;
+using namespace test::io;
+
+namespace
+{
+  class SectionPairDocumentValidatorTest : public ::testing::Test
+  {
+    protected:
+
+      SectionPairDocumentValidatorTest() = default;
+      ~SectionPairDocumentValidatorTest() override = default;
+
+      void SetUp() override
+      {}
+
+      void TearDown() override
+      {}
+  };
+
+  class SectionPairDocumentValidatorTest_LineBreakTest : public ::testing::TestWithParam<string>
+  {
+    protected:
+
+      SectionPairDocumentValidatorTest_LineBreakTest() = default;
+      ~SectionPairDocumentValidatorTest_LineBreakTest() override = default;
+  };
+
+  class SectionPairDocumentValidatorTest_ValidArgumentTest : public ::testing::TestWithParam<string>
+  {
+    protected:
+
+      SectionPairDocumentValidatorTest_ValidArgumentTest() = default;
+      ~SectionPairDocumentValidatorTest_ValidArgumentTest() override = default;
+  };
+
+  class SectionPairDocumentValidatorTest_InvalidArgumentTest : public ::testing::TestWithParam<string>
+  {
+    protected:
+
+      SectionPairDocumentValidatorTest_InvalidArgumentTest() = default;
+      ~SectionPairDocumentValidatorTest_InvalidArgumentTest() override = default;
+  };
+
+  TEST_F(SectionPairDocumentValidatorTest, getClassName)
+  {
+    ASSERT_STREQ("SectionPairDocumentValidator", SectionPairDocumentValidator{"tmp-key"}.getClassName().c_str());
+  }
+
+  TEST_P(SectionPairDocumentValidatorTest_LineBreakTest, isValid)
+  {
+    ASSERT_TRUE(SectionPairDocumentValidator{GetParam()}.isValid());
+  }
+
+  TEST_P(SectionPairDocumentValidatorTest_ValidArgumentTest, isValid)
+  {
+    ASSERT_TRUE(SectionPairDocumentValidator{GetParam()}.isValid());
+  }
+
+  TEST_P(SectionPairDocumentValidatorTest_InvalidArgumentTest, isValid_not_valid)
+  {
+    ASSERT_FALSE(SectionPairDocumentValidator{GetParam()}.isValid());
+  }
+
+  INSTANTIATE_TEST_SUITE_P(LineBreakTest, SectionPairDocumentValidatorTest_LineBreakTest, ::testing::Values(SectionPairDocumentProvider::createSerializedDocument(NewLine::getWindowsNewLine()), SectionPairDocumentProvider::createSerializedDocument(NewLine::getUnixNewLine())));
+  INSTANTIATE_TEST_SUITE_P(InvalidArgumentTest, SectionPairDocumentValidatorTest_InvalidArgumentTest, ::testing::Values("\n[general]\n\ncolors:\nblue\n"));
+  INSTANTIATE_TEST_SUITE_P(ValidArgumentTest, SectionPairDocumentValidatorTest_ValidArgumentTest, ::testing::Values(SectionPairDocumentProvider::createSerializedDocumentComputerExample(NewLine::get())));
+}

+ 22 - 2
test/classes/io/section-pair/SectionPairDocumentProvider.cpp

@@ -3,7 +3,7 @@
 * Company:         Lynar Studios
 * E-Mail:          webmaster@lynarstudios.com
 * Created:         2023-02-16
-* Changed:         2023-02-18
+* Changed:         2023-02-21
 *
 * */
 
@@ -47,7 +47,7 @@ test::io::SectionPairDocumentProvider::~SectionPairDocumentProvider() = default;
 
 ls::std::core::type::byte_field test::io::SectionPairDocumentProvider::createSerializedDocument(const ::std::string &_newLine)
 {
-  ls::std::core::type::byte_field serializedDocument = "# section-pair document" + _newLine + _newLine;
+  ls::std::core::type::byte_field serializedDocument = "# section-pair document" + _newLine;
 
   // general section
 
@@ -62,3 +62,23 @@ ls::std::core::type::byte_field test::io::SectionPairDocumentProvider::createSer
 
   return serializedDocument;
 }
+
+ls::std::core::type::byte_field test::io::SectionPairDocumentProvider::createSerializedDocumentComputerExample(const ::std::string &_newLine)
+{
+  ls::std::core::type::byte_field serializedDocument = "# section-pair document" + _newLine;
+  serializedDocument += _newLine + "[model]" + _newLine + _newLine;
+  serializedDocument += "graphics-card=GTX 720" + _newLine;
+  serializedDocument += "ram-size=4096" + _newLine;
+  serializedDocument += "graphics-ram-size=4096" + _newLine;
+  serializedDocument += "cpu=Intel i7" + _newLine;
+
+  serializedDocument += _newLine + "[requirements]" + _newLine + _newLine;
+  serializedDocument += "graphics-ram-size=2048" + _newLine;
+  serializedDocument += "io-devices:" + _newLine;
+  serializedDocument += "  mouse" + _newLine;
+  serializedDocument += "  keyboard" + _newLine;
+  serializedDocument += "  screen" + _newLine;
+  serializedDocument += "  headset" + _newLine;
+
+  return serializedDocument;
+}

+ 2 - 1
test/classes/io/section-pair/SectionPairDocumentProvider.hpp

@@ -3,7 +3,7 @@
 * Company:         Lynar Studios
 * E-Mail:          webmaster@lynarstudios.com
 * Created:         2023-02-16
-* Changed:         2023-02-18
+* Changed:         2023-02-21
 *
 * */
 
@@ -26,6 +26,7 @@ namespace test::io
 
       static ::std::shared_ptr<ls::std::io::SectionPairDocument> createDocument();
       static ls::std::core::type::byte_field createSerializedDocument(const ::std::string &_newLine);
+      static ls::std::core::type::byte_field createSerializedDocumentComputerExample(const ::std::string &_newLine);
   };
 }