SerializableSectionPairRowTest.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2023-02-12
  6. * Changed: 2023-02-17
  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 SerializableSectionPairRowTest : public ::testing::Test
  20. {
  21. protected:
  22. SerializableSectionPairRowTest() = default;
  23. ~SerializableSectionPairRowTest() override = default;
  24. void SetUp() override
  25. {}
  26. void TearDown() override
  27. {}
  28. };
  29. TEST_F(SerializableSectionPairRowTest, constructor_no_reference)
  30. {
  31. EXPECT_THROW(
  32. {
  33. try
  34. {
  35. SerializableSectionPairRow serializable(nullptr);
  36. }
  37. catch (const IllegalArgumentException &_exception)
  38. {
  39. throw;
  40. }
  41. },
  42. IllegalArgumentException);
  43. }
  44. TEST_F(SerializableSectionPairRowTest, getClassName)
  45. {
  46. ASSERT_STREQ("SerializableSectionPairRow", SerializableSectionPairRow{make_shared<SectionPairRow>("tmp-key", SectionPairRowEnumType::SECTION_PAIR_ROW_SINGLE_VALUE)}.getClassName().c_str());
  47. }
  48. TEST_F(SerializableSectionPairRowTest, getValue)
  49. {
  50. SerializableSectionPairRow serializable{make_shared<SectionPairRow>("tmp-key", SectionPairRowEnumType::SECTION_PAIR_ROW_SINGLE_VALUE)};
  51. ASSERT_TRUE(serializable.getValue() != nullptr);
  52. }
  53. TEST_F(SerializableSectionPairRowTest, marshal_single_value)
  54. {
  55. shared_ptr<SectionPairRow> row = make_shared<SectionPairRow>("favourite-color", SectionPairRowEnumType::SECTION_PAIR_ROW_SINGLE_VALUE);
  56. shared_ptr<SectionPairRowSingleValue> singleValue = dynamic_pointer_cast<SectionPairRowSingleValue>(row->getValue());
  57. singleValue->set("blue");
  58. SerializableSectionPairRow serializable{row};
  59. byte_field expected = "favourite-color=blue" + NewLine::get();
  60. byte_field actual = serializable.marshal();
  61. ASSERT_STREQ(expected.c_str(), actual.c_str());
  62. }
  63. TEST_F(SerializableSectionPairRowTest, marshal_list_value)
  64. {
  65. shared_ptr<SectionPairRow> row = make_shared<SectionPairRow>("favourite-colors", SectionPairRowEnumType::SECTION_PAIR_ROW_LIST_VALUE);
  66. shared_ptr<SectionPairRowListValue> listValue = dynamic_pointer_cast<SectionPairRowListValue>(row->getValue());
  67. listValue->add("blue");
  68. listValue->add("red");
  69. listValue->add("purple");
  70. SerializableSectionPairRow serializable{row};
  71. string expected = "favourite-colors:" + NewLine::get() + " blue" + NewLine::get() + " red" + NewLine::get() + " purple" + NewLine::get();
  72. ASSERT_STREQ(expected.c_str(), serializable.marshal().c_str());
  73. }
  74. TEST_F(SerializableSectionPairRowTest, unmarshal_single_value)
  75. {
  76. shared_ptr<SectionPairRow> row = make_shared<SectionPairRow>("tmp-key", SectionPairRowEnumType::SECTION_PAIR_ROW_SINGLE_VALUE);
  77. shared_ptr<SectionPairRowSingleValue> singleValue = dynamic_pointer_cast<SectionPairRowSingleValue>(row->getValue());
  78. SerializableSectionPairRow serializable{row};
  79. serializable.unmarshal("favourite-color=blue");
  80. ASSERT_STREQ("favourite-color", row->getKey().c_str());
  81. ASSERT_STREQ("blue", singleValue->get().c_str());
  82. }
  83. TEST_F(SerializableSectionPairRowTest, unmarshal_list_value)
  84. {
  85. shared_ptr<SectionPairRow> row = make_shared<SectionPairRow>("tmp-key", SectionPairRowEnumType::SECTION_PAIR_ROW_LIST_VALUE);
  86. shared_ptr<SectionPairRowListValue> listValue = dynamic_pointer_cast<SectionPairRowListValue>(row->getValue());
  87. SerializableSectionPairRow serializable{row};
  88. string data = "favourite-colors:" + NewLine::get() + " blue" + NewLine::get() + " red" + NewLine::get() + " purple" + NewLine::get();
  89. serializable.unmarshal(data);
  90. ASSERT_STREQ("favourite-colors", row->getKey().c_str());
  91. ASSERT_EQ(3, listValue->getSize());
  92. ASSERT_STREQ("blue", listValue->get(0).c_str());
  93. ASSERT_STREQ("red", listValue->get(1).c_str());
  94. ASSERT_STREQ("purple", listValue->get(2).c_str());
  95. }
  96. }