SerializableSectionPairSectionTest.cpp 6.6 KB

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