StringTest.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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-21
  7. *
  8. * */
  9. #include <gtest/gtest.h>
  10. #include "../../../source/boxing/String.hpp"
  11. namespace {
  12. class StringTest : public ::testing::Test {
  13. protected:
  14. StringTest() = default;
  15. ~StringTest() override = default;
  16. void SetUp() override {}
  17. void TearDown() override {}
  18. };
  19. // assignment operators
  20. TEST_F(StringTest, operatorAssignment)
  21. {
  22. ls_std::String text {};
  23. text = "Hi!";
  24. ASSERT_STREQ("Hi!", text.toString().c_str());
  25. }
  26. // arithmetic operators
  27. TEST_F(StringTest, operatorAdd)
  28. {
  29. ls_std::String text {"Hello! "};
  30. ls_std::String end {"How are you? "};
  31. text = text + end + "I'm good by the way!";
  32. ASSERT_STREQ("Hello! How are you? I'm good by the way!", text.toString().c_str());
  33. }
  34. TEST_F(StringTest, operatorHyphen)
  35. {
  36. ls_std::String text {"abcdefghij"};
  37. text = text - 5;
  38. ASSERT_STREQ("abcde", text.toString().c_str());
  39. }
  40. // compound operators
  41. TEST_F(StringTest, operatorAddEqual)
  42. {
  43. ls_std::String text {};
  44. ls_std::String hello {"Hi! "};
  45. ASSERT_STREQ("", text.toString().c_str());
  46. text += hello;
  47. ASSERT_STREQ("Hi! ", text.toString().c_str());
  48. text += "Bye!";
  49. ASSERT_STREQ("Hi! Bye!", text.toString().c_str());
  50. }
  51. // comparison operators
  52. TEST_F(StringTest, operatorEqual)
  53. {
  54. ls_std::String text {"Hi!"};
  55. ls_std::String hello {"Hi!"};
  56. ASSERT_TRUE(text == hello);
  57. ASSERT_TRUE(hello == text);
  58. ASSERT_TRUE(hello == std::string("Hi!"));
  59. ASSERT_TRUE(hello == "Hi!");
  60. }
  61. TEST_F(StringTest, operatorNotEqual)
  62. {
  63. ls_std::String text {"Hi!"};
  64. ls_std::String hello {"Hello!"};
  65. ASSERT_TRUE(text != hello);
  66. ASSERT_TRUE(hello != text);
  67. ASSERT_TRUE(text != std::string("Hello!"));
  68. ASSERT_TRUE(text != "Hello!");
  69. }
  70. // implementation
  71. TEST_F(StringTest, parse)
  72. {
  73. ls_std::String text {};
  74. text.parse("Hello!");
  75. ASSERT_STREQ("Hello!", text.toString().c_str());
  76. }
  77. TEST_F(StringTest, toString)
  78. {
  79. ls_std::String text {"Hello!"};
  80. ASSERT_STREQ("Hello!", text.toString().c_str());
  81. }
  82. // additional functionality
  83. TEST_F(StringTest, contains)
  84. {
  85. ls_std::String text {};
  86. text = "Hey, I'm searching for the keyword 'cake'!";
  87. ASSERT_TRUE(text.contains("cake"));
  88. }
  89. TEST_F(StringTest, containsNegative)
  90. {
  91. ls_std::String text {};
  92. text = "Hey, I'm searching for the keyword 'cake'!";
  93. ASSERT_FALSE(text.contains("butter"));
  94. }
  95. TEST_F(StringTest, endsWith)
  96. {
  97. ls_std::String text {};
  98. text = "abcdef";
  99. ASSERT_TRUE(text.endsWith("ef"));
  100. }
  101. TEST_F(StringTest, equalsIgnoreCase)
  102. {
  103. ls_std::String text {"Hello!"};
  104. ls_std::String hello {"HeLLo!"};
  105. ASSERT_TRUE(text.equalsIgnoreCase(hello));
  106. ASSERT_TRUE(text.equalsIgnoreCase("HeLLO!"));
  107. }
  108. TEST_F(StringTest, endsWithNegative)
  109. {
  110. ls_std::String text {};
  111. text = "abcdef";
  112. ASSERT_FALSE(text.endsWith("efg"));
  113. }
  114. TEST_F(StringTest, padLeft)
  115. {
  116. ls_std::String text {"abcdef"};
  117. ls_std::String anotherText {"ab"};
  118. ls_std::String emptyText {};
  119. ls_std::String longText {"This text is too long to fill!"};
  120. ASSERT_STREQ(" abcdef", text.padLeft(10, ' ').c_str());
  121. ASSERT_STREQ(" ab", anotherText.padLeft(10, ' ').c_str());
  122. ASSERT_STREQ(" ", emptyText.padLeft(10, ' ').c_str());
  123. ASSERT_STREQ("This text is too long to fill!", longText.padLeft(10, ' ').c_str());
  124. }
  125. TEST_F(StringTest, padRight)
  126. {
  127. ls_std::String text {"abcdef"};
  128. ls_std::String anotherText {"ab"};
  129. ls_std::String emptyText {};
  130. ls_std::String longText {"This text is too long to fill!"};
  131. ASSERT_STREQ("abcdef ", text.padRight(10, ' ').c_str());
  132. ASSERT_STREQ("ab ", anotherText.padRight(10, ' ').c_str());
  133. ASSERT_STREQ(" ", emptyText.padRight(10, ' ').c_str());
  134. ASSERT_STREQ("This text is too long to fill!", longText.padRight(10, ' ').c_str());
  135. }
  136. TEST_F(StringTest, reverse)
  137. {
  138. ls_std::String text {};
  139. text = "abcdef";
  140. ASSERT_STREQ("fedcba", text.reverse().c_str());
  141. ASSERT_STREQ("abcdef", text);
  142. }
  143. TEST_F(StringTest, startsWith)
  144. {
  145. ls_std::String text {};
  146. text = "abcdef";
  147. ASSERT_TRUE(text.startsWith("abc"));
  148. }
  149. TEST_F(StringTest, startsWithNegative)
  150. {
  151. ls_std::String text {};
  152. text = "abcdef";
  153. ASSERT_FALSE(text.startsWith("bc"));
  154. }
  155. TEST_F(StringTest, toLowerCase)
  156. {
  157. ls_std::String text {};
  158. text = "aBCdeFgHIJKLmn";
  159. ASSERT_STREQ("abcdefghijklmn", text.toLowerCase().c_str());
  160. ASSERT_STREQ("aBCdeFgHIJKLmn", text);
  161. }
  162. TEST_F(StringTest, toUpperCase)
  163. {
  164. ls_std::String text {};
  165. text = "aBCdeFgHIJKLmn";
  166. ASSERT_STREQ("ABCDEFGHIJKLMN", text.toUpperCase().c_str());
  167. ASSERT_STREQ("aBCdeFgHIJKLmn", text);
  168. }
  169. }