golang.org/x/tools/gopls@v0.15.3/internal/test/integration/misc/import_test.go (about) 1 // Copyright 2021 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 "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, err := command.NewAddImportCommand("Add Import", command.AddImportArgs{ 42 URI: env.Sandbox.Workdir.URI("main.go"), 43 ImportPath: "bytes", 44 }) 45 if err != nil { 46 t.Fatal(err) 47 } 48 env.ExecuteCommand(&protocol.ExecuteCommandParams{ 49 Command: "gopls.add_import", 50 Arguments: cmd.Arguments, 51 }, nil) 52 got := env.BufferText("main.go") 53 if got != want { 54 t.Fatalf("gopls.add_import failed\n%s", compare.Text(want, got)) 55 } 56 }) 57 } 58 59 func TestListImports(t *testing.T) { 60 const files = ` 61 -- go.mod -- 62 module mod.com 63 64 go 1.12 65 -- foo.go -- 66 package foo 67 const C = 1 68 -- import_strings_test.go -- 69 package foo 70 import ( 71 x "strings" 72 "testing" 73 ) 74 75 func TestFoo(t *testing.T) {} 76 -- import_testing_test.go -- 77 package foo 78 79 import "testing" 80 81 func TestFoo2(t *testing.T) {} 82 ` 83 tests := []struct { 84 filename string 85 want command.ListImportsResult 86 }{ 87 { 88 filename: "import_strings_test.go", 89 want: command.ListImportsResult{ 90 Imports: []command.FileImport{ 91 {Name: "x", Path: "strings"}, 92 {Path: "testing"}, 93 }, 94 PackageImports: []command.PackageImport{ 95 {Path: "strings"}, 96 {Path: "testing"}, 97 }, 98 }, 99 }, 100 { 101 filename: "import_testing_test.go", 102 want: command.ListImportsResult{ 103 Imports: []command.FileImport{ 104 {Path: "testing"}, 105 }, 106 PackageImports: []command.PackageImport{ 107 {Path: "strings"}, 108 {Path: "testing"}, 109 }, 110 }, 111 }, 112 } 113 114 Run(t, files, func(t *testing.T, env *Env) { 115 for _, tt := range tests { 116 cmd, err := command.NewListImportsCommand("List Imports", command.URIArg{ 117 URI: env.Sandbox.Workdir.URI(tt.filename), 118 }) 119 if err != nil { 120 t.Fatal(err) 121 } 122 var result command.ListImportsResult 123 env.ExecuteCommand(&protocol.ExecuteCommandParams{ 124 Command: command.ListImports.ID(), 125 Arguments: cmd.Arguments, 126 }, &result) 127 if diff := cmp.Diff(tt.want, result); diff != "" { 128 t.Errorf("unexpected list imports result for %q (-want +got):\n%s", tt.filename, diff) 129 } 130 } 131 132 }) 133 }