SerializableSectionPairDocumentTest.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2023-02-16
  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::type::byte_field;
  16. using ls::std::io::NewLine;
  17. using ls::std::io::SectionPairDocument;
  18. using ls::std::io::SectionPairRowListValue;
  19. using ls::std::io::SectionPairRowSingleValue;
  20. using ls::std::io::SectionPairSection;
  21. using ls::std::io::SerializableSectionPairDocument;
  22. using ls::std::io::SerializableSectionPairParameter;
  23. using std::dynamic_pointer_cast;
  24. using std::make_shared;
  25. using std::shared_ptr;
  26. using std::string;
  27. using test::io::SectionPairDocumentProvider;
  28. using testing::Test;
  29. using testing::TestWithParam;
  30. using testing::Values;
  31. namespace
  32. {
  33. class SerializableSectionPairDocumentTest : public Test
  34. {
  35. protected:
  36. SerializableSectionPairDocumentTest() = default;
  37. ~SerializableSectionPairDocumentTest() override = default;
  38. void SetUp() override
  39. {}
  40. void TearDown() override
  41. {}
  42. };
  43. class SerializableSectionPairDocumentTest_LineBreakTest : public TestWithParam<string>
  44. {
  45. protected:
  46. SerializableSectionPairDocumentTest_LineBreakTest() = default;
  47. ~SerializableSectionPairDocumentTest_LineBreakTest() override = default;
  48. };
  49. TEST_F(SerializableSectionPairDocumentTest, constructor_no_value)
  50. {
  51. SerializableSectionPairParameter parameter{};
  52. EXPECT_THROW(
  53. {
  54. try
  55. {
  56. SerializableSectionPairDocument serializable = SerializableSectionPairDocument(parameter);
  57. }
  58. catch (const IllegalArgumentException &_exception)
  59. {
  60. throw;
  61. }
  62. },
  63. IllegalArgumentException);
  64. }
  65. TEST_F(SerializableSectionPairDocumentTest, getClassName)
  66. {
  67. SerializableSectionPairParameter parameter{};
  68. parameter.setValue(make_shared<SectionPairDocument>());
  69. ASSERT_STREQ("SerializableSectionPairDocument", SerializableSectionPairDocument{parameter}.getClassName().c_str());
  70. }
  71. TEST_F(SerializableSectionPairDocumentTest, getValue)
  72. {
  73. SerializableSectionPairParameter parameter{};
  74. parameter.setValue(make_shared<SectionPairDocument>());
  75. SerializableSectionPairDocument serializable(parameter);
  76. ASSERT_TRUE(serializable.getValue() != nullptr);
  77. }
  78. TEST_P(SerializableSectionPairDocumentTest_LineBreakTest, marshal)
  79. {
  80. string newLine = GetParam();
  81. SerializableSectionPairParameter parameter{};
  82. parameter.setValue(SectionPairDocumentProvider::createDocument());
  83. parameter.setNewLine(newLine);
  84. SerializableSectionPairDocument serializable(parameter);
  85. byte_field expected = SectionPairDocumentProvider::createSerializedDocument(newLine);
  86. ASSERT_STREQ(expected.c_str(), serializable.marshal().c_str());
  87. }
  88. TEST_P(SerializableSectionPairDocumentTest_LineBreakTest, unmarshal)
  89. {
  90. string newLine = GetParam();
  91. SerializableSectionPairParameter parameter{};
  92. parameter.setValue(make_shared<SectionPairDocument>());
  93. parameter.setNewLine(newLine);
  94. SerializableSectionPairDocument serializable(parameter);
  95. serializable.unmarshal(SectionPairDocumentProvider::createSerializedDocument(newLine));
  96. shared_ptr<SectionPairDocument> document = dynamic_pointer_cast<SectionPairDocument>(serializable.getValue());
  97. ASSERT_EQ(2, document->getAmountOfSections());
  98. // check general section
  99. shared_ptr<SectionPairSection> general = document->get(0);
  100. ASSERT_STREQ("general", general->getSectionId().c_str());
  101. ASSERT_EQ(3, general->getRowAmount());
  102. ASSERT_STREQ("name", general->get(0)->getKey().c_str());
  103. ASSERT_STREQ("Sandra", dynamic_pointer_cast<SectionPairRowSingleValue>(general->get(0)->getValue())->get().c_str());
  104. ASSERT_STREQ("age", general->get(1)->getKey().c_str());
  105. ASSERT_STREQ("24", dynamic_pointer_cast<SectionPairRowSingleValue>(general->get(1)->getValue())->get().c_str());
  106. ASSERT_STREQ("hobbies", general->get(2)->getKey().c_str());
  107. shared_ptr<SectionPairRowListValue> hobbies = dynamic_pointer_cast<SectionPairRowListValue>(general->get(2)->getValue());
  108. ASSERT_EQ(3, hobbies->getSize());
  109. ASSERT_STREQ("swimming", hobbies->get(0).c_str());
  110. ASSERT_STREQ("cycling", hobbies->get(1).c_str());
  111. ASSERT_STREQ("singing", hobbies->get(2).c_str());
  112. // check physical section
  113. shared_ptr<SectionPairSection> physical = document->get(1);
  114. ASSERT_STREQ("physical", physical->getSectionId().c_str());
  115. ASSERT_EQ(3, physical->getRowAmount());
  116. ASSERT_STREQ("eye-color", physical->get(0)->getKey().c_str());
  117. ASSERT_STREQ("blue", dynamic_pointer_cast<SectionPairRowSingleValue>(physical->get(0)->getValue())->get().c_str());
  118. ASSERT_STREQ("hair-color", physical->get(1)->getKey().c_str());
  119. ASSERT_STREQ("red", dynamic_pointer_cast<SectionPairRowSingleValue>(physical->get(1)->getValue())->get().c_str());
  120. ASSERT_STREQ("height", physical->get(2)->getKey().c_str());
  121. ASSERT_STREQ("167", dynamic_pointer_cast<SectionPairRowSingleValue>(physical->get(2)->getValue())->get().c_str());
  122. }
  123. INSTANTIATE_TEST_SUITE_P(LineBreakTest, SerializableSectionPairDocumentTest_LineBreakTest, Values(NewLine::getUnixNewLine(), NewLine::getWindowsNewLine()));
  124. }