SectionPairRowTest.cpp 6.0 KB

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