SerializableSectionPairSectionTest.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2023-02-14
  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 SerializableSectionPairSectionTest : public ::testing::Test
  22. {
  23. protected:
  24. SerializableSectionPairSectionTest() = default;
  25. ~SerializableSectionPairSectionTest() override = default;
  26. void SetUp() override
  27. {}
  28. void TearDown() override
  29. {}
  30. };
  31. TEST_F(SerializableSectionPairSectionTest, constructor_no_reference)
  32. {
  33. EXPECT_THROW(
  34. {
  35. try
  36. {
  37. SerializableSectionPairSection serializable(nullptr);
  38. }
  39. catch (const IllegalArgumentException &_exception)
  40. {
  41. throw;
  42. }
  43. },
  44. IllegalArgumentException);
  45. }
  46. TEST_F(SerializableSectionPairSectionTest, getClassName)
  47. {
  48. ASSERT_STREQ("SerializableSectionPairSection", SerializableSectionPairSection{make_shared<SectionPairSection>("general")}.getClassName().c_str());
  49. }
  50. TEST_F(SerializableSectionPairSectionTest, getValue)
  51. {
  52. SerializableSectionPairSection serializable{make_shared<SectionPairSection>("general")};
  53. ASSERT_TRUE(serializable.getValue() != nullptr);
  54. }
  55. TEST_F(SerializableSectionPairSectionTest, marshal_sandra)
  56. {
  57. shared_ptr<SectionPairSection> section = SectionPairSectionProvider::createSectionWithSandraExample();
  58. SerializableSectionPairSection serializable{section};
  59. byte_field expected = SectionPairSectionProvider::createSerializedSectionWithSandraExample();
  60. byte_field actual = serializable.marshal();
  61. ASSERT_STREQ(expected.c_str(), actual.c_str());
  62. }
  63. TEST_F(SerializableSectionPairSectionTest, marshal_tom)
  64. {
  65. shared_ptr<SectionPairSection> section = SectionPairSectionProvider::createSectionWithTomExample();
  66. SerializableSectionPairSection serializable{section};
  67. byte_field expected = SectionPairSectionProvider::createSerializedSectionWithTomExample();
  68. byte_field actual = serializable.marshal();
  69. ASSERT_STREQ(expected.c_str(), actual.c_str());
  70. }
  71. TEST_F(SerializableSectionPairSectionTest, unmarshal_sandra)
  72. {
  73. shared_ptr<SectionPairSection> section = make_shared<SectionPairSection>("tmp-id");
  74. SerializableSectionPairSection serializable{section};
  75. serializable.unmarshal(SectionPairSectionProvider::createSerializedSectionWithSandraExample());
  76. ASSERT_STREQ("general", section->getSectionId().c_str());
  77. ASSERT_EQ(3, section->getRowAmount());
  78. ASSERT_STREQ("name", section->get(0)->getKey().c_str());
  79. ASSERT_STREQ("Sandra", dynamic_pointer_cast<SectionPairRowSingleValue>(section->get(0)->getValue())->get().c_str());
  80. ASSERT_STREQ("age", section->get(1)->getKey().c_str());
  81. ASSERT_STREQ("24", dynamic_pointer_cast<SectionPairRowSingleValue>(section->get(1)->getValue())->get().c_str());
  82. ASSERT_STREQ("hobbies", section->get(2)->getKey().c_str());
  83. shared_ptr<SectionPairRowListValue> listRow = dynamic_pointer_cast<SectionPairRowListValue>(section->get(2)->getValue());
  84. ASSERT_EQ(3, listRow->getSize());
  85. ASSERT_STREQ("swimming", listRow->get(0).c_str());
  86. ASSERT_STREQ("cycling", listRow->get(1).c_str());
  87. ASSERT_STREQ("singing", listRow->get(2).c_str());
  88. }
  89. TEST_F(SerializableSectionPairSectionTest, unmarshal_tom)
  90. {
  91. shared_ptr<SectionPairSection> section = make_shared<SectionPairSection>("tmp-id");
  92. SerializableSectionPairSection serializable{section};
  93. serializable.unmarshal(SectionPairSectionProvider::createSerializedSectionWithTomExample());
  94. ASSERT_STREQ("general", section->getSectionId().c_str());
  95. ASSERT_EQ(3, section->getRowAmount());
  96. ASSERT_STREQ("name", section->get(0)->getKey().c_str());
  97. ASSERT_STREQ("Tom", dynamic_pointer_cast<SectionPairRowSingleValue>(section->get(0)->getValue())->get().c_str());
  98. ASSERT_STREQ("jobs", section->get(1)->getKey().c_str());
  99. shared_ptr<SectionPairRowListValue> listRow = dynamic_pointer_cast<SectionPairRowListValue>(section->get(1)->getValue());
  100. ASSERT_EQ(2, listRow->getSize());
  101. ASSERT_STREQ("Farmer", listRow->get(0).c_str());
  102. ASSERT_STREQ("Bounty Hunter", listRow->get(1).c_str());
  103. ASSERT_STREQ("age", section->get(2)->getKey().c_str());
  104. ASSERT_STREQ("33", dynamic_pointer_cast<SectionPairRowSingleValue>(section->get(2)->getValue())->get().c_str());
  105. }
  106. }