github.com/bingoohuang/gg@v0.0.0-20240325092523-45da7dee9335/pkg/fn/match_test.go (about) 1 // Copyright 2009 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package fn_test 6 7 import ( 8 "testing" 9 10 . "github.com/bingoohuang/gg/pkg/fn" 11 ) 12 13 type MatchTest struct { 14 pattern, s string 15 match bool 16 err error 17 } 18 19 var matchTests = []MatchTest{ 20 {"", "", true, nil}, 21 {"", "abc", false, nil}, 22 {"abc", "abc", true, nil}, 23 {"abc", "Abc", true, nil}, 24 {"*", "abc", true, nil}, 25 {"*c", "abc", true, nil}, 26 {"a*", "a", true, nil}, 27 {"a*", "A", true, nil}, 28 {"a*", "abc", true, nil}, 29 {"a*", "Abc", true, nil}, 30 {"a*", "ab/c", false, nil}, 31 {"a*/b", "abc/b", true, nil}, 32 {"a*/b", "a/c/b", false, nil}, 33 {"a*b*c*d*e*/f", "axbxcxdxe/f", true, nil}, 34 {"a*b*c*d*e*/f", "axbxcxdxexxx/f", true, nil}, 35 {"a*b*c*d*e*/f", "axbxcxdxe/xxx/f", false, nil}, 36 {"a*b*c*d*e*/f", "axbxcxdxexxx/fff", false, nil}, 37 {"a*b?c*x", "abxbbxdbxebxczzx", true, nil}, 38 {"a*b?c*x", "abxbbxdbxebxczzy", false, nil}, 39 {"ab[c]", "abc", true, nil}, 40 {"ab[b-d]", "abc", true, nil}, 41 {"ab[e-g]", "abc", false, nil}, 42 {"ab[^c]", "abc", false, nil}, 43 {"ab[^b-d]", "abc", false, nil}, 44 {"ab[^e-g]", "abc", true, nil}, 45 {"a\\*b", "a*b", true, nil}, 46 {"a\\*b", "ab", false, nil}, 47 {"a?b", "a☺b", true, nil}, 48 {"a[^a]b", "a☺b", true, nil}, 49 {"a???b", "a☺b", false, nil}, 50 {"a[^a][^a][^a]b", "a☺b", false, nil}, 51 {"[a-ζ]*", "α", true, nil}, 52 {"*[a-ζ]", "A", false, nil}, 53 {"a?b", "a/b", false, nil}, 54 {"a*b", "a/b", false, nil}, 55 {"[\\]a]", "]", true, nil}, 56 {"[\\-]", "-", true, nil}, 57 {"[x\\-]", "x", true, nil}, 58 {"[x\\-]", "-", true, nil}, 59 {"[x\\-]", "z", false, nil}, 60 {"[\\-x]", "x", true, nil}, 61 {"[\\-x]", "-", true, nil}, 62 {"[\\-x]", "a", false, nil}, 63 {"[]a]", "]", false, ErrBadPattern}, 64 {"[-]", "-", false, ErrBadPattern}, 65 {"[x-]", "x", false, ErrBadPattern}, 66 {"[x-]", "-", false, ErrBadPattern}, 67 {"[x-]", "z", false, ErrBadPattern}, 68 {"[-x]", "x", false, ErrBadPattern}, 69 {"[-x]", "-", false, ErrBadPattern}, 70 {"[-x]", "a", false, ErrBadPattern}, 71 {"\\", "a", false, ErrBadPattern}, 72 {"[a-b-c]", "a", false, ErrBadPattern}, 73 {"[", "a", false, ErrBadPattern}, 74 {"[^", "a", false, ErrBadPattern}, 75 {"[^bc", "a", false, ErrBadPattern}, 76 {"a[", "a", false, ErrBadPattern}, 77 {"a[", "ab", false, ErrBadPattern}, 78 {"a[", "x", false, ErrBadPattern}, 79 {"a/b[", "x", false, ErrBadPattern}, 80 {"*x", "xxx", true, nil}, 81 } 82 83 func errp(e error) string { 84 if e == nil { 85 return "<nil>" 86 } 87 return e.Error() 88 } 89 90 func TestMatch(t *testing.T) { 91 for _, tt := range matchTests { 92 pattern := tt.pattern 93 s := tt.s 94 ok, err := MatchE(pattern, s, WithCaseSensitive(false)) 95 if ok != tt.match || err != tt.err { 96 t.Errorf("Match(%#q, %#q) = %v, %q want %v, %q", pattern, s, ok, errp(err), tt.match, errp(tt.err)) 97 } 98 } 99 }