| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package misc |
| 6 |
|
| 7 |
import ( |
| 8 |
"testing" |
| 9 |
|
| 10 |
"github.com/google/go-cmp/cmp" |
| 11 |
"github.com/google/go-cmp/cmp/cmpopts" |
| 12 |
"golang.org/x/tools/gopls/internal/protocol" |
| 13 |
"golang.org/x/tools/gopls/internal/protocol/command" |
| 14 |
"golang.org/x/tools/gopls/internal/test/integration" |
| 15 |
) |
| 16 |
|
| 17 |
func TestPackageSymbols(t *testing.T) { |
| 18 |
const files = ` |
| 19 |
-- go.mod -- |
| 20 |
module example.com |
| 21 |
|
| 22 |
go 1.20 |
| 23 |
|
| 24 |
-- a.go -- |
| 25 |
package a |
| 26 |
|
| 27 |
var A = "var" |
| 28 |
type S struct{} |
| 29 |
|
| 30 |
func (s *S) M1() {} |
| 31 |
-- b.go -- |
| 32 |
package a |
| 33 |
|
| 34 |
var b = 1 |
| 35 |
|
| 36 |
func (s *S) M2() {} |
| 37 |
|
| 38 |
func (s *S) M3() {} |
| 39 |
|
| 40 |
func F() {} |
| 41 |
-- unloaded.go -- |
| 42 |
//go:build unloaded |
| 43 |
|
| 44 |
package a |
| 45 |
|
| 46 |
var Unloaded int |
| 47 |
` |
| 48 |
integration.Run(t, files, func(t *testing.T, env *integration.Env) { |
| 49 |
aURI := env.Sandbox.Workdir.URI("a.go") |
| 50 |
bURI := env.Sandbox.Workdir.URI("b.go") |
| 51 |
args, err := command.MarshalArgs(command.PackageSymbolsArgs{ |
| 52 |
URI: aURI, |
| 53 |
}) |
| 54 |
if err != nil { |
| 55 |
t.Fatal(err) |
| 56 |
} |
| 57 |
|
| 58 |
var res command.PackageSymbolsResult |
| 59 |
env.ExecuteCommand(&protocol.ExecuteCommandParams{ |
| 60 |
Command: command.PackageSymbols.String(), |
| 61 |
Arguments: args, |
| 62 |
}, &res) |
| 63 |
|
| 64 |
want := command.PackageSymbolsResult{ |
| 65 |
PackageName: "a", |
| 66 |
Files: []protocol.DocumentURI{aURI, bURI}, |
| 67 |
Symbols: []command.PackageSymbol{ |
| 68 |
{Name: "A", Kind: protocol.Variable, File: 0}, |
| 69 |
{Name: "F", Kind: protocol.Function, File: 1}, |
| 70 |
{Name: "S", Kind: protocol.Struct, File: 0, Children: []command.PackageSymbol{ |
| 71 |
{Name: "M1", Kind: protocol.Method, File: 0}, |
| 72 |
{Name: "M2", Kind: protocol.Method, File: 1}, |
| 73 |
{Name: "M3", Kind: protocol.Method, File: 1}, |
| 74 |
}}, |
| 75 |
{Name: "b", Kind: protocol.Variable, File: 1}, |
| 76 |
}, |
| 77 |
} |
| 78 |
ignore := cmpopts.IgnoreFields(command.PackageSymbol{}, "Range", "SelectionRange", "Detail") |
| 79 |
if diff := cmp.Diff(want, res, ignore); diff != "" { |
| 80 |
t.Errorf("package_symbols returned unexpected diff (-want +got):\n%s", diff) |
| 81 |
} |
| 82 |
|
| 83 |
for file, want := range map[string]command.PackageSymbolsResult{ |
| 84 |
"go.mod": {}, |
| 85 |
"unloaded.go": { |
| 86 |
PackageName: "a", |
| 87 |
Files: []protocol.DocumentURI{env.Sandbox.Workdir.URI("unloaded.go")}, |
| 88 |
Symbols: []command.PackageSymbol{ |
| 89 |
{Name: "Unloaded", Kind: protocol.Variable, File: 0}, |
| 90 |
}, |
| 91 |
}, |
| 92 |
} { |
| 93 |
uri := env.Sandbox.Workdir.URI(file) |
| 94 |
args, err := command.MarshalArgs(command.PackageSymbolsArgs{ |
| 95 |
URI: uri, |
| 96 |
}) |
| 97 |
if err != nil { |
| 98 |
t.Fatal(err) |
| 99 |
} |
| 100 |
var res command.PackageSymbolsResult |
| 101 |
env.ExecuteCommand(&protocol.ExecuteCommandParams{ |
| 102 |
Command: command.PackageSymbols.String(), |
| 103 |
Arguments: args, |
| 104 |
}, &res) |
| 105 |
|
| 106 |
if diff := cmp.Diff(want, res, ignore); diff != "" { |
| 107 |
t.Errorf("package_symbols returned unexpected diff (-want +got):\n%s", diff) |
| 108 |
} |
| 109 |
} |
| 110 |
}) |
| 111 |
} |