SectionPairRowTest.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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-17
  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, constructor_empty_key)
  29. {
  30. EXPECT_THROW(
  31. {
  32. try
  33. {
  34. SectionPairRow row("", SectionPairRowEnumType::SECTION_PAIR_ROW_SINGLE_VALUE);
  35. }
  36. catch (const IllegalArgumentException &_exception)
  37. {
  38. throw;
  39. }
  40. },
  41. IllegalArgumentException);
  42. }
  43. TEST_F(SectionPairRowTest, constructor_invalid_key)
  44. {
  45. EXPECT_THROW(
  46. {
  47. try
  48. {
  49. SectionPairRow row("-tmp-key", SectionPairRowEnumType::SECTION_PAIR_ROW_SINGLE_VALUE);
  50. }
  51. catch (const IllegalArgumentException &_exception)
  52. {
  53. throw;
  54. }
  55. },
  56. IllegalArgumentException);
  57. }
  58. TEST_F(SectionPairRowTest, getClassName)
  59. {
  60. ASSERT_STREQ("SectionPairRow", SectionPairRow("tmp-key", SectionPairRowEnumType::SECTION_PAIR_ROW_LIST_VALUE).getClassName().c_str());
  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. byte_field expected = "favourite-color=blue" + NewLine::get();
  89. byte_field actual = row->marshal();
  90. ASSERT_STREQ(expected.c_str(), actual.c_str());
  91. }
  92. TEST_F(SectionPairRowTest, marshal_list_value)
  93. {
  94. shared_ptr<SectionPairRow> row = make_shared<SectionPairRow>("favourite-colors", SectionPairRowEnumType::SECTION_PAIR_ROW_LIST_VALUE);
  95. shared_ptr<SectionPairRowListValue> listValue = dynamic_pointer_cast<SectionPairRowListValue>(row->getValue());
  96. listValue->add("blue");
  97. listValue->add("red");
  98. listValue->add("purple");
  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, setKey)
  103. {
  104. SectionPairRow row("hobbies", SectionPairRowEnumType::SECTION_PAIR_ROW_LIST_VALUE);
  105. row.setKey("colors");
  106. ASSERT_STREQ("colors", row.getKey().c_str());
  107. }
  108. TEST_F(SectionPairRowTest, setKey_empty_key)
  109. {
  110. SectionPairRow row("tmp-key", SectionPairRowEnumType::SECTION_PAIR_ROW_SINGLE_VALUE);
  111. EXPECT_THROW(
  112. {
  113. try
  114. {
  115. row.setKey("");
  116. }
  117. catch (const IllegalArgumentException &_exception)
  118. {
  119. throw;
  120. }
  121. },
  122. IllegalArgumentException);
  123. }
  124. TEST_F(SectionPairRowTest, setKey_invalid_key)
  125. {
  126. SectionPairRow row("tmp-key", SectionPairRowEnumType::SECTION_PAIR_ROW_SINGLE_VALUE);
  127. EXPECT_THROW(
  128. {
  129. try
  130. {
  131. row.setKey("=33");
  132. }
  133. catch (const IllegalArgumentException &_exception)
  134. {
  135. throw;
  136. }
  137. },
  138. IllegalArgumentException);
  139. }
  140. TEST_F(SectionPairRowTest, unmarshal_single_value)
  141. {
  142. shared_ptr<SectionPairRow> row = make_shared<SectionPairRow>("tmp-key", SectionPairRowEnumType::SECTION_PAIR_ROW_SINGLE_VALUE);
  143. shared_ptr<SectionPairRowSingleValue> singleValue = dynamic_pointer_cast<SectionPairRowSingleValue>(row->getValue());
  144. row->unmarshal("favourite-color=blue");
  145. ASSERT_STREQ("favourite-color", row->getKey().c_str());
  146. ASSERT_STREQ("blue", singleValue->get().c_str());
  147. }
  148. TEST_F(SectionPairRowTest, unmarshal_list_value)
  149. {
  150. shared_ptr<SectionPairRow> row = make_shared<SectionPairRow>("tmp-key", SectionPairRowEnumType::SECTION_PAIR_ROW_LIST_VALUE);
  151. shared_ptr<SectionPairRowListValue> listValue = dynamic_pointer_cast<SectionPairRowListValue>(row->getValue());
  152. string data = "favourite-colors:" + NewLine::get() + " blue" + NewLine::get() + " red" + NewLine::get() + " purple" + NewLine::get();
  153. row->unmarshal(data);
  154. ASSERT_STREQ("favourite-colors", row->getKey().c_str());
  155. ASSERT_STREQ("blue", listValue->get(0).c_str());
  156. ASSERT_STREQ("red", listValue->get(1).c_str());
  157. ASSERT_STREQ("purple", listValue->get(2).c_str());
  158. ASSERT_EQ(3, listValue->getSize());
  159. }
  160. }