github.com/zhongdalu/gf@v1.0.0/g/text/gstr/gstr_z_unit_basic_test.go (about)

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