.gitignore added
[dotfiles/.git] / .config / coc / extensions / coc-go-data / tools / pkg / mod / golang.org / x / tools / gopls@v0.6.9 / internal / regtest / misc / fix_test.go
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/regtest"
11
12         "golang.org/x/tools/internal/lsp/protocol"
13         "golang.org/x/tools/internal/lsp/tests"
14 )
15
16 // A basic test for fillstruct, now that it uses a command.
17 func TestFillStruct(t *testing.T) {
18         const basic = `
19 -- go.mod --
20 module mod.com
21
22 go 1.14
23 -- main.go --
24 package main
25
26 type Info struct {
27         WordCounts map[string]int
28         Words []string
29 }
30
31 func Foo() {
32         _ = Info{}
33 }
34 `
35         Run(t, basic, func(t *testing.T, env *Env) {
36                 env.OpenFile("main.go")
37                 pos := env.RegexpSearch("main.go", "Info{}").ToProtocolPosition()
38                 if err := env.Editor.RefactorRewrite(env.Ctx, "main.go", &protocol.Range{
39                         Start: pos,
40                         End:   pos,
41                 }); err != nil {
42                         t.Fatal(err)
43                 }
44                 want := `package main
45
46 type Info struct {
47         WordCounts map[string]int
48         Words []string
49 }
50
51 func Foo() {
52         _ = Info{
53                 WordCounts: map[string]int{},
54                 Words:      []string{},
55         }
56 }
57 `
58                 if got := env.Editor.BufferText("main.go"); got != want {
59                         t.Fatalf("TestFillStruct failed:\n%s", tests.Diff(t, want, got))
60                 }
61         })
62 }
63
64 func TestFillReturns(t *testing.T) {
65         const files = `
66 -- go.mod --
67 module mod.com
68
69 go 1.12
70 -- main.go --
71 package main
72
73 func Foo() error {
74         return
75 }
76 `
77         Run(t, files, func(t *testing.T, env *Env) {
78                 env.OpenFile("main.go")
79                 var d protocol.PublishDiagnosticsParams
80                 env.Await(OnceMet(
81                         env.DiagnosticAtRegexpWithMessage("main.go", `return`, "wrong number of return values"),
82                         ReadDiagnostics("main.go", &d),
83                 ))
84                 env.ApplyQuickFixes("main.go", d.Diagnostics)
85                 env.Await(EmptyDiagnostics("main.go"))
86         })
87 }