SectionPairRowTest.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. ASSERT_STREQ("favourite-color=blue", row->marshal().c_str());
  89. }
  90. TEST_F(SectionPairRowTest, marshal_list_value)
  91. {
  92. shared_ptr<SectionPairRow> row = make_shared<SectionPairRow>("favourite-colors", SectionPairRowEnumType::SECTION_PAIR_ROW_LIST_VALUE);
  93. shared_ptr<SectionPairRowListValue> listValue = dynamic_pointer_cast<SectionPairRowListValue>(row->getValue());
  94. listValue->add("blue");
  95. listValue->add("red");
  96. listValue->add("purple");
  97. string expected = "favourite-colors:" + NewLine::get() + " blue" + NewLine::get() + " red" + NewLine::get() + " purple" + NewLine::get();
  98. ASSERT_STREQ(expected.c_str(), row->marshal().c_str());
  99. }
  100. TEST_F(SectionPairRowTest, setKey)
  101. {
  102. SectionPairRow row("hobbies", SectionPairRowEnumType::SECTION_PAIR_ROW_LIST_VALUE);
  103. row.setKey("colors");
  104. ASSERT_STREQ("colors", row.getKey().c_str());
  105. }
  106. TEST_F(SectionPairRowTest, setKey_empty_key)
  107. {
  108. SectionPairRow row("tmp-key", SectionPairRowEnumType::SECTION_PAIR_ROW_SINGLE_VALUE);
  109. EXPECT_THROW(
  110. {
  111. try
  112. {
  113. row.setKey("");
  114. }
  115. catch (const IllegalArgumentException &_exception)
  116. {
  117. throw;
  118. }
  119. },
  120. IllegalArgumentException);
  121. }
  122. TEST_F(SectionPairRowTest, setKey_invalid_key)
  123. {
  124. SectionPairRow row("tmp-key", SectionPairRowEnumType::SECTION_PAIR_ROW_SINGLE_VALUE);
  125. EXPECT_THROW(
  126. {
  127. try
  128. {
  129. row.setKey("=33");
  130. }
  131. catch (const IllegalArgumentException &_exception)
  132. {
  133. throw;
  134. }
  135. },
  136. IllegalArgumentException);
  137. }
  138. TEST_F(SectionPairRowTest, unmarshal_single_value)
  139. {
  140. shared_ptr<SectionPairRow> row = make_shared<SectionPairRow>("tmp-key", SectionPairRowEnumType::SECTION_PAIR_ROW_SINGLE_VALUE);
  141. shared_ptr<SectionPairRowSingleValue> singleValue = dynamic_pointer_cast<SectionPairRowSingleValue>(row->getValue());
  142. row->unmarshal("favourite-color=blue");
  143. ASSERT_STREQ("favourite-color", row->getKey().c_str());
  144. ASSERT_STREQ("blue", singleValue->get().c_str());
  145. }
  146. TEST_F(SectionPairRowTest, unmarshal_list_value)
  147. {
  148. shared_ptr<SectionPairRow> row = make_shared<SectionPairRow>("tmp-key", SectionPairRowEnumType::SECTION_PAIR_ROW_LIST_VALUE);
  149. shared_ptr<SectionPairRowListValue> listValue = dynamic_pointer_cast<SectionPairRowListValue>(row->getValue());
  150. string data = "favourite-colors:" + NewLine::get() + " blue" + NewLine::get() + " red" + NewLine::get() + " purple" + NewLine::get();
  151. row->unmarshal(data);
  152. ASSERT_STREQ("favourite-colors", row->getKey().c_str());
  153. ASSERT_STREQ("blue", listValue->get(0).c_str());
  154. ASSERT_STREQ("red", listValue->get(1).c_str());
  155. ASSERT_STREQ("purple", listValue->get(2).c_str());
  156. ASSERT_EQ(3, listValue->getSize());
  157. }
  158. }