SectionPairSectionTest.cpp 7.6 KB

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