github.com/v2fly/tools@v0.100.0/internal/lsp/cmd/test/references.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  	"sort"
    10  	"testing"
    11  
    12  	"github.com/v2fly/tools/internal/span"
    13  )
    14  
    15  func (r *runner) References(t *testing.T, spn span.Span, itemList []span.Span) {
    16  	for _, includeDeclaration := range []bool{true, false} {
    17  		t.Run(fmt.Sprintf("refs-declaration-%v", includeDeclaration), func(t *testing.T) {
    18  			var itemStrings []string
    19  			for i, s := range itemList {
    20  				// We don't want the first result if we aren't including the declaration.
    21  				if i == 0 && !includeDeclaration {
    22  					continue
    23  				}
    24  				itemStrings = append(itemStrings, fmt.Sprint(s))
    25  			}
    26  			sort.Strings(itemStrings)
    27  			var expect string
    28  			for _, s := range itemStrings {
    29  				expect += s + "\n"
    30  			}
    31  			expect = r.Normalize(expect)
    32  
    33  			uri := spn.URI()
    34  			filename := uri.Filename()
    35  			target := filename + fmt.Sprintf(":%v:%v", spn.Start().Line(), spn.Start().Column())
    36  			args := []string{"references"}
    37  			if includeDeclaration {
    38  				args = append(args, "-d")
    39  			}
    40  			args = append(args, target)
    41  			got, stderr := r.NormalizeGoplsCmd(t, args...)
    42  			if stderr != "" {
    43  				t.Errorf("references failed for %s: %s", target, stderr)
    44  			} else if expect != got {
    45  				t.Errorf("references failed for %s expected:\n%s\ngot:\n%s", target, expect, got)
    46  			}
    47  		})
    48  	}
    49  }