SerializableSectionPairRowTest.cpp 8.1 KB

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