SectionPairDocumentTest.cpp 7.0 KB

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