2018-12-14 13:46:12 -07:00
|
|
|
// Copyright 2019 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.
|
|
|
|
|
2019-09-18 21:51:24 -06:00
|
|
|
package cmdtest
|
2018-12-14 13:46:12 -07:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2019-03-15 09:11:23 -06:00
|
|
|
"runtime"
|
2018-12-14 13:46:12 -07:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2019-04-16 13:47:48 -06:00
|
|
|
"golang.org/x/tools/internal/lsp/tests"
|
2019-02-19 19:11:15 -07:00
|
|
|
"golang.org/x/tools/internal/span"
|
2018-12-14 13:46:12 -07:00
|
|
|
)
|
|
|
|
|
2019-03-25 16:30:55 -06:00
|
|
|
const (
|
2019-04-19 09:10:38 -06:00
|
|
|
expectedDefinitionsCount = 28
|
2019-03-25 16:30:55 -06:00
|
|
|
expectedTypeDefinitionsCount = 2
|
|
|
|
)
|
|
|
|
|
2019-04-29 12:57:27 -06:00
|
|
|
type godefMode int
|
|
|
|
|
|
|
|
const (
|
|
|
|
plainGodef = godefMode(1 << iota)
|
|
|
|
jsonGoDef
|
|
|
|
)
|
|
|
|
|
|
|
|
var godefModes = []godefMode{
|
|
|
|
plainGodef,
|
|
|
|
jsonGoDef,
|
|
|
|
}
|
2018-12-14 13:46:12 -07:00
|
|
|
|
2019-09-26 11:56:23 -06:00
|
|
|
func (r *runner) Definition(t *testing.T, spn span.Span, d tests.Definition) {
|
|
|
|
if d.IsType || d.OnlyHover {
|
|
|
|
// TODO: support type definition, hover queries
|
|
|
|
return
|
|
|
|
}
|
|
|
|
d.Src = span.New(d.Src.URI(), span.NewPoint(0, 0, d.Src.Start().Offset()), span.Point{})
|
|
|
|
for _, mode := range godefModes {
|
2019-11-21 17:58:38 -07:00
|
|
|
args := []string{"query"}
|
2019-09-26 11:56:23 -06:00
|
|
|
tag := d.Name + "-definition"
|
|
|
|
if mode&jsonGoDef != 0 {
|
|
|
|
tag += "-json"
|
|
|
|
args = append(args, "-json")
|
|
|
|
}
|
|
|
|
args = append(args, "definition")
|
|
|
|
uri := d.Src.URI()
|
|
|
|
args = append(args, fmt.Sprint(d.Src))
|
2019-11-21 17:58:38 -07:00
|
|
|
got, _ := r.NormalizeGoplsCmd(t, args...)
|
2019-09-26 11:56:23 -06:00
|
|
|
if mode&jsonGoDef != 0 && runtime.GOOS == "windows" {
|
|
|
|
got = strings.Replace(got, "file:///", "file://", -1)
|
2019-04-16 13:47:48 -06:00
|
|
|
}
|
2019-09-26 11:56:23 -06:00
|
|
|
expect := strings.TrimSpace(string(r.data.Golden(tag, uri.Filename(), func() ([]byte, error) {
|
|
|
|
return []byte(got), nil
|
|
|
|
})))
|
|
|
|
if expect != "" && !strings.HasPrefix(got, expect) {
|
|
|
|
t.Errorf("definition %v failed with %#v expected:\n%q\ngot:\n%q", tag, args, expect, got)
|
2019-03-25 16:30:55 -06:00
|
|
|
}
|
2018-12-14 13:46:12 -07:00
|
|
|
}
|
|
|
|
}
|