github.com/tiagovtristao/plz@v13.4.0+incompatible/tools/build_langserver/langserver/reference_test.go (about)

     1  package langserver
     2  
     3  import (
     4  	"context"
     5  	"github.com/thought-machine/please/src/core"
     6  	"testing"
     7  
     8  	"github.com/thought-machine/please/tools/build_langserver/lsp"
     9  
    10  	"github.com/stretchr/testify/assert"
    11  	"os"
    12  	"path"
    13  )
    14  
    15  func TestGetReferences(t *testing.T) {
    16  	ctx := context.Background()
    17  
    18  	// copy over the handler from the setup and get a new analyzer so it would be reading the config
    19  	a, _ := newAnalyzer()
    20  	h := LsHandler{
    21  		repoRoot:  core.RepoRoot,
    22  		workspace: newWorkspaceStore(lsp.DocumentURI(core.RepoRoot)),
    23  		analyzer:  a,
    24  		init: &lsp.InitializeParams{
    25  			RootURI: lsp.DocumentURI(core.RepoRoot),
    26  			Capabilities: lsp.ClientCapabilities{
    27  				TextDocument: lsp.TextDocumentClientCapabilities{
    28  					Completion: lsp.Completion{
    29  						CompletionItem: struct {
    30  							SnippetSupport bool `json:"snippetSupport,omitempty"`
    31  						}{SnippetSupport: true}},
    32  				},
    33  			},
    34  		},
    35  	}
    36  
    37  	h.analyzer.State.Config.Parse.BuildFileName = append(analyzer.State.Config.Parse.BuildFileName,
    38  		"BUILD.test")
    39  
    40  	testDir, err := os.Getwd()
    41  	assert.NoError(t, err)
    42  	uri := lsp.DocumentURI("file://tools/build_langserver/langserver/test_data/reference/BUILD.test")
    43  
    44  	locs, err := h.getReferences(ctx, uri, lsp.Position{Line: 0, Character: 4})
    45  	assert.NoError(t, err)
    46  
    47  	assert.Equal(t, 1, len(locs))
    48  
    49  	// Reference in //src:please
    50  	expected := lsp.Location{
    51  		URI: lsp.DocumentURI("file://" + path.Join(testDir, "tools/build_langserver/langserver/test_data/reference/BUILD.test")),
    52  		Range: lsp.Range{
    53  			Start: lsp.Position{Line: 8, Character: 0},
    54  			End:   lsp.Position{Line: 14, Character: 1},
    55  		},
    56  	}
    57  	assertLocInList(t, locs, expected)
    58  }
    59  
    60  func assertLocInList(t testing.TB, locs []lsp.Location, passLoc lsp.Location) {
    61  	for _, loc := range locs {
    62  		if loc.URI == passLoc.URI {
    63  			assert.Equal(t, loc.Range, passLoc.Range)
    64  			return
    65  		}
    66  	}
    67  	// mark test as fail if not found
    68  	t.Errorf("loc %s is not in the list", passLoc)
    69  }