git.sr.ht/~pingoo/stdx@v0.0.0-20240218134121-094174641f6e/validate/utils_test.go (about) 1 package validate 2 3 import ( 4 "reflect" 5 "testing" 6 ) 7 8 func TestContains(t *testing.T) { 9 t.Parallel() 10 11 var tests = []struct { 12 param1 string 13 param2 string 14 expected bool 15 }{ 16 {"abacada", "", true}, 17 {"abacada", "ritir", false}, 18 {"abacada", "a", true}, 19 {"abacada", "aca", true}, 20 } 21 for _, test := range tests { 22 actual := Contains(test.param1, test.param2) 23 if actual != test.expected { 24 t.Errorf("Expected Contains(%q,%q) to be %v, got %v", test.param1, test.param2, test.expected, actual) 25 } 26 } 27 } 28 29 func TestMatches(t *testing.T) { 30 t.Parallel() 31 32 var tests = []struct { 33 param1 string 34 param2 string 35 expected bool 36 }{ 37 {"123456789", "[0-9]+", true}, 38 {"abacada", "cab$", false}, 39 {"111222333", "((111|222|333)+)+", true}, 40 {"abacaba", "((123+]", false}, 41 } 42 for _, test := range tests { 43 actual := Matches(test.param1, test.param2) 44 if actual != test.expected { 45 t.Errorf("Expected Matches(%q,%q) to be %v, got %v", test.param1, test.param2, test.expected, actual) 46 } 47 } 48 } 49 50 func TestLeftTrim(t *testing.T) { 51 t.Parallel() 52 53 var tests = []struct { 54 param1 string 55 param2 string 56 expected string 57 }{ 58 {" \r\n\tfoo \r\n\t ", "", "foo \r\n\t "}, 59 {"010100201000", "01", "201000"}, 60 } 61 for _, test := range tests { 62 actual := LeftTrim(test.param1, test.param2) 63 if actual != test.expected { 64 t.Errorf("Expected LeftTrim(%q,%q) to be %v, got %v", test.param1, test.param2, test.expected, actual) 65 } 66 } 67 } 68 69 func TestRightTrim(t *testing.T) { 70 t.Parallel() 71 72 var tests = []struct { 73 param1 string 74 param2 string 75 expected string 76 }{ 77 {" \r\n\tfoo \r\n\t ", "", " \r\n\tfoo"}, 78 {"010100201000", "01", "0101002"}, 79 } 80 for _, test := range tests { 81 actual := RightTrim(test.param1, test.param2) 82 if actual != test.expected { 83 t.Errorf("Expected RightTrim(%q,%q) to be %v, got %v", test.param1, test.param2, test.expected, actual) 84 } 85 } 86 } 87 88 func TestTrim(t *testing.T) { 89 t.Parallel() 90 91 var tests = []struct { 92 param1 string 93 param2 string 94 expected string 95 }{ 96 {" \r\n\tfoo \r\n\t ", "", "foo"}, 97 {"010100201000", "01", "2"}, 98 {"1234567890987654321", "1-8", "909"}, 99 } 100 for _, test := range tests { 101 actual := Trim(test.param1, test.param2) 102 if actual != test.expected { 103 t.Errorf("Expected Trim(%q,%q) to be %v, got %v", test.param1, test.param2, test.expected, actual) 104 } 105 } 106 } 107 108 func TestWhiteList(t *testing.T) { 109 t.Parallel() 110 111 var tests = []struct { 112 param1 string 113 param2 string 114 expected string 115 }{ 116 {"abcdef", "abc", "abc"}, 117 {"aaaaaaaaaabbbbbbbbbb", "abc", "aaaaaaaaaabbbbbbbbbb"}, 118 {"a1b2c3", "abc", "abc"}, 119 {" ", "abc", ""}, 120 {"a3a43a5a4a3a2a23a4a5a4a3a4", "a-z", "aaaaaaaaaaaa"}, 121 } 122 for _, test := range tests { 123 actual := WhiteList(test.param1, test.param2) 124 if actual != test.expected { 125 t.Errorf("Expected WhiteList(%q,%q) to be %v, got %v", test.param1, test.param2, test.expected, actual) 126 } 127 } 128 } 129 130 func TestBlackList(t *testing.T) { 131 t.Parallel() 132 133 var tests = []struct { 134 param1 string 135 param2 string 136 expected string 137 }{ 138 {"abcdef", "abc", "def"}, 139 {"aaaaaaaaaabbbbbbbbbb", "abc", ""}, 140 {"a1b2c3", "abc", "123"}, 141 {" ", "abc", " "}, 142 {"a3a43a5a4a3a2a23a4a5a4a3a4", "a-z", "34354322345434"}, 143 } 144 for _, test := range tests { 145 actual := BlackList(test.param1, test.param2) 146 if actual != test.expected { 147 t.Errorf("Expected BlackList(%q,%q) to be %v, got %v", test.param1, test.param2, test.expected, actual) 148 } 149 } 150 } 151 152 func TestStripLow(t *testing.T) { 153 t.Parallel() 154 155 var tests = []struct { 156 param1 string 157 param2 bool 158 expected string 159 }{ 160 {"foo\x00", false, "foo"}, 161 {"\x7Ffoo\x02", false, "foo"}, 162 {"\x01\x09", false, ""}, 163 {"foo\x0A\x0D", false, "foo"}, 164 {"perch\u00e9", false, "perch\u00e9"}, 165 {"\u20ac", false, "\u20ac"}, 166 {"\u2206\x0A", false, "\u2206"}, 167 {"foo\x0A\x0D", true, "foo\x0A\x0D"}, 168 {"\x03foo\x0A\x0D", true, "foo\x0A\x0D"}, 169 } 170 for _, test := range tests { 171 actual := StripLow(test.param1, test.param2) 172 if actual != test.expected { 173 t.Errorf("Expected StripLow(%q,%t) to be %v, got %v", test.param1, test.param2, test.expected, actual) 174 } 175 } 176 } 177 178 func TestReplacePattern(t *testing.T) { 179 t.Parallel() 180 181 var tests = []struct { 182 param1 string 183 param2 string 184 param3 string 185 expected string 186 }{ 187 {"ab123ba", "[0-9]+", "aca", "abacaba"}, 188 {"abacaba", "[0-9]+", "aca", "abacaba"}, 189 {"httpftp://github.comio", "(ftp|io)", "", "http://github.com"}, 190 {"aaaaaaaaaa", "a", "", ""}, 191 {"http123123ftp://git534543hub.comio", "(ftp|io|[0-9]+)", "", "http://github.com"}, 192 } 193 for _, test := range tests { 194 actual := ReplacePattern(test.param1, test.param2, test.param3) 195 if actual != test.expected { 196 t.Errorf("Expected ReplacePattern(%q,%q,%q) to be %v, got %v", test.param1, test.param2, test.param3, test.expected, actual) 197 } 198 } 199 } 200 201 func TestEscape(t *testing.T) { 202 t.Parallel() 203 204 var tests = []struct { 205 param string 206 expected string 207 }{ 208 {`<img alt="foo&bar">`, "<img alt="foo&bar">"}, 209 } 210 for _, test := range tests { 211 actual := Escape(test.param) 212 if actual != test.expected { 213 t.Errorf("Expected Escape(%q) to be %v, got %v", test.param, test.expected, actual) 214 } 215 } 216 } 217 218 func TestUnderscoreToCamelCase(t *testing.T) { 219 t.Parallel() 220 221 var tests = []struct { 222 param string 223 expected string 224 }{ 225 {"a_b_c", "ABC"}, 226 {"my_func", "MyFunc"}, 227 {"1ab_cd", "1abCd"}, 228 } 229 for _, test := range tests { 230 actual := UnderscoreToCamelCase(test.param) 231 if actual != test.expected { 232 t.Errorf("Expected UnderscoreToCamelCase(%q) to be %v, got %v", test.param, test.expected, actual) 233 } 234 } 235 } 236 237 func TestCamelCaseToUnderscore(t *testing.T) { 238 t.Parallel() 239 240 var tests = []struct { 241 param string 242 expected string 243 }{ 244 {"MyFunc", "my_func"}, 245 {"ABC", "a_b_c"}, 246 {"1B", "1_b"}, 247 {"foo_bar", "foo_bar"}, 248 {"FooV2Bar", "foo_v2_bar"}, 249 } 250 for _, test := range tests { 251 actual := CamelCaseToUnderscore(test.param) 252 if actual != test.expected { 253 t.Errorf("Expected CamelCaseToUnderscore(%q) to be %v, got %v", test.param, test.expected, actual) 254 } 255 } 256 } 257 258 func TestReverse(t *testing.T) { 259 t.Parallel() 260 261 var tests = []struct { 262 param string 263 expected string 264 }{ 265 {"abc", "cba"}, 266 {"カタカナ", "ナカタカ"}, 267 } 268 for _, test := range tests { 269 actual := Reverse(test.param) 270 if actual != test.expected { 271 t.Errorf("Expected Reverse(%q) to be %v, got %v", test.param, test.expected, actual) 272 } 273 } 274 } 275 276 func TestGetLines(t *testing.T) { 277 t.Parallel() 278 279 var tests = []struct { 280 param string 281 expected []string 282 }{ 283 {"abc", []string{"abc"}}, 284 {"a\nb\nc", []string{"a", "b", "c"}}, 285 } 286 for _, test := range tests { 287 actual := GetLines(test.param) 288 if !reflect.DeepEqual(actual, test.expected) { 289 t.Errorf("Expected GetLines(%q) to be %v, got %v", test.param, test.expected, actual) 290 } 291 } 292 } 293 294 func TestGetLine(t *testing.T) { 295 t.Parallel() 296 297 var tests = []struct { 298 param1 string 299 param2 int 300 expected string 301 }{ 302 {"abc", 0, "abc"}, 303 {"a\nb\nc", 0, "a"}, 304 {"abc", -1, ""}, 305 {"abacaba\n", 1, ""}, 306 {"abc", 3, ""}, 307 } 308 for _, test := range tests { 309 actual, _ := GetLine(test.param1, test.param2) 310 if actual != test.expected { 311 t.Errorf("Expected GetLine(%q, %d) to be %v, got %v", test.param1, test.param2, test.expected, actual) 312 } 313 } 314 } 315 316 func TestRemoveTags(t *testing.T) { 317 t.Parallel() 318 319 var tests = []struct { 320 param string 321 expected string 322 }{ 323 {"abc", "abc"}, 324 {"<!-- Test -->", ""}, 325 {"<div><div><p><a>Text</a></p></div></div>", "Text"}, 326 {`<a href="#">Link</a>`, "Link"}, 327 } 328 for _, test := range tests { 329 actual := RemoveTags(test.param) 330 if actual != test.expected { 331 t.Errorf("Expected RemoveTags(%q) to be %v, got %v", test.param, test.expected, actual) 332 } 333 } 334 } 335 336 func TestSafeFileName(t *testing.T) { 337 t.Parallel() 338 339 var tests = []struct { 340 param string 341 expected string 342 }{ 343 {"abc", "abc"}, 344 {"123456789 '_-?ASDF@£$%£%^é.html", "123456789-asdf.html"}, 345 {"ReadMe.md", "readme.md"}, 346 {"file:///c:/test.go", "test.go"}, 347 {"../../../Hello World!.txt", "hello-world.txt"}, 348 } 349 for _, test := range tests { 350 actual := SafeFileName(test.param) 351 if actual != test.expected { 352 t.Errorf("Expected SafeFileName(%q) to be %v, got %v", test.param, test.expected, actual) 353 } 354 } 355 } 356 357 func TestNormalizeEmail(t *testing.T) { 358 t.Parallel() 359 360 var tests = []struct { 361 param string 362 expected string 363 }{ 364 {`test@me.com`, `test@me.com`}, 365 {`some.name@gmail.com`, `somename@gmail.com`}, 366 {`some.name@googlemail.com`, `somename@gmail.com`}, 367 {`some.name+extension@gmail.com`, `somename@gmail.com`}, 368 {`some.name+extension@googlemail.com`, `somename@gmail.com`}, 369 {`some.name.middlename+extension@gmail.com`, `somenamemiddlename@gmail.com`}, 370 {`some.name.middlename+extension@googlemail.com`, `somenamemiddlename@gmail.com`}, 371 {`some.name.midd.lena.me.+extension@gmail.com`, `somenamemiddlename@gmail.com`}, 372 {`some.name.midd.lena.me.+extension@googlemail.com`, `somenamemiddlename@gmail.com`}, 373 {`some.name+extension@unknown.com`, `some.name+extension@unknown.com`}, 374 // TODO: {`hans@m端ller.com`, `hans@m端ller.com`}, 375 {`hans`, ``}, 376 } 377 for _, test := range tests { 378 actual, err := NormalizeEmail(test.param) 379 if actual != test.expected { 380 t.Errorf("Expected NormalizeEmail(%q) to be %v, got %v, err %v", test.param, test.expected, actual, err) 381 } 382 } 383 } 384 385 func TestTruncate(t *testing.T) { 386 t.Parallel() 387 388 var tests = []struct { 389 param1 string 390 param2 int 391 param3 string 392 expected string 393 }{ 394 {`Lorem ipsum dolor sit amet, consectetur adipiscing elit.`, 25, `...`, `Lorem ipsum dolor sit amet...`}, 395 {`Measuring programming progress by lines of code is like measuring aircraft building progress by weight.`, 35, ` new born babies!`, `Measuring programming progress by new born babies!`}, 396 {`Testestestestestestestestestest testestestestestestestestest`, 7, `...`, `Testestestestestestestestestest...`}, 397 {`Testing`, 7, `...`, `Testing`}, 398 } 399 for _, test := range tests { 400 actual := Truncate(test.param1, test.param2, test.param3) 401 if actual != test.expected { 402 t.Errorf("Expected Truncate(%q, %d, %q) to be %v, got %v", test.param1, test.param2, test.param3, test.expected, actual) 403 } 404 } 405 } 406 407 func TestPadLeft(t *testing.T) { 408 t.Parallel() 409 410 var tests = []struct { 411 param1 string 412 param2 string 413 param3 int 414 expected string 415 }{ 416 {"こんにちは", "xyz", 12, "xyzxyzxこんにちは"}, 417 {"こんにちは", "xyz", 11, "xyzxyzこんにちは"}, 418 {"abc", "x", 5, "xxabc"}, 419 {"abc", "xyz", 5, "xyabc"}, 420 {"abcde", "xyz", 5, "abcde"}, 421 {"abcde", "xyz", 4, "abcde"}, 422 } 423 for _, test := range tests { 424 actual := PadLeft(test.param1, test.param2, test.param3) 425 if actual != test.expected { 426 t.Errorf("Expected PadLeft(%q,%q,%q) to be %v, got %v", test.param1, test.param2, test.param3, test.expected, actual) 427 } 428 } 429 } 430 431 func TestPadRight(t *testing.T) { 432 t.Parallel() 433 434 var tests = []struct { 435 param1 string 436 param2 string 437 param3 int 438 expected string 439 }{ 440 {"こんにちは", "xyz", 12, "こんにちはxyzxyzx"}, 441 {"こんにちは", "xyz", 11, "こんにちはxyzxyz"}, 442 {"abc", "x", 5, "abcxx"}, 443 {"abc", "xyz", 5, "abcxy"}, 444 {"abcde", "xyz", 5, "abcde"}, 445 {"abcde", "xyz", 4, "abcde"}, 446 } 447 for _, test := range tests { 448 actual := PadRight(test.param1, test.param2, test.param3) 449 if actual != test.expected { 450 t.Errorf("Expected PadRight(%q,%q,%q) to be %v, got %v", test.param1, test.param2, test.param3, test.expected, actual) 451 } 452 } 453 } 454 455 func TestPadBoth(t *testing.T) { 456 t.Parallel() 457 458 var tests = []struct { 459 param1 string 460 param2 string 461 param3 int 462 expected string 463 }{ 464 {"こんにちは", "xyz", 12, "xyzこんにちはxyzx"}, 465 {"こんにちは", "xyz", 11, "xyzこんにちはxyz"}, 466 {"abc", "x", 5, "xabcx"}, 467 {"abc", "xyz", 5, "xabcx"}, 468 {"abcde", "xyz", 5, "abcde"}, 469 {"abcde", "xyz", 4, "abcde"}, 470 } 471 for _, test := range tests { 472 actual := PadBoth(test.param1, test.param2, test.param3) 473 if actual != test.expected { 474 t.Errorf("Expected PadBoth(%q,%q,%q) to be %v, got %v", test.param1, test.param2, test.param3, test.expected, actual) 475 } 476 } 477 }