Debian Codegraph @

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

raw symbols SCIP
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 misc
6
7 import (
8 "testing"
9
10 "golang.org/x/tools/gopls/internal/settings"
11 "golang.org/x/tools/gopls/internal/test/compare"
12 . "golang.org/x/tools/gopls/internal/test/integration"
13
14 "golang.org/x/tools/gopls/internal/protocol"
15 )
16
17 // A basic test for fillstruct, now that it uses a command and supports resolve edits.
18 func TestFillStruct(t *testing.T) {
19 tc := []struct {
20 name string
21 capabilities string
22 wantCommand bool
23 }{
24 {"default", "{}", false},
25 {"no data support", `{"textDocument": {"codeAction": {"dataSupport": false, "resolveSupport": {"properties": ["edit"]}}}}`, true},
26 {"no resolve support", `{"textDocument": {"codeAction": {"dataSupport": true, "resolveSupport": {"properties": []}}}}`, true},
27 {"data and resolve support", `{"textDocument": {"codeAction": {"dataSupport": true, "resolveSupport": {"properties": ["edit"]}}}}`, false},
28 }
29
30 const basic = `
31 -- go.mod --
32 module mod.com
33
34 go 1.14
35 -- main.go --
36 package main
37
38 type Info struct {
39 WordCounts map[string]int
40 Words []string
41 }
42
43 func Foo() {
44 _ = Info{}
45 }
46 `
47
48 for _, tt := range tc {
49 t.Run(tt.name, func(t *testing.T) {
50 runner := WithOptions(CapabilitiesJSON([]byte(tt.capabilities)))
51
52 runner.Run(t, basic, func(t *testing.T, env *Env) {
53 env.OpenFile("main.go")
54 fixes, err := env.Editor.CodeActions(env.Ctx, env.RegexpSearch("main.go", "Info{}"), nil, settings.RefactorRewriteFillStruct)
55 if err != nil {
56 t.Fatal(err)
57 }
58
59 if len(fixes) != 1 {
60 t.Fatalf("expected 1 code action, got %v", len(fixes))
61 }
62 if tt.wantCommand {
63 if fixes[0].Command == nil || fixes[0].Data != nil {
64 t.Errorf("expected code action to have command not data, got %v", fixes[0])
65 }
66 } else {
67 if fixes[0].Command != nil || fixes[0].Data == nil {
68 t.Errorf("expected code action to have command not data, got %v", fixes[0])
69 }
70 }
71
72 // Apply the code action (handles resolving the code action), and check that the result is correct.
73 if err := env.Editor.RefactorRewrite(env.Ctx, env.RegexpSearch("main.go", "Info{}")); err != nil {
74 t.Fatal(err)
75 }
76 want := `package main
77
78 type Info struct {
79 WordCounts map[string]int
80 Words []string
81 }
82
83 func Foo() {
84 _ = Info{
85 WordCounts: map[string]int{},
86 Words: []string{},
87 }
88 }
89 `
90 if got := env.BufferText("main.go"); got != want {
91 t.Fatalf("TestFillStruct failed:\n%s", compare.Text(want, got))
92 }
93 })
94 })
95 }
96 }
97
98 func TestFillReturns(t *testing.T) {
99 const files = `
100 -- go.mod --
101 module mod.com
102
103 go 1.12
104 -- main.go --
105 package main
106
107 func Foo() error {
108 return
109 }
110 `
111 Run(t, files, func(t *testing.T, env *Env) {
112 env.OpenFile("main.go")
113 var d protocol.PublishDiagnosticsParams
114 env.AfterChange(
115 // The error message here changed in 1.18; "return values" covers both forms.
116 Diagnostics(env.AtRegexp("main.go", `return`), WithMessage("return values")),
117 ReadDiagnostics("main.go", &d),
118 )
119 var quickFixes []*protocol.CodeAction
120 for _, act := range env.CodeActionForFile("main.go", d.Diagnostics) {
121 if act.Kind == protocol.QuickFix {
122 act := act // remove in go1.22
123 quickFixes = append(quickFixes, &act)
124 }
125 }
126 if len(quickFixes) != 1 {
127 t.Fatalf("expected 1 quick fix, got %d:\n%v", len(quickFixes), quickFixes)
128 }
129 env.ApplyQuickFixes("main.go", d.Diagnostics)
130 env.AfterChange(NoDiagnostics(ForFile("main.go")))
131 })
132 }
133
134 func TestUnusedParameter_Issue63755(t *testing.T) {
135 // This test verifies the fix for #63755, where codeActions panicked on parameters
136 // of functions with no function body.
137
138 // We should not detect parameters as unused for external functions.
139
140 const files = `
141 -- go.mod --
142 module unused.mod
143
144 go 1.18
145
146 -- external.go --
147 package external
148
149 func External(z int)
150
151 func _() {
152 External(1)
153 }
154 `
155 Run(t, files, func(t *testing.T, env *Env) {
156 env.OpenFile("external.go")
157 _, err := env.Editor.CodeAction(env.Ctx, env.RegexpSearch("external.go", "z"), nil, protocol.CodeActionUnknownTrigger)
158 if err != nil {
159 t.Fatal(err)
160 }
161 // yay, no panic
162 })
163 }