StringUtils.Tests.cpp
1 #include "pch.h" 2 #include "TestHelpers.h" 3 #include <string_utils.h> 4 5 using namespace Microsoft::VisualStudio::CppUnitTestFramework; 6 7 namespace UnitTestsCommonUtils 8 { 9 TEST_CLASS(StringUtilsTests) 10 { 11 public: 12 // left_trim tests 13 TEST_METHOD(LeftTrim_EmptyString_ReturnsEmpty) 14 { 15 std::string_view input = ""; 16 auto result = left_trim(input); 17 Assert::AreEqual(std::string_view(""), result); 18 } 19 20 TEST_METHOD(LeftTrim_NoWhitespace_ReturnsOriginal) 21 { 22 std::string_view input = "hello"; 23 auto result = left_trim(input); 24 Assert::AreEqual(std::string_view("hello"), result); 25 } 26 27 TEST_METHOD(LeftTrim_LeadingSpaces_TrimsSpaces) 28 { 29 std::string_view input = " hello"; 30 auto result = left_trim(input); 31 Assert::AreEqual(std::string_view("hello"), result); 32 } 33 34 TEST_METHOD(LeftTrim_LeadingTabs_TrimsTabs) 35 { 36 std::string_view input = "\t\thello"; 37 auto result = left_trim(input); 38 Assert::AreEqual(std::string_view("hello"), result); 39 } 40 41 TEST_METHOD(LeftTrim_LeadingNewlines_TrimsNewlines) 42 { 43 std::string_view input = "\r\n\nhello"; 44 auto result = left_trim(input); 45 Assert::AreEqual(std::string_view("hello"), result); 46 } 47 48 TEST_METHOD(LeftTrim_MixedWhitespace_TrimsAll) 49 { 50 std::string_view input = " \t\r\nhello"; 51 auto result = left_trim(input); 52 Assert::AreEqual(std::string_view("hello"), result); 53 } 54 55 TEST_METHOD(LeftTrim_TrailingWhitespace_PreservesTrailing) 56 { 57 std::string_view input = " hello "; 58 auto result = left_trim(input); 59 Assert::AreEqual(std::string_view("hello "), result); 60 } 61 62 TEST_METHOD(LeftTrim_OnlyWhitespace_ReturnsEmpty) 63 { 64 std::string_view input = " \t\r\n"; 65 auto result = left_trim(input); 66 Assert::AreEqual(std::string_view(""), result); 67 } 68 69 TEST_METHOD(LeftTrim_CustomChars_TrimsSpecified) 70 { 71 std::string_view input = "xxxhello"; 72 auto result = left_trim(input, std::string_view("x")); 73 Assert::AreEqual(std::string_view("hello"), result); 74 } 75 76 TEST_METHOD(LeftTrim_WideString_Works) 77 { 78 std::wstring_view input = L" hello"; 79 auto result = left_trim(input); 80 Assert::AreEqual(std::wstring_view(L"hello"), result); 81 } 82 83 // right_trim tests 84 TEST_METHOD(RightTrim_EmptyString_ReturnsEmpty) 85 { 86 std::string_view input = ""; 87 auto result = right_trim(input); 88 Assert::AreEqual(std::string_view(""), result); 89 } 90 91 TEST_METHOD(RightTrim_NoWhitespace_ReturnsOriginal) 92 { 93 std::string_view input = "hello"; 94 auto result = right_trim(input); 95 Assert::AreEqual(std::string_view("hello"), result); 96 } 97 98 TEST_METHOD(RightTrim_TrailingSpaces_TrimsSpaces) 99 { 100 std::string_view input = "hello "; 101 auto result = right_trim(input); 102 Assert::AreEqual(std::string_view("hello"), result); 103 } 104 105 TEST_METHOD(RightTrim_TrailingTabs_TrimsTabs) 106 { 107 std::string_view input = "hello\t\t"; 108 auto result = right_trim(input); 109 Assert::AreEqual(std::string_view("hello"), result); 110 } 111 112 TEST_METHOD(RightTrim_TrailingNewlines_TrimsNewlines) 113 { 114 std::string_view input = "hello\r\n\n"; 115 auto result = right_trim(input); 116 Assert::AreEqual(std::string_view("hello"), result); 117 } 118 119 TEST_METHOD(RightTrim_LeadingWhitespace_PreservesLeading) 120 { 121 std::string_view input = " hello "; 122 auto result = right_trim(input); 123 Assert::AreEqual(std::string_view(" hello"), result); 124 } 125 126 TEST_METHOD(RightTrim_OnlyWhitespace_ReturnsEmpty) 127 { 128 std::string_view input = " \t\r\n"; 129 auto result = right_trim(input); 130 Assert::AreEqual(std::string_view(""), result); 131 } 132 133 TEST_METHOD(RightTrim_CustomChars_TrimsSpecified) 134 { 135 std::string_view input = "helloxxx"; 136 auto result = right_trim(input, std::string_view("x")); 137 Assert::AreEqual(std::string_view("hello"), result); 138 } 139 140 TEST_METHOD(RightTrim_WideString_Works) 141 { 142 std::wstring_view input = L"hello "; 143 auto result = right_trim(input); 144 Assert::AreEqual(std::wstring_view(L"hello"), result); 145 } 146 147 // trim tests 148 TEST_METHOD(Trim_EmptyString_ReturnsEmpty) 149 { 150 std::string_view input = ""; 151 auto result = trim(input); 152 Assert::AreEqual(std::string_view(""), result); 153 } 154 155 TEST_METHOD(Trim_NoWhitespace_ReturnsOriginal) 156 { 157 std::string_view input = "hello"; 158 auto result = trim(input); 159 Assert::AreEqual(std::string_view("hello"), result); 160 } 161 162 TEST_METHOD(Trim_BothSides_TrimsBoth) 163 { 164 std::string_view input = " hello "; 165 auto result = trim(input); 166 Assert::AreEqual(std::string_view("hello"), result); 167 } 168 169 TEST_METHOD(Trim_MixedWhitespace_TrimsAll) 170 { 171 std::string_view input = " \t\r\nhello \t\r\n"; 172 auto result = trim(input); 173 Assert::AreEqual(std::string_view("hello"), result); 174 } 175 176 TEST_METHOD(Trim_InternalWhitespace_Preserved) 177 { 178 std::string_view input = " hello world "; 179 auto result = trim(input); 180 Assert::AreEqual(std::string_view("hello world"), result); 181 } 182 183 TEST_METHOD(Trim_OnlyWhitespace_ReturnsEmpty) 184 { 185 std::string_view input = " \t\r\n "; 186 auto result = trim(input); 187 Assert::AreEqual(std::string_view(""), result); 188 } 189 190 TEST_METHOD(Trim_CustomChars_TrimsSpecified) 191 { 192 std::string_view input = "xxxhelloxxx"; 193 auto result = trim(input, std::string_view("x")); 194 Assert::AreEqual(std::string_view("hello"), result); 195 } 196 197 TEST_METHOD(Trim_WideString_Works) 198 { 199 std::wstring_view input = L" hello "; 200 auto result = trim(input); 201 Assert::AreEqual(std::wstring_view(L"hello"), result); 202 } 203 204 // replace_chars tests 205 TEST_METHOD(ReplaceChars_EmptyString_NoChange) 206 { 207 std::string s = ""; 208 replace_chars(s, std::string_view("abc"), 'x'); 209 Assert::AreEqual(std::string(""), s); 210 } 211 212 TEST_METHOD(ReplaceChars_NoMatchingChars_NoChange) 213 { 214 std::string s = "hello"; 215 replace_chars(s, std::string_view("xyz"), '_'); 216 Assert::AreEqual(std::string("hello"), s); 217 } 218 219 TEST_METHOD(ReplaceChars_SingleChar_Replaces) 220 { 221 std::string s = "hello"; 222 replace_chars(s, std::string_view("l"), '_'); 223 Assert::AreEqual(std::string("he__o"), s); 224 } 225 226 TEST_METHOD(ReplaceChars_MultipleChars_ReplacesAll) 227 { 228 std::string s = "hello world"; 229 replace_chars(s, std::string_view("lo"), '_'); 230 Assert::AreEqual(std::string("he___ w_r_d"), s); 231 } 232 233 TEST_METHOD(ReplaceChars_WideString_Works) 234 { 235 std::wstring s = L"hello"; 236 replace_chars(s, std::wstring_view(L"l"), L'_'); 237 Assert::AreEqual(std::wstring(L"he__o"), s); 238 } 239 240 // unwide tests 241 TEST_METHOD(Unwide_EmptyString_ReturnsEmpty) 242 { 243 std::wstring input = L""; 244 auto result = unwide(input); 245 Assert::AreEqual(std::string(""), result); 246 } 247 248 TEST_METHOD(Unwide_AsciiString_Converts) 249 { 250 std::wstring input = L"hello"; 251 auto result = unwide(input); 252 Assert::AreEqual(std::string("hello"), result); 253 } 254 255 TEST_METHOD(Unwide_WithNumbers_Converts) 256 { 257 std::wstring input = L"test123"; 258 auto result = unwide(input); 259 Assert::AreEqual(std::string("test123"), result); 260 } 261 262 TEST_METHOD(Unwide_WithSpecialChars_Converts) 263 { 264 std::wstring input = L"test!@#$%"; 265 auto result = unwide(input); 266 Assert::AreEqual(std::string("test!@#$%"), result); 267 } 268 269 TEST_METHOD(Unwide_MixedCase_PreservesCase) 270 { 271 std::wstring input = L"HeLLo WoRLd"; 272 auto result = unwide(input); 273 Assert::AreEqual(std::string("HeLLo WoRLd"), result); 274 } 275 276 TEST_METHOD(Unwide_LongString_Works) 277 { 278 std::wstring input = L"This is a longer string with multiple words and punctuation!"; 279 auto result = unwide(input); 280 Assert::AreEqual(std::string("This is a longer string with multiple words and punctuation!"), result); 281 } 282 }; 283 }