SectionPairDocumentTest.cpp 5.6 KB

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