-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
import_test.go
127 lines (110 loc) · 2.61 KB
/
import_test.go
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// Copyright 2021 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"
"github.com/google/go-cmp/cmp"
"golang.org/x/tools/gopls/internal/protocol"
"golang.org/x/tools/gopls/internal/protocol/command"
"golang.org/x/tools/gopls/internal/test/compare"
. "golang.org/x/tools/gopls/internal/test/integration"
)
func TestAddImport(t *testing.T) {
const before = `package main
import "fmt"
func main() {
fmt.Println("hello world")
}
`
const want = `package main
import (
"bytes"
"fmt"
)
func main() {
fmt.Println("hello world")
}
`
Run(t, "", func(t *testing.T, env *Env) {
env.CreateBuffer("main.go", before)
cmd := command.NewAddImportCommand("Add Import", command.AddImportArgs{
URI: env.Sandbox.Workdir.URI("main.go"),
ImportPath: "bytes",
})
env.ExecuteCommand(&protocol.ExecuteCommandParams{
Command: command.AddImport.String(),
Arguments: cmd.Arguments,
}, nil)
got := env.BufferText("main.go")
if got != want {
t.Fatalf("gopls.add_import failed\n%s", compare.Text(want, got))
}
})
}
func TestListImports(t *testing.T) {
const files = `
-- go.mod --
module mod.com
go 1.12
-- foo.go --
package foo
const C = 1
-- import_strings_test.go --
package foo
import (
x "strings"
"testing"
)
func TestFoo(t *testing.T) {}
-- import_testing_test.go --
package foo
import "testing"
func TestFoo2(t *testing.T) {}
`
tests := []struct {
filename string
want command.ListImportsResult
}{
{
filename: "import_strings_test.go",
want: command.ListImportsResult{
Imports: []command.FileImport{
{Name: "x", Path: "strings"},
{Path: "testing"},
},
PackageImports: []command.PackageImport{
{Path: "strings"},
{Path: "testing"},
},
},
},
{
filename: "import_testing_test.go",
want: command.ListImportsResult{
Imports: []command.FileImport{
{Path: "testing"},
},
PackageImports: []command.PackageImport{
{Path: "strings"},
{Path: "testing"},
},
},
},
}
Run(t, files, func(t *testing.T, env *Env) {
for _, tt := range tests {
cmd := command.NewListImportsCommand("List Imports", command.URIArg{
URI: env.Sandbox.Workdir.URI(tt.filename),
})
var result command.ListImportsResult
env.ExecuteCommand(&protocol.ExecuteCommandParams{
Command: command.ListImports.String(),
Arguments: cmd.Arguments,
}, &result)
if diff := cmp.Diff(tt.want, result); diff != "" {
t.Errorf("unexpected list imports result for %q (-want +got):\n%s", tt.filename, diff)
}
}
})
}