StringTest.cpp 6.6 KB

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