gitee.com/wgliang/goreporter@v0.0.0-20180902115603-df1b20f7c5d0/linters/simplecode/gotool/match_test.go (about)

     1  // Copyright (c) 2009 The Go Authors. All rights reserved.
     2  //
     3  // Redistribution and use in source and binary forms, with or without
     4  // modification, are permitted provided that the following conditions are
     5  // met:
     6  //
     7  //    * Redistributions of source code must retain the above copyright
     8  // notice, this list of conditions and the following disclaimer.
     9  //    * Redistributions in binary form must reproduce the above
    10  // copyright notice, this list of conditions and the following disclaimer
    11  // in the documentation and/or other materials provided with the
    12  // distribution.
    13  //    * Neither the name of Google Inc. nor the names of its
    14  // contributors may be used to endorse or promote products derived from
    15  // this software without specific prior written permission.
    16  //
    17  // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    18  // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    19  // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    20  // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
    21  // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    22  // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
    23  // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    24  // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
    25  // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    26  // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    27  // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    28  
    29  package gotool
    30  
    31  import (
    32  	"sort"
    33  	"testing"
    34  )
    35  
    36  // This file contains code from the Go distribution.
    37  
    38  var matchPatternTests = []stringPairTest{
    39  	{"...", "foo", true},
    40  	{"net", "net", true},
    41  	{"net", "net/http", false},
    42  	{"net/http", "net", false},
    43  	{"net/http", "net/http", true},
    44  	{"net...", "netchan", true},
    45  	{"net...", "net", true},
    46  	{"net...", "net/http", true},
    47  	{"net...", "not/http", false},
    48  	{"net/...", "netchan", false},
    49  	{"net/...", "net", true},
    50  	{"net/...", "net/http", true},
    51  	{"net/...", "not/http", false},
    52  }
    53  
    54  func TestMatchPattern(t *testing.T) {
    55  	testStringPairs(t, "matchPattern", matchPatternTests, func(pattern, name string) bool {
    56  		return matchPattern(pattern)(name)
    57  	})
    58  }
    59  
    60  var treeCanMatchPatternTests = []stringPairTest{
    61  	{"...", "foo", true},
    62  	{"net", "net", true},
    63  	{"net", "net/http", false},
    64  	{"net/http", "net", true},
    65  	{"net/http", "net/http", true},
    66  	{"net...", "netchan", true},
    67  	{"net...", "net", true},
    68  	{"net...", "net/http", true},
    69  	{"net...", "not/http", false},
    70  	{"net/...", "netchan", false},
    71  	{"net/...", "net", true},
    72  	{"net/...", "net/http", true},
    73  	{"net/...", "not/http", false},
    74  	{"abc.../def", "abcxyz", true},
    75  	{"abc.../def", "xyxabc", false},
    76  	{"x/y/z/...", "x", true},
    77  	{"x/y/z/...", "x/y", true},
    78  	{"x/y/z/...", "x/y/z", true},
    79  	{"x/y/z/...", "x/y/z/w", true},
    80  	{"x/y/z", "x", true},
    81  	{"x/y/z", "x/y", true},
    82  	{"x/y/z", "x/y/z", true},
    83  	{"x/y/z", "x/y/z/w", false},
    84  	{"x/.../y/z", "x/a/b/c", true},
    85  	{"x/.../y/z", "y/x/a/b/c", false},
    86  }
    87  
    88  func TestChildrenCanMatchPattern(t *testing.T) {
    89  	testStringPairs(t, "treeCanMatchPattern", treeCanMatchPatternTests, func(pattern, name string) bool {
    90  		return treeCanMatchPattern(pattern)(name)
    91  	})
    92  }
    93  
    94  var hasPathPrefixTests = []stringPairTest{
    95  	{"abc", "a", false},
    96  	{"a/bc", "a", true},
    97  	{"a", "a", true},
    98  	{"a/bc", "a/", true},
    99  }
   100  
   101  func TestHasPathPrefix(t *testing.T) {
   102  	testStringPairs(t, "hasPathPrefix", hasPathPrefixTests, hasPathPrefix)
   103  }
   104  
   105  type stringPairTest struct {
   106  	in1 string
   107  	in2 string
   108  	out bool
   109  }
   110  
   111  func testStringPairs(t *testing.T, name string, tests []stringPairTest, f func(string, string) bool) {
   112  	for _, tt := range tests {
   113  		if out := f(tt.in1, tt.in2); out != tt.out {
   114  			t.Errorf("%s(%q, %q) = %v, want %v", name, tt.in1, tt.in2, out, tt.out)
   115  		}
   116  	}
   117  }
   118  
   119  // containsString reports whether strings contains x. strings is assumed to be sorted.
   120  func containsString(strings []string, x string) bool {
   121  	return strings[sort.SearchStrings(strings, x)] == x
   122  }
   123  
   124  func TestMatchStdPackages(t *testing.T) {
   125  	packages := DefaultContext.matchPackages("std")
   126  	sort.Strings(packages)
   127  	// some common packages all Go versions should have
   128  	commonPackages := []string{"bufio", "bytes", "crypto", "fmt", "io", "os"}
   129  	for _, p := range commonPackages {
   130  		if !containsString(packages, p) {
   131  			t.Errorf("std package set doesn't contain expected package %s", p)
   132  		}
   133  	}
   134  }