-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
internal/lsp/lsprpc: add test for definition outside of workspace
Add regression tests for GoToDefinition. In particular, exercise the panic from golang/go#37045. Updates golang/go#37045 Updates golang/go#36879 Change-Id: I67b562acd293f47907de0435c14b62c1a22cf2ee Reviewed-on: https://go-review.googlesource.com/c/tools/+/218322 Run-TryBot: Robert Findley <rfindley@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Rebecca Stambler <rstambler@golang.org>
- Loading branch information
Showing
6 changed files
with
169 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
// 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 lsprpc | ||
|
||
import ( | ||
"fmt" | ||
"path" | ||
"testing" | ||
"time" | ||
|
||
"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() | ||
ctx, env, cleanup := setupEnv(t, internalDefinition) | ||
defer cleanup() | ||
|
||
if err := env.editor.OpenFile(ctx, "main.go"); err != nil { | ||
t.Fatal(err) | ||
} | ||
name, pos, err := env.editor.GoToDefinition(ctx, "main.go", fake.Pos{Line: 5, Column: 13}) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
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() | ||
ctx, env, cleanup := setupEnv(t, stdlibDefinition) | ||
defer cleanup() | ||
|
||
if err := env.editor.OpenFile(ctx, "main.go"); err != nil { | ||
t.Fatal(err) | ||
} | ||
name, pos, err := env.editor.GoToDefinition(ctx, "main.go", fake.Pos{Line: 8, Column: 19}) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
fmt.Println(time.Now()) | ||
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, err := env.editor.GoToDefinition(ctx, name, pos) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters