File: import_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 (133 lines) | stat: -rw-r--r-- 2,749 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// 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/lsp/command"
	"golang.org/x/tools/gopls/internal/lsp/protocol"
	. "golang.org/x/tools/gopls/internal/lsp/regtest"
	"golang.org/x/tools/gopls/internal/lsp/tests/compare"
)

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, err := command.NewAddImportCommand("Add Import", command.AddImportArgs{
			URI:        env.Sandbox.Workdir.URI("main.go"),
			ImportPath: "bytes",
		})
		if err != nil {
			t.Fatal(err)
		}
		env.ExecuteCommand(&protocol.ExecuteCommandParams{
			Command:   "gopls.add_import",
			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, err := command.NewListImportsCommand("List Imports", command.URIArg{
				URI: env.Sandbox.Workdir.URI(tt.filename),
			})
			if err != nil {
				t.Fatal(err)
			}
			var result command.ListImportsResult
			env.ExecuteCommand(&protocol.ExecuteCommandParams{
				Command:   command.ListImports.ID(),
				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)
			}
		}

	})
}