SerializableSectionPairSectionTest.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2023-02-14
  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::SectionPairRowListValue;
  18. using ls::std::io::SectionPairRowSingleValue;
  19. using ls::std::io::SectionPairSection;
  20. using ls::std::io::SerializableSectionPairParameter;
  21. using ls::std::io::SerializableSectionPairSection;
  22. using std::dynamic_pointer_cast;
  23. using std::make_shared;
  24. using std::shared_ptr;
  25. using std::string;
  26. using test::io::SectionPairSectionProvider;
  27. using testing::Test;
  28. using testing::TestWithParam;
  29. using testing::Values;
  30. namespace
  31. {
  32. class SerializableSectionPairSectionTest : public Test
  33. {
  34. protected:
  35. SerializableSectionPairSectionTest() = default;
  36. ~SerializableSectionPairSectionTest() override = default;
  37. void SetUp() override
  38. {}
  39. void TearDown() override
  40. {}
  41. };
  42. class SerializableSectionPairSectionTest_LineBreakTest : public ::testing::TestWithParam<string>
  43. {
  44. protected:
  45. SerializableSectionPairSectionTest_LineBreakTest() = default;
  46. ~SerializableSectionPairSectionTest_LineBreakTest() override = default;
  47. };
  48. TEST_F(SerializableSectionPairSectionTest, constructor_no_reference)
  49. {
  50. SerializableSectionPairParameter parameter{};
  51. EXPECT_THROW(
  52. {
  53. try
  54. {
  55. SerializableSectionPairSection serializable(parameter);
  56. }
  57. catch (const IllegalArgumentException &_exception)
  58. {
  59. throw;
  60. }
  61. },
  62. IllegalArgumentException);
  63. }
  64. TEST_F(SerializableSectionPairSectionTest, getClassName)
  65. {
  66. SerializableSectionPairParameter parameter{};
  67. parameter.setValue(make_shared<SectionPairSection>("general"));
  68. ASSERT_STREQ("SerializableSectionPairSection", SerializableSectionPairSection{parameter}.getClassName().c_str());
  69. }
  70. TEST_F(SerializableSectionPairSectionTest, getValue)
  71. {
  72. SerializableSectionPairParameter parameter{};
  73. parameter.setValue(make_shared<SectionPairSection>("general"));
  74. SerializableSectionPairSection serializable{parameter};
  75. ASSERT_TRUE(serializable.getValue() != nullptr);
  76. }
  77. TEST_P(SerializableSectionPairSectionTest_LineBreakTest, marshal_sandra)
  78. {
  79. string newLine = GetParam();
  80. SerializableSectionPairParameter parameter{};
  81. parameter.setValue(SectionPairSectionProvider::createSectionWithSandraExample());
  82. parameter.setNewLine(newLine);
  83. SerializableSectionPairSection serializable{parameter};
  84. byte_field expected = SectionPairSectionProvider::createSerializedSectionWithSandraExample(newLine);
  85. byte_field actual = serializable.marshal();
  86. ASSERT_STREQ(expected.c_str(), actual.c_str());
  87. }
  88. TEST_P(SerializableSectionPairSectionTest_LineBreakTest, marshal_tom)
  89. {
  90. string newLine = GetParam();
  91. SerializableSectionPairParameter parameter{};
  92. parameter.setValue(SectionPairSectionProvider::createSectionWithTomExample());
  93. parameter.setNewLine(newLine);
  94. SerializableSectionPairSection serializable{parameter};
  95. byte_field expected = SectionPairSectionProvider::createSerializedSectionWithTomExample(newLine);
  96. byte_field actual = serializable.marshal();
  97. ASSERT_STREQ(expected.c_str(), actual.c_str());
  98. }
  99. TEST_P(SerializableSectionPairSectionTest_LineBreakTest, unmarshal_sandra)
  100. {
  101. string newLine = GetParam();
  102. SerializableSectionPairParameter parameter{};
  103. shared_ptr<SectionPairSection> section = make_shared<SectionPairSection>("tmp-id");
  104. parameter.setValue(section);
  105. parameter.setNewLine(newLine);
  106. SerializableSectionPairSection serializable{parameter};
  107. serializable.unmarshal(SectionPairSectionProvider::createSerializedSectionWithSandraExample(newLine));
  108. ASSERT_STREQ("general", section->getSectionId().c_str());
  109. ASSERT_EQ(3, section->getRowAmount());
  110. ASSERT_STREQ("name", section->get(0)->getKey().c_str());
  111. ASSERT_STREQ("Sandra", dynamic_pointer_cast<SectionPairRowSingleValue>(section->get(0)->getValue())->get().c_str());
  112. ASSERT_STREQ("age", section->get(1)->getKey().c_str());
  113. ASSERT_STREQ("24", dynamic_pointer_cast<SectionPairRowSingleValue>(section->get(1)->getValue())->get().c_str());
  114. ASSERT_STREQ("hobbies", section->get(2)->getKey().c_str());
  115. shared_ptr<SectionPairRowListValue> listRow = dynamic_pointer_cast<SectionPairRowListValue>(section->get(2)->getValue());
  116. ASSERT_EQ(3, listRow->getSize());
  117. ASSERT_STREQ("swimming", listRow->get(0).c_str());
  118. ASSERT_STREQ("cycling", listRow->get(1).c_str());
  119. ASSERT_STREQ("singing", listRow->get(2).c_str());
  120. }
  121. TEST_P(SerializableSectionPairSectionTest_LineBreakTest, unmarshal_tom)
  122. {
  123. string newLine = GetParam();
  124. SerializableSectionPairParameter parameter{};
  125. shared_ptr<SectionPairSection> section = make_shared<SectionPairSection>("tmp-id");
  126. parameter.setValue(section);
  127. parameter.setNewLine(newLine);
  128. SerializableSectionPairSection serializable{parameter};
  129. serializable.unmarshal(SectionPairSectionProvider::createSerializedSectionWithTomExample(newLine));
  130. ASSERT_STREQ("general", section->getSectionId().c_str());
  131. ASSERT_EQ(3, section->getRowAmount());
  132. ASSERT_STREQ("name", section->get(0)->getKey().c_str());
  133. ASSERT_STREQ("Tom", dynamic_pointer_cast<SectionPairRowSingleValue>(section->get(0)->getValue())->get().c_str());
  134. ASSERT_STREQ("jobs", section->get(1)->getKey().c_str());
  135. shared_ptr<SectionPairRowListValue> listRow = dynamic_pointer_cast<SectionPairRowListValue>(section->get(1)->getValue());
  136. ASSERT_EQ(2, listRow->getSize());
  137. ASSERT_STREQ("Farmer", listRow->get(0).c_str());
  138. ASSERT_STREQ("Bounty Hunter", listRow->get(1).c_str());
  139. ASSERT_STREQ("age", section->get(2)->getKey().c_str());
  140. ASSERT_STREQ("33", dynamic_pointer_cast<SectionPairRowSingleValue>(section->get(2)->getValue())->get().c_str());
  141. }
  142. TEST_F(SerializableSectionPairSectionTest, unmarshal_invalid_input)
  143. {
  144. SerializableSectionPairParameter parameter{};
  145. parameter.setValue(make_shared<SectionPairSection>("tmp-key"));
  146. SerializableSectionPairSection serializable(parameter);
  147. EXPECT_THROW(
  148. {
  149. try
  150. {
  151. serializable.unmarshal(R"(\n[general]\n\n)");
  152. }
  153. catch (const IllegalArgumentException &_exception)
  154. {
  155. throw;
  156. }
  157. },
  158. IllegalArgumentException);
  159. }
  160. INSTANTIATE_TEST_SUITE_P(LineBreakTest, SerializableSectionPairSectionTest_LineBreakTest, Values(NewLine::getUnixNewLine(), NewLine::getWindowsNewLine()));
  161. }