bosun.org@v0.0.0-20210513094433-e25bc3e69a1f/util/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 util
     6  
     7  import "testing"
     8  
     9  type MatchTest struct {
    10  	pattern, s string
    11  	match      bool
    12  	err        error
    13  }
    14  
    15  var matchTests = []MatchTest{
    16  	{"abc", "abc", true, nil},
    17  	{"*", "abc", true, nil},
    18  	{"*c", "abc", true, nil},
    19  	{"a*", "a", true, nil},
    20  	{"a*", "abc", true, nil},
    21  	{"a*", "ab/c", false, nil},
    22  	{"a*/b", "abc/b", true, nil},
    23  	{"a*/b", "a/c/b", false, nil},
    24  	{"a*b*c*d*e*/f", "axbxcxdxe/f", true, nil},
    25  	{"a*b*c*d*e*/f", "axbxcxdxexxx/f", true, nil},
    26  	{"a*b*c*d*e*/f", "axbxcxdxe/xxx/f", false, nil},
    27  	{"a*b*c*d*e*/f", "axbxcxdxexxx/fff", false, nil},
    28  	{"a*b?c*x", "abxbbxdbxebxczzx", true, nil},
    29  	{"a*b?c*x", "abxbbxdbxebxczzy", false, nil},
    30  	{"ab[c]", "abc", true, nil},
    31  	{"ab[b-d]", "abc", true, nil},
    32  	{"ab[e-g]", "abc", false, nil},
    33  	{"ab[^c]", "abc", false, nil},
    34  	{"ab[^b-d]", "abc", false, nil},
    35  	{"ab[^e-g]", "abc", true, nil},
    36  	{"a\\*b", "a*b", true, nil},
    37  	{"a\\*b", "ab", false, nil},
    38  	{"a?b", "a☺b", true, nil},
    39  	{"a[^a]b", "a☺b", true, nil},
    40  	{"a???b", "a☺b", false, nil},
    41  	{"a[^a][^a][^a]b", "a☺b", false, nil},
    42  	{"[a-ζ]*", "α", true, nil},
    43  	{"*[a-ζ]", "A", false, nil},
    44  	{"a?b", "a/b", false, nil},
    45  	{"a*b", "a/b", false, nil},
    46  	{"[\\]a]", "]", true, nil},
    47  	{"[\\-]", "-", true, nil},
    48  	{"[x\\-]", "x", true, nil},
    49  	{"[x\\-]", "-", true, nil},
    50  	{"[x\\-]", "z", false, nil},
    51  	{"[\\-x]", "x", true, nil},
    52  	{"[\\-x]", "-", true, nil},
    53  	{"[\\-x]", "a", false, nil},
    54  	{"[]a]", "]", false, ErrBadPattern},
    55  	{"[-]", "-", false, ErrBadPattern},
    56  	{"[x-]", "x", false, ErrBadPattern},
    57  	{"[x-]", "-", false, ErrBadPattern},
    58  	{"[x-]", "z", false, ErrBadPattern},
    59  	{"[-x]", "x", false, ErrBadPattern},
    60  	{"[-x]", "-", false, ErrBadPattern},
    61  	{"[-x]", "a", false, ErrBadPattern},
    62  	{"\\", "a", false, ErrBadPattern},
    63  	{"[a-b-c]", "a", false, ErrBadPattern},
    64  	{"[", "a", false, ErrBadPattern},
    65  	{"[^", "a", false, ErrBadPattern},
    66  	{"[^bc", "a", false, ErrBadPattern},
    67  	{"a[", "a", false, nil},
    68  	{"a[", "ab", false, ErrBadPattern},
    69  	{"*x", "xxx", true, nil},
    70  	{"a|b", "a", true, nil},
    71  	{"a|b", "c", false, nil},
    72  }
    73  
    74  func errp(e error) string {
    75  	if e == nil {
    76  		return "<nil>"
    77  	}
    78  	return e.Error()
    79  }
    80  
    81  func TestMatch(t *testing.T) {
    82  	for _, tt := range matchTests {
    83  		pattern := tt.pattern
    84  		s := tt.s
    85  		ok, err := Match(pattern, s)
    86  		if ok != tt.match || err != tt.err {
    87  			t.Errorf("Match(%#q, %#q) = %v, %q want %v, %q", pattern, s, ok, errp(err), tt.match, errp(tt.err))
    88  		}
    89  	}
    90  }