SerializableSectionPairRowSingleValueTest.cpp 3.9 KB

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