SerializableSectionPairRowTest.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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-17
  7. *
  8. * */
  9. #include <gtest/gtest.h>
  10. #include <ls-std-io-test.hpp>
  11. #include <ls-std/ls-std-core.hpp>
  12. #include <ls-std/ls-std-io.hpp>
  13. #include <memory>
  14. using namespace ls::std::core;
  15. using namespace ls::std::core::interface_type;
  16. using namespace ls::std::core::type;
  17. using namespace ls::std::io;
  18. using namespace ::std;
  19. using namespace test::io;
  20. namespace
  21. {
  22. class SerializableSectionPairRowTest : public ::testing::Test
  23. {
  24. protected:
  25. SerializableSectionPairRowTest() = default;
  26. ~SerializableSectionPairRowTest() override = default;
  27. void SetUp() override
  28. {}
  29. void TearDown() override
  30. {}
  31. };
  32. class SerializableSectionPairRowSerializationTest : public ::testing::TestWithParam<string>
  33. {
  34. protected:
  35. SerializableSectionPairRowSerializationTest() = default;
  36. ~SerializableSectionPairRowSerializationTest() override = default;
  37. };
  38. TEST_F(SerializableSectionPairRowTest, constructor_no_reference)
  39. {
  40. SerializableSectionPairParameter parameter{};
  41. EXPECT_THROW(
  42. {
  43. try
  44. {
  45. SerializableSectionPairRow serializable(parameter);
  46. }
  47. catch (const IllegalArgumentException &_exception)
  48. {
  49. throw;
  50. }
  51. },
  52. IllegalArgumentException);
  53. }
  54. TEST_F(SerializableSectionPairRowTest, getClassName)
  55. {
  56. SerializableSectionPairParameter parameter{};
  57. parameter.setValue(make_shared<SectionPairRow>("tmp-key", SectionPairRowEnumType::SECTION_PAIR_ROW_SINGLE_VALUE));
  58. ASSERT_STREQ("SerializableSectionPairRow", SerializableSectionPairRow{parameter}.getClassName().c_str());
  59. }
  60. TEST_F(SerializableSectionPairRowTest, getValue)
  61. {
  62. SerializableSectionPairParameter parameter{};
  63. parameter.setValue(make_shared<SectionPairRow>("tmp-key", SectionPairRowEnumType::SECTION_PAIR_ROW_SINGLE_VALUE));
  64. SerializableSectionPairRow serializable{parameter};
  65. ASSERT_TRUE(serializable.getValue() != nullptr);
  66. }
  67. TEST_P(SerializableSectionPairRowSerializationTest, marshal_single_value)
  68. {
  69. string newLine = GetParam();
  70. shared_ptr<SerializableSectionPairRow> serializable = SerializableSectionPairRowProvider::createSingleValueForMarshal(newLine);
  71. byte_field expected = "favourite-color=blue" + newLine;
  72. byte_field actual = serializable->marshal();
  73. ASSERT_STREQ(expected.c_str(), actual.c_str());
  74. }
  75. TEST_P(SerializableSectionPairRowSerializationTest, marshal_list_value)
  76. {
  77. string newLine = GetParam();
  78. shared_ptr<SerializableSectionPairRow> serializable = SerializableSectionPairRowProvider::createListValueForMarshal(newLine);
  79. string expected = "favourite-colors:" + newLine + " blue" + newLine + " red" + newLine + " purple" + newLine;
  80. ASSERT_STREQ(expected.c_str(), serializable->marshal().c_str());
  81. }
  82. TEST_P(SerializableSectionPairRowSerializationTest, unmarshal_single_value)
  83. {
  84. shared_ptr<SerializableSectionPairRow> serializable = SerializableSectionPairRowProvider::createSingleValueForUnmarshal(GetParam());
  85. serializable->unmarshal("favourite-color=blue");
  86. shared_ptr<SectionPairRow> row = dynamic_pointer_cast<SectionPairRow>(serializable->getValue());
  87. ASSERT_STREQ("favourite-color", row->getKey().c_str());
  88. ASSERT_STREQ("blue", dynamic_pointer_cast<SectionPairRowSingleValue>(row->getValue())->get().c_str());
  89. }
  90. TEST_P(SerializableSectionPairRowSerializationTest, unmarshal_list_value)
  91. {
  92. string newLine = GetParam();
  93. shared_ptr<SerializableSectionPairRow> serializable = SerializableSectionPairRowProvider::createListValueForUnmarshal(newLine);
  94. string data = "favourite-colors:" + newLine + " blue" + newLine + " red" + newLine + " purple" + newLine;
  95. serializable->unmarshal(data);
  96. shared_ptr<SectionPairRow> row = dynamic_pointer_cast<SectionPairRow>(serializable->getValue());
  97. ASSERT_STREQ("favourite-colors", row->getKey().c_str());
  98. ASSERT_EQ(3, dynamic_pointer_cast<SectionPairRowListValue>(row->getValue())->getSize());
  99. ASSERT_STREQ("blue", dynamic_pointer_cast<SectionPairRowListValue>(row->getValue())->get(0).c_str());
  100. ASSERT_STREQ("red", dynamic_pointer_cast<SectionPairRowListValue>(row->getValue())->get(1).c_str());
  101. ASSERT_STREQ("purple", dynamic_pointer_cast<SectionPairRowListValue>(row->getValue())->get(2).c_str());
  102. }
  103. INSTANTIATE_TEST_SUITE_P(SerializableSectionPairRowTest, SerializableSectionPairRowSerializationTest, ::testing::Values(NewLine::getUnixNewLine(), NewLine::getWindowsNewLine()));
  104. }