SerializableSectionPairDocumentTest.cpp 5.3 KB

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