SectionPairSectionTest.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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-15
  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, getClassName)
  61. {
  62. ASSERT_STREQ("SectionPairSection", SectionPairSection{"general"}.getClassName().c_str());
  63. }
  64. TEST_F(SectionPairSectionTest, add)
  65. {
  66. shared_ptr<SectionPairSection> section = make_shared<SectionPairSection>("general");
  67. section->add(make_shared<SectionPairRow>("color", SectionPairRowEnumType::SECTION_PAIR_ROW_SINGLE_VALUE));
  68. ASSERT_EQ(1, section->getRowAmount());
  69. }
  70. TEST_F(SectionPairSectionTest, add_row_already_exists)
  71. {
  72. shared_ptr<SectionPairSection> section = make_shared<SectionPairSection>("general");
  73. section->add(make_shared<SectionPairRow>("color", SectionPairRowEnumType::SECTION_PAIR_ROW_SINGLE_VALUE));
  74. EXPECT_THROW(
  75. {
  76. try
  77. {
  78. section->add(make_shared<SectionPairRow>("color", SectionPairRowEnumType::SECTION_PAIR_ROW_SINGLE_VALUE));
  79. }
  80. catch (const IllegalArgumentException &_exception)
  81. {
  82. throw;
  83. }
  84. },
  85. IllegalArgumentException);
  86. }
  87. TEST_F(SectionPairSectionTest, get)
  88. {
  89. shared_ptr<SectionPairSection> section = make_shared<SectionPairSection>("general");
  90. section->add(make_shared<SectionPairRow>("color", SectionPairRowEnumType::SECTION_PAIR_ROW_SINGLE_VALUE));
  91. ASSERT_TRUE(section->get(0) != nullptr);
  92. }
  93. TEST_F(SectionPairSectionTest, get_out_of_bounds)
  94. {
  95. shared_ptr<SectionPairSection> section = make_shared<SectionPairSection>("general");
  96. EXPECT_THROW(
  97. {
  98. try
  99. {
  100. section_pair_row_list_element element = section->get(1);
  101. }
  102. catch (const IndexOutOfBoundsException &_exception)
  103. {
  104. throw;
  105. }
  106. },
  107. IndexOutOfBoundsException);
  108. }
  109. TEST_F(SectionPairSectionTest, getAmount)
  110. {
  111. shared_ptr<SectionPairSection> section = make_shared<SectionPairSection>("general");
  112. ASSERT_EQ(0, section->getRowAmount());
  113. }
  114. TEST_F(SectionPairSectionTest, getList)
  115. {
  116. shared_ptr<SectionPairSection> section = make_shared<SectionPairSection>("general");
  117. ASSERT_TRUE(section->getList().empty());
  118. }
  119. TEST_F(SectionPairSectionTest, getSectionId)
  120. {
  121. shared_ptr<SectionPairSection> section = make_shared<SectionPairSection>("general");
  122. ASSERT_STREQ("general", section->getSectionId().c_str());
  123. }
  124. TEST_F(SectionPairSectionTest, marshal)
  125. {
  126. shared_ptr<SectionPairSection> section = SectionPairSectionProvider::createSectionWithTomExample();
  127. byte_field expected = SectionPairSectionProvider::createSerializedSectionWithTomExample();
  128. byte_field actual = section->marshal();
  129. ASSERT_STREQ(expected.c_str(), actual.c_str());
  130. }
  131. TEST_F(SectionPairSectionTest, setSectionId)
  132. {
  133. shared_ptr<SectionPairSection> section = make_shared<SectionPairSection>("general");
  134. section->setSectionId("personal");
  135. ASSERT_STREQ("personal", section->getSectionId().c_str());
  136. }
  137. TEST_F(SectionPairSectionTest, setSectionId_empty_id)
  138. {
  139. shared_ptr<SectionPairSection> section = make_shared<SectionPairSection>("general");
  140. EXPECT_THROW(
  141. {
  142. try
  143. {
  144. section->setSectionId("");
  145. }
  146. catch (const IllegalArgumentException &_exception)
  147. {
  148. throw;
  149. }
  150. },
  151. IllegalArgumentException);
  152. }
  153. TEST_F(SectionPairSectionTest, setSectionId_invalid_id)
  154. {
  155. shared_ptr<SectionPairSection> section = make_shared<SectionPairSection>("general");
  156. EXPECT_THROW(
  157. {
  158. try
  159. {
  160. section->setSectionId("PERSONAL");
  161. }
  162. catch (const IllegalArgumentException &_exception)
  163. {
  164. throw;
  165. }
  166. },
  167. IllegalArgumentException);
  168. }
  169. TEST_F(SectionPairSectionTest, unmarshal)
  170. {
  171. shared_ptr<SectionPairSection> section = make_shared<SectionPairSection>("tmp-id");
  172. section->unmarshal(SectionPairSectionProvider::createSerializedSectionWithTomExample());
  173. ASSERT_STREQ("general", section->getSectionId().c_str());
  174. ASSERT_EQ(3, section->getRowAmount());
  175. ASSERT_STREQ("name", section->get(0)->getKey().c_str());
  176. ASSERT_STREQ("Tom", dynamic_pointer_cast<SectionPairRowSingleValue>(section->get(0)->getValue())->get().c_str());
  177. ASSERT_STREQ("jobs", section->get(1)->getKey().c_str());
  178. shared_ptr<SectionPairRowListValue> listRow = dynamic_pointer_cast<SectionPairRowListValue>(section->get(1)->getValue());
  179. ASSERT_EQ(2, listRow->getSize());
  180. ASSERT_STREQ("Farmer", listRow->get(0).c_str());
  181. ASSERT_STREQ("Bounty Hunter", listRow->get(1).c_str());
  182. ASSERT_STREQ("age", section->get(2)->getKey().c_str());
  183. ASSERT_STREQ("33", dynamic_pointer_cast<SectionPairRowSingleValue>(section->get(2)->getValue())->get().c_str());
  184. }
  185. }