SerializableSectionPairRowTest.cpp 7.9 KB

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