SerializableSectionPairDocumentTest.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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-16
  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::SectionPairDocument;
  19. using ls::standard::io::SectionPairRowListValue;
  20. using ls::standard::io::SectionPairRowSingleValue;
  21. using ls::standard::io::SectionPairSection;
  22. using ls::standard::io::SerializableSectionPairDocument;
  23. using ls::standard::io::SerializableSectionPairParameter;
  24. using std::dynamic_pointer_cast;
  25. using std::make_shared;
  26. using std::shared_ptr;
  27. using std::string;
  28. using test::io::SectionPairDocumentProvider;
  29. using testing::Test;
  30. using testing::TestWithParam;
  31. using testing::Values;
  32. namespace
  33. {
  34. class SerializableSectionPairDocumentTest : public Test
  35. {
  36. public:
  37. SerializableSectionPairDocumentTest() = default;
  38. ~SerializableSectionPairDocumentTest() override = default;
  39. };
  40. class SerializableSectionPairDocumentTest_LineBreakTest : public TestWithParam<string>
  41. {
  42. public:
  43. SerializableSectionPairDocumentTest_LineBreakTest() = default;
  44. ~SerializableSectionPairDocumentTest_LineBreakTest() override = default;
  45. };
  46. TEST_F(SerializableSectionPairDocumentTest, constructor_no_value)
  47. {
  48. SerializableSectionPairParameter parameter{};
  49. EXPECT_THROW(
  50. {
  51. try
  52. {
  53. auto serializable = SerializableSectionPairDocument(parameter);
  54. }
  55. catch (const IllegalArgumentException &_exception)
  56. {
  57. throw;
  58. }
  59. },
  60. IllegalArgumentException);
  61. }
  62. TEST_F(SerializableSectionPairDocumentTest, getClassName)
  63. {
  64. SerializableSectionPairParameter parameter{};
  65. parameter.setValue(make_shared<SectionPairDocument>());
  66. ASSERT_STREQ("SerializableSectionPairDocument", SerializableSectionPairDocument{parameter}.getClassName().c_str());
  67. }
  68. TEST_F(SerializableSectionPairDocumentTest, getValue)
  69. {
  70. SerializableSectionPairParameter parameter{};
  71. parameter.setValue(make_shared<SectionPairDocument>());
  72. const SerializableSectionPairDocument serializable(parameter);
  73. ASSERT_TRUE(serializable.getValue() != nullptr);
  74. }
  75. TEST_P(SerializableSectionPairDocumentTest_LineBreakTest, marshal)
  76. {
  77. const string& newLine = GetParam();
  78. SerializableSectionPairParameter parameter{};
  79. parameter.setValue(SectionPairDocumentProvider::createDocument());
  80. parameter.setNewLine(newLine);
  81. SerializableSectionPairDocument serializable(parameter);
  82. const byte_field expected = SectionPairDocumentProvider::createSerializedDocument(newLine);
  83. ASSERT_STREQ(expected.c_str(), serializable.marshal().c_str());
  84. }
  85. TEST_P(SerializableSectionPairDocumentTest_LineBreakTest, unmarshal)
  86. {
  87. const string& newLine = GetParam();
  88. SerializableSectionPairParameter parameter{};
  89. parameter.setValue(make_shared<SectionPairDocument>());
  90. parameter.setNewLine(newLine);
  91. SerializableSectionPairDocument serializable(parameter);
  92. serializable.unmarshal(SectionPairDocumentProvider::createSerializedDocument(newLine));
  93. shared_ptr<SectionPairDocument> document = dynamic_pointer_cast<SectionPairDocument>(serializable.getValue());
  94. ASSERT_EQ(2, document->getAmountOfSections());
  95. // check general section
  96. shared_ptr<SectionPairSection> general = document->get(0);
  97. ASSERT_STREQ("general", general->getSectionId().c_str());
  98. ASSERT_EQ(3, general->getRowAmount());
  99. ASSERT_STREQ("name", general->get(0)->getKey().c_str());
  100. ASSERT_STREQ("Sandra", dynamic_pointer_cast<SectionPairRowSingleValue>(general->get(0)->getValue())->get().c_str());
  101. ASSERT_STREQ("age", general->get(1)->getKey().c_str());
  102. ASSERT_STREQ("24", dynamic_pointer_cast<SectionPairRowSingleValue>(general->get(1)->getValue())->get().c_str());
  103. ASSERT_STREQ("hobbies", general->get(2)->getKey().c_str());
  104. shared_ptr<SectionPairRowListValue> hobbies = dynamic_pointer_cast<SectionPairRowListValue>(general->get(2)->getValue());
  105. ASSERT_EQ(3, hobbies->getSize());
  106. ASSERT_STREQ("swimming", hobbies->get(0).c_str());
  107. ASSERT_STREQ("cycling", hobbies->get(1).c_str());
  108. ASSERT_STREQ("singing", hobbies->get(2).c_str());
  109. // check physical section
  110. shared_ptr<SectionPairSection> physical = document->get(1);
  111. ASSERT_STREQ("physical", physical->getSectionId().c_str());
  112. ASSERT_EQ(3, physical->getRowAmount());
  113. ASSERT_STREQ("eye-color", physical->get(0)->getKey().c_str());
  114. ASSERT_STREQ("blue", dynamic_pointer_cast<SectionPairRowSingleValue>(physical->get(0)->getValue())->get().c_str());
  115. ASSERT_STREQ("hair-color", physical->get(1)->getKey().c_str());
  116. ASSERT_STREQ("red", dynamic_pointer_cast<SectionPairRowSingleValue>(physical->get(1)->getValue())->get().c_str());
  117. ASSERT_STREQ("height", physical->get(2)->getKey().c_str());
  118. ASSERT_STREQ("167", dynamic_pointer_cast<SectionPairRowSingleValue>(physical->get(2)->getValue())->get().c_str());
  119. }
  120. INSTANTIATE_TEST_SUITE_P(LineBreakTest, SerializableSectionPairDocumentTest_LineBreakTest, Values(NewLine::getUnixNewLine(), NewLine::getWindowsNewLine()));
  121. }