SectionPairRowTest.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2023-02-08
  6. * Changed: 2023-02-13
  7. *
  8. * */
  9. #include <gtest/gtest.h>
  10. #include <ls-std/ls-std-core.hpp>
  11. #include <ls-std/ls-std-io.hpp>
  12. using namespace ls::std::core;
  13. using namespace ls::std::core::type;
  14. using namespace ls::std::io;
  15. using namespace ::std;
  16. namespace
  17. {
  18. class SectionPairRowTest : public ::testing::Test
  19. {
  20. protected:
  21. SectionPairRowTest() = default;
  22. ~SectionPairRowTest() override = default;
  23. void SetUp() override
  24. {}
  25. void TearDown() override
  26. {}
  27. };
  28. TEST_F(SectionPairRowTest, getClassName)
  29. {
  30. ASSERT_STREQ("SectionPairRow", SectionPairRow("tmp-key", SectionPairRowEnumType::SECTION_PAIR_ROW_LIST_VALUE).getClassName().c_str());
  31. }
  32. TEST_F(SectionPairRowTest, constructor_empty_key)
  33. {
  34. EXPECT_THROW(
  35. {
  36. try
  37. {
  38. SectionPairRow row("", SectionPairRowEnumType::SECTION_PAIR_ROW_SINGLE_VALUE);
  39. }
  40. catch (const IllegalArgumentException &_exception)
  41. {
  42. throw;
  43. }
  44. },
  45. IllegalArgumentException);
  46. }
  47. TEST_F(SectionPairRowTest, constructor_invalid_key)
  48. {
  49. EXPECT_THROW(
  50. {
  51. try
  52. {
  53. SectionPairRow row("-tmp-key", SectionPairRowEnumType::SECTION_PAIR_ROW_SINGLE_VALUE);
  54. }
  55. catch (const IllegalArgumentException &_exception)
  56. {
  57. throw;
  58. }
  59. },
  60. IllegalArgumentException);
  61. }
  62. TEST_F(SectionPairRowTest, getKey)
  63. {
  64. ls::std::io::section_pair_identifier key = "tmp-key";
  65. SectionPairRow row(key, SectionPairRowEnumType::SECTION_PAIR_ROW_SINGLE_VALUE);
  66. ASSERT_STREQ(key.c_str(), row.getKey().c_str());
  67. }
  68. TEST_F(SectionPairRowTest, getValue)
  69. {
  70. SectionPairRow row("hobbies", SectionPairRowEnumType::SECTION_PAIR_ROW_LIST_VALUE);
  71. shared_ptr<SectionPairRowValue> value = row.getValue();
  72. ASSERT_TRUE(value != nullptr);
  73. ASSERT_EQ(SectionPairRowEnumType::SECTION_PAIR_ROW_LIST_VALUE, value->getType());
  74. }
  75. TEST_F(SectionPairRowTest, isSingleValue)
  76. {
  77. ASSERT_TRUE(SectionPairRow("tmp-key", SectionPairRowEnumType::SECTION_PAIR_ROW_SINGLE_VALUE).isSingleValue());
  78. }
  79. TEST_F(SectionPairRowTest, isList)
  80. {
  81. ASSERT_FALSE(SectionPairRow("tmp-key", SectionPairRowEnumType::SECTION_PAIR_ROW_SINGLE_VALUE).isList());
  82. }
  83. TEST_F(SectionPairRowTest, marshal_single_value)
  84. {
  85. shared_ptr<SectionPairRow> row = make_shared<SectionPairRow>("favourite-color", SectionPairRowEnumType::SECTION_PAIR_ROW_SINGLE_VALUE);
  86. shared_ptr<SectionPairRowSingleValue> singleValue = dynamic_pointer_cast<SectionPairRowSingleValue>(row->getValue());
  87. singleValue->set("blue");
  88. row->setSerializable(make_shared<SerializableSectionPairRow>(row));
  89. ASSERT_STREQ("favourite-color=blue", row->marshal().c_str());
  90. }
  91. TEST_F(SectionPairRowTest, marshal_list_value)
  92. {
  93. shared_ptr<SectionPairRow> row = make_shared<SectionPairRow>("favourite-colors", SectionPairRowEnumType::SECTION_PAIR_ROW_LIST_VALUE);
  94. shared_ptr<SectionPairRowListValue> listValue = dynamic_pointer_cast<SectionPairRowListValue>(row->getValue());
  95. listValue->add("blue");
  96. listValue->add("red");
  97. listValue->add("purple");
  98. listValue->setSerializable(make_shared<SerializableSectionPairRowListValue>(listValue));
  99. row->setSerializable(make_shared<SerializableSectionPairRow>(row));
  100. string expected = "favourite-colors:" + NewLine::get() + " blue" + NewLine::get() + " red" + NewLine::get() + " purple" + NewLine::get();
  101. ASSERT_STREQ(expected.c_str(), row->marshal().c_str());
  102. }
  103. TEST_F(SectionPairRowTest, marshal_no_serializable)
  104. {
  105. SectionPairRow row("hobbies", SectionPairRowEnumType::SECTION_PAIR_ROW_LIST_VALUE);
  106. EXPECT_THROW(
  107. {
  108. try
  109. {
  110. byte_field data = row.marshal();
  111. }
  112. catch (const NullPointerException &_exception)
  113. {
  114. throw;
  115. }
  116. },
  117. NullPointerException);
  118. }
  119. TEST_F(SectionPairRowTest, setKey)
  120. {
  121. SectionPairRow row("hobbies", SectionPairRowEnumType::SECTION_PAIR_ROW_LIST_VALUE);
  122. row.setKey("colors");
  123. ASSERT_STREQ("colors", row.getKey().c_str());
  124. }
  125. TEST_F(SectionPairRowTest, setKey_empty_key)
  126. {
  127. SectionPairRow row("tmp-key", SectionPairRowEnumType::SECTION_PAIR_ROW_SINGLE_VALUE);
  128. EXPECT_THROW(
  129. {
  130. try
  131. {
  132. row.setKey("");
  133. }
  134. catch (const IllegalArgumentException &_exception)
  135. {
  136. throw;
  137. }
  138. },
  139. IllegalArgumentException);
  140. }
  141. TEST_F(SectionPairRowTest, setKey_invalid_key)
  142. {
  143. SectionPairRow row("tmp-key", SectionPairRowEnumType::SECTION_PAIR_ROW_SINGLE_VALUE);
  144. EXPECT_THROW(
  145. {
  146. try
  147. {
  148. row.setKey("=33");
  149. }
  150. catch (const IllegalArgumentException &_exception)
  151. {
  152. throw;
  153. }
  154. },
  155. IllegalArgumentException);
  156. }
  157. TEST_F(SectionPairRowTest, setSerializable_no_reference)
  158. {
  159. SectionPairRow row("tmp-key", SectionPairRowEnumType::SECTION_PAIR_ROW_SINGLE_VALUE);
  160. EXPECT_THROW(
  161. {
  162. try
  163. {
  164. row.setSerializable(nullptr);
  165. }
  166. catch (const IllegalArgumentException &_exception)
  167. {
  168. throw;
  169. }
  170. },
  171. IllegalArgumentException);
  172. }
  173. TEST_F(SectionPairRowTest, unmarshal_single_value)
  174. {
  175. shared_ptr<SectionPairRow> row = make_shared<SectionPairRow>("tmp-key", SectionPairRowEnumType::SECTION_PAIR_ROW_SINGLE_VALUE);
  176. row->setSerializable(make_shared<SerializableSectionPairRow>(row));
  177. shared_ptr<SectionPairRowSingleValue> singleValue = dynamic_pointer_cast<SectionPairRowSingleValue>(row->getValue());
  178. row->unmarshal("favourite-color=blue");
  179. ASSERT_STREQ("favourite-color", row->getKey().c_str());
  180. ASSERT_STREQ("blue", singleValue->get().c_str());
  181. }
  182. TEST_F(SectionPairRowTest, unmarshal_list_value)
  183. {
  184. shared_ptr<SectionPairRow> row = make_shared<SectionPairRow>("tmp-key", SectionPairRowEnumType::SECTION_PAIR_ROW_LIST_VALUE);
  185. row->setSerializable(make_shared<SerializableSectionPairRow>(row));
  186. shared_ptr<SectionPairRowListValue> listValue = dynamic_pointer_cast<SectionPairRowListValue>(row->getValue());
  187. listValue->setSerializable(make_shared<SerializableSectionPairRowListValue>(listValue));
  188. string data = "favourite-colors:" + NewLine::get() + " blue" + NewLine::get() + " red" + NewLine::get() + " purple" + NewLine::get();
  189. row->unmarshal(data);
  190. ASSERT_STREQ("favourite-colors", row->getKey().c_str());
  191. ASSERT_STREQ("blue", listValue->get(0).c_str());
  192. ASSERT_STREQ("red", listValue->get(1).c_str());
  193. ASSERT_STREQ("purple", listValue->get(2).c_str());
  194. ASSERT_EQ(3, listValue->getSize());
  195. }
  196. TEST_F(SectionPairRowTest, unmarshal_no_serializable)
  197. {
  198. SectionPairRow row("hobbies", SectionPairRowEnumType::SECTION_PAIR_ROW_SINGLE_VALUE);
  199. EXPECT_THROW(
  200. {
  201. try
  202. {
  203. row.unmarshal("hobbies:");
  204. }
  205. catch (const NullPointerException &_exception)
  206. {
  207. throw;
  208. }
  209. },
  210. NullPointerException);
  211. }
  212. }