github.com/joomcode/cue@v0.4.4-0.20221111115225-539fe3512047/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 } 90 91 func errp(e error) string { 92 if e == nil { 93 return "<nil>" 94 } 95 return e.Error() 96 } 97 98 func TestMatch(t *testing.T) { 99 for _, os := range []OS{Unix, Windows, Plan9} { 100 for _, tt := range matchTests { 101 pattern := tt.pattern 102 s := tt.s 103 if os == Windows { 104 if strings.Contains(pattern, "\\") { 105 // no escape allowed on windows. 106 continue 107 } 108 pattern = Clean(pattern, os) 109 s = Clean(s, os) 110 } 111 ok, err := Match(pattern, s, os) 112 if ok != tt.match || err != tt.err { 113 t.Errorf("Match(%#q, %#q, %q) = %v, %q want %v, %q", 114 pattern, s, os, ok, errp(err), tt.match, errp(tt.err)) 115 } 116 } 117 } 118 }