SerializableSectionPairSectionTest.cpp 6.4 KB

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