SectionPairSectionTest.cpp 6.5 KB

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