StringTest.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-08-14
  6. * Changed: 2022-05-20
  7. *
  8. * */
  9. #include <gtest/gtest.h>
  10. #include <ls_std/ls_std_boxing.hpp>
  11. namespace
  12. {
  13. class StringTest : public ::testing::Test
  14. {
  15. protected:
  16. StringTest() = default;
  17. ~StringTest() override = default;
  18. void SetUp() override
  19. {}
  20. void TearDown() override
  21. {}
  22. };
  23. // assignment operators
  24. TEST_F(StringTest, operator_assignment)
  25. {
  26. ls::std::boxing::String text{};
  27. text = "Hi!";
  28. ASSERT_STREQ("Hi!", text.toString().c_str());
  29. }
  30. // arithmetic operators
  31. TEST_F(StringTest, operator_add)
  32. {
  33. ls::std::boxing::String greetings{"Hello! "};
  34. ls::std::boxing::String question{"How are you? "};
  35. const ::std::string& answer = "I'm good by the way!";
  36. greetings = greetings + question + answer;
  37. ASSERT_STREQ("Hello! How are you? I'm good by the way!", greetings.toString().c_str());
  38. }
  39. TEST_F(StringTest, operator_minus)
  40. {
  41. ls::std::boxing::String text{"abcdefghij"};
  42. text = text - 5;
  43. ASSERT_STREQ("abcde", text.toString().c_str());
  44. }
  45. // compound operators
  46. TEST_F(StringTest, operator_add_assign_with_reference)
  47. {
  48. ls::std::boxing::String text{};
  49. ls::std::boxing::String hello{"Hi!"};
  50. text += hello;
  51. ASSERT_STREQ("Hi!", text.toString().c_str());
  52. }
  53. TEST_F(StringTest, operator_add_assign_with_value)
  54. {
  55. ls::std::boxing::String text{};
  56. text += "Hi!";
  57. ASSERT_STREQ("Hi!", text.toString().c_str());
  58. }
  59. // comparison operators
  60. TEST_F(StringTest, operator_equals_with_reference)
  61. {
  62. ls::std::boxing::String text{"Hi!"};
  63. ls::std::boxing::String hello{"Hi!"};
  64. ASSERT_TRUE(text == hello);
  65. ASSERT_TRUE(hello == text);
  66. }
  67. TEST_F(StringTest, operator_equals_with_value)
  68. {
  69. ls::std::boxing::String hello{"Hi!"};
  70. ASSERT_TRUE(hello == "Hi!");
  71. }
  72. TEST_F(StringTest, operator_not_equals_with_reference)
  73. {
  74. ls::std::boxing::String text{"Hi!"};
  75. ls::std::boxing::String hello{"Hello!"};
  76. ASSERT_TRUE(text != hello);
  77. }
  78. TEST_F(StringTest, operator_not_equals_with_value)
  79. {
  80. ls::std::boxing::String text{"Hi!"};
  81. ASSERT_TRUE(text != "Hello!");
  82. }
  83. // implementation
  84. TEST_F(StringTest, parse)
  85. {
  86. ls::std::boxing::String text{};
  87. text.parse("Hello!");
  88. ASSERT_STREQ("Hello!", text.toString().c_str());
  89. }
  90. TEST_F(StringTest, toString)
  91. {
  92. ls::std::boxing::String text{"Hello!"};
  93. ASSERT_STREQ("Hello!", text.toString().c_str());
  94. }
  95. // additional functionality
  96. TEST_F(StringTest, contains)
  97. {
  98. ls::std::boxing::String text{};
  99. text = "Hey, I'm searching for the keyword 'cake'!";
  100. ASSERT_TRUE(text.contains("cake"));
  101. }
  102. TEST_F(StringTest, contains_does_not_contain_search_word)
  103. {
  104. ls::std::boxing::String text{};
  105. text = "Hey, I'm searching for the keyword 'cake'!";
  106. ASSERT_FALSE(text.contains("butter"));
  107. }
  108. TEST_F(StringTest, endsWith)
  109. {
  110. ls::std::boxing::String text{};
  111. text = "abcdef";
  112. ASSERT_TRUE(text.endsWith("ef"));
  113. }
  114. TEST_F(StringTest, endsWith_does_not_end_with_pattern)
  115. {
  116. ls::std::boxing::String text{};
  117. text = "abcdef";
  118. ASSERT_FALSE(text.endsWith("efg"));
  119. }
  120. TEST_F(StringTest, equalsIgnoreCase)
  121. {
  122. ls::std::boxing::String text{"Hello!"};
  123. ls::std::boxing::String hello{"HeLLo!"};
  124. ASSERT_TRUE(text.equalsIgnoreCase(hello));
  125. ASSERT_TRUE(text.equalsIgnoreCase("HeLLO!"));
  126. }
  127. TEST_F(StringTest, getByteData)
  128. {
  129. ls::std::boxing::String text{"Hallo!"};
  130. ASSERT_STREQ("Hallo!", text.getByteData().data());
  131. }
  132. TEST_F(StringTest, padLeft)
  133. {
  134. ls::std::boxing::String text{"abcdef"};
  135. ls::std::boxing::String anotherText{"ab"};
  136. ls::std::boxing::String emptyText{};
  137. ls::std::boxing::String longText{"This text is too long to fill!"};
  138. ASSERT_STREQ(" abcdef", text.padLeft(10, ' ').c_str());
  139. ASSERT_STREQ(" ab", anotherText.padLeft(10, ' ').c_str());
  140. ASSERT_STREQ(" ", emptyText.padLeft(10, ' ').c_str());
  141. ASSERT_STREQ("This text is too long to fill!", longText.padLeft(10, ' ').c_str());
  142. }
  143. TEST_F(StringTest, padRight)
  144. {
  145. ls::std::boxing::String text{"abcdef"};
  146. ls::std::boxing::String anotherText{"ab"};
  147. ls::std::boxing::String emptyText{};
  148. ls::std::boxing::String longText{"This text is too long to fill!"};
  149. ASSERT_STREQ("abcdef ", text.padRight(10, ' ').c_str());
  150. ASSERT_STREQ("ab ", anotherText.padRight(10, ' ').c_str());
  151. ASSERT_STREQ(" ", emptyText.padRight(10, ' ').c_str());
  152. ASSERT_STREQ("This text is too long to fill!", longText.padRight(10, ' ').c_str());
  153. }
  154. TEST_F(StringTest, reverse)
  155. {
  156. ls::std::boxing::String text{};
  157. text = "abcdef";
  158. ASSERT_STREQ("fedcba", text.reverse().c_str());
  159. ASSERT_STREQ("abcdef", text); // verify, that original string didn't change
  160. }
  161. TEST_F(StringTest, startsWith)
  162. {
  163. ls::std::boxing::String text{};
  164. text = "abcdef";
  165. ASSERT_TRUE(text.startsWith("abc"));
  166. }
  167. TEST_F(StringTest, startsWith_does_not_start_with_pattern)
  168. {
  169. ls::std::boxing::String text{};
  170. text = "abcdef";
  171. ASSERT_FALSE(text.startsWith("bc"));
  172. }
  173. TEST_F(StringTest, toLowerCase)
  174. {
  175. ls::std::boxing::String text{};
  176. text = "aBCdeFgHIJKLmn";
  177. ASSERT_STREQ("abcdefghijklmn", text.toLowerCase().c_str());
  178. ASSERT_STREQ("aBCdeFgHIJKLmn", text); // verify, that original String didn't change
  179. }
  180. TEST_F(StringTest, toUpperCase)
  181. {
  182. ls::std::boxing::String text{};
  183. text = "aBCdeFgHIJKLmn";
  184. ASSERT_STREQ("ABCDEFGHIJKLMN", text.toUpperCase().c_str());
  185. ASSERT_STREQ("aBCdeFgHIJKLmn", text); // verify, that original String didn't change
  186. }
  187. }