SerializableSectionPairDocumentTest.cpp 4.9 KB

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