SerializableSectionPairDocumentTest.cpp 4.9 KB

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