SectionPairSectionTest.cpp 6.5 KB

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