SerializableSectionPairRowTest.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2023-02-12
  6. * Changed: 2023-02-23
  7. *
  8. * */
  9. #include <array>
  10. #include <gtest/gtest.h>
  11. #include <ls-std-io-test.hpp>
  12. #include <ls-std/ls-std-core.hpp>
  13. #include <ls-std/ls-std-io.hpp>
  14. #include <memory>
  15. using ls::std::core::IllegalArgumentException;
  16. using ls::std::core::type::byte_field;
  17. using ls::std::io::NewLine;
  18. using ls::std::io::SectionPairRow;
  19. using ls::std::io::SectionPairRowEnumType;
  20. using ls::std::io::SectionPairRowListValue;
  21. using ls::std::io::SectionPairRowSingleValue;
  22. using ls::std::io::SerializableSectionPairParameter;
  23. using ls::std::io::SerializableSectionPairRow;
  24. using std::array;
  25. using std::dynamic_pointer_cast;
  26. using std::make_shared;
  27. using std::shared_ptr;
  28. using std::string;
  29. using test::io::SerializableSectionPairRowProvider;
  30. using testing::Test;
  31. using testing::TestWithParam;
  32. using testing::Values;
  33. namespace
  34. {
  35. class SerializableSectionPairRowTest : public Test
  36. {
  37. protected:
  38. SerializableSectionPairRowTest() = default;
  39. ~SerializableSectionPairRowTest() override = default;
  40. void SetUp() override
  41. {}
  42. void TearDown() override
  43. {}
  44. };
  45. class SerializableSectionPairRowTest_LineBreakTest : public TestWithParam<string>
  46. {
  47. protected:
  48. SerializableSectionPairRowTest_LineBreakTest() = default;
  49. ~SerializableSectionPairRowTest_LineBreakTest() override = default;
  50. };
  51. class SerializableSectionPairRowTest_IsValidSingleValueTest : public TestWithParam<array<string, 3>>
  52. {
  53. protected:
  54. SerializableSectionPairRowTest_IsValidSingleValueTest() = default;
  55. ~SerializableSectionPairRowTest_IsValidSingleValueTest() override = default;
  56. };
  57. class SerializableSectionPairRowTest_IsInvalidSingleValueTest : public TestWithParam<string>
  58. {
  59. protected:
  60. SerializableSectionPairRowTest_IsInvalidSingleValueTest() = default;
  61. ~SerializableSectionPairRowTest_IsInvalidSingleValueTest() override = default;
  62. };
  63. class SerializableSectionPairRowTest_IsInvalidListValueTest : public TestWithParam<string>
  64. {
  65. protected:
  66. SerializableSectionPairRowTest_IsInvalidListValueTest() = default;
  67. ~SerializableSectionPairRowTest_IsInvalidListValueTest() override = default;
  68. };
  69. TEST_F(SerializableSectionPairRowTest, constructor_no_reference)
  70. {
  71. SerializableSectionPairParameter parameter{};
  72. EXPECT_THROW(
  73. {
  74. try
  75. {
  76. SerializableSectionPairRow serializable(parameter);
  77. }
  78. catch (const IllegalArgumentException &_exception)
  79. {
  80. throw;
  81. }
  82. },
  83. IllegalArgumentException);
  84. }
  85. TEST_F(SerializableSectionPairRowTest, getClassName)
  86. {
  87. SerializableSectionPairParameter parameter{};
  88. parameter.setValue(make_shared<SectionPairRow>("tmp-key", SectionPairRowEnumType::SECTION_PAIR_ROW_SINGLE_VALUE));
  89. ASSERT_STREQ("SerializableSectionPairRow", SerializableSectionPairRow{parameter}.getClassName().c_str());
  90. }
  91. TEST_F(SerializableSectionPairRowTest, getValue)
  92. {
  93. SerializableSectionPairParameter parameter{};
  94. parameter.setValue(make_shared<SectionPairRow>("tmp-key", SectionPairRowEnumType::SECTION_PAIR_ROW_SINGLE_VALUE));
  95. SerializableSectionPairRow serializable{parameter};
  96. ASSERT_TRUE(serializable.getValue() != nullptr);
  97. }
  98. TEST_P(SerializableSectionPairRowTest_LineBreakTest, marshal_single_value)
  99. {
  100. string newLine = GetParam();
  101. shared_ptr<SerializableSectionPairRow> serializable = SerializableSectionPairRowProvider::createSingleValueForMarshal(newLine);
  102. byte_field expected = "favourite-color=blue" + newLine;
  103. byte_field actual = serializable->marshal();
  104. ASSERT_STREQ(expected.c_str(), actual.c_str());
  105. }
  106. TEST_P(SerializableSectionPairRowTest_LineBreakTest, marshal_list_value)
  107. {
  108. string newLine = GetParam();
  109. shared_ptr<SerializableSectionPairRow> serializable = SerializableSectionPairRowProvider::createListValueForMarshal(newLine);
  110. string expected = "favourite-colors:" + newLine + " blue" + newLine + " red" + newLine + " purple" + newLine;
  111. ASSERT_STREQ(expected.c_str(), serializable->marshal().c_str());
  112. }
  113. TEST_P(SerializableSectionPairRowTest_LineBreakTest, unmarshal_single_value)
  114. {
  115. shared_ptr<SerializableSectionPairRow> serializable = SerializableSectionPairRowProvider::createSingleValueForUnmarshal(GetParam());
  116. serializable->unmarshal("favourite-color=blue");
  117. shared_ptr<SectionPairRow> row = dynamic_pointer_cast<SectionPairRow>(serializable->getValue());
  118. ASSERT_STREQ("favourite-color", row->getKey().c_str());
  119. ASSERT_STREQ("blue", dynamic_pointer_cast<SectionPairRowSingleValue>(row->getValue())->get().c_str());
  120. }
  121. TEST_P(SerializableSectionPairRowTest_LineBreakTest, unmarshal_list_value)
  122. {
  123. string newLine = GetParam();
  124. shared_ptr<SerializableSectionPairRow> serializable = SerializableSectionPairRowProvider::createListValueForUnmarshal(newLine);
  125. string data = "favourite-colors:" + newLine + " blue" + newLine + " red" + newLine + " purple" + newLine;
  126. serializable->unmarshal(data);
  127. shared_ptr<SectionPairRow> row = dynamic_pointer_cast<SectionPairRow>(serializable->getValue());
  128. ASSERT_STREQ("favourite-colors", row->getKey().c_str());
  129. ASSERT_EQ(3, dynamic_pointer_cast<SectionPairRowListValue>(row->getValue())->getSize());
  130. ASSERT_STREQ("blue", dynamic_pointer_cast<SectionPairRowListValue>(row->getValue())->get(0).c_str());
  131. ASSERT_STREQ("red", dynamic_pointer_cast<SectionPairRowListValue>(row->getValue())->get(1).c_str());
  132. ASSERT_STREQ("purple", dynamic_pointer_cast<SectionPairRowListValue>(row->getValue())->get(2).c_str());
  133. }
  134. TEST_P(SerializableSectionPairRowTest_IsValidSingleValueTest, unmarshal_single_value)
  135. {
  136. shared_ptr<SerializableSectionPairRow> serializable = SerializableSectionPairRowProvider::createSingleValueForUnmarshal(NewLine::getWindowsNewLine());
  137. serializable->unmarshal(GetParam().at(0));
  138. shared_ptr<SectionPairRow> row = dynamic_pointer_cast<SectionPairRow>(serializable->getValue());
  139. ASSERT_STREQ(GetParam().at(1).c_str(), row->getKey().c_str());
  140. ASSERT_STREQ(GetParam().at(2).c_str(), dynamic_pointer_cast<SectionPairRowSingleValue>(row->getValue())->get().c_str());
  141. }
  142. TEST_P(SerializableSectionPairRowTest_IsInvalidSingleValueTest, unmarshal_single_value)
  143. {
  144. shared_ptr<SerializableSectionPairRow> serializable = SerializableSectionPairRowProvider::createSingleValueForUnmarshal(NewLine::get());
  145. EXPECT_THROW(
  146. {
  147. try
  148. {
  149. serializable->unmarshal(GetParam());
  150. }
  151. catch (const IllegalArgumentException &_exception)
  152. {
  153. throw;
  154. }
  155. },
  156. IllegalArgumentException);
  157. }
  158. TEST_P(SerializableSectionPairRowTest_IsInvalidListValueTest, unmarshal_list_value)
  159. {
  160. shared_ptr<SerializableSectionPairRow> serializable = SerializableSectionPairRowProvider::createListValueForUnmarshal(NewLine::get());
  161. EXPECT_THROW(
  162. {
  163. try
  164. {
  165. serializable->unmarshal(GetParam());
  166. }
  167. catch (const IllegalArgumentException &_exception)
  168. {
  169. throw;
  170. }
  171. },
  172. IllegalArgumentException);
  173. }
  174. INSTANTIATE_TEST_SUITE_P(LineBreakTest, SerializableSectionPairRowTest_LineBreakTest, Values(NewLine::getUnixNewLine(), NewLine::getWindowsNewLine()));
  175. INSTANTIATE_TEST_SUITE_P(IsValidSingleValueTest, SerializableSectionPairRowTest_IsValidSingleValueTest, Values(array<string, 3>{"favourite-color=blue", "favourite-color", "blue"}, array<string, 3>{"hair-color=red" + NewLine::getWindowsNewLine(), "hair-color", "red"}));
  176. INSTANTIATE_TEST_SUITE_P(IsInvalidSingleValueTest, SerializableSectionPairRowTest_IsInvalidSingleValueTest, Values("favourite-color", "color="));
  177. INSTANTIATE_TEST_SUITE_P(IsInvalidListValueTest, SerializableSectionPairRowTest_IsInvalidListValueTest, Values("favourite-color:\n", "colors:\r\n blue"));
  178. }