Debian Codegraph @

gopls/internal/test/integration/diagnostics/undeclared_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 diagnostics
6
7 import (
8 "slices"
9 "testing"
10
11 "golang.org/x/tools/gopls/internal/protocol"
12 . "golang.org/x/tools/gopls/internal/test/integration"
13 )
14
15 func TestUndeclaredDiagnostics(t *testing.T) {
16 src := `
17 -- go.mod --
18 module mod.com
19
20 go 1.12
21 -- a/a.go --
22 package a
23
24 func _() int {
25 return x
26 }
27 -- b/b.go --
28 package b
29
30 func _() int {
31 var y int
32 y = y
33 return y
34 }
35 `
36 Run(t, src, func(t *testing.T, env *Env) {
37 isUnnecessary := func(diag protocol.Diagnostic) bool {
38 return slices.Contains(diag.Tags, protocol.Unnecessary)
39 }
40
41 // 'x' is undeclared, but still necessary.
42 env.OpenFile("a/a.go")
43 var adiags protocol.PublishDiagnosticsParams
44 env.AfterChange(
45 Diagnostics(env.AtRegexp("a/a.go", "x")),
46 ReadDiagnostics("a/a.go", &adiags),
47 )
48 if got := len(adiags.Diagnostics); got != 1 {
49 t.Errorf("len(Diagnostics) = %d, want 1", got)
50 }
51 if diag := adiags.Diagnostics[0]; isUnnecessary(diag) {
52 t.Errorf("%v tagged unnecessary, want necessary", diag)
53 }
54
55 // 'y = y' is pointless, and should be detected as unnecessary.
56 env.OpenFile("b/b.go")
57 var bdiags protocol.PublishDiagnosticsParams
58 env.AfterChange(
59 Diagnostics(env.AtRegexp("b/b.go", "y = y")),
60 ReadDiagnostics("b/b.go", &bdiags),
61 )
62 if got := len(bdiags.Diagnostics); got != 1 {
63 t.Errorf("len(Diagnostics) = %d, want 1", got)
64 }
65 if diag := bdiags.Diagnostics[0]; !isUnnecessary(diag) {
66 t.Errorf("%v tagged necessary, want unnecessary", diag)
67 }
68 })
69 }