SectionPairDocumentTest.cpp 6.9 KB

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