SerializableSectionPairRowListValueTest.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2023-02-11
  6. * Changed: 2023-05-18
  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 ls::std::core::IllegalArgumentException;
  14. using ls::std::io::NewLine;
  15. using ls::std::io::SectionPairRowListValue;
  16. using ls::std::io::SerializableSectionPairParameter;
  17. using ls::std::io::SerializableSectionPairRowListValue;
  18. using std::make_shared;
  19. using std::shared_ptr;
  20. using std::string;
  21. using testing::Test;
  22. using testing::TestWithParam;
  23. using testing::Values;
  24. namespace
  25. {
  26. class SerializableSectionPairRowListValueTest : public Test
  27. {
  28. public:
  29. SerializableSectionPairRowListValueTest() = default;
  30. ~SerializableSectionPairRowListValueTest() override = default;
  31. };
  32. class SerializableSectionPairRowListValueTest_LineBreakTest : public TestWithParam<string>
  33. {
  34. public:
  35. SerializableSectionPairRowListValueTest_LineBreakTest() = default;
  36. ~SerializableSectionPairRowListValueTest_LineBreakTest() override = default;
  37. };
  38. TEST_F(SerializableSectionPairRowListValueTest, constructor_no_reference)
  39. {
  40. SerializableSectionPairParameter parameter{};
  41. EXPECT_THROW(
  42. {
  43. try
  44. {
  45. SerializableSectionPairRowListValue serializable(parameter);
  46. }
  47. catch (const IllegalArgumentException &_exception)
  48. {
  49. throw;
  50. }
  51. },
  52. IllegalArgumentException);
  53. }
  54. TEST_F(SerializableSectionPairRowListValueTest, getClassName)
  55. {
  56. SerializableSectionPairParameter parameter{};
  57. parameter.setValue(make_shared<SectionPairRowListValue>());
  58. ASSERT_STREQ("SerializableSectionPairRowListValue", SerializableSectionPairRowListValue{parameter}.getClassName().c_str());
  59. }
  60. TEST_F(SerializableSectionPairRowListValueTest, getValue)
  61. {
  62. SerializableSectionPairParameter parameter{};
  63. auto value = make_shared<SectionPairRowListValue>();
  64. parameter.setValue(value);
  65. SerializableSectionPairRowListValue serializable(parameter);
  66. ASSERT_TRUE(value == serializable.getValue());
  67. }
  68. TEST_P(SerializableSectionPairRowListValueTest_LineBreakTest, marshal)
  69. {
  70. SerializableSectionPairParameter parameter{};
  71. auto value = make_shared<SectionPairRowListValue>();
  72. value->add("Drumming");
  73. value->add("Reading");
  74. value->add("Coding");
  75. parameter.setValue(value);
  76. string newLine = GetParam();
  77. parameter.setNewLine(newLine);
  78. SerializableSectionPairRowListValue serializable(parameter);
  79. string expected = " Drumming" + newLine + " Reading" + newLine + " Coding" + newLine;
  80. ASSERT_STREQ(expected.c_str(), serializable.marshal().c_str());
  81. }
  82. TEST_P(SerializableSectionPairRowListValueTest_LineBreakTest, unmarshal)
  83. {
  84. SerializableSectionPairParameter parameter{};
  85. auto value = make_shared<SectionPairRowListValue>();
  86. parameter.setValue(value);
  87. string newLine = GetParam();
  88. parameter.setNewLine(newLine);
  89. SerializableSectionPairRowListValue serializable(parameter);
  90. string serializedListValue = " Drumming" + newLine + " Reading" + newLine + " Coding" + newLine;
  91. serializable.unmarshal(serializedListValue);
  92. ASSERT_EQ(3, value->getSize());
  93. ASSERT_STREQ("Drumming", value->get(0).c_str());
  94. ASSERT_STREQ("Reading", value->get(1).c_str());
  95. ASSERT_STREQ("Coding", value->get(2).c_str());
  96. }
  97. TEST_F(SerializableSectionPairRowListValueTest, unmarshal_empty_string)
  98. {
  99. SerializableSectionPairParameter parameter{};
  100. parameter.setValue(make_shared<SectionPairRowListValue>());
  101. SerializableSectionPairRowListValue serializable(parameter);
  102. EXPECT_THROW(
  103. {
  104. try
  105. {
  106. serializable.unmarshal("");
  107. }
  108. catch (const IllegalArgumentException &_exception)
  109. {
  110. throw;
  111. }
  112. },
  113. IllegalArgumentException);
  114. }
  115. INSTANTIATE_TEST_SUITE_P(LineBreakTest, SerializableSectionPairRowListValueTest_LineBreakTest, Values(NewLine::getUnixNewLine(), NewLine::getWindowsNewLine()));
  116. }