SerializableSectionPairSectionTest.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2023-02-14
  6. * Changed: 2023-02-20
  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::type;
  16. using namespace ls::std::io;
  17. using namespace ::std;
  18. using namespace test::io;
  19. namespace
  20. {
  21. class SerializableSectionPairSectionTest : public ::testing::Test
  22. {
  23. protected:
  24. SerializableSectionPairSectionTest() = default;
  25. ~SerializableSectionPairSectionTest() override = default;
  26. void SetUp() override
  27. {}
  28. void TearDown() override
  29. {}
  30. };
  31. class SerializableSectionPairSectionTest_LineBreakTest : public ::testing::TestWithParam<string>
  32. {
  33. protected:
  34. SerializableSectionPairSectionTest_LineBreakTest() = default;
  35. ~SerializableSectionPairSectionTest_LineBreakTest() override = default;
  36. };
  37. TEST_F(SerializableSectionPairSectionTest, constructor_no_reference)
  38. {
  39. SerializableSectionPairParameter parameter{};
  40. EXPECT_THROW(
  41. {
  42. try
  43. {
  44. SerializableSectionPairSection serializable(parameter);
  45. }
  46. catch (const IllegalArgumentException &_exception)
  47. {
  48. throw;
  49. }
  50. },
  51. IllegalArgumentException);
  52. }
  53. TEST_F(SerializableSectionPairSectionTest, getClassName)
  54. {
  55. SerializableSectionPairParameter parameter{};
  56. parameter.setValue(make_shared<SectionPairSection>("general"));
  57. ASSERT_STREQ("SerializableSectionPairSection", SerializableSectionPairSection{parameter}.getClassName().c_str());
  58. }
  59. TEST_F(SerializableSectionPairSectionTest, getValue)
  60. {
  61. SerializableSectionPairParameter parameter{};
  62. parameter.setValue(make_shared<SectionPairSection>("general"));
  63. SerializableSectionPairSection serializable{parameter};
  64. ASSERT_TRUE(serializable.getValue() != nullptr);
  65. }
  66. TEST_P(SerializableSectionPairSectionTest_LineBreakTest, marshal_sandra)
  67. {
  68. string newLine = GetParam();
  69. SerializableSectionPairParameter parameter{};
  70. parameter.setValue(SectionPairSectionProvider::createSectionWithSandraExample());
  71. parameter.setNewLine(newLine);
  72. SerializableSectionPairSection serializable{parameter};
  73. byte_field expected = SectionPairSectionProvider::createSerializedSectionWithSandraExample(newLine);
  74. byte_field actual = serializable.marshal();
  75. ASSERT_STREQ(expected.c_str(), actual.c_str());
  76. }
  77. TEST_P(SerializableSectionPairSectionTest_LineBreakTest, marshal_tom)
  78. {
  79. string newLine = GetParam();
  80. SerializableSectionPairParameter parameter{};
  81. parameter.setValue(SectionPairSectionProvider::createSectionWithTomExample());
  82. parameter.setNewLine(newLine);
  83. SerializableSectionPairSection serializable{parameter};
  84. byte_field expected = SectionPairSectionProvider::createSerializedSectionWithTomExample(newLine);
  85. byte_field actual = serializable.marshal();
  86. ASSERT_STREQ(expected.c_str(), actual.c_str());
  87. }
  88. TEST_P(SerializableSectionPairSectionTest_LineBreakTest, unmarshal_sandra)
  89. {
  90. string newLine = GetParam();
  91. SerializableSectionPairParameter parameter{};
  92. shared_ptr<SectionPairSection> section = make_shared<SectionPairSection>("tmp-id");
  93. parameter.setValue(section);
  94. parameter.setNewLine(newLine);
  95. SerializableSectionPairSection serializable{parameter};
  96. serializable.unmarshal(SectionPairSectionProvider::createSerializedSectionWithSandraExample(newLine));
  97. ASSERT_STREQ("general", section->getSectionId().c_str());
  98. ASSERT_EQ(3, section->getRowAmount());
  99. ASSERT_STREQ("name", section->get(0)->getKey().c_str());
  100. ASSERT_STREQ("Sandra", dynamic_pointer_cast<SectionPairRowSingleValue>(section->get(0)->getValue())->get().c_str());
  101. ASSERT_STREQ("age", section->get(1)->getKey().c_str());
  102. ASSERT_STREQ("24", dynamic_pointer_cast<SectionPairRowSingleValue>(section->get(1)->getValue())->get().c_str());
  103. ASSERT_STREQ("hobbies", section->get(2)->getKey().c_str());
  104. shared_ptr<SectionPairRowListValue> listRow = dynamic_pointer_cast<SectionPairRowListValue>(section->get(2)->getValue());
  105. ASSERT_EQ(3, listRow->getSize());
  106. ASSERT_STREQ("swimming", listRow->get(0).c_str());
  107. ASSERT_STREQ("cycling", listRow->get(1).c_str());
  108. ASSERT_STREQ("singing", listRow->get(2).c_str());
  109. }
  110. TEST_P(SerializableSectionPairSectionTest_LineBreakTest, unmarshal_tom)
  111. {
  112. string newLine = GetParam();
  113. SerializableSectionPairParameter parameter{};
  114. shared_ptr<SectionPairSection> section = make_shared<SectionPairSection>("tmp-id");
  115. parameter.setValue(section);
  116. parameter.setNewLine(newLine);
  117. SerializableSectionPairSection serializable{parameter};
  118. serializable.unmarshal(SectionPairSectionProvider::createSerializedSectionWithTomExample(newLine));
  119. ASSERT_STREQ("general", section->getSectionId().c_str());
  120. ASSERT_EQ(3, section->getRowAmount());
  121. ASSERT_STREQ("name", section->get(0)->getKey().c_str());
  122. ASSERT_STREQ("Tom", dynamic_pointer_cast<SectionPairRowSingleValue>(section->get(0)->getValue())->get().c_str());
  123. ASSERT_STREQ("jobs", section->get(1)->getKey().c_str());
  124. shared_ptr<SectionPairRowListValue> listRow = dynamic_pointer_cast<SectionPairRowListValue>(section->get(1)->getValue());
  125. ASSERT_EQ(2, listRow->getSize());
  126. ASSERT_STREQ("Farmer", listRow->get(0).c_str());
  127. ASSERT_STREQ("Bounty Hunter", listRow->get(1).c_str());
  128. ASSERT_STREQ("age", section->get(2)->getKey().c_str());
  129. ASSERT_STREQ("33", dynamic_pointer_cast<SectionPairRowSingleValue>(section->get(2)->getValue())->get().c_str());
  130. }
  131. TEST_F(SerializableSectionPairSectionTest, unmarshal_invalid_input)
  132. {
  133. SerializableSectionPairParameter parameter{};
  134. parameter.setValue(make_shared<SectionPairSection>("tmp-key"));
  135. SerializableSectionPairSection serializable(parameter);
  136. EXPECT_THROW(
  137. {
  138. try
  139. {
  140. serializable.unmarshal(R"(\n[general]\n\n)");
  141. }
  142. catch (const IllegalArgumentException &_exception)
  143. {
  144. throw;
  145. }
  146. },
  147. IllegalArgumentException);
  148. }
  149. INSTANTIATE_TEST_SUITE_P(LineBreakTest, SerializableSectionPairSectionTest_LineBreakTest, ::testing::Values(NewLine::getUnixNewLine(), NewLine::getWindowsNewLine()));
  150. }