SerializableSectionPairRowTest.cpp 7.6 KB

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