SectionPairRowTest.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2023-02-08
  6. * Changed: 2023-05-18
  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 ls::std::core::IllegalArgumentException;
  13. using ls::std::core::type::byte_field;
  14. using ls::std::io::NewLine;
  15. using ls::std::io::SectionPairRow;
  16. using ls::std::io::SectionPairRowEnumType;
  17. using ls::std::io::SectionPairRowListValue;
  18. using ls::std::io::SectionPairRowSingleValue;
  19. using ls::std::io::SectionPairRowValue;
  20. using std::dynamic_pointer_cast;
  21. using std::make_shared;
  22. using std::shared_ptr;
  23. using std::string;
  24. using testing::Test;
  25. namespace
  26. {
  27. class SectionPairRowTest : public Test
  28. {
  29. public:
  30. SectionPairRowTest() = default;
  31. ~SectionPairRowTest() override = default;
  32. };
  33. TEST_F(SectionPairRowTest, constructor_empty_key)
  34. {
  35. EXPECT_THROW(
  36. {
  37. try
  38. {
  39. SectionPairRow row("", SectionPairRowEnumType::SECTION_PAIR_ROW_SINGLE_VALUE);
  40. }
  41. catch (const IllegalArgumentException &_exception)
  42. {
  43. throw;
  44. }
  45. },
  46. IllegalArgumentException);
  47. }
  48. TEST_F(SectionPairRowTest, constructor_invalid_key)
  49. {
  50. EXPECT_THROW(
  51. {
  52. try
  53. {
  54. SectionPairRow row("-tmp-key", SectionPairRowEnumType::SECTION_PAIR_ROW_SINGLE_VALUE);
  55. }
  56. catch (const IllegalArgumentException &_exception)
  57. {
  58. throw;
  59. }
  60. },
  61. IllegalArgumentException);
  62. }
  63. TEST_F(SectionPairRowTest, getClassName)
  64. {
  65. ASSERT_STREQ("SectionPairRow", SectionPairRow("tmp-key", SectionPairRowEnumType::SECTION_PAIR_ROW_LIST_VALUE).getClassName().c_str());
  66. }
  67. TEST_F(SectionPairRowTest, getKey)
  68. {
  69. ls::std::io::section_pair_identifier key = "tmp-key";
  70. SectionPairRow row(key, SectionPairRowEnumType::SECTION_PAIR_ROW_SINGLE_VALUE);
  71. ASSERT_STREQ(key.c_str(), row.getKey().c_str());
  72. }
  73. TEST_F(SectionPairRowTest, getValue)
  74. {
  75. SectionPairRow row("hobbies", SectionPairRowEnumType::SECTION_PAIR_ROW_LIST_VALUE);
  76. shared_ptr<SectionPairRowValue> value = row.getValue();
  77. ASSERT_TRUE(value != nullptr);
  78. ASSERT_EQ(SectionPairRowEnumType::SECTION_PAIR_ROW_LIST_VALUE, value->getType());
  79. }
  80. TEST_F(SectionPairRowTest, isSingleValue)
  81. {
  82. ASSERT_TRUE(SectionPairRow("tmp-key", SectionPairRowEnumType::SECTION_PAIR_ROW_SINGLE_VALUE).isSingleValue());
  83. }
  84. TEST_F(SectionPairRowTest, isList)
  85. {
  86. ASSERT_FALSE(SectionPairRow("tmp-key", SectionPairRowEnumType::SECTION_PAIR_ROW_SINGLE_VALUE).isList());
  87. }
  88. TEST_F(SectionPairRowTest, marshal_single_value)
  89. {
  90. auto row = make_shared<SectionPairRow>("favourite-color", SectionPairRowEnumType::SECTION_PAIR_ROW_SINGLE_VALUE);
  91. shared_ptr<SectionPairRowSingleValue> singleValue = dynamic_pointer_cast<SectionPairRowSingleValue>(row->getValue());
  92. singleValue->set("blue");
  93. byte_field expected = "favourite-color=blue" + NewLine::get();
  94. byte_field actual = row->marshal();
  95. ASSERT_STREQ(expected.c_str(), actual.c_str());
  96. }
  97. TEST_F(SectionPairRowTest, marshal_list_value)
  98. {
  99. auto row = make_shared<SectionPairRow>("favourite-colors", SectionPairRowEnumType::SECTION_PAIR_ROW_LIST_VALUE);
  100. shared_ptr<SectionPairRowListValue> listValue = dynamic_pointer_cast<SectionPairRowListValue>(row->getValue());
  101. listValue->add("blue");
  102. listValue->add("red");
  103. listValue->add("purple");
  104. string newLine = NewLine::get();
  105. string expected = "favourite-colors:" + newLine + " blue" + newLine + " red" + newLine + " purple" + newLine;
  106. ASSERT_STREQ(expected.c_str(), row->marshal().c_str());
  107. }
  108. TEST_F(SectionPairRowTest, setKey)
  109. {
  110. SectionPairRow row("hobbies", SectionPairRowEnumType::SECTION_PAIR_ROW_LIST_VALUE);
  111. row.setKey("colors");
  112. ASSERT_STREQ("colors", row.getKey().c_str());
  113. }
  114. TEST_F(SectionPairRowTest, setKey_empty_key)
  115. {
  116. SectionPairRow row("tmp-key", SectionPairRowEnumType::SECTION_PAIR_ROW_SINGLE_VALUE);
  117. EXPECT_THROW(
  118. {
  119. try
  120. {
  121. row.setKey("");
  122. }
  123. catch (const IllegalArgumentException &_exception)
  124. {
  125. throw;
  126. }
  127. },
  128. IllegalArgumentException);
  129. }
  130. TEST_F(SectionPairRowTest, setKey_invalid_key)
  131. {
  132. SectionPairRow row("tmp-key", SectionPairRowEnumType::SECTION_PAIR_ROW_SINGLE_VALUE);
  133. EXPECT_THROW(
  134. {
  135. try
  136. {
  137. row.setKey("=33");
  138. }
  139. catch (const IllegalArgumentException &_exception)
  140. {
  141. throw;
  142. }
  143. },
  144. IllegalArgumentException);
  145. }
  146. TEST_F(SectionPairRowTest, unmarshal_single_value)
  147. {
  148. auto row = make_shared<SectionPairRow>("tmp-key", SectionPairRowEnumType::SECTION_PAIR_ROW_SINGLE_VALUE);
  149. shared_ptr<SectionPairRowSingleValue> singleValue = dynamic_pointer_cast<SectionPairRowSingleValue>(row->getValue());
  150. row->unmarshal("favourite-color=blue");
  151. ASSERT_STREQ("favourite-color", row->getKey().c_str());
  152. ASSERT_STREQ("blue", singleValue->get().c_str());
  153. }
  154. TEST_F(SectionPairRowTest, unmarshal_list_value)
  155. {
  156. auto row = make_shared<SectionPairRow>("tmp-key", SectionPairRowEnumType::SECTION_PAIR_ROW_LIST_VALUE);
  157. shared_ptr<SectionPairRowListValue> listValue = dynamic_pointer_cast<SectionPairRowListValue>(row->getValue());
  158. string newLine = NewLine::get();
  159. string data = "favourite-colors:" + newLine + " blue" + newLine + " red" + newLine + " purple" + newLine;
  160. row->unmarshal(data);
  161. ASSERT_STREQ("favourite-colors", row->getKey().c_str());
  162. ASSERT_STREQ("blue", listValue->get(0).c_str());
  163. ASSERT_STREQ("red", listValue->get(1).c_str());
  164. ASSERT_STREQ("purple", listValue->get(2).c_str());
  165. ASSERT_EQ(3, listValue->getSize());
  166. }
  167. }