StringTest.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-08-14
  6. * Changed: 2021-07-08
  7. *
  8. * */
  9. #include <gtest/gtest.h>
  10. #include <ls_std/ls_std.hpp>
  11. #include <TestHelper.hpp>
  12. namespace
  13. {
  14. class StringTest : public ::testing::Test
  15. {
  16. protected:
  17. StringTest() = default;
  18. ~StringTest() override = default;
  19. void SetUp() override
  20. {}
  21. void TearDown() override
  22. {}
  23. static std::pair<std::shared_ptr<ls_std::File>, std::shared_ptr<ls_std::String>> createPersistentTestString()
  24. {
  25. std::shared_ptr<ls_std::String> sentence = std::make_shared<ls_std::String>();
  26. std::string path = TestHelper::getResourcesFolderLocation() + "tmp_storable_string.json";
  27. std::shared_ptr<ls_std::File> file = std::make_shared<ls_std::File>(path);
  28. file->createNewFile();
  29. ls_std::FileWriter writer{*file};
  30. writer.write(R"({"value":"Hello!"})");
  31. auto serializable = std::make_shared<ls_std::SerializableJsonString>(sentence);
  32. sentence->setSerializable(std::dynamic_pointer_cast<ls_std::ISerializable>(serializable));
  33. auto storable = std::make_shared<ls_std::StorableFile>(path);
  34. sentence->setStorable(std::dynamic_pointer_cast<ls_std::IStorable>(storable));
  35. return std::pair<std::shared_ptr<ls_std::File>, std::shared_ptr<ls_std::String>>(file, sentence);
  36. }
  37. };
  38. // assignment operators
  39. TEST_F(StringTest, operator_assignment)
  40. {
  41. ls_std::String text{};
  42. text = "Hi!";
  43. ASSERT_STREQ("Hi!", text.toString().c_str());
  44. }
  45. // arithmetic operators
  46. TEST_F(StringTest, operator_add)
  47. {
  48. ls_std::String greetings{"Hello! "};
  49. ls_std::String question{"How are you? "};
  50. const std::string& answer = "I'm good by the way!";
  51. greetings = greetings + question + answer;
  52. ASSERT_STREQ("Hello! How are you? I'm good by the way!", greetings.toString().c_str());
  53. }
  54. TEST_F(StringTest, operator_minus)
  55. {
  56. ls_std::String text{"abcdefghij"};
  57. text = text - 5;
  58. ASSERT_STREQ("abcde", text.toString().c_str());
  59. }
  60. // compound operators
  61. TEST_F(StringTest, operator_add_assign_with_reference)
  62. {
  63. ls_std::String text{};
  64. ls_std::String hello{"Hi!"};
  65. text += hello;
  66. ASSERT_STREQ("Hi!", text.toString().c_str());
  67. }
  68. TEST_F(StringTest, operator_add_assign_with_value)
  69. {
  70. ls_std::String text{};
  71. text += "Hi!";
  72. ASSERT_STREQ("Hi!", text.toString().c_str());
  73. }
  74. // comparison operators
  75. TEST_F(StringTest, operator_equals_with_reference)
  76. {
  77. ls_std::String text{"Hi!"};
  78. ls_std::String hello{"Hi!"};
  79. ASSERT_TRUE(text == hello);
  80. ASSERT_TRUE(hello == text);
  81. }
  82. TEST_F(StringTest, operator_equals_with_value)
  83. {
  84. ls_std::String hello{"Hi!"};
  85. ASSERT_TRUE(hello == "Hi!");
  86. }
  87. TEST_F(StringTest, operator_not_equals_with_reference)
  88. {
  89. ls_std::String text{"Hi!"};
  90. ls_std::String hello{"Hello!"};
  91. ASSERT_TRUE(text != hello);
  92. }
  93. TEST_F(StringTest, operator_not_equals_with_value)
  94. {
  95. ls_std::String text{"Hi!"};
  96. ASSERT_TRUE(text != "Hello!");
  97. }
  98. // implementation
  99. TEST_F(StringTest, load)
  100. {
  101. // preparation
  102. auto storableString = createPersistentTestString();
  103. // check
  104. storableString.second->load();
  105. ASSERT_STREQ("Hello!", *storableString.second);
  106. storableString.first->remove();
  107. }
  108. TEST_F(StringTest, marshal)
  109. {
  110. std::shared_ptr<ls_std::String> sentence = std::make_shared<ls_std::String>("Hello!");
  111. auto serializable = std::make_shared<ls_std::SerializableJsonString>(sentence);
  112. sentence->setSerializable(std::dynamic_pointer_cast<ls_std::ISerializable>(serializable));
  113. ASSERT_STREQ(R"({"value":"Hello!"})", sentence->marshal().c_str());
  114. }
  115. TEST_F(StringTest, parse)
  116. {
  117. ls_std::String text{};
  118. text.parse("Hello!");
  119. ASSERT_STREQ("Hello!", text.toString().c_str());
  120. }
  121. TEST_F(StringTest, toString)
  122. {
  123. ls_std::String text{"Hello!"};
  124. ASSERT_STREQ("Hello!", text.toString().c_str());
  125. }
  126. TEST_F(StringTest, unmarshal)
  127. {
  128. std::shared_ptr<ls_std::String> sentence = std::make_shared<ls_std::String>("Hello!");
  129. auto serializable = std::make_shared<ls_std::SerializableJsonString>(sentence);
  130. sentence->setSerializable(std::dynamic_pointer_cast<ls_std::ISerializable>(serializable));
  131. sentence->unmarshal(R"({"value":"Test!"})");
  132. ASSERT_STREQ("Test!", *sentence);
  133. }
  134. // additional functionality
  135. TEST_F(StringTest, contains)
  136. {
  137. ls_std::String text{};
  138. text = "Hey, I'm searching for the keyword 'cake'!";
  139. ASSERT_TRUE(text.contains("cake"));
  140. }
  141. TEST_F(StringTest, contains_does_not_contain_search_word)
  142. {
  143. ls_std::String text{};
  144. text = "Hey, I'm searching for the keyword 'cake'!";
  145. ASSERT_FALSE(text.contains("butter"));
  146. }
  147. TEST_F(StringTest, endsWith)
  148. {
  149. ls_std::String text{};
  150. text = "abcdef";
  151. ASSERT_TRUE(text.endsWith("ef"));
  152. }
  153. TEST_F(StringTest, endsWith_does_not_end_with_pattern)
  154. {
  155. ls_std::String text{};
  156. text = "abcdef";
  157. ASSERT_FALSE(text.endsWith("efg"));
  158. }
  159. TEST_F(StringTest, equalsIgnoreCase)
  160. {
  161. ls_std::String text{"Hello!"};
  162. ls_std::String hello{"HeLLo!"};
  163. ASSERT_TRUE(text.equalsIgnoreCase(hello));
  164. ASSERT_TRUE(text.equalsIgnoreCase("HeLLO!"));
  165. }
  166. TEST_F(StringTest, getByteData)
  167. {
  168. ls_std::String text{"Hallo!"};
  169. ASSERT_STREQ("Hallo!", text.getByteData().data());
  170. }
  171. TEST_F(StringTest, padLeft)
  172. {
  173. ls_std::String text{"abcdef"};
  174. ls_std::String anotherText{"ab"};
  175. ls_std::String emptyText{};
  176. ls_std::String longText{"This text is too long to fill!"};
  177. ASSERT_STREQ(" abcdef", text.padLeft(10, ' ').c_str());
  178. ASSERT_STREQ(" ab", anotherText.padLeft(10, ' ').c_str());
  179. ASSERT_STREQ(" ", emptyText.padLeft(10, ' ').c_str());
  180. ASSERT_STREQ("This text is too long to fill!", longText.padLeft(10, ' ').c_str());
  181. }
  182. TEST_F(StringTest, padRight)
  183. {
  184. ls_std::String text{"abcdef"};
  185. ls_std::String anotherText{"ab"};
  186. ls_std::String emptyText{};
  187. ls_std::String longText{"This text is too long to fill!"};
  188. ASSERT_STREQ("abcdef ", text.padRight(10, ' ').c_str());
  189. ASSERT_STREQ("ab ", anotherText.padRight(10, ' ').c_str());
  190. ASSERT_STREQ(" ", emptyText.padRight(10, ' ').c_str());
  191. ASSERT_STREQ("This text is too long to fill!", longText.padRight(10, ' ').c_str());
  192. }
  193. TEST_F(StringTest, reverse)
  194. {
  195. ls_std::String text{};
  196. text = "abcdef";
  197. ASSERT_STREQ("fedcba", text.reverse().c_str());
  198. ASSERT_STREQ("abcdef", text); // verify, that original string didn't change
  199. }
  200. TEST_F(StringTest, startsWith)
  201. {
  202. ls_std::String text{};
  203. text = "abcdef";
  204. ASSERT_TRUE(text.startsWith("abc"));
  205. }
  206. TEST_F(StringTest, startsWith_does_not_start_with_pattern)
  207. {
  208. ls_std::String text{};
  209. text = "abcdef";
  210. ASSERT_FALSE(text.startsWith("bc"));
  211. }
  212. TEST_F(StringTest, toLowerCase)
  213. {
  214. ls_std::String text{};
  215. text = "aBCdeFgHIJKLmn";
  216. ASSERT_STREQ("abcdefghijklmn", text.toLowerCase().c_str());
  217. ASSERT_STREQ("aBCdeFgHIJKLmn", text); // verify, that original String didn't change
  218. }
  219. TEST_F(StringTest, toUpperCase)
  220. {
  221. ls_std::String text{};
  222. text = "aBCdeFgHIJKLmn";
  223. ASSERT_STREQ("ABCDEFGHIJKLMN", text.toUpperCase().c_str());
  224. ASSERT_STREQ("aBCdeFgHIJKLmn", text); // verify, that original String didn't change
  225. }
  226. TEST_F(StringTest, setSerializable_no_reference)
  227. {
  228. ls_std::String x{};
  229. EXPECT_THROW({
  230. try
  231. {
  232. x.setSerializable(nullptr);
  233. }
  234. catch (const ls_std::IllegalArgumentException &_exception)
  235. {
  236. throw;
  237. }
  238. }, ls_std::IllegalArgumentException);
  239. }
  240. TEST_F(StringTest, setStorable_no_reference)
  241. {
  242. ls_std::String x{};
  243. EXPECT_THROW({
  244. try
  245. {
  246. x.setStorable(nullptr);
  247. }
  248. catch (const ls_std::IllegalArgumentException &_exception)
  249. {
  250. throw;
  251. }
  252. }, ls_std::IllegalArgumentException);
  253. }
  254. }