File: fix_test.go

package info (click to toggle)
golang-golang-x-tools 1%3A0.5.0%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bookworm-backports
  • size: 16,592 kB
  • sloc: javascript: 2,011; asm: 1,635; sh: 192; yacc: 155; makefile: 52; ansic: 8
file content (107 lines) | stat: -rw-r--r-- 2,323 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// Copyright 2020 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package misc

import (
	"testing"

	. "golang.org/x/tools/gopls/internal/lsp/regtest"
	"golang.org/x/tools/gopls/internal/lsp/tests/compare"

	"golang.org/x/tools/gopls/internal/lsp/protocol"
)

// A basic test for fillstruct, now that it uses a command.
func TestFillStruct(t *testing.T) {
	const basic = `
-- go.mod --
module mod.com

go 1.14
-- main.go --
package main

type Info struct {
	WordCounts map[string]int
	Words []string
}

func Foo() {
	_ = Info{}
}
`
	Run(t, basic, func(t *testing.T, env *Env) {
		env.OpenFile("main.go")
		pos := env.RegexpSearch("main.go", "Info{}").ToProtocolPosition()
		if err := env.Editor.RefactorRewrite(env.Ctx, "main.go", &protocol.Range{
			Start: pos,
			End:   pos,
		}); err != nil {
			t.Fatal(err)
		}
		want := `package main

type Info struct {
	WordCounts map[string]int
	Words []string
}

func Foo() {
	_ = Info{
		WordCounts: map[string]int{},
		Words:      []string{},
	}
}
`
		if got := env.BufferText("main.go"); got != want {
			t.Fatalf("TestFillStruct failed:\n%s", compare.Text(want, got))
		}
	})
}

func TestFillReturns(t *testing.T) {
	const files = `
-- go.mod --
module mod.com

go 1.12
-- main.go --
package main

func Foo() error {
	return
}
`
	Run(t, files, func(t *testing.T, env *Env) {
		env.OpenFile("main.go")
		var d protocol.PublishDiagnosticsParams
		env.Await(OnceMet(
			// The error message here changed in 1.18; "return values" covers both forms.
			env.DiagnosticAtRegexpWithMessage("main.go", `return`, "return values"),
			ReadDiagnostics("main.go", &d),
		))
		codeActions := env.CodeAction("main.go", d.Diagnostics)
		if len(codeActions) != 2 {
			t.Fatalf("expected 2 code actions, got %v", len(codeActions))
		}
		var foundQuickFix, foundFixAll bool
		for _, a := range codeActions {
			if a.Kind == protocol.QuickFix {
				foundQuickFix = true
			}
			if a.Kind == protocol.SourceFixAll {
				foundFixAll = true
			}
		}
		if !foundQuickFix {
			t.Fatalf("expected quickfix code action, got none")
		}
		if !foundFixAll {
			t.Fatalf("expected fixall code action, got none")
		}
		env.ApplyQuickFixes("main.go", d.Diagnostics)
		env.Await(EmptyDiagnostics("main.go"))
	})
}