SerializableSectionPairSectionTest.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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-16
  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, getValue)
  47. {
  48. SerializableSectionPairSection serializable{make_shared<SectionPairSection>("general")};
  49. ASSERT_TRUE(serializable.getValue() != nullptr);
  50. }
  51. TEST_F(SerializableSectionPairSectionTest, marshal_sandra)
  52. {
  53. shared_ptr<SectionPairSection> section = SectionPairSectionProvider::createSectionWithSandraExample();
  54. SerializableSectionPairSection serializable{section};
  55. byte_field expected = SectionPairSectionProvider::createSerializedSectionWithSandraExample();
  56. byte_field actual = serializable.marshal();
  57. ASSERT_STREQ(expected.c_str(), actual.c_str());
  58. }
  59. TEST_F(SerializableSectionPairSectionTest, marshal_tom)
  60. {
  61. shared_ptr<SectionPairSection> section = SectionPairSectionProvider::createSectionWithTomExample();
  62. SerializableSectionPairSection serializable{section};
  63. byte_field expected = SectionPairSectionProvider::createSerializedSectionWithTomExample();
  64. byte_field actual = serializable.marshal();
  65. ASSERT_STREQ(expected.c_str(), actual.c_str());
  66. }
  67. TEST_F(SerializableSectionPairSectionTest, unmarshal_sandra)
  68. {
  69. shared_ptr<SectionPairSection> section = make_shared<SectionPairSection>("tmp-id");
  70. SerializableSectionPairSection serializable{section};
  71. serializable.unmarshal(SectionPairSectionProvider::createSerializedSectionWithSandraExample());
  72. ASSERT_STREQ("general", section->getSectionId().c_str());
  73. ASSERT_EQ(3, section->getRowAmount());
  74. ASSERT_STREQ("name", section->get(0)->getKey().c_str());
  75. ASSERT_STREQ("Sandra", dynamic_pointer_cast<SectionPairRowSingleValue>(section->get(0)->getValue())->get().c_str());
  76. ASSERT_STREQ("age", section->get(1)->getKey().c_str());
  77. ASSERT_STREQ("24", dynamic_pointer_cast<SectionPairRowSingleValue>(section->get(1)->getValue())->get().c_str());
  78. ASSERT_STREQ("hobbies", section->get(2)->getKey().c_str());
  79. shared_ptr<SectionPairRowListValue> listRow = dynamic_pointer_cast<SectionPairRowListValue>(section->get(2)->getValue());
  80. ASSERT_EQ(3, listRow->getSize());
  81. ASSERT_STREQ("swimming", listRow->get(0).c_str());
  82. ASSERT_STREQ("cycling", listRow->get(1).c_str());
  83. ASSERT_STREQ("singing", listRow->get(2).c_str());
  84. }
  85. TEST_F(SerializableSectionPairSectionTest, unmarshal_tom)
  86. {
  87. shared_ptr<SectionPairSection> section = make_shared<SectionPairSection>("tmp-id");
  88. SerializableSectionPairSection serializable{section};
  89. serializable.unmarshal(SectionPairSectionProvider::createSerializedSectionWithTomExample());
  90. ASSERT_STREQ("general", section->getSectionId().c_str());
  91. ASSERT_EQ(3, section->getRowAmount());
  92. ASSERT_STREQ("name", section->get(0)->getKey().c_str());
  93. ASSERT_STREQ("Tom", dynamic_pointer_cast<SectionPairRowSingleValue>(section->get(0)->getValue())->get().c_str());
  94. ASSERT_STREQ("jobs", section->get(1)->getKey().c_str());
  95. shared_ptr<SectionPairRowListValue> listRow = dynamic_pointer_cast<SectionPairRowListValue>(section->get(1)->getValue());
  96. ASSERT_EQ(2, listRow->getSize());
  97. ASSERT_STREQ("Farmer", listRow->get(0).c_str());
  98. ASSERT_STREQ("Bounty Hunter", listRow->get(1).c_str());
  99. ASSERT_STREQ("age", section->get(2)->getKey().c_str());
  100. ASSERT_STREQ("33", dynamic_pointer_cast<SectionPairRowSingleValue>(section->get(2)->getValue())->get().c_str());
  101. }
  102. }