SectionPairRowTest.cpp 6.1 KB

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