SectionPairRowTest.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2023-02-08
  6. * Changed: 2025-12-21
  7. *
  8. * */
  9. #include <gtest/gtest.h>
  10. #include <ls-std/ls-std-core.hpp>
  11. #include <ls-std/ls-std-io.hpp>
  12. using ls::std::core::IllegalArgumentException;
  13. using ls::std::core::type::byte_field;
  14. using ls::std::io::NewLine;
  15. using ls::std::io::SectionPairRow;
  16. using ls::std::io::SectionPairRowEnumType;
  17. using ls::std::io::SectionPairRowListValue;
  18. using ls::std::io::SectionPairRowSingleValue;
  19. using ls::std::io::SectionPairRowValue;
  20. using ls::std::io::section_pair_identifier;
  21. using std::dynamic_pointer_cast;
  22. using std::make_shared;
  23. using std::shared_ptr;
  24. using std::string;
  25. using testing::Test;
  26. namespace
  27. {
  28. class SectionPairRowTest : public Test
  29. {
  30. public:
  31. SectionPairRowTest() = default;
  32. ~SectionPairRowTest() override = default;
  33. };
  34. TEST_F(SectionPairRowTest, constructor_empty_key)
  35. {
  36. EXPECT_THROW(
  37. {
  38. try
  39. {
  40. SectionPairRow row("", SectionPairRowEnumType::SECTION_PAIR_ROW_SINGLE_VALUE);
  41. }
  42. catch (const IllegalArgumentException &_exception)
  43. {
  44. throw;
  45. }
  46. },
  47. IllegalArgumentException);
  48. }
  49. TEST_F(SectionPairRowTest, constructor_invalid_key)
  50. {
  51. EXPECT_THROW(
  52. {
  53. try
  54. {
  55. SectionPairRow row("-tmp-key", SectionPairRowEnumType::SECTION_PAIR_ROW_SINGLE_VALUE);
  56. }
  57. catch (const IllegalArgumentException &_exception)
  58. {
  59. throw;
  60. }
  61. },
  62. IllegalArgumentException);
  63. }
  64. TEST_F(SectionPairRowTest, getClassName)
  65. {
  66. ASSERT_STREQ("SectionPairRow", SectionPairRow("tmp-key", SectionPairRowEnumType::SECTION_PAIR_ROW_LIST_VALUE).getClassName().c_str());
  67. }
  68. TEST_F(SectionPairRowTest, getKey)
  69. {
  70. const section_pair_identifier key = "tmp-key";
  71. const SectionPairRow row(key, SectionPairRowEnumType::SECTION_PAIR_ROW_SINGLE_VALUE);
  72. ASSERT_STREQ(key.c_str(), row.getKey().c_str());
  73. }
  74. TEST_F(SectionPairRowTest, getValue)
  75. {
  76. const SectionPairRow row("hobbies", SectionPairRowEnumType::SECTION_PAIR_ROW_LIST_VALUE);
  77. const shared_ptr<SectionPairRowValue> value = row.getValue();
  78. ASSERT_TRUE(value != nullptr);
  79. ASSERT_EQ(SectionPairRowEnumType::SECTION_PAIR_ROW_LIST_VALUE, value->getType());
  80. }
  81. TEST_F(SectionPairRowTest, isSingleValue)
  82. {
  83. ASSERT_TRUE(SectionPairRow("tmp-key", SectionPairRowEnumType::SECTION_PAIR_ROW_SINGLE_VALUE).isSingleValue());
  84. }
  85. TEST_F(SectionPairRowTest, isList)
  86. {
  87. ASSERT_FALSE(SectionPairRow("tmp-key", SectionPairRowEnumType::SECTION_PAIR_ROW_SINGLE_VALUE).isList());
  88. }
  89. TEST_F(SectionPairRowTest, marshal_single_value)
  90. {
  91. const auto row = make_shared<SectionPairRow>("favourite-color", SectionPairRowEnumType::SECTION_PAIR_ROW_SINGLE_VALUE);
  92. const shared_ptr<SectionPairRowSingleValue> singleValue = dynamic_pointer_cast<SectionPairRowSingleValue>(row->getValue());
  93. singleValue->set("blue");
  94. const byte_field expected = "favourite-color=blue" + NewLine::get();
  95. const byte_field actual = row->marshal();
  96. ASSERT_STREQ(expected.c_str(), actual.c_str());
  97. }
  98. TEST_F(SectionPairRowTest, marshal_list_value)
  99. {
  100. const auto row = make_shared<SectionPairRow>("favourite-colors", SectionPairRowEnumType::SECTION_PAIR_ROW_LIST_VALUE);
  101. const shared_ptr<SectionPairRowListValue> listValue = dynamic_pointer_cast<SectionPairRowListValue>(row->getValue());
  102. listValue->add("blue");
  103. listValue->add("red");
  104. listValue->add("purple");
  105. const string newLine = NewLine::get();
  106. const string expected = "favourite-colors:" + newLine + " blue" + newLine + " red" + newLine + " purple" + newLine;
  107. ASSERT_STREQ(expected.c_str(), row->marshal().c_str());
  108. }
  109. TEST_F(SectionPairRowTest, setKey)
  110. {
  111. SectionPairRow row("hobbies", SectionPairRowEnumType::SECTION_PAIR_ROW_LIST_VALUE);
  112. row.setKey("colors");
  113. ASSERT_STREQ("colors", row.getKey().c_str());
  114. }
  115. TEST_F(SectionPairRowTest, setKey_empty_key)
  116. {
  117. SectionPairRow row("tmp-key", SectionPairRowEnumType::SECTION_PAIR_ROW_SINGLE_VALUE);
  118. EXPECT_THROW(
  119. {
  120. try
  121. {
  122. row.setKey("");
  123. }
  124. catch (const IllegalArgumentException &_exception)
  125. {
  126. throw;
  127. }
  128. },
  129. IllegalArgumentException);
  130. }
  131. TEST_F(SectionPairRowTest, setKey_invalid_key)
  132. {
  133. SectionPairRow row("tmp-key", SectionPairRowEnumType::SECTION_PAIR_ROW_SINGLE_VALUE);
  134. EXPECT_THROW(
  135. {
  136. try
  137. {
  138. row.setKey("=33");
  139. }
  140. catch (const IllegalArgumentException &_exception)
  141. {
  142. throw;
  143. }
  144. },
  145. IllegalArgumentException);
  146. }
  147. TEST_F(SectionPairRowTest, unmarshal_single_value)
  148. {
  149. const auto row = make_shared<SectionPairRow>("tmp-key", SectionPairRowEnumType::SECTION_PAIR_ROW_SINGLE_VALUE);
  150. const shared_ptr<SectionPairRowSingleValue> singleValue = dynamic_pointer_cast<SectionPairRowSingleValue>(row->getValue());
  151. row->unmarshal("favourite-color=blue");
  152. ASSERT_STREQ("favourite-color", row->getKey().c_str());
  153. ASSERT_STREQ("blue", singleValue->get().c_str());
  154. }
  155. TEST_F(SectionPairRowTest, unmarshal_list_value)
  156. {
  157. const auto row = make_shared<SectionPairRow>("tmp-key", SectionPairRowEnumType::SECTION_PAIR_ROW_LIST_VALUE);
  158. const shared_ptr<SectionPairRowListValue> listValue = dynamic_pointer_cast<SectionPairRowListValue>(row->getValue());
  159. const string newLine = NewLine::get();
  160. const string data = "favourite-colors:" + newLine + " blue" + newLine + " red" + newLine + " purple" + newLine;
  161. row->unmarshal(data);
  162. ASSERT_STREQ("favourite-colors", row->getKey().c_str());
  163. ASSERT_STREQ("blue", listValue->get(0).c_str());
  164. ASSERT_STREQ("red", listValue->get(1).c_str());
  165. ASSERT_STREQ("purple", listValue->get(2).c_str());
  166. ASSERT_EQ(3, listValue->getSize());
  167. }
  168. }