github.com/wangyougui/gf/v2@v2.6.5/text/gregex/gregex_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 gregex_test
    10  
    11  import (
    12  	"strings"
    13  	"testing"
    14  
    15  	"github.com/wangyougui/gf/v2/test/gtest"
    16  	"github.com/wangyougui/gf/v2/text/gregex"
    17  )
    18  
    19  var (
    20  	PatternErr = `([\d+`
    21  )
    22  
    23  func Test_Quote(t *testing.T) {
    24  	gtest.C(t, func(t *gtest.T) {
    25  		s1 := `[foo]` //`\[foo\]`
    26  		t.Assert(gregex.Quote(s1), `\[foo\]`)
    27  	})
    28  }
    29  
    30  func Test_Validate(t *testing.T) {
    31  	gtest.C(t, func(t *gtest.T) {
    32  		var s1 = `(.+):(\d+)`
    33  		t.Assert(gregex.Validate(s1), nil)
    34  		s1 = `((.+):(\d+)`
    35  		t.Assert(gregex.Validate(s1) == nil, false)
    36  	})
    37  }
    38  
    39  func Test_IsMatch(t *testing.T) {
    40  	gtest.C(t, func(t *gtest.T) {
    41  		var pattern = `(.+):(\d+)`
    42  		s1 := []byte(`sfs:2323`)
    43  		t.Assert(gregex.IsMatch(pattern, s1), true)
    44  		s1 = []byte(`sfs2323`)
    45  		t.Assert(gregex.IsMatch(pattern, s1), false)
    46  		s1 = []byte(`sfs:`)
    47  		t.Assert(gregex.IsMatch(pattern, s1), false)
    48  		// error pattern
    49  		t.Assert(gregex.IsMatch(PatternErr, s1), false)
    50  	})
    51  }
    52  
    53  func Test_IsMatchString(t *testing.T) {
    54  	gtest.C(t, func(t *gtest.T) {
    55  		var pattern = `(.+):(\d+)`
    56  		s1 := `sfs:2323`
    57  		t.Assert(gregex.IsMatchString(pattern, s1), true)
    58  		s1 = `sfs2323`
    59  		t.Assert(gregex.IsMatchString(pattern, s1), false)
    60  		s1 = `sfs:`
    61  		t.Assert(gregex.IsMatchString(pattern, s1), false)
    62  		// error pattern
    63  		t.Assert(gregex.IsMatchString(PatternErr, s1), false)
    64  	})
    65  }
    66  
    67  func Test_Match(t *testing.T) {
    68  	gtest.C(t, func(t *gtest.T) {
    69  		re := "a(a+b+)b"
    70  		wantSubs := "aaabb"
    71  		s := "acbb" + wantSubs + "dd"
    72  		subs, err := gregex.Match(re, []byte(s))
    73  		t.AssertNil(err)
    74  		if string(subs[0]) != wantSubs {
    75  			t.Fatalf("regex:%s,Match(%q)[0] = %q; want %q", re, s, subs[0], wantSubs)
    76  		}
    77  		if string(subs[1]) != "aab" {
    78  			t.Fatalf("Match(%q)[1] = %q; want %q", s, subs[1], "aab")
    79  		}
    80  		// error pattern
    81  		_, err = gregex.Match(PatternErr, []byte(s))
    82  		t.AssertNE(err, nil)
    83  	})
    84  }
    85  
    86  func Test_MatchString(t *testing.T) {
    87  	gtest.C(t, func(t *gtest.T) {
    88  		re := "a(a+b+)b"
    89  		wantSubs := "aaabb"
    90  		s := "acbb" + wantSubs + "dd"
    91  		subs, err := gregex.MatchString(re, s)
    92  		t.AssertNil(err)
    93  		if string(subs[0]) != wantSubs {
    94  			t.Fatalf("regex:%s,Match(%q)[0] = %q; want %q", re, s, subs[0], wantSubs)
    95  		}
    96  		if string(subs[1]) != "aab" {
    97  			t.Fatalf("Match(%q)[1] = %q; want %q", s, subs[1], "aab")
    98  		}
    99  		// error pattern
   100  		_, err = gregex.MatchString(PatternErr, s)
   101  		t.AssertNE(err, nil)
   102  	})
   103  }
   104  
   105  func Test_MatchAll(t *testing.T) {
   106  	gtest.C(t, func(t *gtest.T) {
   107  		re := "a(a+b+)b"
   108  		wantSubs := "aaabb"
   109  		s := "acbb" + wantSubs + "dd"
   110  		s = s + `其他的` + s
   111  		subs, err := gregex.MatchAll(re, []byte(s))
   112  		t.AssertNil(err)
   113  		if string(subs[0][0]) != wantSubs {
   114  			t.Fatalf("regex:%s,Match(%q)[0] = %q; want %q", re, s, subs[0][0], wantSubs)
   115  		}
   116  		if string(subs[0][1]) != "aab" {
   117  			t.Fatalf("Match(%q)[1] = %q; want %q", s, subs[0][1], "aab")
   118  		}
   119  
   120  		if string(subs[1][0]) != wantSubs {
   121  			t.Fatalf("regex:%s,Match(%q)[0] = %q; want %q", re, s, subs[1][0], wantSubs)
   122  		}
   123  		if string(subs[1][1]) != "aab" {
   124  			t.Fatalf("Match(%q)[1] = %q; want %q", s, subs[1][1], "aab")
   125  		}
   126  		// error pattern
   127  		_, err = gregex.MatchAll(PatternErr, []byte(s))
   128  		t.AssertNE(err, nil)
   129  	})
   130  }
   131  
   132  func Test_MatchAllString(t *testing.T) {
   133  	gtest.C(t, func(t *gtest.T) {
   134  		re := "a(a+b+)b"
   135  		wantSubs := "aaabb"
   136  		s := "acbb" + wantSubs + "dd"
   137  		subs, err := gregex.MatchAllString(re, s+`其他的`+s)
   138  		t.AssertNil(err)
   139  		if string(subs[0][0]) != wantSubs {
   140  			t.Fatalf("regex:%s,Match(%q)[0] = %q; want %q", re, s, subs[0][0], wantSubs)
   141  		}
   142  		if string(subs[0][1]) != "aab" {
   143  			t.Fatalf("Match(%q)[1] = %q; want %q", s, subs[0][1], "aab")
   144  		}
   145  
   146  		if string(subs[1][0]) != wantSubs {
   147  			t.Fatalf("regex:%s,Match(%q)[0] = %q; want %q", re, s, subs[1][0], wantSubs)
   148  		}
   149  		if string(subs[1][1]) != "aab" {
   150  			t.Fatalf("Match(%q)[1] = %q; want %q", s, subs[1][1], "aab")
   151  		}
   152  		// error pattern
   153  		_, err = gregex.MatchAllString(PatternErr, s)
   154  		t.AssertNE(err, nil)
   155  	})
   156  }
   157  
   158  func Test_Replace(t *testing.T) {
   159  	gtest.C(t, func(t *gtest.T) {
   160  		re := "a(a+b+)b"
   161  		wantSubs := "aaabb"
   162  		replace := "12345"
   163  		s := "acbb" + wantSubs + "dd"
   164  		wanted := "acbb" + replace + "dd"
   165  		replacedStr, err := gregex.Replace(re, []byte(replace), []byte(s))
   166  		t.AssertNil(err)
   167  		if string(replacedStr) != wanted {
   168  			t.Fatalf("regex:%s,old:%s; want %q", re, s, wanted)
   169  		}
   170  		// error pattern
   171  		_, err = gregex.Replace(PatternErr, []byte(replace), []byte(s))
   172  		t.AssertNE(err, nil)
   173  	})
   174  }
   175  
   176  func Test_ReplaceString(t *testing.T) {
   177  	gtest.C(t, func(t *gtest.T) {
   178  		re := "a(a+b+)b"
   179  		wantSubs := "aaabb"
   180  		replace := "12345"
   181  		s := "acbb" + wantSubs + "dd"
   182  		wanted := "acbb" + replace + "dd"
   183  		replacedStr, err := gregex.ReplaceString(re, replace, s)
   184  		t.AssertNil(err)
   185  		if replacedStr != wanted {
   186  			t.Fatalf("regex:%s,old:%s; want %q", re, s, wanted)
   187  		}
   188  		// error pattern
   189  		_, err = gregex.ReplaceString(PatternErr, replace, s)
   190  		t.AssertNE(err, nil)
   191  	})
   192  }
   193  
   194  func Test_ReplaceFun(t *testing.T) {
   195  	gtest.C(t, func(t *gtest.T) {
   196  		re := "a(a+b+)b"
   197  		wantSubs := "aaabb"
   198  		//replace :="12345"
   199  		s := "acbb" + wantSubs + "dd"
   200  		wanted := "acbb[x" + wantSubs + "y]dd"
   201  		wanted = "acbb" + "3个a" + "dd"
   202  		replacedStr, err := gregex.ReplaceFunc(re, []byte(s), func(s []byte) []byte {
   203  			if strings.Contains(string(s), "aaa") {
   204  				return []byte("3个a")
   205  			}
   206  			return []byte("[x" + string(s) + "y]")
   207  		})
   208  		t.AssertNil(err)
   209  		if string(replacedStr) != wanted {
   210  			t.Fatalf("regex:%s,old:%s; want %q", re, s, wanted)
   211  		}
   212  		// error pattern
   213  		_, err = gregex.ReplaceFunc(PatternErr, []byte(s), func(s []byte) []byte {
   214  			return []byte("")
   215  		})
   216  		t.AssertNE(err, nil)
   217  	})
   218  }
   219  
   220  func Test_ReplaceFuncMatch(t *testing.T) {
   221  	gtest.C(t, func(t *gtest.T) {
   222  		s := []byte("1234567890")
   223  		p := `(\d{3})(\d{3})(.+)`
   224  		s0, e0 := gregex.ReplaceFuncMatch(p, s, func(match [][]byte) []byte {
   225  			return match[0]
   226  		})
   227  		t.Assert(e0, nil)
   228  		t.Assert(s0, s)
   229  		s1, e1 := gregex.ReplaceFuncMatch(p, s, func(match [][]byte) []byte {
   230  			return match[1]
   231  		})
   232  		t.Assert(e1, nil)
   233  		t.Assert(s1, []byte("123"))
   234  		s2, e2 := gregex.ReplaceFuncMatch(p, s, func(match [][]byte) []byte {
   235  			return match[2]
   236  		})
   237  		t.Assert(e2, nil)
   238  		t.Assert(s2, []byte("456"))
   239  		s3, e3 := gregex.ReplaceFuncMatch(p, s, func(match [][]byte) []byte {
   240  			return match[3]
   241  		})
   242  		t.Assert(e3, nil)
   243  		t.Assert(s3, []byte("7890"))
   244  		// error pattern
   245  		_, err := gregex.ReplaceFuncMatch(PatternErr, s, func(match [][]byte) []byte {
   246  			return match[3]
   247  		})
   248  		t.AssertNE(err, nil)
   249  	})
   250  }
   251  
   252  func Test_ReplaceStringFunc(t *testing.T) {
   253  	gtest.C(t, func(t *gtest.T) {
   254  		re := "a(a+b+)b"
   255  		wantSubs := "aaabb"
   256  		//replace :="12345"
   257  		s := "acbb" + wantSubs + "dd"
   258  		wanted := "acbb[x" + wantSubs + "y]dd"
   259  		wanted = "acbb" + "3个a" + "dd"
   260  		replacedStr, err := gregex.ReplaceStringFunc(re, s, func(s string) string {
   261  			if strings.Contains(s, "aaa") {
   262  				return "3个a"
   263  			}
   264  			return "[x" + s + "y]"
   265  		})
   266  		t.AssertNil(err)
   267  		if replacedStr != wanted {
   268  			t.Fatalf("regex:%s,old:%s; want %q", re, s, wanted)
   269  		}
   270  		// error pattern
   271  		_, err = gregex.ReplaceStringFunc(PatternErr, s, func(s string) string {
   272  			return ""
   273  		})
   274  		t.AssertNE(err, nil)
   275  	})
   276  }
   277  
   278  func Test_ReplaceStringFuncMatch(t *testing.T) {
   279  	gtest.C(t, func(t *gtest.T) {
   280  		s := "1234567890"
   281  		p := `(\d{3})(\d{3})(.+)`
   282  		s0, e0 := gregex.ReplaceStringFuncMatch(p, s, func(match []string) string {
   283  			return match[0]
   284  		})
   285  		t.Assert(e0, nil)
   286  		t.Assert(s0, s)
   287  		s1, e1 := gregex.ReplaceStringFuncMatch(p, s, func(match []string) string {
   288  			return match[1]
   289  		})
   290  		t.Assert(e1, nil)
   291  		t.Assert(s1, "123")
   292  		s2, e2 := gregex.ReplaceStringFuncMatch(p, s, func(match []string) string {
   293  			return match[2]
   294  		})
   295  		t.Assert(e2, nil)
   296  		t.Assert(s2, "456")
   297  		s3, e3 := gregex.ReplaceStringFuncMatch(p, s, func(match []string) string {
   298  			return match[3]
   299  		})
   300  		t.Assert(e3, nil)
   301  		t.Assert(s3, "7890")
   302  		// error pattern
   303  		_, err := gregex.ReplaceStringFuncMatch(PatternErr, s, func(match []string) string {
   304  			return ""
   305  		})
   306  		t.AssertNE(err, nil)
   307  	})
   308  }
   309  
   310  func Test_Split(t *testing.T) {
   311  	gtest.C(t, func(t *gtest.T) {
   312  		re := "a(a+b+)b"
   313  		matched := "aaabb"
   314  		item0 := "acbb"
   315  		item1 := "dd"
   316  		s := item0 + matched + item1
   317  		t.Assert(gregex.IsMatchString(re, matched), true)
   318  		items := gregex.Split(re, s) //split string with matched
   319  		if items[0] != item0 {
   320  			t.Fatalf("regex:%s,Split(%q) want %q", re, s, item0)
   321  		}
   322  		if items[1] != item1 {
   323  			t.Fatalf("regex:%s,Split(%q) want %q", re, s, item0)
   324  		}
   325  	})
   326  
   327  	gtest.C(t, func(t *gtest.T) {
   328  		re := "a(a+b+)b"
   329  		notmatched := "aaxbb"
   330  		item0 := "acbb"
   331  		item1 := "dd"
   332  		s := item0 + notmatched + item1
   333  		t.Assert(gregex.IsMatchString(re, notmatched), false)
   334  		items := gregex.Split(re, s) //split string with notmatched then nosplitting
   335  		if items[0] != s {
   336  			t.Fatalf("regex:%s,Split(%q) want %q", re, s, item0)
   337  		}
   338  		// error pattern
   339  		items = gregex.Split(PatternErr, s)
   340  		t.AssertEQ(items, nil)
   341  
   342  	})
   343  }