SectionPairDocumentTest.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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)
  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_out_of_bounds)
  78. {
  79. shared_ptr<SectionPairDocument> document = make_shared<SectionPairDocument>();
  80. EXPECT_THROW(
  81. {
  82. try
  83. {
  84. shared_ptr<SectionPairSection> section = document->get(0);
  85. }
  86. catch (const IndexOutOfBoundsException &_exception)
  87. {
  88. throw;
  89. }
  90. },
  91. IndexOutOfBoundsException);
  92. }
  93. TEST_F(SectionPairDocumentTest, getAmountOfSections)
  94. {
  95. shared_ptr<SectionPairDocument> document = make_shared<SectionPairDocument>();
  96. document->add(make_shared<SectionPairSection>("general"));
  97. ASSERT_EQ(1, document->getAmountOfSections());
  98. }
  99. TEST_F(SectionPairDocumentTest, getClassName)
  100. {
  101. shared_ptr<SectionPairDocument> document = make_shared<SectionPairDocument>();
  102. ASSERT_STREQ("SectionPairDocument", document->getClassName().c_str());
  103. }
  104. TEST_F(SectionPairDocumentTest, getHeader)
  105. {
  106. shared_ptr<SectionPairDocument> document = make_shared<SectionPairDocument>();
  107. ASSERT_STREQ("# section-pair document", document->getHeader().c_str());
  108. }
  109. TEST_F(SectionPairDocumentTest, getSectionList)
  110. {
  111. shared_ptr<SectionPairDocument> document = make_shared<SectionPairDocument>();
  112. ASSERT_TRUE(document->getSectionList().empty());
  113. }
  114. TEST_F(SectionPairDocumentTest, marshal)
  115. {
  116. shared_ptr<SectionPairDocument> document = SectionPairDocumentProvider::createDocument();
  117. byte_field expected = SectionPairDocumentProvider::createSerializedDocument(NewLine::get());
  118. ASSERT_STREQ(expected.c_str(), document->marshal().c_str());
  119. }
  120. TEST_F(SectionPairDocumentTest, unmarshal)
  121. {
  122. shared_ptr<SectionPairDocument> document = make_shared<SectionPairDocument>();
  123. document->unmarshal(SectionPairDocumentProvider::createSerializedDocument(NewLine::get()));
  124. ASSERT_EQ(2, document->getAmountOfSections());
  125. // check general section
  126. shared_ptr<SectionPairSection> general = document->get(0);
  127. ASSERT_STREQ("general", general->getSectionId().c_str());
  128. ASSERT_EQ(3, general->getRowAmount());
  129. ASSERT_STREQ("name", general->get(0)->getKey().c_str());
  130. ASSERT_STREQ("Sandra", dynamic_pointer_cast<SectionPairRowSingleValue>(general->get(0)->getValue())->get().c_str());
  131. ASSERT_STREQ("age", general->get(1)->getKey().c_str());
  132. ASSERT_STREQ("24", dynamic_pointer_cast<SectionPairRowSingleValue>(general->get(1)->getValue())->get().c_str());
  133. ASSERT_STREQ("hobbies", general->get(2)->getKey().c_str());
  134. shared_ptr<SectionPairRowListValue> hobbies = dynamic_pointer_cast<SectionPairRowListValue>(general->get(2)->getValue());
  135. ASSERT_EQ(3, hobbies->getSize());
  136. ASSERT_STREQ("swimming", hobbies->get(0).c_str());
  137. ASSERT_STREQ("cycling", hobbies->get(1).c_str());
  138. ASSERT_STREQ("singing", hobbies->get(2).c_str());
  139. // check physical section
  140. shared_ptr<SectionPairSection> physical = document->get(1);
  141. ASSERT_STREQ("physical", physical->getSectionId().c_str());
  142. ASSERT_EQ(3, physical->getRowAmount());
  143. ASSERT_STREQ("eye-color", physical->get(0)->getKey().c_str());
  144. ASSERT_STREQ("blue", dynamic_pointer_cast<SectionPairRowSingleValue>(physical->get(0)->getValue())->get().c_str());
  145. ASSERT_STREQ("hair-color", physical->get(1)->getKey().c_str());
  146. ASSERT_STREQ("red", dynamic_pointer_cast<SectionPairRowSingleValue>(physical->get(1)->getValue())->get().c_str());
  147. ASSERT_STREQ("height", physical->get(2)->getKey().c_str());
  148. ASSERT_STREQ("167", dynamic_pointer_cast<SectionPairRowSingleValue>(physical->get(2)->getValue())->get().c_str());
  149. }
  150. }