Pārlūkot izejas kodu

Add look up methods to SectionPairSection class

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

+ 6 - 3
include/ls-std/io/section-pair/model/SectionPairSection.hpp

@@ -3,7 +3,7 @@
 * Company:         Lynar Studios
 * E-Mail:          webmaster@lynarstudios.com
 * Created:         2023-02-13
-* Changed:         2023-02-22
+* Changed:         2023-02-23
 *
 * */
 
@@ -28,12 +28,14 @@ namespace ls::std::io
       explicit SectionPairSection(const ls::std::io::section_pair_identifier &_sectionId);
       ~SectionPairSection() noexcept override;
 
-      void add(const section_pair_row_list_element &_row);
+      void add(const ls::std::io::section_pair_row_list_element &_row);
       void clear();
-      [[nodiscard]] section_pair_row_list_element get(size_t _index);
+      [[nodiscard]] ls::std::io::section_pair_row_list_element get(size_t _index);
+      [[nodiscard]] ls::std::io::section_pair_row_list_element get(const ls::std::io::section_pair_identifier &_key);
       [[nodiscard]] ls::std::io::section_pair_row_list getList();
       [[nodiscard]] size_t getRowAmount();
       [[nodiscard]] ls::std::io::section_pair_identifier getSectionId();
+      [[nodiscard]] bool hasRow(const ls::std::io::section_pair_identifier &_key);
       [[nodiscard]] ls::std::core::type::byte_field marshal() override;
       void reserveNewLine(const ::std::string &_reservedNewLine);
       void setSectionId(const ls::std::io::section_pair_identifier &_sectionId);
@@ -47,6 +49,7 @@ namespace ls::std::io
       ::std::shared_ptr<ls::std::core::interface_type::ISerializable> serializable{};
 
       void _createSerializable();
+      [[nodiscard]] ls::std::io::section_pair_row_list_element _get(const ls::std::io::section_pair_identifier &_key);
       [[nodiscard]] bool _hasRow(const ls::std::io::section_pair_identifier &_key);
       void _rowExistenceCheck(const ls::std::io::section_pair_identifier &_key);
       void _setSectionId(const ls::std::io::section_pair_identifier &_sectionId);

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

@@ -75,6 +75,11 @@ section_pair_row_list_element SectionPairSection::get(size_t _index)
   return element;
 }
 
+section_pair_row_list_element SectionPairSection::get(const section_pair_identifier &_key)
+{
+  return this->_get(_key);
+}
+
 section_pair_row_list SectionPairSection::getList()
 {
   return this->rows;
@@ -90,6 +95,11 @@ section_pair_identifier SectionPairSection::getSectionId()
   return this->sectionId;
 }
 
+bool SectionPairSection::hasRow(const section_pair_identifier &_key)
+{
+  return this->_hasRow(_key);
+}
+
 byte_field SectionPairSection::marshal()
 {
   ConditionalFunctionExecutor{this->serializable == nullptr}.execute([this] { _createSerializable(); });
@@ -125,6 +135,22 @@ void SectionPairSection::_createSerializable()
   this->serializable = make_shared<SerializableSectionPairSection>(parameter);
 }
 
+section_pair_row_list_element SectionPairSection::_get(const section_pair_identifier &_key)
+{
+  section_pair_row_list_element element{};
+
+  for (const auto &_row : this->rows)
+  {
+    if (_row->getKey() == _key)
+    {
+      element = _row;
+      break;
+    }
+  }
+
+  return element;
+}
+
 bool SectionPairSection::_hasRow(const section_pair_identifier &_key)
 {
   bool rowExists{};

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

@@ -113,7 +113,7 @@ namespace
     ASSERT_TRUE(section->getList().empty());
   }
 
-  TEST_F(SectionPairSectionTest, get)
+  TEST_F(SectionPairSectionTest, get_by_index)
   {
     shared_ptr<SectionPairSection> section = make_shared<SectionPairSection>("general");
     section->add(make_shared<SectionPairRow>("color", SectionPairRowEnumType::SECTION_PAIR_ROW_SINGLE_VALUE));
@@ -121,7 +121,7 @@ namespace
     ASSERT_TRUE(section->get(0) != nullptr);
   }
 
-  TEST_F(SectionPairSectionTest, get_out_of_bounds)
+  TEST_F(SectionPairSectionTest, get_by_index_out_of_bounds)
   {
     shared_ptr<SectionPairSection> section = make_shared<SectionPairSection>("general");
 
@@ -139,6 +139,22 @@ namespace
         IndexOutOfBoundsException);
   }
 
+  TEST_F(SectionPairSectionTest, get_by_key)
+  {
+    shared_ptr<SectionPairSection> section = make_shared<SectionPairSection>("general");
+    section->add(make_shared<SectionPairRow>("color", SectionPairRowEnumType::SECTION_PAIR_ROW_SINGLE_VALUE));
+
+    ASSERT_TRUE(section->get("color") != nullptr);
+  }
+
+  TEST_F(SectionPairSectionTest, get_by_key_not_found)
+  {
+    shared_ptr<SectionPairSection> section = make_shared<SectionPairSection>("general");
+    section->add(make_shared<SectionPairRow>("color", SectionPairRowEnumType::SECTION_PAIR_ROW_SINGLE_VALUE));
+
+    ASSERT_TRUE(section->get("hobbies") == nullptr);
+  }
+
   TEST_F(SectionPairSectionTest, getAmount)
   {
     shared_ptr<SectionPairSection> section = make_shared<SectionPairSection>("general");
@@ -162,6 +178,22 @@ namespace
     ASSERT_STREQ("general", section->getSectionId().c_str());
   }
 
+  TEST_F(SectionPairSectionTest, hasRow)
+  {
+    shared_ptr<SectionPairSection> section = make_shared<SectionPairSection>("general");
+    section->add(make_shared<SectionPairRow>("color", SectionPairRowEnumType::SECTION_PAIR_ROW_SINGLE_VALUE));
+
+    ASSERT_TRUE(section->hasRow("color"));
+  }
+
+  TEST_F(SectionPairSectionTest, hasRow_not_found)
+  {
+    shared_ptr<SectionPairSection> section = make_shared<SectionPairSection>("general");
+    section->add(make_shared<SectionPairRow>("color", SectionPairRowEnumType::SECTION_PAIR_ROW_SINGLE_VALUE));
+
+    ASSERT_FALSE(section->hasRow("hobbies"));
+  }
+
   TEST_F(SectionPairSectionTest, marshal)
   {
     shared_ptr<SectionPairSection> section = SectionPairSectionProvider::createSectionWithTomExample();