Home | History | Annotate | Download | only in path
      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
      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", "ab", true, nil},
     39 	{"a[^a]b", "ab", true, nil},
     40 	{"a???b", "ab", false, nil},
     41 	{"a[^a][^a][^a]b", "ab", 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 }
     71 
     72 func TestMatch(t *testing.T) {
     73 	for _, tt := range matchTests {
     74 		ok, err := Match(tt.pattern, tt.s)
     75 		if ok != tt.match || err != tt.err {
     76 			t.Errorf("Match(%#q, %#q) = %v, %v want %v, nil", tt.pattern, tt.s, ok, err, tt.match)
     77 		}
     78 	}
     79 }
     80