SerializableSectionPairRowTest.cpp 7.6 KB

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