SerializableSectionPairDocumentTest.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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-17
  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. TEST_F(SerializableSectionPairDocumentTest, constructor_no_value)
  32. {
  33. EXPECT_THROW(
  34. {
  35. try
  36. {
  37. SerializableSectionPairDocument serializable = SerializableSectionPairDocument(nullptr);
  38. }
  39. catch (const IllegalArgumentException &_exception)
  40. {
  41. throw;
  42. }
  43. },
  44. IllegalArgumentException);
  45. }
  46. TEST_F(SerializableSectionPairDocumentTest, getClassName)
  47. {
  48. ASSERT_STREQ("SerializableSectionPairDocument", SerializableSectionPairDocument{make_shared<SectionPairDocument>()}.getClassName().c_str());
  49. }
  50. TEST_F(SerializableSectionPairDocumentTest, getValue)
  51. {
  52. SerializableSectionPairDocument serializable(make_shared<SectionPairDocument>());
  53. ASSERT_TRUE(serializable.getValue() != nullptr);
  54. }
  55. TEST_F(SerializableSectionPairDocumentTest, marshal)
  56. {
  57. SerializableSectionPairDocument serializable(SectionPairDocumentProvider::createDocument());
  58. byte_field expected = SectionPairDocumentProvider::createSerializedDocument();
  59. ASSERT_STREQ(expected.c_str(), serializable.marshal().c_str());
  60. }
  61. TEST_F(SerializableSectionPairDocumentTest, unmarshal)
  62. {
  63. SerializableSectionPairDocument serializable(make_shared<SectionPairDocument>());
  64. serializable.unmarshal(SectionPairDocumentProvider::createSerializedDocument());
  65. shared_ptr<SectionPairDocument> document = dynamic_pointer_cast<SectionPairDocument>(serializable.getValue());
  66. ASSERT_EQ(2, document->getAmountOfSections());
  67. // check general section
  68. shared_ptr<SectionPairSection> general = document->get(0);
  69. ASSERT_STREQ("general", general->getSectionId().c_str());
  70. ASSERT_EQ(3, general->getRowAmount());
  71. ASSERT_STREQ("name", general->get(0)->getKey().c_str());
  72. ASSERT_STREQ("Sandra", dynamic_pointer_cast<SectionPairRowSingleValue>(general->get(0)->getValue())->get().c_str());
  73. ASSERT_STREQ("age", general->get(1)->getKey().c_str());
  74. ASSERT_STREQ("24", dynamic_pointer_cast<SectionPairRowSingleValue>(general->get(1)->getValue())->get().c_str());
  75. ASSERT_STREQ("hobbies", general->get(2)->getKey().c_str());
  76. shared_ptr<SectionPairRowListValue> hobbies = dynamic_pointer_cast<SectionPairRowListValue>(general->get(2)->getValue());
  77. ASSERT_EQ(3, hobbies->getSize());
  78. ASSERT_STREQ("swimming", hobbies->get(0).c_str());
  79. ASSERT_STREQ("cycling", hobbies->get(1).c_str());
  80. ASSERT_STREQ("singing", hobbies->get(2).c_str());
  81. // check physical section
  82. shared_ptr<SectionPairSection> physical = document->get(1);
  83. ASSERT_STREQ("physical", physical->getSectionId().c_str());
  84. ASSERT_EQ(3, physical->getRowAmount());
  85. ASSERT_STREQ("eye-color", physical->get(0)->getKey().c_str());
  86. ASSERT_STREQ("blue", dynamic_pointer_cast<SectionPairRowSingleValue>(physical->get(0)->getValue())->get().c_str());
  87. ASSERT_STREQ("hair-color", physical->get(1)->getKey().c_str());
  88. ASSERT_STREQ("red", dynamic_pointer_cast<SectionPairRowSingleValue>(physical->get(1)->getValue())->get().c_str());
  89. ASSERT_STREQ("height", physical->get(2)->getKey().c_str());
  90. ASSERT_STREQ("167", dynamic_pointer_cast<SectionPairRowSingleValue>(physical->get(2)->getValue())->get().c_str());
  91. }
  92. }