Debian Codegraph @

gopls/internal/test/integration/misc/highlight_test.go

raw symbols SCIP
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.FirstDefinition(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.FirstDefinition(env.RegexpSearch("main.go", `fmt\.(Printf)`))
57 file := env.Sandbox.Workdir.URIToPath(defLoc.URI)
58 env.OpenFile(file)
59 loc := env.RegexpSearch(file, `func Printf\((format) string`)
60
61 checkHighlights(env, loc, 2)
62 })
63 }
64
65 func TestThirdPartyPackageHighlight_Issue43511(t *testing.T) {
66 const proxy = `
67 -- [email protected]/go.mod --
68 module example.com
69
70 go 1.12
71 -- [email protected]/global/global.go --
72 package global
73
74 const A = 1
75
76 func foo() {
77 _ = A
78 }
79
80 func bar() int {
81 return A + A
82 }
83 -- [email protected]/local/local.go --
84 package local
85
86 func foo() int {
87 const b = 2
88
89 return b * b * (b+1) + b
90 }`
91
92 const mod = `
93 -- go.mod --
94 module mod.com
95
96 go 1.12
97
98 require example.com v1.2.3
99 -- main.go --
100 package main
101
102 import (
103 _ "example.com/global"
104 _ "example.com/local"
105 )
106
107 func main() {}`
108
109 WithOptions(
110 ProxyFiles(proxy),
111 WriteGoSum("."),
112 ).Run(t, mod, func(t *testing.T, env *Env) {
113 env.OpenFile("main.go")
114
115 defLoc := env.FirstDefinition(env.RegexpSearch("main.go", `"example.com/global"`))
116 file := env.Sandbox.Workdir.URIToPath(defLoc.URI)
117 env.OpenFile(file)
118 loc := env.RegexpSearch(file, `const (A)`)
119 checkHighlights(env, loc, 4)
120
121 defLoc = env.FirstDefinition(env.RegexpSearch("main.go", `"example.com/local"`))
122 file = env.Sandbox.Workdir.URIToPath(defLoc.URI)
123 env.OpenFile(file)
124 loc = env.RegexpSearch(file, `const (b)`)
125 checkHighlights(env, loc, 5)
126 })
127 }
128
129 func checkHighlights(env *Env, loc protocol.Location, highlightCount int) {
130 t := env.TB
131 t.Helper()
132
133 highlights := env.DocumentHighlight(loc)
134 if len(highlights) != highlightCount {
135 t.Fatalf("expected %v highlight(s), got %v", highlightCount, len(highlights))
136 }
137
138 references := env.References(loc)
139 if len(highlights) != len(references) {
140 t.Fatalf("number of highlights and references is expected to be equal: %v != %v", len(highlights), len(references))
141 }
142
143 sort.Slice(highlights, func(i, j int) bool {
144 return protocol.CompareRange(highlights[i].Range, highlights[j].Range) < 0
145 })
146 sort.Slice(references, func(i, j int) bool {
147 return protocol.CompareRange(references[i].Range, references[j].Range) < 0
148 })
149 for i := range highlights {
150 if highlights[i].Range != references[i].Range {
151 t.Errorf("highlight and reference ranges are expected to be equal: %v != %v", highlights[i].Range, references[i].Range)
152 }
153 }
154 }