github.com/gogf/gf@v1.16.9/text/gstr/gstr_z_unit_basic_test.go (about) 1 // Copyright GoFrame Author(https://goframe.org). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with this file, 5 // You can obtain one at https://github.com/gogf/gf. 6 7 // go test *.go -bench=".*" 8 9 package gstr_test 10 11 import ( 12 "testing" 13 14 "github.com/gogf/gf/frame/g" 15 "github.com/gogf/gf/test/gtest" 16 "github.com/gogf/gf/text/gstr" 17 ) 18 19 func Test_Replace(t *testing.T) { 20 gtest.C(t, func(t *gtest.T) { 21 s1 := "abcdEFG乱入的中文abcdefg" 22 t.Assert(gstr.Replace(s1, "ab", "AB"), "ABcdEFG乱入的中文ABcdefg") 23 t.Assert(gstr.Replace(s1, "EF", "ef"), "abcdefG乱入的中文abcdefg") 24 t.Assert(gstr.Replace(s1, "MN", "mn"), s1) 25 26 t.Assert(gstr.ReplaceByArray(s1, g.ArrayStr{ 27 "a", "A", 28 "A", "-", 29 "a", 30 }), "-bcdEFG乱入的中文-bcdefg") 31 32 t.Assert(gstr.ReplaceByMap(s1, g.MapStrStr{ 33 "a": "A", 34 "G": "g", 35 }), "AbcdEFg乱入的中文Abcdefg") 36 }) 37 } 38 39 func Test_ReplaceI_1(t *testing.T) { 40 gtest.C(t, func(t *gtest.T) { 41 s1 := "abcd乱入的中文ABCD" 42 s2 := "a" 43 t.Assert(gstr.ReplaceI(s1, "ab", "aa"), "aacd乱入的中文aaCD") 44 t.Assert(gstr.ReplaceI(s1, "ab", "aa", 0), "abcd乱入的中文ABCD") 45 t.Assert(gstr.ReplaceI(s1, "ab", "aa", 1), "aacd乱入的中文ABCD") 46 47 t.Assert(gstr.ReplaceI(s1, "abcd", "-"), "-乱入的中文-") 48 t.Assert(gstr.ReplaceI(s1, "abcd", "-", 1), "-乱入的中文ABCD") 49 50 t.Assert(gstr.ReplaceI(s1, "abcd乱入的", ""), "中文ABCD") 51 t.Assert(gstr.ReplaceI(s1, "ABCD乱入的", ""), "中文ABCD") 52 53 t.Assert(gstr.ReplaceI(s2, "A", "-"), "-") 54 t.Assert(gstr.ReplaceI(s2, "a", "-"), "-") 55 56 t.Assert(gstr.ReplaceIByArray(s1, g.ArrayStr{ 57 "abcd乱入的", "-", 58 "-", "=", 59 "a", 60 }), "=中文ABCD") 61 62 t.Assert(gstr.ReplaceIByMap(s1, g.MapStrStr{ 63 "ab": "-", 64 "CD": "=", 65 }), "-=乱入的中文-=") 66 }) 67 } 68 69 func Test_ToLower(t *testing.T) { 70 gtest.C(t, func(t *gtest.T) { 71 s1 := "abcdEFG乱入的中文abcdefg" 72 e1 := "abcdefg乱入的中文abcdefg" 73 t.Assert(gstr.ToLower(s1), e1) 74 }) 75 } 76 77 func Test_ToUpper(t *testing.T) { 78 gtest.C(t, func(t *gtest.T) { 79 s1 := "abcdEFG乱入的中文abcdefg" 80 e1 := "ABCDEFG乱入的中文ABCDEFG" 81 t.Assert(gstr.ToUpper(s1), e1) 82 }) 83 } 84 85 func Test_UcFirst(t *testing.T) { 86 gtest.C(t, func(t *gtest.T) { 87 s1 := "abcdEFG乱入的中文abcdefg" 88 e1 := "AbcdEFG乱入的中文abcdefg" 89 t.Assert(gstr.UcFirst(""), "") 90 t.Assert(gstr.UcFirst(s1), e1) 91 t.Assert(gstr.UcFirst(e1), e1) 92 }) 93 } 94 95 func Test_LcFirst(t *testing.T) { 96 gtest.C(t, func(t *gtest.T) { 97 s1 := "AbcdEFG乱入的中文abcdefg" 98 e1 := "abcdEFG乱入的中文abcdefg" 99 t.Assert(gstr.LcFirst(""), "") 100 t.Assert(gstr.LcFirst(s1), e1) 101 t.Assert(gstr.LcFirst(e1), e1) 102 }) 103 } 104 105 func Test_UcWords(t *testing.T) { 106 gtest.C(t, func(t *gtest.T) { 107 s1 := "我爱GF: i love go frame" 108 e1 := "我爱GF: I Love Go Frame" 109 t.Assert(gstr.UcWords(s1), e1) 110 }) 111 } 112 113 func Test_IsLetterLower(t *testing.T) { 114 gtest.C(t, func(t *gtest.T) { 115 t.Assert(gstr.IsLetterLower('a'), true) 116 t.Assert(gstr.IsLetterLower('A'), false) 117 t.Assert(gstr.IsLetterLower('1'), false) 118 }) 119 } 120 121 func Test_IsLetterUpper(t *testing.T) { 122 gtest.C(t, func(t *gtest.T) { 123 t.Assert(gstr.IsLetterUpper('a'), false) 124 t.Assert(gstr.IsLetterUpper('A'), true) 125 t.Assert(gstr.IsLetterUpper('1'), false) 126 }) 127 } 128 129 func Test_IsNumeric(t *testing.T) { 130 gtest.C(t, func(t *gtest.T) { 131 t.Assert(gstr.IsNumeric("1a我"), false) 132 t.Assert(gstr.IsNumeric("0123"), true) 133 t.Assert(gstr.IsNumeric("我是中国人"), false) 134 }) 135 } 136 137 func Test_SubStr(t *testing.T) { 138 gtest.C(t, func(t *gtest.T) { 139 t.Assert(gstr.SubStr("我爱GoFrame", 0), "我爱GoFrame") 140 t.Assert(gstr.SubStr("我爱GoFrame", 6), "GoFrame") 141 t.Assert(gstr.SubStr("我爱GoFrame", 6, 2), "Go") 142 t.Assert(gstr.SubStr("我爱GoFrame", -1, 30), "我爱GoFrame") 143 t.Assert(gstr.SubStr("我爱GoFrame", 30, 30), "") 144 }) 145 } 146 147 func Test_SubStrRune(t *testing.T) { 148 gtest.C(t, func(t *gtest.T) { 149 t.Assert(gstr.SubStrRune("我爱GoFrame", 0), "我爱GoFrame") 150 t.Assert(gstr.SubStrRune("我爱GoFrame", 2), "GoFrame") 151 t.Assert(gstr.SubStrRune("我爱GoFrame", 2, 2), "Go") 152 t.Assert(gstr.SubStrRune("我爱GoFrame", -1, 30), "我爱GoFrame") 153 t.Assert(gstr.SubStrRune("我爱GoFrame", 30, 30), "") 154 }) 155 } 156 157 func Test_StrLimit(t *testing.T) { 158 gtest.C(t, func(t *gtest.T) { 159 t.Assert(gstr.StrLimit("我爱GoFrame", 6), "我爱...") 160 t.Assert(gstr.StrLimit("我爱GoFrame", 6, ""), "我爱") 161 t.Assert(gstr.StrLimit("我爱GoFrame", 6, "**"), "我爱**") 162 t.Assert(gstr.StrLimit("我爱GoFrame", 8, ""), "我爱Go") 163 t.Assert(gstr.StrLimit("*", 4, ""), "*") 164 }) 165 } 166 167 func Test_StrLimitRune(t *testing.T) { 168 gtest.C(t, func(t *gtest.T) { 169 t.Assert(gstr.StrLimitRune("我爱GoFrame", 2), "我爱...") 170 t.Assert(gstr.StrLimitRune("我爱GoFrame", 2, ""), "我爱") 171 t.Assert(gstr.StrLimitRune("我爱GoFrame", 2, "**"), "我爱**") 172 t.Assert(gstr.StrLimitRune("我爱GoFrame", 4, ""), "我爱Go") 173 t.Assert(gstr.StrLimitRune("*", 4, ""), "*") 174 }) 175 } 176 177 func Test_HasPrefix(t *testing.T) { 178 gtest.C(t, func(t *gtest.T) { 179 t.Assert(gstr.HasPrefix("我爱GoFrame", "我爱"), true) 180 t.Assert(gstr.HasPrefix("en我爱GoFrame", "我爱"), false) 181 t.Assert(gstr.HasPrefix("en我爱GoFrame", "en"), true) 182 }) 183 } 184 185 func Test_HasSuffix(t *testing.T) { 186 gtest.C(t, func(t *gtest.T) { 187 t.Assert(gstr.HasSuffix("我爱GoFrame", "GoFrame"), true) 188 t.Assert(gstr.HasSuffix("en我爱GoFrame", "a"), false) 189 t.Assert(gstr.HasSuffix("GoFrame很棒", "棒"), true) 190 }) 191 } 192 193 func Test_Reverse(t *testing.T) { 194 gtest.C(t, func(t *gtest.T) { 195 t.Assert(gstr.Reverse("我爱123"), "321爱我") 196 }) 197 } 198 199 func Test_NumberFormat(t *testing.T) { 200 gtest.C(t, func(t *gtest.T) { 201 t.Assert(gstr.NumberFormat(1234567.8910, 2, ".", ","), "1,234,567.89") 202 t.Assert(gstr.NumberFormat(1234567.8910, 2, "#", "/"), "1/234/567#89") 203 t.Assert(gstr.NumberFormat(-1234567.8910, 2, "#", "/"), "-1/234/567#89") 204 }) 205 } 206 207 func Test_ChunkSplit(t *testing.T) { 208 gtest.C(t, func(t *gtest.T) { 209 t.Assert(gstr.ChunkSplit("1234", 1, "#"), "1#2#3#4#") 210 t.Assert(gstr.ChunkSplit("我爱123", 1, "#"), "我#爱#1#2#3#") 211 t.Assert(gstr.ChunkSplit("1234", 1, ""), "1\r\n2\r\n3\r\n4\r\n") 212 }) 213 } 214 215 func Test_SplitAndTrim(t *testing.T) { 216 gtest.C(t, func(t *gtest.T) { 217 s := ` 218 219 010 220 221 020 222 223 ` 224 a := gstr.SplitAndTrim(s, "\n", "0") 225 t.Assert(len(a), 2) 226 t.Assert(a[0], "1") 227 t.Assert(a[1], "2") 228 }) 229 } 230 231 func Test_Fields(t *testing.T) { 232 gtest.C(t, func(t *gtest.T) { 233 t.Assert(gstr.Fields("我爱 Go Frame"), []string{ 234 "我爱", "Go", "Frame", 235 }) 236 }) 237 } 238 239 func Test_CountWords(t *testing.T) { 240 gtest.C(t, func(t *gtest.T) { 241 t.Assert(gstr.CountWords("我爱 Go Go Go"), map[string]int{ 242 "Go": 3, 243 "我爱": 1, 244 }) 245 }) 246 } 247 248 func Test_CountChars(t *testing.T) { 249 gtest.C(t, func(t *gtest.T) { 250 t.Assert(gstr.CountChars("我爱 Go Go Go"), map[string]int{ 251 " ": 3, 252 "G": 3, 253 "o": 3, 254 "我": 1, 255 "爱": 1, 256 }) 257 t.Assert(gstr.CountChars("我爱 Go Go Go", true), map[string]int{ 258 "G": 3, 259 "o": 3, 260 "我": 1, 261 "爱": 1, 262 }) 263 }) 264 } 265 266 func Test_WordWrap(t *testing.T) { 267 gtest.C(t, func(t *gtest.T) { 268 t.Assert(gstr.WordWrap("12 34", 2, "<br>"), "12<br>34") 269 t.Assert(gstr.WordWrap("12 34", 2, "\n"), "12\n34") 270 t.Assert(gstr.WordWrap("我爱 GF", 2, "\n"), "我爱\nGF") 271 t.Assert(gstr.WordWrap("A very long woooooooooooooooooord. and something", 7, "<br>"), 272 "A very<br>long<br>woooooooooooooooooord.<br>and<br>something") 273 }) 274 } 275 276 func Test_RuneLen(t *testing.T) { 277 gtest.C(t, func(t *gtest.T) { 278 t.Assert(gstr.RuneLen("1234"), 4) 279 t.Assert(gstr.RuneLen("我爱GoFrame"), 9) 280 }) 281 } 282 283 func Test_Repeat(t *testing.T) { 284 gtest.C(t, func(t *gtest.T) { 285 t.Assert(gstr.Repeat("go", 3), "gogogo") 286 t.Assert(gstr.Repeat("好的", 3), "好的好的好的") 287 }) 288 } 289 290 func Test_Str(t *testing.T) { 291 gtest.C(t, func(t *gtest.T) { 292 t.Assert(gstr.Str("name@example.com", "@"), "@example.com") 293 t.Assert(gstr.Str("name@example.com", ""), "") 294 t.Assert(gstr.Str("name@example.com", "z"), "") 295 }) 296 } 297 298 func Test_StrEx(t *testing.T) { 299 gtest.C(t, func(t *gtest.T) { 300 t.Assert(gstr.StrEx("name@example.com", "@"), "example.com") 301 t.Assert(gstr.StrEx("name@example.com", ""), "") 302 t.Assert(gstr.StrEx("name@example.com", "z"), "") 303 }) 304 } 305 306 func Test_StrTill(t *testing.T) { 307 gtest.C(t, func(t *gtest.T) { 308 t.Assert(gstr.StrTill("name@example.com", "@"), "name@") 309 t.Assert(gstr.StrTill("name@example.com", ""), "") 310 t.Assert(gstr.StrTill("name@example.com", "z"), "") 311 }) 312 } 313 314 func Test_StrTillEx(t *testing.T) { 315 gtest.C(t, func(t *gtest.T) { 316 t.Assert(gstr.StrTillEx("name@example.com", "@"), "name") 317 t.Assert(gstr.StrTillEx("name@example.com", ""), "") 318 t.Assert(gstr.StrTillEx("name@example.com", "z"), "") 319 }) 320 } 321 322 func Test_Shuffle(t *testing.T) { 323 gtest.C(t, func(t *gtest.T) { 324 t.Assert(len(gstr.Shuffle("123456")), 6) 325 }) 326 } 327 328 func Test_Split(t *testing.T) { 329 gtest.C(t, func(t *gtest.T) { 330 t.Assert(gstr.Split("1.2", "."), []string{"1", "2"}) 331 t.Assert(gstr.Split("我爱 - GoFrame", " - "), []string{"我爱", "GoFrame"}) 332 }) 333 } 334 335 func Test_Join(t *testing.T) { 336 gtest.C(t, func(t *gtest.T) { 337 t.Assert(gstr.Join([]string{"我爱", "GoFrame"}, " - "), "我爱 - GoFrame") 338 }) 339 } 340 341 func Test_Explode(t *testing.T) { 342 gtest.C(t, func(t *gtest.T) { 343 t.Assert(gstr.Explode(" - ", "我爱 - GoFrame"), []string{"我爱", "GoFrame"}) 344 }) 345 } 346 347 func Test_Implode(t *testing.T) { 348 gtest.C(t, func(t *gtest.T) { 349 t.Assert(gstr.Implode(" - ", []string{"我爱", "GoFrame"}), "我爱 - GoFrame") 350 }) 351 } 352 353 func Test_Chr(t *testing.T) { 354 gtest.C(t, func(t *gtest.T) { 355 t.Assert(gstr.Chr(65), "A") 356 }) 357 } 358 359 func Test_Ord(t *testing.T) { 360 gtest.C(t, func(t *gtest.T) { 361 t.Assert(gstr.Ord("A"), 65) 362 }) 363 } 364 365 func Test_HideStr(t *testing.T) { 366 gtest.C(t, func(t *gtest.T) { 367 t.Assert(gstr.HideStr("15928008611", 40, "*"), "159****8611") 368 t.Assert(gstr.HideStr("john@kohg.cn", 40, "*"), "jo*n@kohg.cn") 369 t.Assert(gstr.HideStr("张三", 50, "*"), "张*") 370 t.Assert(gstr.HideStr("张小三", 50, "*"), "张*三") 371 t.Assert(gstr.HideStr("欧阳小三", 50, "*"), "欧**三") 372 }) 373 } 374 375 func Test_Nl2Br(t *testing.T) { 376 gtest.C(t, func(t *gtest.T) { 377 t.Assert(gstr.Nl2Br("1\n2"), "1<br>2") 378 t.Assert(gstr.Nl2Br("1\r\n2"), "1<br>2") 379 t.Assert(gstr.Nl2Br("1\r\n2", true), "1<br />2") 380 }) 381 } 382 383 func Test_AddSlashes(t *testing.T) { 384 gtest.C(t, func(t *gtest.T) { 385 t.Assert(gstr.AddSlashes(`1'2"3\`), `1\'2\"3\\`) 386 }) 387 } 388 389 func Test_StripSlashes(t *testing.T) { 390 gtest.C(t, func(t *gtest.T) { 391 t.Assert(gstr.StripSlashes(`1\'2\"3\\`), `1'2"3\`) 392 }) 393 } 394 395 func Test_QuoteMeta(t *testing.T) { 396 gtest.C(t, func(t *gtest.T) { 397 t.Assert(gstr.QuoteMeta(`.\+*?[^]($)`), `\.\\\+\*\?\[\^\]\(\$\)`) 398 t.Assert(gstr.QuoteMeta(`.\+*中国?[^]($)`), `\.\\\+\*中国\?\[\^\]\(\$\)`) 399 t.Assert(gstr.QuoteMeta(`.''`, `'`), `.\'\'`) 400 t.Assert(gstr.QuoteMeta(`中国.''`, `'`), `中国.\'\'`) 401 }) 402 } 403 404 func Test_Count(t *testing.T) { 405 gtest.C(t, func(t *gtest.T) { 406 s := "abcdaAD" 407 t.Assert(gstr.Count(s, "0"), 0) 408 t.Assert(gstr.Count(s, "a"), 2) 409 t.Assert(gstr.Count(s, "b"), 1) 410 t.Assert(gstr.Count(s, "d"), 1) 411 }) 412 } 413 414 func Test_CountI(t *testing.T) { 415 gtest.C(t, func(t *gtest.T) { 416 s := "abcdaAD" 417 t.Assert(gstr.CountI(s, "0"), 0) 418 t.Assert(gstr.CountI(s, "a"), 3) 419 t.Assert(gstr.CountI(s, "b"), 1) 420 t.Assert(gstr.CountI(s, "d"), 2) 421 }) 422 } 423 424 func Test_Compare(t *testing.T) { 425 gtest.C(t, func(t *gtest.T) { 426 t.Assert(gstr.Compare("a", "b"), -1) 427 t.Assert(gstr.Compare("a", "a"), 0) 428 t.Assert(gstr.Compare("b", "a"), 1) 429 }) 430 } 431 432 func Test_Equal(t *testing.T) { 433 gtest.C(t, func(t *gtest.T) { 434 t.Assert(gstr.Equal("a", "A"), true) 435 t.Assert(gstr.Equal("a", "a"), true) 436 t.Assert(gstr.Equal("b", "a"), false) 437 }) 438 } 439 440 func Test_Contains(t *testing.T) { 441 gtest.C(t, func(t *gtest.T) { 442 t.Assert(gstr.Contains("abc", "a"), true) 443 t.Assert(gstr.Contains("abc", "A"), false) 444 t.Assert(gstr.Contains("abc", "ab"), true) 445 t.Assert(gstr.Contains("abc", "abc"), true) 446 }) 447 } 448 449 func Test_ContainsI(t *testing.T) { 450 gtest.C(t, func(t *gtest.T) { 451 t.Assert(gstr.ContainsI("abc", "a"), true) 452 t.Assert(gstr.ContainsI("abc", "A"), true) 453 t.Assert(gstr.ContainsI("abc", "Ab"), true) 454 t.Assert(gstr.ContainsI("abc", "ABC"), true) 455 t.Assert(gstr.ContainsI("abc", "ABCD"), false) 456 t.Assert(gstr.ContainsI("abc", "D"), false) 457 }) 458 } 459 460 func Test_ContainsAny(t *testing.T) { 461 gtest.C(t, func(t *gtest.T) { 462 t.Assert(gstr.ContainsAny("abc", "a"), true) 463 t.Assert(gstr.ContainsAny("abc", "cd"), true) 464 t.Assert(gstr.ContainsAny("abc", "de"), false) 465 t.Assert(gstr.ContainsAny("abc", "A"), false) 466 }) 467 } 468 469 func Test_SearchArray(t *testing.T) { 470 gtest.C(t, func(t *gtest.T) { 471 a := g.SliceStr{"a", "b", "c"} 472 t.AssertEQ(gstr.SearchArray(a, "a"), 0) 473 t.AssertEQ(gstr.SearchArray(a, "b"), 1) 474 t.AssertEQ(gstr.SearchArray(a, "c"), 2) 475 t.AssertEQ(gstr.SearchArray(a, "d"), -1) 476 }) 477 } 478 479 func Test_InArray(t *testing.T) { 480 gtest.C(t, func(t *gtest.T) { 481 a := g.SliceStr{"a", "b", "c"} 482 t.AssertEQ(gstr.InArray(a, "a"), true) 483 t.AssertEQ(gstr.InArray(a, "b"), true) 484 t.AssertEQ(gstr.InArray(a, "c"), true) 485 t.AssertEQ(gstr.InArray(a, "d"), false) 486 }) 487 }