internal/lsp: refactor query definition test to allow adding a new test

Change-Id: I360f21012f26118947dbac7c39e17c9c1f599379
Reviewed-on: https://go-review.googlesource.com/c/tools/+/168177
Run-TryBot: Ian Cottrell <iancottrell@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Michael Matloob <matloob@golang.org>
This commit is contained in:
Ian Cottrell 2019-03-18 15:07:21 -04:00
parent 63e6ed9258
commit a41300a290
3 changed files with 24 additions and 13 deletions

View File

@ -26,11 +26,14 @@ type Definition struct {
Description string `json:"description"` // description of the denoted object Description string `json:"description"` // description of the denoted object
} }
// This constant is printed in the help, and then used in a test to verify the // These constant is printed in the help, and then used in a test to verify the
// help is still valid. // help is still valid.
// It should be the byte offset in this file of the "Set" in "flag.FlagSet" from // They refer to "Set" in "flag.FlagSet" from the DetailedHelp method below.
// the DetailedHelp method below. const (
const exampleOffset = 1311 exampleLine = 46
exampleColumn = 47
exampleOffset = 1319
)
// definition implements the definition noun for the query command. // definition implements the definition noun for the query command.
type definition struct { type definition struct {
@ -44,10 +47,11 @@ func (d *definition) DetailedHelp(f *flag.FlagSet) {
fmt.Fprintf(f.Output(), ` fmt.Fprintf(f.Output(), `
Example: show the definition of the identifier at syntax at offset %[1]v in this file (flag.FlagSet): Example: show the definition of the identifier at syntax at offset %[1]v in this file (flag.FlagSet):
$ gopls definition internal/lsp/cmd/definition.go:#%[1]v $ gopls definition internal/lsp/cmd/definition.go:%[1]v:%[2]v
$ gopls definition internal/lsp/cmd/definition.go:#%[3]v
gopls definition flags are: gopls definition flags are:
`, exampleOffset) `, exampleLine, exampleColumn, exampleOffset)
f.PrintDefaults() f.PrintDefaults()
} }

View File

@ -37,14 +37,17 @@ func TestDefinitionHelpExample(t *testing.T) {
return return
} }
thisFile := filepath.Join(dir, "definition.go") thisFile := filepath.Join(dir, "definition.go")
args := []string{"query", "definition", fmt.Sprintf("%v:#%v", thisFile, cmd.ExampleOffset)} baseArgs := []string{"query", "definition"}
expect := regexp.MustCompile(`^[\w/\\:_]+flag[/\\]flag.go:\d+:\d+-\d+: defined here as type flag.FlagSet struct{.*}$`) expect := regexp.MustCompile(`^[\w/\\:_]+flag[/\\]flag.go:\d+:\d+-\d+: defined here as type flag.FlagSet struct{.*}$`)
for _, query := range []string{fmt.Sprintf("%v:#%v", thisFile, cmd.ExampleOffset)} {
args := append(baseArgs, query)
got := captureStdOut(t, func() { got := captureStdOut(t, func() {
tool.Main(context.Background(), &cmd.Application{}, args) tool.Main(context.Background(), &cmd.Application{}, args)
}) })
if !expect.MatchString(got) { if !expect.MatchString(got) {
t.Errorf("test with %v\nexpected:\n%s\ngot:\n%s", args, expect, got) t.Errorf("test with %v\nexpected:\n%s\ngot:\n%s", args, expect, got)
} }
}
} }
func TestDefinition(t *testing.T) { func TestDefinition(t *testing.T) {

View File

@ -4,4 +4,8 @@
package cmd package cmd
const ExampleOffset = exampleOffset const (
ExampleLine = exampleLine
ExampleColumn = exampleColumn
ExampleOffset = exampleOffset
)