SerializableSectionPairRowTest.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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-19
  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 SerializableSectionPairRowSerializationLineBreakTest : public ::testing::TestWithParam<string>
  34. {
  35. protected:
  36. SerializableSectionPairRowSerializationLineBreakTest() = default;
  37. ~SerializableSectionPairRowSerializationLineBreakTest() override = default;
  38. };
  39. class SerializableSectionPairRowSerializationValidTest : public ::testing::TestWithParam<array<string, 3>>
  40. {
  41. protected:
  42. SerializableSectionPairRowSerializationValidTest() = default;
  43. ~SerializableSectionPairRowSerializationValidTest() override = default;
  44. };
  45. class SerializableSectionPairRowSerializationNotValidTest : public ::testing::TestWithParam<string>
  46. {
  47. protected:
  48. SerializableSectionPairRowSerializationNotValidTest() = default;
  49. ~SerializableSectionPairRowSerializationNotValidTest() override = default;
  50. };
  51. TEST_F(SerializableSectionPairRowTest, constructor_no_reference)
  52. {
  53. SerializableSectionPairParameter parameter{};
  54. EXPECT_THROW(
  55. {
  56. try
  57. {
  58. SerializableSectionPairRow serializable(parameter);
  59. }
  60. catch (const IllegalArgumentException &_exception)
  61. {
  62. throw;
  63. }
  64. },
  65. IllegalArgumentException);
  66. }
  67. TEST_F(SerializableSectionPairRowTest, getClassName)
  68. {
  69. SerializableSectionPairParameter parameter{};
  70. parameter.setValue(make_shared<SectionPairRow>("tmp-key", SectionPairRowEnumType::SECTION_PAIR_ROW_SINGLE_VALUE));
  71. ASSERT_STREQ("SerializableSectionPairRow", SerializableSectionPairRow{parameter}.getClassName().c_str());
  72. }
  73. TEST_F(SerializableSectionPairRowTest, getValue)
  74. {
  75. SerializableSectionPairParameter parameter{};
  76. parameter.setValue(make_shared<SectionPairRow>("tmp-key", SectionPairRowEnumType::SECTION_PAIR_ROW_SINGLE_VALUE));
  77. SerializableSectionPairRow serializable{parameter};
  78. ASSERT_TRUE(serializable.getValue() != nullptr);
  79. }
  80. TEST_P(SerializableSectionPairRowSerializationLineBreakTest, marshal_single_value)
  81. {
  82. string newLine = GetParam();
  83. shared_ptr<SerializableSectionPairRow> serializable = SerializableSectionPairRowProvider::createSingleValueForMarshal(newLine);
  84. byte_field expected = "favourite-color=blue" + newLine;
  85. byte_field actual = serializable->marshal();
  86. ASSERT_STREQ(expected.c_str(), actual.c_str());
  87. }
  88. TEST_P(SerializableSectionPairRowSerializationLineBreakTest, marshal_list_value)
  89. {
  90. string newLine = GetParam();
  91. shared_ptr<SerializableSectionPairRow> serializable = SerializableSectionPairRowProvider::createListValueForMarshal(newLine);
  92. string expected = "favourite-colors:" + newLine + " blue" + newLine + " red" + newLine + " purple" + newLine;
  93. ASSERT_STREQ(expected.c_str(), serializable->marshal().c_str());
  94. }
  95. TEST_P(SerializableSectionPairRowSerializationLineBreakTest, unmarshal_single_value)
  96. {
  97. shared_ptr<SerializableSectionPairRow> serializable = SerializableSectionPairRowProvider::createSingleValueForUnmarshal(GetParam());
  98. serializable->unmarshal("favourite-color=blue");
  99. shared_ptr<SectionPairRow> row = dynamic_pointer_cast<SectionPairRow>(serializable->getValue());
  100. ASSERT_STREQ("favourite-color", row->getKey().c_str());
  101. ASSERT_STREQ("blue", dynamic_pointer_cast<SectionPairRowSingleValue>(row->getValue())->get().c_str());
  102. }
  103. TEST_P(SerializableSectionPairRowSerializationValidTest, unmarshal_single_value)
  104. {
  105. shared_ptr<SerializableSectionPairRow> serializable = SerializableSectionPairRowProvider::createSingleValueForUnmarshal(NewLine::getWindowsNewLine());
  106. serializable->unmarshal(GetParam().at(0));
  107. shared_ptr<SectionPairRow> row = dynamic_pointer_cast<SectionPairRow>(serializable->getValue());
  108. ASSERT_STREQ(GetParam().at(1).c_str(), row->getKey().c_str());
  109. ASSERT_STREQ(GetParam().at(2).c_str(), dynamic_pointer_cast<SectionPairRowSingleValue>(row->getValue())->get().c_str());
  110. }
  111. TEST_P(SerializableSectionPairRowSerializationNotValidTest, unmarshal_single_value)
  112. {
  113. shared_ptr<SerializableSectionPairRow> serializable = SerializableSectionPairRowProvider::createSingleValueForUnmarshal(NewLine::get());
  114. EXPECT_THROW(
  115. {
  116. try
  117. {
  118. serializable->unmarshal(GetParam());
  119. }
  120. catch (const IllegalArgumentException &_exception)
  121. {
  122. throw;
  123. }
  124. },
  125. IllegalArgumentException);
  126. }
  127. TEST_P(SerializableSectionPairRowSerializationLineBreakTest, unmarshal_list_value)
  128. {
  129. string newLine = GetParam();
  130. shared_ptr<SerializableSectionPairRow> serializable = SerializableSectionPairRowProvider::createListValueForUnmarshal(newLine);
  131. string data = "favourite-colors:" + newLine + " blue" + newLine + " red" + newLine + " purple" + newLine;
  132. serializable->unmarshal(data);
  133. shared_ptr<SectionPairRow> row = dynamic_pointer_cast<SectionPairRow>(serializable->getValue());
  134. ASSERT_STREQ("favourite-colors", row->getKey().c_str());
  135. ASSERT_EQ(3, dynamic_pointer_cast<SectionPairRowListValue>(row->getValue())->getSize());
  136. ASSERT_STREQ("blue", dynamic_pointer_cast<SectionPairRowListValue>(row->getValue())->get(0).c_str());
  137. ASSERT_STREQ("red", dynamic_pointer_cast<SectionPairRowListValue>(row->getValue())->get(1).c_str());
  138. ASSERT_STREQ("purple", dynamic_pointer_cast<SectionPairRowListValue>(row->getValue())->get(2).c_str());
  139. }
  140. INSTANTIATE_TEST_SUITE_P(SerializableSectionPairRowTest, SerializableSectionPairRowSerializationLineBreakTest, ::testing::Values(NewLine::getUnixNewLine(), NewLine::getWindowsNewLine()));
  141. INSTANTIATE_TEST_SUITE_P(SerializableSectionPairRowTest, SerializableSectionPairRowSerializationValidTest, ::testing::Values(array<string, 3>{"favourite-color=blue", "favourite-color", "blue"}, array<string, 3>{"hair-color=red" + NewLine::getWindowsNewLine(), "hair-color", "red"}));
  142. INSTANTIATE_TEST_SUITE_P(SerializableSectionPairRowTest, SerializableSectionPairRowSerializationNotValidTest, ::testing::Values("favourite-color", "color="));
  143. }