github.com/april1989/origin-go-tools@v0.0.32/internal/lsp/cmd/test/definition.go (about)

     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.
     4  
     5  package cmdtest
     6  
     7  import (
     8  	"fmt"
     9  	"runtime"
    10  	"strings"
    11  	"testing"
    12  
    13  	"github.com/april1989/origin-go-tools/internal/lsp/diff"
    14  	"github.com/april1989/origin-go-tools/internal/lsp/diff/myers"
    15  	"github.com/april1989/origin-go-tools/internal/lsp/tests"
    16  	"github.com/april1989/origin-go-tools/internal/span"
    17  )
    18  
    19  type godefMode int
    20  
    21  const (
    22  	plainGodef = godefMode(1 << iota)
    23  	jsonGoDef
    24  )
    25  
    26  var godefModes = []godefMode{
    27  	plainGodef,
    28  	jsonGoDef,
    29  }
    30  
    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
    34  		return
    35  	}
    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 {
    41  			tag += "-json"
    42  			args = append(args, "-json")
    43  		}
    44  		uri := d.Src.URI()
    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)
    49  		}
    50  		expect := strings.TrimSpace(string(r.data.Golden(tag, uri.Filename(), func() ([]byte, error) {
    51  			return []byte(got), nil
    52  		})))
    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))
    56  		}
    57  	}
    58  }