SerializableSectionPairSectionTest.cpp 6.7 KB

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