golang.org/x/tools/gopls@v0.15.3/internal/golang/workspace_symbol_test.go (about)

     1  // Copyright 2020 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 golang
     6  
     7  import (
     8  	"testing"
     9  
    10  	"golang.org/x/tools/gopls/internal/cache"
    11  )
    12  
    13  func TestParseQuery(t *testing.T) {
    14  	tests := []struct {
    15  		query, s  string
    16  		wantMatch bool
    17  	}{
    18  		{"", "anything", false},
    19  		{"any", "anything", true},
    20  		{"any$", "anything", false},
    21  		{"ing$", "anything", true},
    22  		{"ing$", "anythinG", true},
    23  		{"inG$", "anything", false},
    24  		{"^any", "anything", true},
    25  		{"^any", "Anything", true},
    26  		{"^Any", "anything", false},
    27  		{"at", "anything", true},
    28  		// TODO: this appears to be a bug in the fuzzy matching algorithm. 'At'
    29  		// should cause a case-sensitive match.
    30  		// {"At", "anything", false},
    31  		{"At", "Anything", true},
    32  		{"'yth", "Anything", true},
    33  		{"'yti", "Anything", false},
    34  		{"'any 'thing", "Anything", true},
    35  		{"anythn nythg", "Anything", true},
    36  		{"ntx", "Anything", false},
    37  		{"anythn", "anything", true},
    38  		{"ing", "anything", true},
    39  		{"anythn nythgx", "anything", false},
    40  	}
    41  
    42  	for _, test := range tests {
    43  		matcher := parseQuery(test.query, newFuzzyMatcher)
    44  		if _, score := matcher([]string{test.s}); score > 0 != test.wantMatch {
    45  			t.Errorf("parseQuery(%q) match for %q: %.2g, want match: %t", test.query, test.s, score, test.wantMatch)
    46  		}
    47  	}
    48  }
    49  
    50  func TestFiltererDisallow(t *testing.T) {
    51  	tests := []struct {
    52  		filters  []string
    53  		included []string
    54  		excluded []string
    55  	}{
    56  		{
    57  			[]string{"+**/c.go"},
    58  			[]string{"a/c.go", "a/b/c.go"},
    59  			[]string{},
    60  		},
    61  		{
    62  			[]string{"+a/**/c.go"},
    63  			[]string{"a/b/c.go", "a/b/d/c.go", "a/c.go"},
    64  			[]string{},
    65  		},
    66  		{
    67  			[]string{"-a/c.go", "+a/**"},
    68  			[]string{"a/c.go"},
    69  			[]string{},
    70  		},
    71  		{
    72  			[]string{"+a/**/c.go", "-**/c.go"},
    73  			[]string{},
    74  			[]string{"a/b/c.go"},
    75  		},
    76  		{
    77  			[]string{"+a/**/c.go", "-a/**"},
    78  			[]string{},
    79  			[]string{"a/b/c.go"},
    80  		},
    81  		{
    82  			[]string{"+**/c.go", "-a/**/c.go"},
    83  			[]string{},
    84  			[]string{"a/b/c.go"},
    85  		},
    86  		{
    87  			[]string{"+foobar", "-foo"},
    88  			[]string{"foobar", "foobar/a"},
    89  			[]string{"foo", "foo/a"},
    90  		},
    91  		{
    92  			[]string{"+", "-"},
    93  			[]string{},
    94  			[]string{"foobar", "foobar/a", "foo", "foo/a"},
    95  		},
    96  		{
    97  			[]string{"-", "+"},
    98  			[]string{"foobar", "foobar/a", "foo", "foo/a"},
    99  			[]string{},
   100  		},
   101  		{
   102  			[]string{"-a/**/b/**/c.go"},
   103  			[]string{},
   104  			[]string{"a/x/y/z/b/f/g/h/c.go"},
   105  		},
   106  		// tests for unsupported glob operators
   107  		{
   108  			[]string{"+**/c.go", "-a/*/c.go"},
   109  			[]string{"a/b/c.go"},
   110  			[]string{},
   111  		},
   112  		{
   113  			[]string{"+**/c.go", "-a/?/c.go"},
   114  			[]string{"a/b/c.go"},
   115  			[]string{},
   116  		},
   117  		{
   118  			[]string{"-b"}, // should only filter paths prefixed with the "b" directory
   119  			[]string{"a/b/c.go", "bb"},
   120  			[]string{"b/c/d.go", "b"},
   121  		},
   122  	}
   123  
   124  	for _, test := range tests {
   125  		filterer := cache.NewFilterer(test.filters)
   126  		for _, inc := range test.included {
   127  			if filterer.Disallow(inc) {
   128  				t.Errorf("Filters %v excluded %v, wanted included", test.filters, inc)
   129  			}
   130  		}
   131  
   132  		for _, exc := range test.excluded {
   133  			if !filterer.Disallow(exc) {
   134  				t.Errorf("Filters %v included %v, wanted excluded", test.filters, exc)
   135  			}
   136  		}
   137  	}
   138  }