SectionPairDocumentTest.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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-18
  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 namespace ls::std::core;
  15. using namespace ls::std::core::type;
  16. using namespace ls::std::io;
  17. using namespace ::std;
  18. using namespace test::io;
  19. namespace
  20. {
  21. class SectionPairDocumentTest : public ::testing::Test
  22. {
  23. protected:
  24. SectionPairDocumentTest() = default;
  25. ~SectionPairDocumentTest() override = default;
  26. void SetUp() override
  27. {}
  28. void TearDown() override
  29. {}
  30. };
  31. TEST_F(SectionPairDocumentTest, add)
  32. {
  33. shared_ptr<SectionPairDocument> document = make_shared<SectionPairDocument>();
  34. ASSERT_TRUE(document->getSectionList().empty());
  35. document->add(make_shared<SectionPairSection>("general"));
  36. ASSERT_EQ(1, document->getAmountOfSections());
  37. }
  38. TEST_F(SectionPairDocumentTest, add_section_id_already_exists)
  39. {
  40. shared_ptr<SectionPairDocument> document = make_shared<SectionPairDocument>();
  41. document->add(make_shared<SectionPairSection>("general"));
  42. EXPECT_THROW(
  43. {
  44. try
  45. {
  46. document->add(make_shared<SectionPairSection>("general"));
  47. }
  48. catch (const IllegalArgumentException &_exception)
  49. {
  50. throw;
  51. }
  52. },
  53. IllegalArgumentException);
  54. }
  55. TEST_F(SectionPairDocumentTest, clear)
  56. {
  57. shared_ptr<SectionPairDocument> document = make_shared<SectionPairDocument>();
  58. document->add(make_shared<SectionPairSection>("general"));
  59. ASSERT_EQ(1, document->getAmountOfSections());
  60. document->clear();
  61. ASSERT_TRUE(document->getSectionList().empty());
  62. }
  63. TEST_F(SectionPairDocumentTest, get)
  64. {
  65. shared_ptr<SectionPairDocument> document = make_shared<SectionPairDocument>();
  66. document->add(make_shared<SectionPairSection>("general"));
  67. ASSERT_TRUE(document->get(0) != nullptr);
  68. }
  69. TEST_F(SectionPairDocumentTest, get_out_of_bounds)
  70. {
  71. shared_ptr<SectionPairDocument> document = make_shared<SectionPairDocument>();
  72. EXPECT_THROW(
  73. {
  74. try
  75. {
  76. shared_ptr<SectionPairSection> section = document->get(0);
  77. }
  78. catch (const IndexOutOfBoundsException &_exception)
  79. {
  80. throw;
  81. }
  82. },
  83. IndexOutOfBoundsException);
  84. }
  85. TEST_F(SectionPairDocumentTest, getAmountOfSections)
  86. {
  87. shared_ptr<SectionPairDocument> document = make_shared<SectionPairDocument>();
  88. document->add(make_shared<SectionPairSection>("general"));
  89. ASSERT_EQ(1, document->getAmountOfSections());
  90. }
  91. TEST_F(SectionPairDocumentTest, getClassName)
  92. {
  93. shared_ptr<SectionPairDocument> document = make_shared<SectionPairDocument>();
  94. ASSERT_STREQ("SectionPairDocument", document->getClassName().c_str());
  95. }
  96. TEST_F(SectionPairDocumentTest, getHeader)
  97. {
  98. shared_ptr<SectionPairDocument> document = make_shared<SectionPairDocument>();
  99. ASSERT_STREQ("# section-pair document", document->getHeader().c_str());
  100. }
  101. TEST_F(SectionPairDocumentTest, getSectionList)
  102. {
  103. shared_ptr<SectionPairDocument> document = make_shared<SectionPairDocument>();
  104. ASSERT_TRUE(document->getSectionList().empty());
  105. }
  106. TEST_F(SectionPairDocumentTest, marshal)
  107. {
  108. shared_ptr<SectionPairDocument> document = SectionPairDocumentProvider::createDocument();
  109. byte_field expected = SectionPairDocumentProvider::createSerializedDocument(NewLine::get());
  110. ASSERT_STREQ(expected.c_str(), document->marshal().c_str());
  111. }
  112. TEST_F(SectionPairDocumentTest, unmarshal)
  113. {
  114. shared_ptr<SectionPairDocument> document = make_shared<SectionPairDocument>();
  115. document->unmarshal(SectionPairDocumentProvider::createSerializedDocument(NewLine::get()));
  116. ASSERT_EQ(2, document->getAmountOfSections());
  117. // check general section
  118. shared_ptr<SectionPairSection> general = document->get(0);
  119. ASSERT_STREQ("general", general->getSectionId().c_str());
  120. ASSERT_EQ(3, general->getRowAmount());
  121. ASSERT_STREQ("name", general->get(0)->getKey().c_str());
  122. ASSERT_STREQ("Sandra", dynamic_pointer_cast<SectionPairRowSingleValue>(general->get(0)->getValue())->get().c_str());
  123. ASSERT_STREQ("age", general->get(1)->getKey().c_str());
  124. ASSERT_STREQ("24", dynamic_pointer_cast<SectionPairRowSingleValue>(general->get(1)->getValue())->get().c_str());
  125. ASSERT_STREQ("hobbies", general->get(2)->getKey().c_str());
  126. shared_ptr<SectionPairRowListValue> hobbies = dynamic_pointer_cast<SectionPairRowListValue>(general->get(2)->getValue());
  127. ASSERT_EQ(3, hobbies->getSize());
  128. ASSERT_STREQ("swimming", hobbies->get(0).c_str());
  129. ASSERT_STREQ("cycling", hobbies->get(1).c_str());
  130. ASSERT_STREQ("singing", hobbies->get(2).c_str());
  131. // check physical section
  132. shared_ptr<SectionPairSection> physical = document->get(1);
  133. ASSERT_STREQ("physical", physical->getSectionId().c_str());
  134. ASSERT_EQ(3, physical->getRowAmount());
  135. ASSERT_STREQ("eye-color", physical->get(0)->getKey().c_str());
  136. ASSERT_STREQ("blue", dynamic_pointer_cast<SectionPairRowSingleValue>(physical->get(0)->getValue())->get().c_str());
  137. ASSERT_STREQ("hair-color", physical->get(1)->getKey().c_str());
  138. ASSERT_STREQ("red", dynamic_pointer_cast<SectionPairRowSingleValue>(physical->get(1)->getValue())->get().c_str());
  139. ASSERT_STREQ("height", physical->get(2)->getKey().c_str());
  140. ASSERT_STREQ("167", dynamic_pointer_cast<SectionPairRowSingleValue>(physical->get(2)->getValue())->get().c_str());
  141. }
  142. }