StringTest.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-08-14
  6. * Changed: 2020-08-30
  7. *
  8. * */
  9. #include <gtest/gtest.h>
  10. #include "../../../source/boxing/String.hpp"
  11. #include "../../../source/serialization/boxing/SerializableJSONString.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, marshal)
  73. {
  74. std::shared_ptr<ls_std::String> x = std::make_shared<ls_std::String>("Hello!");
  75. auto serializable = std::make_shared<ls_std::SerializableJSONString>(x);
  76. x->setSerializable(std::dynamic_pointer_cast<ls_std::ISerializable>(serializable));
  77. ASSERT_STREQ(R"({"class":"String","value":"Hello!"})", x->marshal().c_str());
  78. *x = "Test!";
  79. ASSERT_STREQ(R"({"class":"String","value":"Test!"})", x->marshal().c_str());
  80. }
  81. TEST_F(StringTest, parse)
  82. {
  83. ls_std::String text {};
  84. text.parse("Hello!");
  85. ASSERT_STREQ("Hello!", text.toString().c_str());
  86. }
  87. TEST_F(StringTest, toString)
  88. {
  89. ls_std::String text {"Hello!"};
  90. ASSERT_STREQ("Hello!", text.toString().c_str());
  91. }
  92. TEST_F(StringTest, unmarshal)
  93. {
  94. std::shared_ptr<ls_std::String> x = std::make_shared<ls_std::String>("Hello!");
  95. ASSERT_STREQ("Hello!", *x);
  96. auto serializable = std::make_shared<ls_std::SerializableJSONString>(x);
  97. x->setSerializable(std::dynamic_pointer_cast<ls_std::ISerializable>(serializable));
  98. x->unmarshal(R"({"class":"String","value":"Test!"})");
  99. ASSERT_STREQ("Test!", *x);
  100. }
  101. // additional functionality
  102. TEST_F(StringTest, contains)
  103. {
  104. ls_std::String text {};
  105. text = "Hey, I'm searching for the keyword 'cake'!";
  106. ASSERT_TRUE(text.contains("cake"));
  107. }
  108. TEST_F(StringTest, containsNegative)
  109. {
  110. ls_std::String text {};
  111. text = "Hey, I'm searching for the keyword 'cake'!";
  112. ASSERT_FALSE(text.contains("butter"));
  113. }
  114. TEST_F(StringTest, endsWith)
  115. {
  116. ls_std::String text {};
  117. text = "abcdef";
  118. ASSERT_TRUE(text.endsWith("ef"));
  119. }
  120. TEST_F(StringTest, equalsIgnoreCase)
  121. {
  122. ls_std::String text {"Hello!"};
  123. ls_std::String hello {"HeLLo!"};
  124. ASSERT_TRUE(text.equalsIgnoreCase(hello));
  125. ASSERT_TRUE(text.equalsIgnoreCase("HeLLO!"));
  126. }
  127. TEST_F(StringTest, endsWithNegative)
  128. {
  129. ls_std::String text {};
  130. text = "abcdef";
  131. ASSERT_FALSE(text.endsWith("efg"));
  132. }
  133. TEST_F(StringTest, padLeft)
  134. {
  135. ls_std::String text {"abcdef"};
  136. ls_std::String anotherText {"ab"};
  137. ls_std::String emptyText {};
  138. ls_std::String longText {"This text is too long to fill!"};
  139. ASSERT_STREQ(" abcdef", text.padLeft(10, ' ').c_str());
  140. ASSERT_STREQ(" ab", anotherText.padLeft(10, ' ').c_str());
  141. ASSERT_STREQ(" ", emptyText.padLeft(10, ' ').c_str());
  142. ASSERT_STREQ("This text is too long to fill!", longText.padLeft(10, ' ').c_str());
  143. }
  144. TEST_F(StringTest, padRight)
  145. {
  146. ls_std::String text {"abcdef"};
  147. ls_std::String anotherText {"ab"};
  148. ls_std::String emptyText {};
  149. ls_std::String longText {"This text is too long to fill!"};
  150. ASSERT_STREQ("abcdef ", text.padRight(10, ' ').c_str());
  151. ASSERT_STREQ("ab ", anotherText.padRight(10, ' ').c_str());
  152. ASSERT_STREQ(" ", emptyText.padRight(10, ' ').c_str());
  153. ASSERT_STREQ("This text is too long to fill!", longText.padRight(10, ' ').c_str());
  154. }
  155. TEST_F(StringTest, reverse)
  156. {
  157. ls_std::String text {};
  158. text = "abcdef";
  159. ASSERT_STREQ("fedcba", text.reverse().c_str());
  160. ASSERT_STREQ("abcdef", text);
  161. }
  162. TEST_F(StringTest, startsWith)
  163. {
  164. ls_std::String text {};
  165. text = "abcdef";
  166. ASSERT_TRUE(text.startsWith("abc"));
  167. }
  168. TEST_F(StringTest, startsWithNegative)
  169. {
  170. ls_std::String text {};
  171. text = "abcdef";
  172. ASSERT_FALSE(text.startsWith("bc"));
  173. }
  174. TEST_F(StringTest, toLowerCase)
  175. {
  176. ls_std::String text {};
  177. text = "aBCdeFgHIJKLmn";
  178. ASSERT_STREQ("abcdefghijklmn", text.toLowerCase().c_str());
  179. ASSERT_STREQ("aBCdeFgHIJKLmn", text);
  180. }
  181. TEST_F(StringTest, toUpperCase)
  182. {
  183. ls_std::String text {};
  184. text = "aBCdeFgHIJKLmn";
  185. ASSERT_STREQ("ABCDEFGHIJKLMN", text.toUpperCase().c_str());
  186. ASSERT_STREQ("aBCdeFgHIJKLmn", text);
  187. }
  188. }