SectionPairSectionTest.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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)
  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_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, getAmount)
  124. {
  125. shared_ptr<SectionPairSection> section = make_shared<SectionPairSection>("general");
  126. ASSERT_EQ(0, section->getRowAmount());
  127. }
  128. TEST_F(SectionPairSectionTest, getClassName)
  129. {
  130. ASSERT_STREQ("SectionPairSection", SectionPairSection{"general"}.getClassName().c_str());
  131. }
  132. TEST_F(SectionPairSectionTest, getList)
  133. {
  134. shared_ptr<SectionPairSection> section = make_shared<SectionPairSection>("general");
  135. ASSERT_TRUE(section->getList().empty());
  136. }
  137. TEST_F(SectionPairSectionTest, getSectionId)
  138. {
  139. shared_ptr<SectionPairSection> section = make_shared<SectionPairSection>("general");
  140. ASSERT_STREQ("general", section->getSectionId().c_str());
  141. }
  142. TEST_F(SectionPairSectionTest, marshal)
  143. {
  144. shared_ptr<SectionPairSection> section = SectionPairSectionProvider::createSectionWithTomExample();
  145. byte_field expected = SectionPairSectionProvider::createSerializedSectionWithTomExample(NewLine::get());
  146. byte_field actual = section->marshal();
  147. ASSERT_STREQ(expected.c_str(), actual.c_str());
  148. }
  149. TEST_F(SectionPairSectionTest, setSectionId)
  150. {
  151. shared_ptr<SectionPairSection> section = make_shared<SectionPairSection>("general");
  152. section->setSectionId("personal");
  153. ASSERT_STREQ("personal", section->getSectionId().c_str());
  154. }
  155. TEST_F(SectionPairSectionTest, setSectionId_empty_id)
  156. {
  157. shared_ptr<SectionPairSection> section = make_shared<SectionPairSection>("general");
  158. EXPECT_THROW(
  159. {
  160. try
  161. {
  162. section->setSectionId("");
  163. }
  164. catch (const IllegalArgumentException &_exception)
  165. {
  166. throw;
  167. }
  168. },
  169. IllegalArgumentException);
  170. }
  171. TEST_F(SectionPairSectionTest, setSectionId_invalid_id)
  172. {
  173. shared_ptr<SectionPairSection> section = make_shared<SectionPairSection>("general");
  174. EXPECT_THROW(
  175. {
  176. try
  177. {
  178. section->setSectionId("PERSONAL");
  179. }
  180. catch (const IllegalArgumentException &_exception)
  181. {
  182. throw;
  183. }
  184. },
  185. IllegalArgumentException);
  186. }
  187. TEST_F(SectionPairSectionTest, unmarshal)
  188. {
  189. shared_ptr<SectionPairSection> section = make_shared<SectionPairSection>("tmp-id");
  190. section->unmarshal(SectionPairSectionProvider::createSerializedSectionWithTomExample(NewLine::get()));
  191. ASSERT_STREQ("general", section->getSectionId().c_str());
  192. ASSERT_EQ(3, section->getRowAmount());
  193. ASSERT_STREQ("name", section->get(0)->getKey().c_str());
  194. ASSERT_STREQ("Tom", dynamic_pointer_cast<SectionPairRowSingleValue>(section->get(0)->getValue())->get().c_str());
  195. ASSERT_STREQ("jobs", section->get(1)->getKey().c_str());
  196. shared_ptr<SectionPairRowListValue> listRow = dynamic_pointer_cast<SectionPairRowListValue>(section->get(1)->getValue());
  197. ASSERT_EQ(2, listRow->getSize());
  198. ASSERT_STREQ("Farmer", listRow->get(0).c_str());
  199. ASSERT_STREQ("Bounty Hunter", listRow->get(1).c_str());
  200. ASSERT_STREQ("age", section->get(2)->getKey().c_str());
  201. ASSERT_STREQ("33", dynamic_pointer_cast<SectionPairRowSingleValue>(section->get(2)->getValue())->get().c_str());
  202. }
  203. }