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

     1  // Copyright 2022 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 misc
     6  
     7  import (
     8  	"testing"
     9  
    10  	"github.com/google/go-cmp/cmp"
    11  	. "golang.org/x/tools/gopls/internal/test/integration"
    12  	"golang.org/x/tools/gopls/internal/settings"
    13  )
    14  
    15  func TestWorkspaceSymbolMissingMetadata(t *testing.T) {
    16  	const files = `
    17  -- go.mod --
    18  module mod.com
    19  
    20  go 1.17
    21  -- a.go --
    22  package p
    23  
    24  const K1 = "a.go"
    25  -- exclude.go --
    26  
    27  //go:build exclude
    28  // +build exclude
    29  
    30  package exclude
    31  
    32  const K2 = "exclude.go"
    33  `
    34  
    35  	Run(t, files, func(t *testing.T, env *Env) {
    36  		env.OpenFile("a.go")
    37  		checkSymbols(env, "K", "K1")
    38  
    39  		// Opening up an ignored file will result in an overlay with missing
    40  		// metadata, but this shouldn't break workspace symbols requests.
    41  		env.OpenFile("exclude.go")
    42  		checkSymbols(env, "K", "K1")
    43  	})
    44  }
    45  
    46  func TestWorkspaceSymbolSorting(t *testing.T) {
    47  	const files = `
    48  -- go.mod --
    49  module mod.com
    50  
    51  go 1.17
    52  -- a/a.go --
    53  package a
    54  
    55  const (
    56  	Foo = iota
    57  	FooBar
    58  	Fooey
    59  	Fooex
    60  	Fooest
    61  )
    62  `
    63  
    64  	var symbolMatcher = string(settings.SymbolFastFuzzy)
    65  	WithOptions(
    66  		Settings{"symbolMatcher": symbolMatcher},
    67  	).Run(t, files, func(t *testing.T, env *Env) {
    68  		checkSymbols(env, "Foo",
    69  			"Foo",    // prefer exact segment matches first
    70  			"FooBar", // ...followed by exact word matches
    71  			"Fooex",  // shorter than Fooest, FooBar, lexically before Fooey
    72  			"Fooey",  // shorter than Fooest, Foobar
    73  			"Fooest",
    74  		)
    75  	})
    76  }
    77  
    78  func TestWorkspaceSymbolSpecialPatterns(t *testing.T) {
    79  	const files = `
    80  -- go.mod --
    81  module mod.com
    82  
    83  go 1.17
    84  -- a/a.go --
    85  package a
    86  
    87  const (
    88  	AxxBxxCxx
    89  	ABC
    90  )
    91  `
    92  
    93  	var symbolMatcher = string(settings.SymbolFastFuzzy)
    94  	WithOptions(
    95  		Settings{"symbolMatcher": symbolMatcher},
    96  	).Run(t, files, func(t *testing.T, env *Env) {
    97  		checkSymbols(env, "ABC", "ABC", "AxxBxxCxx")
    98  		checkSymbols(env, "'ABC", "ABC")
    99  		checkSymbols(env, "^mod.com", "mod.com/a.ABC", "mod.com/a.AxxBxxCxx")
   100  		checkSymbols(env, "^mod.com Axx", "mod.com/a.AxxBxxCxx")
   101  		checkSymbols(env, "C$", "ABC")
   102  	})
   103  }
   104  
   105  func checkSymbols(env *Env, query string, want ...string) {
   106  	env.T.Helper()
   107  	var got []string
   108  	for _, info := range env.Symbol(query) {
   109  		got = append(got, info.Name)
   110  	}
   111  	if diff := cmp.Diff(got, want); diff != "" {
   112  		env.T.Errorf("unexpected Symbol(%q) result (+want -got):\n%s", query, diff)
   113  	}
   114  }