github.com/razvanm/vanadium-go-1.3@v0.0.0-20160721203343-4a65068e5915/src/cmd/go/match_test.go (about)

     1  // Copyright 2012 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 main
     6  
     7  import "testing"
     8  
     9  var matchPatternTests = []stringPairTest{
    10  	{"...", "foo", true},
    11  	{"net", "net", true},
    12  	{"net", "net/http", false},
    13  	{"net/http", "net", false},
    14  	{"net/http", "net/http", true},
    15  	{"net...", "netchan", true},
    16  	{"net...", "net", true},
    17  	{"net...", "net/http", true},
    18  	{"net...", "not/http", false},
    19  	{"net/...", "netchan", false},
    20  	{"net/...", "net", true},
    21  	{"net/...", "net/http", true},
    22  	{"net/...", "not/http", false},
    23  }
    24  
    25  func TestMatchPattern(t *testing.T) {
    26  	testStringPairs(t, "matchPattern", matchPatternTests, func(pattern, name string) bool {
    27  		return matchPattern(pattern)(name)
    28  	})
    29  }
    30  
    31  var treeCanMatchPatternTests = []stringPairTest{
    32  	{"...", "foo", true},
    33  	{"net", "net", true},
    34  	{"net", "net/http", false},
    35  	{"net/http", "net", true},
    36  	{"net/http", "net/http", true},
    37  	{"net...", "netchan", true},
    38  	{"net...", "net", true},
    39  	{"net...", "net/http", true},
    40  	{"net...", "not/http", false},
    41  	{"net/...", "netchan", false},
    42  	{"net/...", "net", true},
    43  	{"net/...", "net/http", true},
    44  	{"net/...", "not/http", false},
    45  	{"abc.../def", "abcxyz", true},
    46  	{"abc.../def", "xyxabc", false},
    47  	{"x/y/z/...", "x", true},
    48  	{"x/y/z/...", "x/y", true},
    49  	{"x/y/z/...", "x/y/z", true},
    50  	{"x/y/z/...", "x/y/z/w", true},
    51  	{"x/y/z", "x", true},
    52  	{"x/y/z", "x/y", true},
    53  	{"x/y/z", "x/y/z", true},
    54  	{"x/y/z", "x/y/z/w", false},
    55  	{"x/.../y/z", "x/a/b/c", true},
    56  	{"x/.../y/z", "y/x/a/b/c", false},
    57  }
    58  
    59  func TestChildrenCanMatchPattern(t *testing.T) {
    60  	testStringPairs(t, "treeCanMatchPattern", treeCanMatchPatternTests, func(pattern, name string) bool {
    61  		return treeCanMatchPattern(pattern)(name)
    62  	})
    63  }
    64  
    65  var hasPathPrefixTests = []stringPairTest{
    66  	{"abc", "a", false},
    67  	{"a/bc", "a", true},
    68  	{"a", "a", true},
    69  	{"a/bc", "a/", true},
    70  }
    71  
    72  func TestHasPathPrefix(t *testing.T) {
    73  	testStringPairs(t, "hasPathPrefix", hasPathPrefixTests, hasPathPrefix)
    74  }
    75  
    76  type stringPairTest struct {
    77  	in1 string
    78  	in2 string
    79  	out bool
    80  }
    81  
    82  func testStringPairs(t *testing.T, name string, tests []stringPairTest, f func(string, string) bool) {
    83  	for _, tt := range tests {
    84  		if out := f(tt.in1, tt.in2); out != tt.out {
    85  			t.Errorf("%s(%q, %q) = %v, want %v", name, tt.in1, tt.in2, out, tt.out)
    86  		}
    87  	}
    88  }