aboutsummaryrefslogtreecommitdiff
path: root/internal/lsp/cmd/test/definition.go
blob: c82d9a6c1ae2e2fcfb8c6aea6146a2e4bafeab34 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// 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.

package cmdtest

import (
	"fmt"
	"runtime"
	"strings"
	"testing"

	"golang.org/x/tools/internal/lsp/diff"
	"golang.org/x/tools/internal/lsp/diff/myers"
	"golang.org/x/tools/internal/lsp/tests"
	"golang.org/x/tools/internal/span"
)

type godefMode int

const (
	plainGodef = godefMode(1 << iota)
	jsonGoDef
)

var godefModes = []godefMode{
	plainGodef,
	jsonGoDef,
}

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 {
		args := []string{"definition", "-markdown"}
		tag := d.Name + "-definition"
		if mode&jsonGoDef != 0 {
			tag += "-json"
			args = append(args, "-json")
		}
		uri := d.Src.URI()
		args = append(args, fmt.Sprint(d.Src))
		got, _ := r.NormalizeGoplsCmd(t, args...)
		if mode&jsonGoDef != 0 && runtime.GOOS == "windows" {
			got = strings.Replace(got, "file:///", "file://", -1)
		}
		expect := strings.TrimSpace(string(r.data.Golden(tag, uri.Filename(), func() ([]byte, error) {
			return []byte(got), nil
		})))
		if expect != "" && !strings.HasPrefix(got, expect) {
			d, err := myers.ComputeEdits("", expect, got)
			if err != nil {
				t.Fatal(err)
			}
			t.Errorf("definition %v failed with %#v\n%s", tag, args, diff.ToUnified("expect", "got", expect, d))
		}
	}
}