SerializableSectionPairSectionTest.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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-15
  7. *
  8. * */
  9. #include <gtest/gtest.h>
  10. #include <ls-std/ls-std-core.hpp>
  11. #include <ls-std/ls-std-io.hpp>
  12. #include <memory>
  13. using namespace ls::std::core;
  14. using namespace ls::std::core::type;
  15. using namespace ls::std::io;
  16. using namespace ::std;
  17. namespace
  18. {
  19. class SerializableSectionPairSectionTest : public ::testing::Test
  20. {
  21. protected:
  22. SerializableSectionPairSectionTest() = default;
  23. ~SerializableSectionPairSectionTest() override = default;
  24. void SetUp() override
  25. {}
  26. void TearDown() override
  27. {}
  28. static shared_ptr<SectionPairSection> createSection()
  29. {
  30. shared_ptr<SectionPairSection> section = make_shared<SectionPairSection>("general");
  31. shared_ptr<SectionPairRow> name = make_shared<SectionPairRow>("name", SectionPairRowEnumType::SECTION_PAIR_ROW_SINGLE_VALUE);
  32. dynamic_pointer_cast<SectionPairRowSingleValue>(name->getValue())->set("Tom");
  33. section->add(name);
  34. shared_ptr<SectionPairRow> jobs = make_shared<SectionPairRow>("jobs", SectionPairRowEnumType::SECTION_PAIR_ROW_LIST_VALUE);
  35. shared_ptr<SectionPairRowListValue> jobList = dynamic_pointer_cast<SectionPairRowListValue>(jobs->getValue());
  36. jobList->add("Farmer");
  37. jobList->add("Bounty Hunter");
  38. section->add(jobs);
  39. shared_ptr<SectionPairRow> age = make_shared<SectionPairRow>("age", SectionPairRowEnumType::SECTION_PAIR_ROW_SINGLE_VALUE);
  40. dynamic_pointer_cast<SectionPairRowSingleValue>(age->getValue())->set("33");
  41. section->add(age);
  42. return section;
  43. }
  44. static byte_field createSerializedSection()
  45. {
  46. byte_field serializedSection = NewLine::get() + "[general]" + NewLine::get();
  47. byte_field serializedNameRow = "name=Tom" + NewLine::get();
  48. byte_field serializedJobsRow = "jobs:" + NewLine::get() + " Farmer" + NewLine::get() + " Bounty Hunter" + NewLine::get();
  49. byte_field serializedAgeRow = "age=33" + NewLine::get();
  50. return serializedSection + serializedNameRow + serializedJobsRow + serializedAgeRow;
  51. }
  52. };
  53. TEST_F(SerializableSectionPairSectionTest, constructor_no_reference)
  54. {
  55. EXPECT_THROW(
  56. {
  57. try
  58. {
  59. SerializableSectionPairSection serializable(nullptr);
  60. }
  61. catch (const IllegalArgumentException &_exception)
  62. {
  63. throw;
  64. }
  65. },
  66. IllegalArgumentException);
  67. }
  68. TEST_F(SerializableSectionPairSectionTest, getValue)
  69. {
  70. SerializableSectionPairSection serializable{make_shared<SectionPairSection>("general")};
  71. ASSERT_TRUE(serializable.getValue() != nullptr);
  72. }
  73. TEST_F(SerializableSectionPairSectionTest, marshal)
  74. {
  75. shared_ptr<SectionPairSection> section = SerializableSectionPairSectionTest::createSection();
  76. SerializableSectionPairSection serializable{section};
  77. byte_field expected = SerializableSectionPairSectionTest::createSerializedSection();
  78. byte_field actual = serializable.marshal();
  79. ASSERT_STREQ(expected.c_str(), actual.c_str());
  80. }
  81. TEST_F(SerializableSectionPairSectionTest, unmarshal)
  82. {
  83. shared_ptr<SectionPairSection> section = make_shared<SectionPairSection>("tmp-id");
  84. SerializableSectionPairSection serializable{section};
  85. serializable.unmarshal(SerializableSectionPairSectionTest::createSerializedSection());
  86. ASSERT_STREQ("general", section->getSectionId().c_str());
  87. ASSERT_EQ(3, section->getRowAmount());
  88. ASSERT_STREQ("name", section->get(0)->getKey().c_str());
  89. ASSERT_STREQ("Tom", dynamic_pointer_cast<SectionPairRowSingleValue>(section->get(0)->getValue())->get().c_str());
  90. ASSERT_STREQ("jobs", section->get(1)->getKey().c_str());
  91. shared_ptr<SectionPairRowListValue> listRow = dynamic_pointer_cast<SectionPairRowListValue>(section->get(1)->getValue());
  92. ASSERT_EQ(2, listRow->getSize());
  93. ASSERT_STREQ("Farmer", listRow->get(0).c_str());
  94. ASSERT_STREQ("Bounty Hunter", listRow->get(1).c_str());
  95. ASSERT_STREQ("age", section->get(2)->getKey().c_str());
  96. ASSERT_STREQ("33", dynamic_pointer_cast<SectionPairRowSingleValue>(section->get(2)->getValue())->get().c_str());
  97. }
  98. }