SectionPairDocumentTest.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Co-Author: Claude Sonnet 4.6 (LLM)
  4. * Company: Lynar Studios
  5. * E-Mail: webmaster@lynarstudios.com
  6. * Created: 2023-02-15
  7. * Changed: 2026-06-23
  8. *
  9. * */
  10. #include <gtest/gtest.h>
  11. #include <ls-std-io-test.hpp>
  12. #include <ls-std/ls-std-core.hpp>
  13. #include <ls-std/ls-std-io.hpp>
  14. #include <memory>
  15. using ls::standard::core::IllegalArgumentException;
  16. using ls::standard::core::IndexOutOfBoundsException;
  17. using ls::standard::core::type::byte_field;
  18. using ls::standard::io::NewLine;
  19. using ls::standard::io::SectionPairDocument;
  20. using ls::standard::io::SectionPairRowListValue;
  21. using ls::standard::io::SectionPairRowSingleValue;
  22. using ls::standard::io::SectionPairSection;
  23. using std::dynamic_pointer_cast;
  24. using std::make_shared;
  25. using std::shared_ptr;
  26. using test::io::SectionPairDocumentProvider;
  27. using testing::Test;
  28. namespace
  29. {
  30. class SectionPairDocumentTest : public Test
  31. {
  32. public:
  33. SectionPairDocumentTest() = default;
  34. ~SectionPairDocumentTest() override = default;
  35. };
  36. TEST_F(SectionPairDocumentTest, add)
  37. {
  38. const auto document = make_shared<SectionPairDocument>();
  39. ASSERT_TRUE(document->getSectionList().empty());
  40. document->add(make_shared<SectionPairSection>("general"));
  41. ASSERT_EQ(1, document->getAmountOfSections());
  42. }
  43. TEST_F(SectionPairDocumentTest, add_section_id_already_exists)
  44. {
  45. const auto document = make_shared<SectionPairDocument>();
  46. document->add(make_shared<SectionPairSection>("general"));
  47. EXPECT_THROW(
  48. {
  49. try
  50. {
  51. document->add(make_shared<SectionPairSection>("general"));
  52. }
  53. catch (const IllegalArgumentException &_exception)
  54. {
  55. throw;
  56. }
  57. },
  58. IllegalArgumentException);
  59. }
  60. TEST_F(SectionPairDocumentTest, clear)
  61. {
  62. const auto document = make_shared<SectionPairDocument>();
  63. document->add(make_shared<SectionPairSection>("general"));
  64. ASSERT_EQ(1, document->getAmountOfSections());
  65. document->clear();
  66. ASSERT_TRUE(document->getSectionList().empty());
  67. }
  68. TEST_F(SectionPairDocumentTest, get_by_index)
  69. {
  70. const auto document = make_shared<SectionPairDocument>();
  71. document->add(make_shared<SectionPairSection>("general"));
  72. ASSERT_TRUE(document->get(0) != nullptr);
  73. }
  74. TEST_F(SectionPairDocumentTest, get_by_section_id)
  75. {
  76. const auto document = make_shared<SectionPairDocument>();
  77. document->add(make_shared<SectionPairSection>("general"));
  78. ASSERT_TRUE(document->get("general") != nullptr);
  79. }
  80. TEST_F(SectionPairDocumentTest, get_by_section_id_not_found)
  81. {
  82. const auto document = make_shared<SectionPairDocument>();
  83. document->add(make_shared<SectionPairSection>("general"));
  84. ASSERT_TRUE(document->get("about") == nullptr);
  85. }
  86. TEST_F(SectionPairDocumentTest, get_by_index_out_of_bounds)
  87. {
  88. const auto document = make_shared<SectionPairDocument>();
  89. EXPECT_THROW(
  90. {
  91. try
  92. {
  93. shared_ptr<SectionPairSection> section = document->get(0);
  94. }
  95. catch (const IndexOutOfBoundsException &_exception)
  96. {
  97. throw;
  98. }
  99. },
  100. IndexOutOfBoundsException);
  101. }
  102. TEST_F(SectionPairDocumentTest, getAmountOfSections)
  103. {
  104. const auto document = make_shared<SectionPairDocument>();
  105. document->add(make_shared<SectionPairSection>("general"));
  106. ASSERT_EQ(1, document->getAmountOfSections());
  107. }
  108. TEST_F(SectionPairDocumentTest, getClassName)
  109. {
  110. const auto document = make_shared<SectionPairDocument>();
  111. ASSERT_STREQ("SectionPairDocument", document->getClassName().c_str());
  112. }
  113. TEST_F(SectionPairDocumentTest, getHeader)
  114. {
  115. const auto document = make_shared<SectionPairDocument>();
  116. ASSERT_STREQ("# section-pair document", document->getHeader().c_str());
  117. }
  118. TEST_F(SectionPairDocumentTest, getSectionList)
  119. {
  120. const auto document = make_shared<SectionPairDocument>();
  121. ASSERT_TRUE(document->getSectionList().empty());
  122. }
  123. TEST_F(SectionPairDocumentTest, hasSection)
  124. {
  125. const auto document = make_shared<SectionPairDocument>();
  126. document->add(make_shared<SectionPairSection>("general"));
  127. ASSERT_TRUE(document->hasSection("general"));
  128. }
  129. TEST_F(SectionPairDocumentTest, hasSection_not_found)
  130. {
  131. const auto document = make_shared<SectionPairDocument>();
  132. document->add(make_shared<SectionPairSection>("general"));
  133. ASSERT_FALSE(document->hasSection("about"));
  134. }
  135. TEST_F(SectionPairDocumentTest, marshal)
  136. {
  137. const shared_ptr<SectionPairDocument> document = SectionPairDocumentProvider::createDocument();
  138. const byte_field expected = SectionPairDocumentProvider::createSerializedDocument(NewLine::get());
  139. ASSERT_STREQ(expected.c_str(), document->marshal().c_str());
  140. }
  141. TEST_F(SectionPairDocumentTest, unmarshal)
  142. {
  143. auto document = make_shared<SectionPairDocument>();
  144. document->unmarshal(SectionPairDocumentProvider::createSerializedDocument(NewLine::get()));
  145. ASSERT_EQ(2, document->getAmountOfSections());
  146. // check general section
  147. shared_ptr<SectionPairSection> general = document->get(0);
  148. ASSERT_STREQ("general", general->getSectionId().c_str());
  149. ASSERT_EQ(3, general->getRowAmount());
  150. ASSERT_STREQ("name", general->get(0)->getKey().c_str());
  151. ASSERT_STREQ("Sandra", dynamic_pointer_cast<SectionPairRowSingleValue>(general->get(0)->getValue())->get().c_str());
  152. ASSERT_STREQ("age", general->get(1)->getKey().c_str());
  153. ASSERT_STREQ("24", dynamic_pointer_cast<SectionPairRowSingleValue>(general->get(1)->getValue())->get().c_str());
  154. ASSERT_STREQ("hobbies", general->get(2)->getKey().c_str());
  155. shared_ptr<SectionPairRowListValue> hobbies = dynamic_pointer_cast<SectionPairRowListValue>(general->get(2)->getValue());
  156. ASSERT_EQ(3, hobbies->getSize());
  157. ASSERT_STREQ("swimming", hobbies->get(0).c_str());
  158. ASSERT_STREQ("cycling", hobbies->get(1).c_str());
  159. ASSERT_STREQ("singing", hobbies->get(2).c_str());
  160. // check physical section
  161. shared_ptr<SectionPairSection> physical = document->get(1);
  162. ASSERT_STREQ("physical", physical->getSectionId().c_str());
  163. ASSERT_EQ(3, physical->getRowAmount());
  164. ASSERT_STREQ("eye-color", physical->get(0)->getKey().c_str());
  165. ASSERT_STREQ("blue", dynamic_pointer_cast<SectionPairRowSingleValue>(physical->get(0)->getValue())->get().c_str());
  166. ASSERT_STREQ("hair-color", physical->get(1)->getKey().c_str());
  167. ASSERT_STREQ("red", dynamic_pointer_cast<SectionPairRowSingleValue>(physical->get(1)->getValue())->get().c_str());
  168. ASSERT_STREQ("height", physical->get(2)->getKey().c_str());
  169. ASSERT_STREQ("167", dynamic_pointer_cast<SectionPairRowSingleValue>(physical->get(2)->getValue())->get().c_str());
  170. }
  171. }