SerializableSectionPairRowSingleValueTest.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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::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::Values;
  25. namespace
  26. {
  27. class SerializableSectionPairRowSingleValueTest : public Test
  28. {
  29. protected:
  30. SerializableSectionPairRowSingleValueTest() = default;
  31. ~SerializableSectionPairRowSingleValueTest() override = default;
  32. void SetUp() override
  33. {}
  34. void TearDown() override
  35. {}
  36. };
  37. class SerializableSectionPairRowSingleValueTest_LineBreakTest : public ::testing::TestWithParam<string>
  38. {
  39. protected:
  40. SerializableSectionPairRowSingleValueTest_LineBreakTest() = default;
  41. ~SerializableSectionPairRowSingleValueTest_LineBreakTest() override = default;
  42. };
  43. TEST_F(SerializableSectionPairRowSingleValueTest, constructor_no_reference)
  44. {
  45. SerializableSectionPairParameter parameter{};
  46. EXPECT_THROW(
  47. {
  48. try
  49. {
  50. SerializableSectionPairRowSingleValue serializable(parameter);
  51. }
  52. catch (const IllegalArgumentException &_exception)
  53. {
  54. throw;
  55. }
  56. },
  57. IllegalArgumentException);
  58. }
  59. TEST_F(SerializableSectionPairRowSingleValueTest, getClassName)
  60. {
  61. SerializableSectionPairParameter parameter{};
  62. parameter.setValue(make_shared<SectionPairRowSingleValue>("empty"));
  63. ASSERT_STREQ("SerializableSectionPairRowSingleValue", SerializableSectionPairRowSingleValue{parameter}.getClassName().c_str());
  64. }
  65. TEST_F(SerializableSectionPairRowSingleValueTest, getValue)
  66. {
  67. SerializableSectionPairParameter parameter{};
  68. shared_ptr<SectionPairRowSingleValue> value = make_shared<SectionPairRowSingleValue>("empty");
  69. parameter.setValue(value);
  70. SerializableSectionPairRowSingleValue serializable(parameter);
  71. ASSERT_TRUE(value == serializable.getValue());
  72. }
  73. TEST_P(SerializableSectionPairRowSingleValueTest_LineBreakTest, marshal)
  74. {
  75. string newLine = GetParam();
  76. SerializableSectionPairParameter parameter{};
  77. parameter.setValue(make_shared<SectionPairRowSingleValue>("empty"));
  78. parameter.setNewLine(newLine);
  79. SerializableSectionPairRowSingleValue serializable(parameter);
  80. byte_field expected = "empty" + newLine;
  81. byte_field actual = serializable.marshal();
  82. ASSERT_STREQ(expected.c_str(), actual.c_str());
  83. }
  84. TEST_F(SerializableSectionPairRowSingleValueTest, unmarshal)
  85. {
  86. SerializableSectionPairParameter parameter{};
  87. parameter.setValue(make_shared<SectionPairRowSingleValue>("empty"));
  88. SerializableSectionPairRowSingleValue serializable(parameter);
  89. serializable.unmarshal("blue");
  90. ASSERT_STREQ("blue", dynamic_pointer_cast<SectionPairRowSingleValue>(parameter.getValue())->get().c_str());
  91. }
  92. TEST_F(SerializableSectionPairRowSingleValueTest, unmarshal_empty_string)
  93. {
  94. SerializableSectionPairParameter parameter{};
  95. parameter.setValue(make_shared<SectionPairRowSingleValue>("empty"));
  96. SerializableSectionPairRowSingleValue serializable(parameter);
  97. EXPECT_THROW(
  98. {
  99. try
  100. {
  101. serializable.unmarshal("");
  102. }
  103. catch (const IllegalArgumentException &_exception)
  104. {
  105. throw;
  106. }
  107. },
  108. IllegalArgumentException);
  109. }
  110. INSTANTIATE_TEST_SUITE_P(LineBreakTest, SerializableSectionPairRowSingleValueTest_LineBreakTest, Values(NewLine::getUnixNewLine(), NewLine::getWindowsNewLine()));
  111. }