StringTest.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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-14
  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, reverse)
  115. {
  116. ls_std::String text {};
  117. text = "abcdef";
  118. ASSERT_STREQ("fedcba", text.reverse().c_str());
  119. ASSERT_STREQ("abcdef", text);
  120. }
  121. TEST_F(StringTest, startsWith)
  122. {
  123. ls_std::String text {};
  124. text = "abcdef";
  125. ASSERT_TRUE(text.startsWith("abc"));
  126. }
  127. TEST_F(StringTest, startsWithNegative)
  128. {
  129. ls_std::String text {};
  130. text = "abcdef";
  131. ASSERT_FALSE(text.startsWith("bc"));
  132. }
  133. TEST_F(StringTest, toLowerCase)
  134. {
  135. ls_std::String text {};
  136. text = "aBCdeFgHIJKLmn";
  137. ASSERT_STREQ("abcdefghijklmn", text.toLowerCase().c_str());
  138. ASSERT_STREQ("aBCdeFgHIJKLmn", text);
  139. }
  140. TEST_F(StringTest, toUpperCase)
  141. {
  142. ls_std::String text {};
  143. text = "aBCdeFgHIJKLmn";
  144. ASSERT_STREQ("ABCDEFGHIJKLMN", text.toUpperCase().c_str());
  145. ASSERT_STREQ("aBCdeFgHIJKLmn", text);
  146. }
  147. }