1
0
mirror of https://github.com/golang/go synced 2024-10-01 03:28:32 -06:00
go/internal/lsp/regtest/definition_test.go
Rob Findley 0fd2d649e6 internal/lsp/lsprpc: add an LSP forwarder and regtest environment
Add a new Forwarder type to the lsprpc package, which implements the
jsonrpc2.StreamServer interface. This will be used to establish some
parity in the implementation of shared and singleton gopls servers.

Much more testing is needed, as is handling for the many edge cases
around forwarding the LSP, but since this is functionally equivalent to
TCP forwarding (and the -remote flag was already broken), I went ahead
and used the Forwarder to replace the forward method in the serve
command. This means that we can now use the combination of -listen and
-remote to chain together gopls servers... not that there's any reason
to do this.

Also, wrap the new regression tests with a focus on expressiveness when
testing the happy path, as well as parameterizing them so that they can
be run against different client/server execution environments. This
started to be sizable enough to warrant moving them to a separate
regtest package. The lsprpc package tests will instead focus on unit
testing the client-server binding logic.

Updates golang/go#36879
Updates golang/go#34111

Change-Id: Ib98131a58aabc69299845d2ecefceccfc1199574
Reviewed-on: https://go-review.googlesource.com/c/tools/+/218698
Run-TryBot: Robert Findley <rfindley@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Heschi Kreinick <heschi@google.com>
2020-02-14 14:21:06 +00:00

85 lines
1.8 KiB
Go

// Copyright 2020 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 regtest
import (
"context"
"path"
"testing"
"golang.org/x/tools/internal/lsp/fake"
)
const internalDefinition = `
-- go.mod --
module mod
go 1.12
-- main.go --
package main
import "fmt"
func main() {
fmt.Println(message)
}
-- const.go --
package main
const message = "Hello World."
`
func TestGoToInternalDefinition(t *testing.T) {
t.Parallel()
runner.Run(t, internalDefinition, func(ctx context.Context, t *testing.T, env *Env) {
env.OpenFile("main.go")
name, pos := env.GoToDefinition("main.go", fake.Pos{Line: 5, Column: 13})
if want := "const.go"; name != want {
t.Errorf("GoToDefinition: got file %q, want %q", name, want)
}
if want := (fake.Pos{Line: 2, Column: 6}); pos != want {
t.Errorf("GoToDefinition: got position %v, want %v", pos, want)
}
})
}
const stdlibDefinition = `
-- go.mod --
module mod
go 1.12
-- main.go --
package main
import (
"fmt"
"time"
)
func main() {
fmt.Println(time.Now())
}`
func TestGoToStdlibDefinition(t *testing.T) {
t.Parallel()
runner.Run(t, stdlibDefinition, func(ctx context.Context, t *testing.T, env *Env) {
env.OpenFile("main.go")
name, pos := env.GoToDefinition("main.go", fake.Pos{Line: 8, Column: 19})
if got, want := path.Base(name), "time.go"; got != want {
t.Errorf("GoToDefinition: got file %q, want %q", name, want)
}
// Test that we can jump to definition from outside our workspace.
// See golang.org/issues/37045.
newName, newPos := env.GoToDefinition(name, pos)
if newName != name {
t.Errorf("GoToDefinition is not idempotent: got %q, want %q", newName, name)
}
if newPos != pos {
t.Errorf("GoToDefinition is not idempotent: got %v, want %v", newPos, pos)
}
})
}