github.com/xushiwei/go@v0.0.0-20130601165731-2b9d83f45bc9/src/pkg/path/filepath/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 filepath
     6  
     7  import (
     8  	"runtime"
     9  	"strings"
    10  	"testing"
    11  )
    12  
    13  type MatchTest struct {
    14  	pattern, s string
    15  	match      bool
    16  	err        error
    17  }
    18  
    19  var matchTests = []MatchTest{
    20  	{"abc", "abc", true, nil},
    21  	{"*", "abc", true, nil},
    22  	{"*c", "abc", true, nil},
    23  	{"a*", "a", true, nil},
    24  	{"a*", "abc", true, nil},
    25  	{"a*", "ab/c", false, nil},
    26  	{"a*/b", "abc/b", true, nil},
    27  	{"a*/b", "a/c/b", false, nil},
    28  	{"a*b*c*d*e*/f", "axbxcxdxe/f", true, nil},
    29  	{"a*b*c*d*e*/f", "axbxcxdxexxx/f", true, nil},
    30  	{"a*b*c*d*e*/f", "axbxcxdxe/xxx/f", false, nil},
    31  	{"a*b*c*d*e*/f", "axbxcxdxexxx/fff", false, nil},
    32  	{"a*b?c*x", "abxbbxdbxebxczzx", true, nil},
    33  	{"a*b?c*x", "abxbbxdbxebxczzy", false, nil},
    34  	{"ab[c]", "abc", true, nil},
    35  	{"ab[b-d]", "abc", true, nil},
    36  	{"ab[e-g]", "abc", false, nil},
    37  	{"ab[^c]", "abc", false, nil},
    38  	{"ab[^b-d]", "abc", false, nil},
    39  	{"ab[^e-g]", "abc", true, nil},
    40  	{"a\\*b", "a*b", true, nil},
    41  	{"a\\*b", "ab", false, nil},
    42  	{"a?b", "a☺b", true, nil},
    43  	{"a[^a]b", "a☺b", true, nil},
    44  	{"a???b", "a☺b", false, nil},
    45  	{"a[^a][^a][^a]b", "a☺b", false, nil},
    46  	{"[a-ζ]*", "α", true, nil},
    47  	{"*[a-ζ]", "A", false, nil},
    48  	{"a?b", "a/b", false, nil},
    49  	{"a*b", "a/b", false, nil},
    50  	{"[\\]a]", "]", true, nil},
    51  	{"[\\-]", "-", true, nil},
    52  	{"[x\\-]", "x", true, nil},
    53  	{"[x\\-]", "-", true, nil},
    54  	{"[x\\-]", "z", false, nil},
    55  	{"[\\-x]", "x", true, nil},
    56  	{"[\\-x]", "-", true, nil},
    57  	{"[\\-x]", "a", false, nil},
    58  	{"[]a]", "]", false, ErrBadPattern},
    59  	{"[-]", "-", false, ErrBadPattern},
    60  	{"[x-]", "x", false, ErrBadPattern},
    61  	{"[x-]", "-", false, ErrBadPattern},
    62  	{"[x-]", "z", false, ErrBadPattern},
    63  	{"[-x]", "x", false, ErrBadPattern},
    64  	{"[-x]", "-", false, ErrBadPattern},
    65  	{"[-x]", "a", false, ErrBadPattern},
    66  	{"\\", "a", false, ErrBadPattern},
    67  	{"[a-b-c]", "a", false, ErrBadPattern},
    68  	{"*x", "xxx", true, nil},
    69  }
    70  
    71  func errp(e error) string {
    72  	if e == nil {
    73  		return "<nil>"
    74  	}
    75  	return e.Error()
    76  }
    77  
    78  func TestMatch(t *testing.T) {
    79  	for _, tt := range matchTests {
    80  		pattern := tt.pattern
    81  		s := tt.s
    82  		if runtime.GOOS == "windows" {
    83  			if strings.Index(pattern, "\\") >= 0 {
    84  				// no escape allowed on windows.
    85  				continue
    86  			}
    87  			pattern = Clean(pattern)
    88  			s = Clean(s)
    89  		}
    90  		ok, err := Match(pattern, s)
    91  		if ok != tt.match || err != tt.err {
    92  			t.Errorf("Match(%#q, %#q) = %v, %q want %v, %q", pattern, s, ok, errp(err), tt.match, errp(tt.err))
    93  		}
    94  	}
    95  }
    96  
    97  // contains returns true if vector contains the string s.
    98  func contains(vector []string, s string) bool {
    99  	for _, elem := range vector {
   100  		if elem == s {
   101  			return true
   102  		}
   103  	}
   104  	return false
   105  }
   106  
   107  var globTests = []struct {
   108  	pattern, result string
   109  }{
   110  	{"match.go", "match.go"},
   111  	{"mat?h.go", "match.go"},
   112  	{"*", "match.go"},
   113  	{"../*/match.go", "../filepath/match.go"},
   114  }
   115  
   116  func TestGlob(t *testing.T) {
   117  	for _, tt := range globTests {
   118  		pattern := tt.pattern
   119  		result := tt.result
   120  		if runtime.GOOS == "windows" {
   121  			pattern = Clean(pattern)
   122  			result = Clean(result)
   123  		}
   124  		matches, err := Glob(pattern)
   125  		if err != nil {
   126  			t.Errorf("Glob error for %q: %s", pattern, err)
   127  			continue
   128  		}
   129  		if !contains(matches, result) {
   130  			t.Errorf("Glob(%#q) = %#v want %v", pattern, matches, result)
   131  		}
   132  	}
   133  	for _, pattern := range []string{"no_match", "../*/no_match"} {
   134  		matches, err := Glob(pattern)
   135  		if err != nil {
   136  			t.Errorf("Glob error for %q: %s", pattern, err)
   137  			continue
   138  		}
   139  		if len(matches) != 0 {
   140  			t.Errorf("Glob(%#q) = %#v want []", pattern, matches)
   141  		}
   142  	}
   143  }
   144  
   145  func TestGlobError(t *testing.T) {
   146  	_, err := Glob("[7]")
   147  	if err != nil {
   148  		t.Error("expected error for bad pattern; got none")
   149  	}
   150  }