SerializableSectionPairRowTest.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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-13
  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::io;
  15. using namespace ::std;
  16. namespace
  17. {
  18. class SerializableSectionPairRowTest : public ::testing::Test
  19. {
  20. protected:
  21. SerializableSectionPairRowTest() = default;
  22. ~SerializableSectionPairRowTest() override = default;
  23. void SetUp() override
  24. {}
  25. void TearDown() override
  26. {}
  27. };
  28. TEST_F(SerializableSectionPairRowTest, constructor_no_reference)
  29. {
  30. EXPECT_THROW(
  31. {
  32. try
  33. {
  34. SerializableSectionPairRow serializable(nullptr);
  35. }
  36. catch (const IllegalArgumentException &_exception)
  37. {
  38. throw;
  39. }
  40. },
  41. IllegalArgumentException);
  42. }
  43. TEST_F(SerializableSectionPairRowTest, getValue)
  44. {
  45. SerializableSectionPairRow serializable{make_shared<SectionPairRow>("tmp-key", SectionPairRowEnumType::SECTION_PAIR_ROW_SINGLE_VALUE)};
  46. ASSERT_TRUE(serializable.getValue() != nullptr);
  47. }
  48. TEST_F(SerializableSectionPairRowTest, marshal_single_value)
  49. {
  50. shared_ptr<SectionPairRow> row = make_shared<SectionPairRow>("favourite-color", SectionPairRowEnumType::SECTION_PAIR_ROW_SINGLE_VALUE);
  51. shared_ptr<SectionPairRowSingleValue> singleValue = dynamic_pointer_cast<SectionPairRowSingleValue>(row->getValue());
  52. singleValue->set("blue");
  53. SerializableSectionPairRow serializable{row};
  54. ASSERT_STREQ("favourite-color=blue", serializable.marshal().c_str());
  55. }
  56. TEST_F(SerializableSectionPairRowTest, marshal_list_value)
  57. {
  58. shared_ptr<SectionPairRow> row = make_shared<SectionPairRow>("favourite-colors", SectionPairRowEnumType::SECTION_PAIR_ROW_LIST_VALUE);
  59. shared_ptr<SectionPairRowListValue> listValue = dynamic_pointer_cast<SectionPairRowListValue>(row->getValue());
  60. listValue->add("blue");
  61. listValue->add("red");
  62. listValue->add("purple");
  63. SerializableSectionPairRow serializable{row};
  64. string expected = "favourite-colors:" + NewLine::get() + " blue" + NewLine::get() + " red" + NewLine::get() + " purple" + NewLine::get();
  65. ASSERT_STREQ(expected.c_str(), serializable.marshal().c_str());
  66. }
  67. TEST_F(SerializableSectionPairRowTest, unmarshal_single_value)
  68. {
  69. shared_ptr<SectionPairRow> row = make_shared<SectionPairRow>("tmp-key", SectionPairRowEnumType::SECTION_PAIR_ROW_SINGLE_VALUE);
  70. shared_ptr<SectionPairRowSingleValue> singleValue = dynamic_pointer_cast<SectionPairRowSingleValue>(row->getValue());
  71. SerializableSectionPairRow serializable{row};
  72. serializable.unmarshal("favourite-color=blue");
  73. ASSERT_STREQ("favourite-color", row->getKey().c_str());
  74. ASSERT_STREQ("blue", singleValue->get().c_str());
  75. }
  76. TEST_F(SerializableSectionPairRowTest, unmarshal_list_value)
  77. {
  78. shared_ptr<SectionPairRow> row = make_shared<SectionPairRow>("tmp-key", SectionPairRowEnumType::SECTION_PAIR_ROW_LIST_VALUE);
  79. shared_ptr<SectionPairRowListValue> listValue = dynamic_pointer_cast<SectionPairRowListValue>(row->getValue());
  80. SerializableSectionPairRow serializable{row};
  81. string data = "favourite-colors:" + NewLine::get() + " blue" + NewLine::get() + " red" + NewLine::get() + " purple" + NewLine::get();
  82. serializable.unmarshal(data);
  83. ASSERT_STREQ("favourite-colors", row->getKey().c_str());
  84. ASSERT_EQ(3, listValue->getSize());
  85. ASSERT_STREQ("blue", listValue->get(0).c_str());
  86. ASSERT_STREQ("red", listValue->get(1).c_str());
  87. ASSERT_STREQ("purple", listValue->get(2).c_str());
  88. }
  89. }