SectionPairSectionTest.cpp 8.1 KB

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