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