1 // Copyright 2019 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
13 "golang.org/x/tools/internal/lsp/diff"
14 "golang.org/x/tools/internal/lsp/diff/myers"
15 "golang.org/x/tools/internal/lsp/tests"
16 "golang.org/x/tools/internal/span"
22 plainGodef = godefMode(1 << iota)
26 var godefModes = []godefMode{
31 func (r *runner) Definition(t *testing.T, spn span.Span, d tests.Definition) {
32 if d.IsType || d.OnlyHover {
33 // TODO: support type definition, hover queries
36 d.Src = span.New(d.Src.URI(), span.NewPoint(0, 0, d.Src.Start().Offset()), span.Point{})
37 for _, mode := range godefModes {
38 args := []string{"definition", "-markdown"}
39 tag := d.Name + "-definition"
40 if mode&jsonGoDef != 0 {
42 args = append(args, "-json")
45 args = append(args, fmt.Sprint(d.Src))
46 got, _ := r.NormalizeGoplsCmd(t, args...)
47 if mode&jsonGoDef != 0 && runtime.GOOS == "windows" {
48 got = strings.Replace(got, "file:///", "file://", -1)
50 expect := strings.TrimSpace(string(r.data.Golden(tag, uri.Filename(), func() ([]byte, error) {
51 return []byte(got), nil
53 if expect != "" && !strings.HasPrefix(got, expect) {
54 d := myers.ComputeEdits("", expect, got)
55 t.Errorf("definition %v failed with %#v\n%s", tag, args, diff.ToUnified("expect", "got", expect, d))