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

     1  // Copyright 2021 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  	"sort"
     9  	"testing"
    10  
    11  	"golang.org/x/tools/gopls/internal/protocol"
    12  	. "golang.org/x/tools/gopls/internal/test/integration"
    13  )
    14  
    15  func TestWorkspacePackageHighlight(t *testing.T) {
    16  	const mod = `
    17  -- go.mod --
    18  module mod.com
    19  
    20  go 1.12
    21  -- main.go --
    22  package main
    23  
    24  func main() {
    25  	var A string = "A"
    26  	x := "x-" + A
    27  	println(A, x)
    28  }`
    29  
    30  	Run(t, mod, func(t *testing.T, env *Env) {
    31  		const file = "main.go"
    32  		env.OpenFile(file)
    33  		loc := env.GoToDefinition(env.RegexpSearch(file, `var (A) string`))
    34  
    35  		checkHighlights(env, loc, 3)
    36  	})
    37  }
    38  
    39  func TestStdPackageHighlight_Issue43511(t *testing.T) {
    40  	const mod = `
    41  -- go.mod --
    42  module mod.com
    43  
    44  go 1.12
    45  -- main.go --
    46  package main
    47  
    48  import "fmt"
    49  
    50  func main() {
    51  	fmt.Printf()
    52  }`
    53  
    54  	Run(t, mod, func(t *testing.T, env *Env) {
    55  		env.OpenFile("main.go")
    56  		defLoc := env.GoToDefinition(env.RegexpSearch("main.go", `fmt\.(Printf)`))
    57  		file := env.Sandbox.Workdir.URIToPath(defLoc.URI)
    58  		loc := env.RegexpSearch(file, `func Printf\((format) string`)
    59  
    60  		checkHighlights(env, loc, 2)
    61  	})
    62  }
    63  
    64  func TestThirdPartyPackageHighlight_Issue43511(t *testing.T) {
    65  	const proxy = `
    66  -- example.com@v1.2.3/go.mod --
    67  module example.com
    68  
    69  go 1.12
    70  -- example.com@v1.2.3/global/global.go --
    71  package global
    72  
    73  const A = 1
    74  
    75  func foo() {
    76  	_ = A
    77  }
    78  
    79  func bar() int {
    80  	return A + A
    81  }
    82  -- example.com@v1.2.3/local/local.go --
    83  package local
    84  
    85  func foo() int {
    86  	const b = 2
    87  
    88  	return b * b * (b+1) + b
    89  }`
    90  
    91  	const mod = `
    92  -- go.mod --
    93  module mod.com
    94  
    95  go 1.12
    96  
    97  require example.com v1.2.3
    98  -- go.sum --
    99  example.com v1.2.3 h1:WFzrgiQJwEDJNLDUOV1f9qlasQkvzXf2UNLaNIqbWsI=
   100  example.com v1.2.3/go.mod h1:Y2Rc5rVWjWur0h3pd9aEvK5Pof8YKDANh9gHA2Maujo=
   101  -- main.go --
   102  package main
   103  
   104  import (
   105  	_ "example.com/global"
   106  	_ "example.com/local"
   107  )
   108  
   109  func main() {}`
   110  
   111  	WithOptions(
   112  		ProxyFiles(proxy),
   113  	).Run(t, mod, func(t *testing.T, env *Env) {
   114  		env.OpenFile("main.go")
   115  
   116  		defLoc := env.GoToDefinition(env.RegexpSearch("main.go", `"example.com/global"`))
   117  		file := env.Sandbox.Workdir.URIToPath(defLoc.URI)
   118  		loc := env.RegexpSearch(file, `const (A)`)
   119  		checkHighlights(env, loc, 4)
   120  
   121  		defLoc = env.GoToDefinition(env.RegexpSearch("main.go", `"example.com/local"`))
   122  		file = env.Sandbox.Workdir.URIToPath(defLoc.URI)
   123  		loc = env.RegexpSearch(file, `const (b)`)
   124  		checkHighlights(env, loc, 5)
   125  	})
   126  }
   127  
   128  func checkHighlights(env *Env, loc protocol.Location, highlightCount int) {
   129  	t := env.T
   130  	t.Helper()
   131  
   132  	highlights := env.DocumentHighlight(loc)
   133  	if len(highlights) != highlightCount {
   134  		t.Fatalf("expected %v highlight(s), got %v", highlightCount, len(highlights))
   135  	}
   136  
   137  	references := env.References(loc)
   138  	if len(highlights) != len(references) {
   139  		t.Fatalf("number of highlights and references is expected to be equal: %v != %v", len(highlights), len(references))
   140  	}
   141  
   142  	sort.Slice(highlights, func(i, j int) bool {
   143  		return protocol.CompareRange(highlights[i].Range, highlights[j].Range) < 0
   144  	})
   145  	sort.Slice(references, func(i, j int) bool {
   146  		return protocol.CompareRange(references[i].Range, references[j].Range) < 0
   147  	})
   148  	for i := range highlights {
   149  		if highlights[i].Range != references[i].Range {
   150  			t.Errorf("highlight and reference ranges are expected to be equal: %v != %v", highlights[i].Range, references[i].Range)
   151  		}
   152  	}
   153  }