StringTest.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-08-14
  6. * Changed: 2021-05-02
  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. };
  24. // assignment operators
  25. TEST_F(StringTest, operatorAssignment)
  26. {
  27. ls_std::String text{};
  28. text = "Hi!";
  29. ASSERT_STREQ("Hi!", text.toString().c_str());
  30. }
  31. // arithmetic operators
  32. TEST_F(StringTest, operatorAdd)
  33. {
  34. ls_std::String text{"Hello! "};
  35. ls_std::String end{"How are you? "};
  36. text = text + end + "I'm good by the way!";
  37. ASSERT_STREQ("Hello! How are you? I'm good by the way!", text.toString().c_str());
  38. }
  39. TEST_F(StringTest, operatorHyphen)
  40. {
  41. ls_std::String text{"abcdefghij"};
  42. text = text - 5;
  43. ASSERT_STREQ("abcde", text.toString().c_str());
  44. }
  45. // compound operators
  46. TEST_F(StringTest, operatorAddEqual)
  47. {
  48. ls_std::String text{};
  49. ls_std::String hello{"Hi! "};
  50. ASSERT_STREQ("", text.toString().c_str());
  51. text += hello;
  52. ASSERT_STREQ("Hi! ", text.toString().c_str());
  53. text += "Bye!";
  54. ASSERT_STREQ("Hi! Bye!", text.toString().c_str());
  55. }
  56. // comparison operators
  57. TEST_F(StringTest, operatorEqual)
  58. {
  59. ls_std::String text{"Hi!"};
  60. ls_std::String hello{"Hi!"};
  61. ASSERT_TRUE(text == hello);
  62. ASSERT_TRUE(hello == text);
  63. ASSERT_TRUE(hello == std::string("Hi!"));
  64. ASSERT_TRUE(hello == "Hi!");
  65. }
  66. TEST_F(StringTest, operatorNotEqual)
  67. {
  68. ls_std::String text{"Hi!"};
  69. ls_std::String hello{"Hello!"};
  70. ASSERT_TRUE(text != hello);
  71. ASSERT_TRUE(hello != text);
  72. ASSERT_TRUE(text != std::string("Hello!"));
  73. ASSERT_TRUE(text != "Hello!");
  74. }
  75. // implementation
  76. TEST_F(StringTest, load)
  77. {
  78. // preparation
  79. std::shared_ptr<ls_std::String> x = std::make_shared<ls_std::String>();
  80. std::string path = TestHelper::getResourcesFolderLocation() + "tmp_storable_string.json";
  81. ls_std::File file{path};
  82. file.createNewFile();
  83. ls_std::FileWriter writer{file};
  84. writer.write(R"({"value":"Hello!"})");
  85. auto serializable = std::make_shared<ls_std::SerializableJsonString>(x);
  86. x->setSerializable(std::dynamic_pointer_cast<ls_std::ISerializable>(serializable));
  87. auto storable = std::make_shared<ls_std::StorableFile>(path);
  88. x->setStorable(std::dynamic_pointer_cast<ls_std::IStorable>(storable));
  89. // check
  90. x->load();
  91. ASSERT_STREQ("Hello!", *x);
  92. file.remove();
  93. }
  94. TEST_F(StringTest, marshal)
  95. {
  96. std::shared_ptr<ls_std::String> x = std::make_shared<ls_std::String>("Hello!");
  97. auto serializable = std::make_shared<ls_std::SerializableJsonString>(x);
  98. x->setSerializable(std::dynamic_pointer_cast<ls_std::ISerializable>(serializable));
  99. ASSERT_STREQ(R"({"value":"Hello!"})", x->marshal().c_str());
  100. *x = "Test!";
  101. ASSERT_STREQ(R"({"value":"Test!"})", x->marshal().c_str());
  102. }
  103. TEST_F(StringTest, parse)
  104. {
  105. ls_std::String text{};
  106. text.parse("Hello!");
  107. ASSERT_STREQ("Hello!", text.toString().c_str());
  108. }
  109. TEST_F(StringTest, toString)
  110. {
  111. ls_std::String text{"Hello!"};
  112. ASSERT_STREQ("Hello!", text.toString().c_str());
  113. }
  114. TEST_F(StringTest, unmarshal)
  115. {
  116. std::shared_ptr<ls_std::String> x = std::make_shared<ls_std::String>("Hello!");
  117. ASSERT_STREQ("Hello!", *x);
  118. auto serializable = std::make_shared<ls_std::SerializableJsonString>(x);
  119. x->setSerializable(std::dynamic_pointer_cast<ls_std::ISerializable>(serializable));
  120. x->unmarshal(R"({"value":"Test!"})");
  121. ASSERT_STREQ("Test!", *x);
  122. }
  123. // additional functionality
  124. TEST_F(StringTest, contains)
  125. {
  126. ls_std::String text{};
  127. text = "Hey, I'm searching for the keyword 'cake'!";
  128. ASSERT_TRUE(text.contains("cake"));
  129. }
  130. TEST_F(StringTest, containsNegative)
  131. {
  132. ls_std::String text{};
  133. text = "Hey, I'm searching for the keyword 'cake'!";
  134. ASSERT_FALSE(text.contains("butter"));
  135. }
  136. TEST_F(StringTest, endsWith)
  137. {
  138. ls_std::String text{};
  139. text = "abcdef";
  140. ASSERT_TRUE(text.endsWith("ef"));
  141. }
  142. TEST_F(StringTest, endsWithNegative)
  143. {
  144. ls_std::String text{};
  145. text = "abcdef";
  146. ASSERT_FALSE(text.endsWith("efg"));
  147. }
  148. TEST_F(StringTest, equalsIgnoreCase)
  149. {
  150. ls_std::String text{"Hello!"};
  151. ls_std::String hello{"HeLLo!"};
  152. ASSERT_TRUE(text.equalsIgnoreCase(hello));
  153. ASSERT_TRUE(text.equalsIgnoreCase("HeLLO!"));
  154. }
  155. TEST_F(StringTest, getByteData)
  156. {
  157. ls_std::String text{"Hallo!"};
  158. ASSERT_STREQ("Hallo!", text.getByteData().data());
  159. }
  160. TEST_F(StringTest, padLeft)
  161. {
  162. ls_std::String text{"abcdef"};
  163. ls_std::String anotherText{"ab"};
  164. ls_std::String emptyText{};
  165. ls_std::String longText{"This text is too long to fill!"};
  166. ASSERT_STREQ(" abcdef", text.padLeft(10, ' ').c_str());
  167. ASSERT_STREQ(" ab", anotherText.padLeft(10, ' ').c_str());
  168. ASSERT_STREQ(" ", emptyText.padLeft(10, ' ').c_str());
  169. ASSERT_STREQ("This text is too long to fill!", longText.padLeft(10, ' ').c_str());
  170. }
  171. TEST_F(StringTest, padRight)
  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.padRight(10, ' ').c_str());
  178. ASSERT_STREQ("ab ", anotherText.padRight(10, ' ').c_str());
  179. ASSERT_STREQ(" ", emptyText.padRight(10, ' ').c_str());
  180. ASSERT_STREQ("This text is too long to fill!", longText.padRight(10, ' ').c_str());
  181. }
  182. TEST_F(StringTest, reverse)
  183. {
  184. ls_std::String text{};
  185. text = "abcdef";
  186. ASSERT_STREQ("fedcba", text.reverse().c_str());
  187. ASSERT_STREQ("abcdef", text);
  188. }
  189. TEST_F(StringTest, startsWith)
  190. {
  191. ls_std::String text{};
  192. text = "abcdef";
  193. ASSERT_TRUE(text.startsWith("abc"));
  194. }
  195. TEST_F(StringTest, startsWithNegative)
  196. {
  197. ls_std::String text{};
  198. text = "abcdef";
  199. ASSERT_FALSE(text.startsWith("bc"));
  200. }
  201. TEST_F(StringTest, toLowerCase)
  202. {
  203. ls_std::String text{};
  204. text = "aBCdeFgHIJKLmn";
  205. ASSERT_STREQ("abcdefghijklmn", text.toLowerCase().c_str());
  206. ASSERT_STREQ("aBCdeFgHIJKLmn", text);
  207. }
  208. TEST_F(StringTest, toUpperCase)
  209. {
  210. ls_std::String text{};
  211. text = "aBCdeFgHIJKLmn";
  212. ASSERT_STREQ("ABCDEFGHIJKLMN", text.toUpperCase().c_str());
  213. ASSERT_STREQ("aBCdeFgHIJKLmn", text);
  214. }
  215. }