SerializableSectionPairRowTest.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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-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 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, getValue)
  45. {
  46. SerializableSectionPairRow serializable{make_shared<SectionPairRow>("tmp-key", SectionPairRowEnumType::SECTION_PAIR_ROW_SINGLE_VALUE)};
  47. ASSERT_TRUE(serializable.getValue() != nullptr);
  48. }
  49. TEST_F(SerializableSectionPairRowTest, marshal_single_value)
  50. {
  51. shared_ptr<SectionPairRow> row = make_shared<SectionPairRow>("favourite-color", SectionPairRowEnumType::SECTION_PAIR_ROW_SINGLE_VALUE);
  52. shared_ptr<SectionPairRowSingleValue> singleValue = dynamic_pointer_cast<SectionPairRowSingleValue>(row->getValue());
  53. singleValue->set("blue");
  54. SerializableSectionPairRow serializable{row};
  55. byte_field expected = "favourite-color=blue" + NewLine::get();
  56. byte_field actual = serializable.marshal();
  57. ASSERT_STREQ(expected.c_str(), actual.c_str());
  58. }
  59. TEST_F(SerializableSectionPairRowTest, marshal_list_value)
  60. {
  61. shared_ptr<SectionPairRow> row = make_shared<SectionPairRow>("favourite-colors", SectionPairRowEnumType::SECTION_PAIR_ROW_LIST_VALUE);
  62. shared_ptr<SectionPairRowListValue> listValue = dynamic_pointer_cast<SectionPairRowListValue>(row->getValue());
  63. listValue->add("blue");
  64. listValue->add("red");
  65. listValue->add("purple");
  66. SerializableSectionPairRow serializable{row};
  67. string expected = "favourite-colors:" + NewLine::get() + " blue" + NewLine::get() + " red" + NewLine::get() + " purple" + NewLine::get();
  68. ASSERT_STREQ(expected.c_str(), serializable.marshal().c_str());
  69. }
  70. TEST_F(SerializableSectionPairRowTest, unmarshal_single_value)
  71. {
  72. shared_ptr<SectionPairRow> row = make_shared<SectionPairRow>("tmp-key", SectionPairRowEnumType::SECTION_PAIR_ROW_SINGLE_VALUE);
  73. shared_ptr<SectionPairRowSingleValue> singleValue = dynamic_pointer_cast<SectionPairRowSingleValue>(row->getValue());
  74. SerializableSectionPairRow serializable{row};
  75. serializable.unmarshal("favourite-color=blue");
  76. ASSERT_STREQ("favourite-color", row->getKey().c_str());
  77. ASSERT_STREQ("blue", singleValue->get().c_str());
  78. }
  79. TEST_F(SerializableSectionPairRowTest, unmarshal_list_value)
  80. {
  81. shared_ptr<SectionPairRow> row = make_shared<SectionPairRow>("tmp-key", SectionPairRowEnumType::SECTION_PAIR_ROW_LIST_VALUE);
  82. shared_ptr<SectionPairRowListValue> listValue = dynamic_pointer_cast<SectionPairRowListValue>(row->getValue());
  83. SerializableSectionPairRow serializable{row};
  84. string data = "favourite-colors:" + NewLine::get() + " blue" + NewLine::get() + " red" + NewLine::get() + " purple" + NewLine::get();
  85. serializable.unmarshal(data);
  86. ASSERT_STREQ("favourite-colors", row->getKey().c_str());
  87. ASSERT_EQ(3, listValue->getSize());
  88. ASSERT_STREQ("blue", listValue->get(0).c_str());
  89. ASSERT_STREQ("red", listValue->get(1).c_str());
  90. ASSERT_STREQ("purple", listValue->get(2).c_str());
  91. }
  92. }