SectionPairRowTest.cpp 5.7 KB

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