github.com/gogf/gf/v2@v2.7.4/text/gstr/gstr_z_unit_trim_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/v2/test/gtest" 15 "github.com/gogf/gf/v2/text/gstr" 16 ) 17 18 func Test_Trim(t *testing.T) { 19 gtest.C(t, func(t *gtest.T) { 20 t.Assert(gstr.Trim(" 123456\n "), "123456") 21 t.Assert(gstr.Trim("#123456#;", "#;"), "123456") 22 }) 23 } 24 25 func Test_TrimStr(t *testing.T) { 26 gtest.C(t, func(t *gtest.T) { 27 t.Assert(gstr.TrimStr("gogo我爱gogo", "go"), "我爱") 28 }) 29 gtest.C(t, func(t *gtest.T) { 30 t.Assert(gstr.TrimStr("gogo我爱gogo", "go", 1), "go我爱go") 31 t.Assert(gstr.TrimStr("gogo我爱gogo", "go", 2), "我爱") 32 t.Assert(gstr.TrimStr("gogo我爱gogo", "go", -1), "我爱") 33 }) 34 gtest.C(t, func(t *gtest.T) { 35 t.Assert(gstr.TrimStr("啊我爱中国人啊", "啊"), "我爱中国人") 36 }) 37 } 38 39 func Test_TrimRight(t *testing.T) { 40 gtest.C(t, func(t *gtest.T) { 41 t.Assert(gstr.TrimRight(" 123456\n "), " 123456") 42 t.Assert(gstr.TrimRight("#123456#;", "#;"), "#123456") 43 }) 44 } 45 46 func Test_TrimRightStr(t *testing.T) { 47 gtest.C(t, func(t *gtest.T) { 48 t.Assert(gstr.TrimRightStr("gogo我爱gogo", "go"), "gogo我爱") 49 t.Assert(gstr.TrimRightStr("gogo我爱gogo", "go我爱gogo"), "go") 50 }) 51 gtest.C(t, func(t *gtest.T) { 52 t.Assert(gstr.TrimRightStr("gogo我爱gogo", "go", 1), "gogo我爱go") 53 t.Assert(gstr.TrimRightStr("gogo我爱gogo", "go", 2), "gogo我爱") 54 t.Assert(gstr.TrimRightStr("gogo我爱gogo", "go", -1), "gogo我爱") 55 }) 56 gtest.C(t, func(t *gtest.T) { 57 t.Assert(gstr.TrimRightStr("我爱中国人", "人"), "我爱中国") 58 t.Assert(gstr.TrimRightStr("我爱中国人", "爱中国人"), "我") 59 }) 60 } 61 62 func Test_TrimLeft(t *testing.T) { 63 gtest.C(t, func(t *gtest.T) { 64 t.Assert(gstr.TrimLeft(" \r123456\n "), "123456\n ") 65 t.Assert(gstr.TrimLeft("#;123456#;", "#;"), "123456#;") 66 }) 67 } 68 69 func Test_TrimLeftStr(t *testing.T) { 70 gtest.C(t, func(t *gtest.T) { 71 t.Assert(gstr.TrimLeftStr("gogo我爱gogo", "go"), "我爱gogo") 72 t.Assert(gstr.TrimLeftStr("gogo我爱gogo", "gogo我爱go"), "go") 73 }) 74 gtest.C(t, func(t *gtest.T) { 75 t.Assert(gstr.TrimLeftStr("gogo我爱gogo", "go", 1), "go我爱gogo") 76 t.Assert(gstr.TrimLeftStr("gogo我爱gogo", "go", 2), "我爱gogo") 77 t.Assert(gstr.TrimLeftStr("gogo我爱gogo", "go", -1), "我爱gogo") 78 }) 79 gtest.C(t, func(t *gtest.T) { 80 t.Assert(gstr.TrimLeftStr("我爱中国人", "我爱"), "中国人") 81 t.Assert(gstr.TrimLeftStr("我爱中国人", "我爱中国"), "人") 82 }) 83 } 84 85 func Test_TrimAll(t *testing.T) { 86 gtest.C(t, func(t *gtest.T) { 87 t.Assert(gstr.TrimAll("gogo我go\n爱gogo\n", "go"), "我爱") 88 }) 89 gtest.C(t, func(t *gtest.T) { 90 t.Assert(gstr.TrimAll("gogo\n我go爱gogo", "go"), "我爱") 91 t.Assert(gstr.TrimAll("gogo\n我go爱gogo\n", "go"), "我爱") 92 t.Assert(gstr.TrimAll("gogo\n我go\n爱gogo", "go"), "我爱") 93 }) 94 gtest.C(t, func(t *gtest.T) { 95 t.Assert(gstr.TrimAll("啊我爱\n啊中国\n人啊", "啊"), "我爱中国人") 96 }) 97 }