StringTest.cpp 6.6 KB

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