SerializableSectionPairRowListValueTest.cpp 4.3 KB

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