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