Ver código fonte

Add look up methods to SectionPairDocument class

Patrick-Christopher Mattulat 1 ano atrás
pai
commit
601db724ba

+ 7 - 4
include/ls-std/io/section-pair/model/SectionPairDocument.hpp

@@ -3,7 +3,7 @@
 * Company:         Lynar Studios
 * E-Mail:          webmaster@lynarstudios.com
 * Created:         2023-02-15
-* Changed:         2023-02-22
+* Changed:         2023-02-23
 *
 * */
 
@@ -26,12 +26,14 @@ namespace ls::std::io
       SectionPairDocument();
       ~SectionPairDocument() noexcept override;
 
-      void add(const section_pair_document_section_list_element &_section);
+      void add(const ls::std::io::section_pair_document_section_list_element &_section);
       void clear();
-      [[nodiscard]] section_pair_document_section_list_element get(size_t _index);
+      [[nodiscard]] ls::std::io::section_pair_document_section_list_element get(size_t _index);
+      [[nodiscard]] ls::std::io::section_pair_document_section_list_element get(const ls::std::io::section_pair_identifier &_sectionId);
       [[nodiscard]] size_t getAmountOfSections();
       [[nodiscard]] ::std::string getHeader();
-      [[nodiscard]] section_pair_document_section_list getSectionList();
+      [[nodiscard]] ls::std::io::section_pair_document_section_list getSectionList();
+      [[nodiscard]] bool hasSection(const ls::std::io::section_pair_identifier &_sectionId);
       [[nodiscard]] ls::std::core::type::byte_field marshal() override;
       void reserveNewLine(const ::std::string &_reservedNewLine);
       void unmarshal(const ls::std::core::type::byte_field &_data) override;
@@ -45,6 +47,7 @@ namespace ls::std::io
 
       void _checkSectionExistence(const ls::std::io::section_pair_identifier &_sectionId);
       void _createSerializable();
+      [[nodiscard]] ls::std::io::section_pair_document_section_list_element _get(const ls::std::io::section_pair_identifier &_sectionId);
       [[nodiscard]] bool _hasSection(const ls::std::io::section_pair_identifier &_identifier);
   };
 }

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

@@ -70,6 +70,11 @@ section_pair_document_section_list_element SectionPairDocument::get(size_t _inde
   return element;
 }
 
+section_pair_document_section_list_element SectionPairDocument::get(const section_pair_identifier &_sectionId)
+{
+  return this->_get(_sectionId);
+}
+
 size_t SectionPairDocument::getAmountOfSections()
 {
   return this->sections.size();
@@ -85,6 +90,11 @@ section_pair_document_section_list SectionPairDocument::getSectionList()
   return this->sections;
 }
 
+bool SectionPairDocument::hasSection(const section_pair_identifier &_sectionId)
+{
+  return this->_hasSection(_sectionId);
+}
+
 byte_field SectionPairDocument::marshal()
 {
   ConditionalFunctionExecutor{this->serializable == nullptr}.execute([this] { _createSerializable(); });
@@ -124,6 +134,22 @@ void SectionPairDocument::_createSerializable()
   this->serializable = make_shared<SerializableSectionPairDocument>(parameter);
 }
 
+section_pair_document_section_list_element SectionPairDocument::_get(const section_pair_identifier &_sectionId)
+{
+  section_pair_document_section_list_element element{};
+
+  for (const auto &_section : this->sections)
+  {
+    if (_section->getSectionId() == _sectionId)
+    {
+      element = _section;
+      break;
+    }
+  }
+
+  return element;
+}
+
 bool SectionPairDocument::_hasSection(const section_pair_identifier &_identifier)
 {
   bool sectionExists{};

+ 34 - 2
test/cases/io/section-pair/model/SectionPairDocumentTest.cpp

@@ -81,7 +81,7 @@ namespace
     ASSERT_TRUE(document->getSectionList().empty());
   }
 
-  TEST_F(SectionPairDocumentTest, get)
+  TEST_F(SectionPairDocumentTest, get_by_index)
   {
     shared_ptr<SectionPairDocument> document = make_shared<SectionPairDocument>();
     document->add(make_shared<SectionPairSection>("general"));
@@ -89,7 +89,23 @@ namespace
     ASSERT_TRUE(document->get(0) != nullptr);
   }
 
-  TEST_F(SectionPairDocumentTest, get_out_of_bounds)
+  TEST_F(SectionPairDocumentTest, get_by_section_id)
+  {
+    shared_ptr<SectionPairDocument> document = make_shared<SectionPairDocument>();
+    document->add(make_shared<SectionPairSection>("general"));
+
+    ASSERT_TRUE(document->get("general") != nullptr);
+  }
+
+  TEST_F(SectionPairDocumentTest, get_by_section_id_not_found)
+  {
+    shared_ptr<SectionPairDocument> document = make_shared<SectionPairDocument>();
+    document->add(make_shared<SectionPairSection>("general"));
+
+    ASSERT_TRUE(document->get("about") == nullptr);
+  }
+
+  TEST_F(SectionPairDocumentTest, get_by_index_out_of_bounds)
   {
     shared_ptr<SectionPairDocument> document = make_shared<SectionPairDocument>();
 
@@ -133,6 +149,22 @@ namespace
     ASSERT_TRUE(document->getSectionList().empty());
   }
 
+  TEST_F(SectionPairDocumentTest, hasSection)
+  {
+    shared_ptr<SectionPairDocument> document = make_shared<SectionPairDocument>();
+    document->add(make_shared<SectionPairSection>("general"));
+
+    ASSERT_TRUE(document->hasSection("general"));
+  }
+
+  TEST_F(SectionPairDocumentTest, hasSection_not_found)
+  {
+    shared_ptr<SectionPairDocument> document = make_shared<SectionPairDocument>();
+    document->add(make_shared<SectionPairSection>("general"));
+
+    ASSERT_FALSE(document->hasSection("about"));
+  }
+
   TEST_F(SectionPairDocumentTest, marshal)
   {
     shared_ptr<SectionPairDocument> document = SectionPairDocumentProvider::createDocument();