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