cuelang.org/go@v0.10.1/pkg/path/match_test.go (about)

     1  // Copyright 2020 CUE Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  // Copyright 2009 The Go Authors. All rights reserved.
    16  // Use of this source code is governed by a BSD-style
    17  // license that can be found in the LICENSE file.
    18  
    19  package path
    20  
    21  import (
    22  	"strings"
    23  	"testing"
    24  )
    25  
    26  type MatchTest struct {
    27  	pattern, s string
    28  	match      bool
    29  	err        error
    30  }
    31  
    32  var matchTests = []MatchTest{
    33  	{"abc", "abc", true, nil},
    34  	{"*", "abc", true, nil},
    35  	{"*c", "abc", true, nil},
    36  	{"a*", "a", true, nil},
    37  	{"a*", "abc", true, nil},
    38  	{"a*", "ab/c", false, nil},
    39  	{"a*/b", "abc/b", true, nil},
    40  	{"a*/b", "a/c/b", false, nil},
    41  	{"a*b*c*d*e*/f", "axbxcxdxe/f", true, nil},
    42  	{"a*b*c*d*e*/f", "axbxcxdxexxx/f", true, nil},
    43  	{"a*b*c*d*e*/f", "axbxcxdxe/xxx/f", false, nil},
    44  	{"a*b*c*d*e*/f", "axbxcxdxexxx/fff", false, nil},
    45  	{"a*b?c*x", "abxbbxdbxebxczzx", true, nil},
    46  	{"a*b?c*x", "abxbbxdbxebxczzy", false, nil},
    47  	{"ab[c]", "abc", true, nil},
    48  	{"ab[b-d]", "abc", true, nil},
    49  	{"ab[e-g]", "abc", false, nil},
    50  	{"ab[^c]", "abc", false, nil},
    51  	{"ab[^b-d]", "abc", false, nil},
    52  	{"ab[^e-g]", "abc", true, nil},
    53  	{"a\\*b", "a*b", true, nil},
    54  	{"a\\*b", "ab", false, nil},
    55  	{"a?b", "a☺b", true, nil},
    56  	{"a[^a]b", "a☺b", true, nil},
    57  	{"a???b", "a☺b", false, nil},
    58  	{"a[^a][^a][^a]b", "a☺b", false, nil},
    59  	{"[a-ζ]*", "α", true, nil},
    60  	{"*[a-ζ]", "A", false, nil},
    61  	{"a?b", "a/b", false, nil},
    62  	{"a*b", "a/b", false, nil},
    63  	{"[\\]a]", "]", true, nil},
    64  	{"[\\-]", "-", true, nil},
    65  	{"[x\\-]", "x", true, nil},
    66  	{"[x\\-]", "-", true, nil},
    67  	{"[x\\-]", "z", false, nil},
    68  	{"[\\-x]", "x", true, nil},
    69  	{"[\\-x]", "-", true, nil},
    70  	{"[\\-x]", "a", false, nil},
    71  	{"[]a]", "]", false, ErrBadPattern},
    72  	{"[-]", "-", false, ErrBadPattern},
    73  	{"[x-]", "x", false, ErrBadPattern},
    74  	{"[x-]", "-", false, ErrBadPattern},
    75  	{"[x-]", "z", false, ErrBadPattern},
    76  	{"[-x]", "x", false, ErrBadPattern},
    77  	{"[-x]", "-", false, ErrBadPattern},
    78  	{"[-x]", "a", false, ErrBadPattern},
    79  	{"\\", "a", false, ErrBadPattern},
    80  	{"[a-b-c]", "a", false, ErrBadPattern},
    81  	{"[", "a", false, ErrBadPattern},
    82  	{"[^", "a", false, ErrBadPattern},
    83  	{"[^bc", "a", false, ErrBadPattern},
    84  	{"a[", "a", false, ErrBadPattern},
    85  	{"a[", "ab", false, ErrBadPattern},
    86  	{"a[", "x", false, ErrBadPattern},
    87  	{"a/b[", "x", false, ErrBadPattern},
    88  	{"*x", "xxx", true, nil},
    89  	{"**", "ab/c", false, errStarStarDisallowed},
    90  	{"**/c", "ab/c", false, errStarStarDisallowed},
    91  	{"a/b/**", "", false, errStarStarDisallowed},
    92  	{"\\**", "*ab", true, nil},
    93  	{"[x**y]", "*", true, nil},
    94  }
    95  
    96  func errp(e error) string {
    97  	if e == nil {
    98  		return "<nil>"
    99  	}
   100  	return e.Error()
   101  }
   102  
   103  func TestMatch(t *testing.T) {
   104  	testEachOS(t, []OS{Unix, Windows, Plan9}, func(t *testing.T, os OS) {
   105  		for _, tt := range matchTests {
   106  			pattern := tt.pattern
   107  			s := tt.s
   108  			if os == Windows {
   109  				if strings.Contains(pattern, "\\") {
   110  					// no escape allowed on windows.
   111  					continue
   112  				}
   113  				pattern = Clean(pattern, os)
   114  				s = Clean(s, os)
   115  			}
   116  			ok, err := Match(pattern, s, os)
   117  			if ok != tt.match || err != tt.err {
   118  				t.Errorf("Match(%#q, %#q, %q) = %v, %q want %v, %q",
   119  					pattern, s, os, ok, errp(err), tt.match, errp(tt.err))
   120  			}
   121  		}
   122  	})
   123  }