SectionPairSectionTest.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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-13
  7. * Changed: 2026-06-23
  8. *
  9. * */
  10. #include <gtest/gtest.h>
  11. #include <ls-std-io-test.hpp>
  12. #include <ls-std/ls-std-core.hpp>
  13. #include <ls-std/ls-std-io.hpp>
  14. using ls::standard::core::IllegalArgumentException;
  15. using ls::standard::core::IndexOutOfBoundsException;
  16. using ls::standard::core::type::byte_field;
  17. using ls::standard::io::NewLine;
  18. using ls::standard::io::section_pair_row_list_element;
  19. using ls::standard::io::SectionPairRow;
  20. using ls::standard::io::SectionPairRowEnumType;
  21. using ls::standard::io::SectionPairRowListValue;
  22. using ls::standard::io::SectionPairRowSingleValue;
  23. using ls::standard::io::SectionPairSection;
  24. using std::dynamic_pointer_cast;
  25. using std::make_shared;
  26. using std::shared_ptr;
  27. using test::io::SectionPairSectionProvider;
  28. using testing::Test;
  29. namespace
  30. {
  31. class SectionPairSectionTest : public Test
  32. {
  33. public:
  34. SectionPairSectionTest() = default;
  35. ~SectionPairSectionTest() override = default;
  36. };
  37. TEST_F(SectionPairSectionTest, constructor_empty_id)
  38. {
  39. EXPECT_THROW(
  40. {
  41. try
  42. {
  43. SectionPairSection section{""};
  44. }
  45. catch (const IllegalArgumentException &_exception)
  46. {
  47. throw;
  48. }
  49. },
  50. IllegalArgumentException);
  51. }
  52. TEST_F(SectionPairSectionTest, constructor_invalid_id)
  53. {
  54. EXPECT_THROW(
  55. {
  56. try
  57. {
  58. auto section = SectionPairSection{"SERVER"};
  59. }
  60. catch (const IllegalArgumentException &_exception)
  61. {
  62. throw;
  63. }
  64. },
  65. IllegalArgumentException);
  66. }
  67. TEST_F(SectionPairSectionTest, add)
  68. {
  69. const auto section = make_shared<SectionPairSection>("general");
  70. section->add(make_shared<SectionPairRow>("color", SectionPairRowEnumType::SECTION_PAIR_ROW_SINGLE_VALUE));
  71. ASSERT_EQ(1, section->getRowAmount());
  72. }
  73. TEST_F(SectionPairSectionTest, add_row_already_exists)
  74. {
  75. const auto section = make_shared<SectionPairSection>("general");
  76. section->add(make_shared<SectionPairRow>("color", SectionPairRowEnumType::SECTION_PAIR_ROW_SINGLE_VALUE));
  77. EXPECT_THROW(
  78. {
  79. try
  80. {
  81. section->add(make_shared<SectionPairRow>("color", SectionPairRowEnumType::SECTION_PAIR_ROW_SINGLE_VALUE));
  82. }
  83. catch (const IllegalArgumentException &_exception)
  84. {
  85. throw;
  86. }
  87. },
  88. IllegalArgumentException);
  89. }
  90. TEST_F(SectionPairSectionTest, clear)
  91. {
  92. const auto section = make_shared<SectionPairSection>("general");
  93. section->add(make_shared<SectionPairRow>("color", SectionPairRowEnumType::SECTION_PAIR_ROW_SINGLE_VALUE));
  94. ASSERT_EQ(1, section->getRowAmount());
  95. section->clear();
  96. ASSERT_TRUE(section->getList().empty());
  97. }
  98. TEST_F(SectionPairSectionTest, get_by_index)
  99. {
  100. const auto section = make_shared<SectionPairSection>("general");
  101. section->add(make_shared<SectionPairRow>("color", SectionPairRowEnumType::SECTION_PAIR_ROW_SINGLE_VALUE));
  102. ASSERT_TRUE(section->get(0) != nullptr);
  103. }
  104. TEST_F(SectionPairSectionTest, get_by_index_out_of_bounds)
  105. {
  106. const auto section = make_shared<SectionPairSection>("general");
  107. EXPECT_THROW(
  108. {
  109. try
  110. {
  111. section_pair_row_list_element element = section->get(1);
  112. }
  113. catch (const IndexOutOfBoundsException &_exception)
  114. {
  115. throw;
  116. }
  117. },
  118. IndexOutOfBoundsException);
  119. }
  120. TEST_F(SectionPairSectionTest, get_by_key)
  121. {
  122. const auto section = make_shared<SectionPairSection>("general");
  123. section->add(make_shared<SectionPairRow>("color", SectionPairRowEnumType::SECTION_PAIR_ROW_SINGLE_VALUE));
  124. ASSERT_TRUE(section->get("color") != nullptr);
  125. }
  126. TEST_F(SectionPairSectionTest, get_by_key_not_found)
  127. {
  128. const auto section = make_shared<SectionPairSection>("general");
  129. section->add(make_shared<SectionPairRow>("color", SectionPairRowEnumType::SECTION_PAIR_ROW_SINGLE_VALUE));
  130. ASSERT_TRUE(section->get("hobbies") == nullptr);
  131. }
  132. TEST_F(SectionPairSectionTest, getAmount)
  133. {
  134. const auto section = make_shared<SectionPairSection>("general");
  135. ASSERT_EQ(0, section->getRowAmount());
  136. }
  137. TEST_F(SectionPairSectionTest, getClassName)
  138. {
  139. ASSERT_STREQ("SectionPairSection", SectionPairSection{"general"}.getClassName().c_str());
  140. }
  141. TEST_F(SectionPairSectionTest, getList)
  142. {
  143. const auto section = make_shared<SectionPairSection>("general");
  144. ASSERT_TRUE(section->getList().empty());
  145. }
  146. TEST_F(SectionPairSectionTest, getSectionId)
  147. {
  148. const auto section = make_shared<SectionPairSection>("general");
  149. ASSERT_STREQ("general", section->getSectionId().c_str());
  150. }
  151. TEST_F(SectionPairSectionTest, hasRow)
  152. {
  153. const auto section = make_shared<SectionPairSection>("general");
  154. section->add(make_shared<SectionPairRow>("color", SectionPairRowEnumType::SECTION_PAIR_ROW_SINGLE_VALUE));
  155. ASSERT_TRUE(section->hasRow("color"));
  156. }
  157. TEST_F(SectionPairSectionTest, hasRow_not_found)
  158. {
  159. const auto section = make_shared<SectionPairSection>("general");
  160. section->add(make_shared<SectionPairRow>("color", SectionPairRowEnumType::SECTION_PAIR_ROW_SINGLE_VALUE));
  161. ASSERT_FALSE(section->hasRow("hobbies"));
  162. }
  163. TEST_F(SectionPairSectionTest, marshal)
  164. {
  165. shared_ptr<SectionPairSection> section = SectionPairSectionProvider::createSectionWithTomExample();
  166. const byte_field expected = SectionPairSectionProvider::createSerializedSectionWithTomExample(NewLine::get());
  167. const byte_field actual = section->marshal();
  168. ASSERT_STREQ(expected.c_str(), actual.c_str());
  169. }
  170. TEST_F(SectionPairSectionTest, setSectionId)
  171. {
  172. const auto section = make_shared<SectionPairSection>("general");
  173. section->setSectionId("personal");
  174. ASSERT_STREQ("personal", section->getSectionId().c_str());
  175. }
  176. TEST_F(SectionPairSectionTest, setSectionId_empty_id)
  177. {
  178. const auto section = make_shared<SectionPairSection>("general");
  179. EXPECT_THROW(
  180. {
  181. try
  182. {
  183. section->setSectionId("");
  184. }
  185. catch (const IllegalArgumentException &_exception)
  186. {
  187. throw;
  188. }
  189. },
  190. IllegalArgumentException);
  191. }
  192. TEST_F(SectionPairSectionTest, setSectionId_invalid_id)
  193. {
  194. const auto section = make_shared<SectionPairSection>("general");
  195. EXPECT_THROW(
  196. {
  197. try
  198. {
  199. section->setSectionId("PERSONAL");
  200. }
  201. catch (const IllegalArgumentException &_exception)
  202. {
  203. throw;
  204. }
  205. },
  206. IllegalArgumentException);
  207. }
  208. TEST_F(SectionPairSectionTest, unmarshal)
  209. {
  210. const auto section = make_shared<SectionPairSection>("tmp-id");
  211. section->unmarshal(SectionPairSectionProvider::createSerializedSectionWithTomExample(NewLine::get()));
  212. ASSERT_STREQ("general", section->getSectionId().c_str());
  213. ASSERT_EQ(3, section->getRowAmount());
  214. ASSERT_STREQ("name", section->get(0)->getKey().c_str());
  215. ASSERT_STREQ("Tom", dynamic_pointer_cast<SectionPairRowSingleValue>(section->get(0)->getValue())->get().c_str());
  216. ASSERT_STREQ("jobs", section->get(1)->getKey().c_str());
  217. const shared_ptr<SectionPairRowListValue> listRow = dynamic_pointer_cast<SectionPairRowListValue>(section->get(1)->getValue());
  218. ASSERT_EQ(2, listRow->getSize());
  219. ASSERT_STREQ("Farmer", listRow->get(0).c_str());
  220. ASSERT_STREQ("Bounty Hunter", listRow->get(1).c_str());
  221. ASSERT_STREQ("age", section->get(2)->getKey().c_str());
  222. ASSERT_STREQ("33", dynamic_pointer_cast<SectionPairRowSingleValue>(section->get(2)->getValue())->get().c_str());
  223. }
  224. }