ソースを参照

Add SectionPairDocument class

This model class represents a Section Pair document.
Patrick-Christopher Mattulat 2 年 前
コミット
099ebe8cdf

+ 2 - 0
CMakeLists.txt

@@ -177,6 +177,7 @@ set(SOURCE_FILES_IO
         ${CMAKE_CURRENT_SOURCE_DIR}/source/ls-std/io/logging/LogLevel.cpp
         ${CMAKE_CURRENT_SOURCE_DIR}/source/ls-std/io/section-pair/evaluator/SectionPairIdentifierArgumentEvaluator.cpp
         ${CMAKE_CURRENT_SOURCE_DIR}/source/ls-std/io/section-pair/evaluator/SectionPairRowValueArgumentEvaluator.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
         ${CMAKE_CURRENT_SOURCE_DIR}/source/ls-std/io/section-pair/model/SectionPairRowListValue.cpp
         ${CMAKE_CURRENT_SOURCE_DIR}/source/ls-std/io/section-pair/model/SectionPairRowSingleValue.cpp
@@ -280,6 +281,7 @@ if (${LS_STD_BUILD_WITH_TESTS})
             ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/io/section-pair/validator/SectionPairIdentifierValidatorTest.cpp
             ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/io/section-pair/validator/SectionPairRowValueValidatorTest.cpp
             ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/io/section-pair/validator/SectionPairSectionIdUnmarshalValidatorTest.cpp
+            ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/io/section-pair/model/SectionPairDocumentTest.cpp
             ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/io/section-pair/model/SectionPairRowListValueTest.cpp
             ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/io/section-pair/model/SectionPairRowSingleValueTest.cpp
             ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/io/section-pair/model/SectionPairRowTest.cpp

+ 41 - 0
include/ls-std/io/section-pair/model/SectionPairDocument.hpp

@@ -0,0 +1,41 @@
+/*
+* Author:          Patrick-Christopher Mattulat
+* Company:         Lynar Studios
+* E-Mail:          webmaster@lynarstudios.com
+* Created:         2023-02-15
+* Changed:         2023-02-15
+*
+* */
+
+#ifndef LS_STD_SECTION_PAIR_DOCUMENT_HPP
+#define LS_STD_SECTION_PAIR_DOCUMENT_HPP
+
+#include <ls-std/core/Class.hpp>
+#include <ls-std/io/section-pair/SectionPairTypes.hpp>
+#include <ls-std/io/section-pair/type/SectionPairDocumentTypes.hpp>
+#include <ls-std/os/dynamic-goal.hpp>
+
+namespace ls::std::io
+{
+  class LS_STD_DYNAMIC_GOAL SectionPairDocument : public ls::std::core::Class
+  {
+    public:
+
+      SectionPairDocument();
+      ~SectionPairDocument() override;
+
+      void add(const section_pair_document_section_list_element &_section);
+      [[nodiscard]] section_pair_document_section_list_element get(size_t _index);
+      [[nodiscard]] size_t getAmountOfSections();
+      [[nodiscard]] section_pair_document_section_list getSectionList();
+
+    private:
+
+      ls::std::io::section_pair_document_section_list sections{};
+
+      void _checkSectionExistence(const ls::std::io::section_pair_identifier &_sectionId);
+      [[nodiscard]] bool _hasSection(const ls::std::io::section_pair_identifier &_identifier);
+  };
+}
+
+#endif

+ 23 - 0
include/ls-std/io/section-pair/type/SectionPairDocumentTypes.hpp

@@ -0,0 +1,23 @@
+/*
+* Author:          Patrick-Christopher Mattulat
+* Company:         Lynar Studios
+* E-Mail:          webmaster@lynarstudios.com
+* Created:         2023-02-15
+* Changed:         2023-02-15
+*
+* */
+
+#ifndef LS_STD_SECTION_PAIR_DOCUMENT_TYPES_HPP
+#define LS_STD_SECTION_PAIR_DOCUMENT_TYPES_HPP
+
+#include <list>
+#include <ls-std/io/section-pair/model/SectionPairSection.hpp>
+#include <memory>
+
+namespace ls::std::io
+{
+  using section_pair_document_section_list_element = ::std::shared_ptr<ls::std::io::SectionPairSection>;
+  using section_pair_document_section_list = ::std::list<ls::std::io::section_pair_document_section_list_element>;
+}
+
+#endif

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

@@ -24,6 +24,7 @@
 #include <ls-std/io/section-pair/SectionPairTypes.hpp>
 #include <ls-std/io/section-pair/evaluator/SectionPairIdentifierArgumentEvaluator.hpp>
 #include <ls-std/io/section-pair/evaluator/SectionPairRowValueArgumentEvaluator.hpp>
+#include <ls-std/io/section-pair/model/SectionPairDocument.hpp>
 #include <ls-std/io/section-pair/model/SectionPairRow.hpp>
 #include <ls-std/io/section-pair/model/SectionPairRowListValue.hpp>
 #include <ls-std/io/section-pair/model/SectionPairRowSingleValue.hpp>
@@ -33,6 +34,7 @@
 #include <ls-std/io/section-pair/serialization/SerializableSectionPairRowListValue.hpp>
 #include <ls-std/io/section-pair/serialization/SerializableSectionPairRowSingleValue.hpp>
 #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/SectionPairIdentifierValidator.hpp>
 #include <ls-std/io/section-pair/validator/SectionPairRowValueValidator.hpp>

+ 80 - 0
source/ls-std/io/section-pair/model/SectionPairDocument.cpp

@@ -0,0 +1,80 @@
+/*
+* Author:          Patrick-Christopher Mattulat
+* Company:         Lynar Studios
+* E-Mail:          webmaster@lynarstudios.com
+* Created:         2023-02-15
+* Changed:         2023-02-15
+*
+* */
+
+#include <ls-std/core/evaluator/IndexOutOfBoundsEvaluator.hpp>
+#include <ls-std/core/evaluator/NullPointerArgumentEvaluator.hpp>
+#include <ls-std/core/exception/IllegalArgumentException.hpp>
+#include <ls-std/io/section-pair/model/SectionPairDocument.hpp>
+
+ls::std::io::SectionPairDocument::SectionPairDocument() : ls::std::core::Class("SectionPairDocument")
+{}
+
+ls::std::io::SectionPairDocument::~SectionPairDocument() = default;
+
+void ls::std::io::SectionPairDocument::add(const section_pair_document_section_list_element &_section)
+{
+  ::std::string message = this->getClassName() + ": add section attempt failed, since \"_section\" argument is null!";
+  ls::std::core::NullPointerArgumentEvaluator(::std::reinterpret_pointer_cast<void>(_section), message).evaluate();
+  this->_checkSectionExistence(_section->getSectionId());
+  this->sections.push_back(_section);
+}
+
+ls::std::io::section_pair_document_section_list_element ls::std::io::SectionPairDocument::get(size_t _index)
+{
+  ls::std::io::section_pair_document_section_list_element element{};
+  ls::std::core::IndexOutOfBoundsEvaluator{_index, this->sections.size()}.evaluate();
+  size_t index{};
+
+  for (const auto &_element : this->sections)
+  {
+    if (_index == index)
+    {
+      element = _element;
+      break;
+    }
+
+    ++index;
+  }
+
+  return element;
+}
+
+size_t ls::std::io::SectionPairDocument::getAmountOfSections()
+{
+  return this->sections.size();
+}
+
+ls::std::io::section_pair_document_section_list ls::std::io::SectionPairDocument::getSectionList()
+{
+  return this->sections;
+}
+
+void ls::std::io::SectionPairDocument::_checkSectionExistence(const ls::std::io::section_pair_identifier &_sectionId)
+{
+  if (this->_hasSection(_sectionId))
+  {
+    throw ls::std::core::IllegalArgumentException{this->getClassName() + "section ID \"" + _sectionId + "\" already exists in document!"};
+  }
+}
+
+bool ls::std::io::SectionPairDocument::_hasSection(const ls::std::io::section_pair_identifier &_identifier)
+{
+  bool sectionExists{};
+
+  for (const auto &_section : this->sections)
+  {
+    if (_section->getSectionId() == _identifier)
+    {
+      sectionExists = true;
+      break;
+    }
+  }
+
+  return sectionExists;
+}

+ 103 - 0
test/cases/io/section-pair/model/SectionPairDocumentTest.cpp

@@ -0,0 +1,103 @@
+/*
+* Author:          Patrick-Christopher Mattulat
+* Company:         Lynar Studios
+* E-Mail:          webmaster@lynarstudios.com
+* Created:         2023-02-15
+* Changed:         2023-02-15
+*
+* */
+
+#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::core::type;
+using namespace ls::std::io;
+using namespace ::std;
+
+namespace
+{
+  class SectionPairDocumentTest : public ::testing::Test
+  {
+    protected:
+
+      SectionPairDocumentTest() = default;
+      ~SectionPairDocumentTest() override = default;
+
+      void SetUp() override
+      {}
+
+      void TearDown() override
+      {}
+  };
+
+  TEST_F(SectionPairDocumentTest, add)
+  {
+    shared_ptr<SectionPairDocument> document = make_shared<SectionPairDocument>();
+
+    ASSERT_TRUE(document->getSectionList().empty());
+    document->add(make_shared<SectionPairSection>("general"));
+    ASSERT_EQ(1, document->getAmountOfSections());
+  }
+
+  TEST_F(SectionPairDocumentTest, add_section_id_already_exists)
+  {
+    shared_ptr<SectionPairDocument> document = make_shared<SectionPairDocument>();
+    document->add(make_shared<SectionPairSection>("general"));
+
+    EXPECT_THROW(
+        {
+          try
+          {
+            document->add(make_shared<SectionPairSection>("general"));
+          }
+          catch (const IllegalArgumentException &_exception)
+          {
+            throw;
+          }
+        },
+        IllegalArgumentException);
+  }
+
+  TEST_F(SectionPairDocumentTest, get)
+  {
+    shared_ptr<SectionPairDocument> document = make_shared<SectionPairDocument>();
+    document->add(make_shared<SectionPairSection>("general"));
+
+    ASSERT_TRUE(document->get(0) != nullptr);
+  }
+
+  TEST_F(SectionPairDocumentTest, get_out_of_bounds)
+  {
+    shared_ptr<SectionPairDocument> document = make_shared<SectionPairDocument>();
+
+    EXPECT_THROW(
+        {
+          try
+          {
+            shared_ptr<SectionPairSection> section = document->get(0);
+          }
+          catch (const IndexOutOfBoundsException &_exception)
+          {
+            throw;
+          }
+        },
+        IndexOutOfBoundsException);
+  }
+
+  TEST_F(SectionPairDocumentTest, getAmountOfSections)
+  {
+    shared_ptr<SectionPairDocument> document = make_shared<SectionPairDocument>();
+    document->add(make_shared<SectionPairSection>("general"));
+
+    ASSERT_EQ(1, document->getAmountOfSections());
+  }
+
+  TEST_F(SectionPairDocumentTest, getSectionList)
+  {
+    shared_ptr<SectionPairDocument> document = make_shared<SectionPairDocument>();
+    ASSERT_TRUE(document->getSectionList().empty());
+  }
+}