| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package misc |
| 6 |
|
| 7 |
import ( |
| 8 |
"testing" |
| 9 |
|
| 10 |
"github.com/google/go-cmp/cmp" |
| 11 |
"golang.org/x/tools/gopls/internal/protocol" |
| 12 |
"golang.org/x/tools/gopls/internal/protocol/command" |
| 13 |
"golang.org/x/tools/gopls/internal/test/compare" |
| 14 |
. "golang.org/x/tools/gopls/internal/test/integration" |
| 15 |
) |
| 16 |
|
| 17 |
func TestAddImport(t *testing.T) { |
| 18 |
const before = `package main |
| 19 |
|
| 20 |
import "fmt" |
| 21 |
|
| 22 |
func main() { |
| 23 |
fmt.Println("hello world") |
| 24 |
} |
| 25 |
` |
| 26 |
|
| 27 |
const want = `package main |
| 28 |
|
| 29 |
import ( |
| 30 |
"bytes" |
| 31 |
"fmt" |
| 32 |
) |
| 33 |
|
| 34 |
func main() { |
| 35 |
fmt.Println("hello world") |
| 36 |
} |
| 37 |
` |
| 38 |
|
| 39 |
Run(t, "", func(t *testing.T, env *Env) { |
| 40 |
env.CreateBuffer("main.go", before) |
| 41 |
cmd := command.NewAddImportCommand("Add Import", command.AddImportArgs{ |
| 42 |
URI: env.Sandbox.Workdir.URI("main.go"), |
| 43 |
ImportPath: "bytes", |
| 44 |
}) |
| 45 |
env.ExecuteCommand(&protocol.ExecuteCommandParams{ |
| 46 |
Command: command.AddImport.String(), |
| 47 |
Arguments: cmd.Arguments, |
| 48 |
}, nil) |
| 49 |
got := env.BufferText("main.go") |
| 50 |
if got != want { |
| 51 |
t.Fatalf("gopls.add_import failed\n%s", compare.Text(want, got)) |
| 52 |
} |
| 53 |
}) |
| 54 |
} |
| 55 |
|
| 56 |
func TestListImports(t *testing.T) { |
| 57 |
const files = ` |
| 58 |
-- go.mod -- |
| 59 |
module mod.com |
| 60 |
|
| 61 |
go 1.12 |
| 62 |
-- foo.go -- |
| 63 |
package foo |
| 64 |
const C = 1 |
| 65 |
-- import_strings_test.go -- |
| 66 |
package foo |
| 67 |
import ( |
| 68 |
x "strings" |
| 69 |
"testing" |
| 70 |
) |
| 71 |
|
| 72 |
func TestFoo(t *testing.T) {} |
| 73 |
-- import_testing_test.go -- |
| 74 |
package foo |
| 75 |
|
| 76 |
import "testing" |
| 77 |
|
| 78 |
func TestFoo2(t *testing.T) {} |
| 79 |
` |
| 80 |
tests := []struct { |
| 81 |
filename string |
| 82 |
want command.ListImportsResult |
| 83 |
}{ |
| 84 |
{ |
| 85 |
filename: "import_strings_test.go", |
| 86 |
want: command.ListImportsResult{ |
| 87 |
Imports: []command.FileImport{ |
| 88 |
{Name: "x", Path: "strings"}, |
| 89 |
{Path: "testing"}, |
| 90 |
}, |
| 91 |
PackageImports: []command.PackageImport{ |
| 92 |
{Path: "strings"}, |
| 93 |
{Path: "testing"}, |
| 94 |
}, |
| 95 |
}, |
| 96 |
}, |
| 97 |
{ |
| 98 |
filename: "import_testing_test.go", |
| 99 |
want: command.ListImportsResult{ |
| 100 |
Imports: []command.FileImport{ |
| 101 |
{Path: "testing"}, |
| 102 |
}, |
| 103 |
PackageImports: []command.PackageImport{ |
| 104 |
{Path: "strings"}, |
| 105 |
{Path: "testing"}, |
| 106 |
}, |
| 107 |
}, |
| 108 |
}, |
| 109 |
} |
| 110 |
|
| 111 |
Run(t, files, func(t *testing.T, env *Env) { |
| 112 |
for _, tt := range tests { |
| 113 |
cmd := command.NewListImportsCommand("List Imports", command.URIArg{ |
| 114 |
URI: env.Sandbox.Workdir.URI(tt.filename), |
| 115 |
}) |
| 116 |
var result command.ListImportsResult |
| 117 |
env.ExecuteCommand(&protocol.ExecuteCommandParams{ |
| 118 |
Command: command.ListImports.String(), |
| 119 |
Arguments: cmd.Arguments, |
| 120 |
}, &result) |
| 121 |
if diff := cmp.Diff(tt.want, result); diff != "" { |
| 122 |
t.Errorf("unexpected list imports result for %q (-want +got):\n%s", tt.filename, diff) |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
}) |
| 127 |
} |