SectionPairRowTest.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. row->setSerializable(make_shared<SerializableSectionPairRow>(row));
  99. string expected = "favourite-colors:" + NewLine::get() + " blue" + NewLine::get() + " red" + NewLine::get() + " purple" + NewLine::get();
  100. ASSERT_STREQ(expected.c_str(), row->marshal().c_str());
  101. }
  102. TEST_F(SectionPairRowTest, marshal_no_serializable)
  103. {
  104. SectionPairRow row("hobbies", SectionPairRowEnumType::SECTION_PAIR_ROW_LIST_VALUE);
  105. EXPECT_THROW(
  106. {
  107. try
  108. {
  109. byte_field data = row.marshal();
  110. }
  111. catch (const NullPointerException &_exception)
  112. {
  113. throw;
  114. }
  115. },
  116. NullPointerException);
  117. }
  118. TEST_F(SectionPairRowTest, setKey)
  119. {
  120. SectionPairRow row("hobbies", SectionPairRowEnumType::SECTION_PAIR_ROW_LIST_VALUE);
  121. row.setKey("colors");
  122. ASSERT_STREQ("colors", row.getKey().c_str());
  123. }
  124. TEST_F(SectionPairRowTest, setKey_empty_key)
  125. {
  126. SectionPairRow row("tmp-key", SectionPairRowEnumType::SECTION_PAIR_ROW_SINGLE_VALUE);
  127. EXPECT_THROW(
  128. {
  129. try
  130. {
  131. row.setKey("");
  132. }
  133. catch (const IllegalArgumentException &_exception)
  134. {
  135. throw;
  136. }
  137. },
  138. IllegalArgumentException);
  139. }
  140. TEST_F(SectionPairRowTest, setKey_invalid_key)
  141. {
  142. SectionPairRow row("tmp-key", SectionPairRowEnumType::SECTION_PAIR_ROW_SINGLE_VALUE);
  143. EXPECT_THROW(
  144. {
  145. try
  146. {
  147. row.setKey("=33");
  148. }
  149. catch (const IllegalArgumentException &_exception)
  150. {
  151. throw;
  152. }
  153. },
  154. IllegalArgumentException);
  155. }
  156. TEST_F(SectionPairRowTest, setSerializable_no_reference)
  157. {
  158. SectionPairRow row("tmp-key", SectionPairRowEnumType::SECTION_PAIR_ROW_SINGLE_VALUE);
  159. EXPECT_THROW(
  160. {
  161. try
  162. {
  163. row.setSerializable(nullptr);
  164. }
  165. catch (const IllegalArgumentException &_exception)
  166. {
  167. throw;
  168. }
  169. },
  170. IllegalArgumentException);
  171. }
  172. TEST_F(SectionPairRowTest, unmarshal_single_value)
  173. {
  174. shared_ptr<SectionPairRow> row = make_shared<SectionPairRow>("tmp-key", SectionPairRowEnumType::SECTION_PAIR_ROW_SINGLE_VALUE);
  175. row->setSerializable(make_shared<SerializableSectionPairRow>(row));
  176. shared_ptr<SectionPairRowSingleValue> singleValue = dynamic_pointer_cast<SectionPairRowSingleValue>(row->getValue());
  177. row->unmarshal("favourite-color=blue");
  178. ASSERT_STREQ("favourite-color", row->getKey().c_str());
  179. ASSERT_STREQ("blue", singleValue->get().c_str());
  180. }
  181. TEST_F(SectionPairRowTest, unmarshal_list_value)
  182. {
  183. shared_ptr<SectionPairRow> row = make_shared<SectionPairRow>("tmp-key", SectionPairRowEnumType::SECTION_PAIR_ROW_LIST_VALUE);
  184. row->setSerializable(make_shared<SerializableSectionPairRow>(row));
  185. shared_ptr<SectionPairRowListValue> listValue = dynamic_pointer_cast<SectionPairRowListValue>(row->getValue());
  186. string data = "favourite-colors:" + NewLine::get() + " blue" + NewLine::get() + " red" + NewLine::get() + " purple" + NewLine::get();
  187. row->unmarshal(data);
  188. ASSERT_STREQ("favourite-colors", row->getKey().c_str());
  189. ASSERT_STREQ("blue", listValue->get(0).c_str());
  190. ASSERT_STREQ("red", listValue->get(1).c_str());
  191. ASSERT_STREQ("purple", listValue->get(2).c_str());
  192. ASSERT_EQ(3, listValue->getSize());
  193. }
  194. TEST_F(SectionPairRowTest, unmarshal_no_serializable)
  195. {
  196. SectionPairRow row("hobbies", SectionPairRowEnumType::SECTION_PAIR_ROW_SINGLE_VALUE);
  197. EXPECT_THROW(
  198. {
  199. try
  200. {
  201. row.unmarshal("hobbies:");
  202. }
  203. catch (const NullPointerException &_exception)
  204. {
  205. throw;
  206. }
  207. },
  208. NullPointerException);
  209. }
  210. }