Skip to content

Commit

Permalink
test(definition): tests for variable definition
Browse files Browse the repository at this point in the history
  • Loading branch information
qvalentin committed Jul 2, 2024
1 parent 64267bb commit 581b71b
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 1 deletion.
1 change: 1 addition & 0 deletions internal/handler/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

func (h *langHandler) Completion(ctx context.Context, params *lsp.CompletionParams) (result *lsp.CompletionList, err error) {
logger.Debug("Running completion with params", params)

genericDocumentUseCase, err := h.NewGenericDocumentUseCase(params.TextDocumentPositionParams, lsplocal.NestedNodeAtPositionForCompletion)
if err != nil {
return nil, err
Expand Down
3 changes: 2 additions & 1 deletion internal/handler/definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ func (h *langHandler) Definition(_ context.Context, params *lsp.DefinitionParams
if err != nil {
return nil, err
}

usecases := []languagefeatures.DefinitionUseCase{
languagefeatures.NewBuiltInObjectsFeature(genericDocumentUseCase), // has to be before template context
languagefeatures.NewVariablesFeature(genericDocumentUseCase),
languagefeatures.NewTemplateContextFeature(genericDocumentUseCase),
languagefeatures.NewIncludesCallFeature(genericDocumentUseCase),
languagefeatures.NewVariablesFeature(genericDocumentUseCase),
}

for _, usecase := range usecases {
Expand Down
69 changes: 69 additions & 0 deletions internal/handler/definition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ package handler
import (
"context"
"reflect"
"strings"
"testing"

"github.com/mrjosh/helm-ls/internal/adapter/yamlls"
"github.com/mrjosh/helm-ls/internal/charts"
lsplocal "github.com/mrjosh/helm-ls/internal/lsp"
"github.com/mrjosh/helm-ls/internal/util"
"github.com/stretchr/testify/assert"
"go.lsp.dev/protocol"
lsp "go.lsp.dev/protocol"
"go.lsp.dev/uri"
yamlv3 "gopkg.in/yaml.v3"
Expand Down Expand Up @@ -329,3 +331,70 @@ func TestDefinitionValueFileMulitpleValues(t *testing.T) {
},
}, nil)
}

func TestDefinitionSingleLine(t *testing.T) {
testCases := []struct {
// defines a definition test where ^ is the position where the defintion is triggered
// and §result§ marks the range of the result
templateWithMarks string
}{
{"{{ §$test := 1§ }} {{ $te^st }}"},
{"{{ §$test := .Values.test§ }} {{ $te^st.with.selectorexpression }}"},
{"{{ §$test := $.Values.test§ }} {{ $te^st.with.selectorexpression. }}"},
{"{{ §$test := .Values.test§ }} {{ $te^st }}"},
{"{{ range §$test := .Values.test§ }} {{ $te^st }} {{ end }}"},
{"{{ range §$test := $.Values.test§ }} {{ $te^st.something }} {{ end }}"},
{"{{ range §$test := $.Values.test§ }} {{ $te^st. }} {{ end }}"},
{"{{ range §$test := $.Values.test§ }} {{ if not $te^st }} y {{ else }} n {{ end }}"},
}
for _, tc := range testCases {
t.Run(tc.templateWithMarks, func(t *testing.T) {
col := strings.Index(tc.templateWithMarks, "^")
buf := strings.Replace(tc.templateWithMarks, "^", "", 1)
pos := protocol.Position{Line: 0, Character: uint32(col - 3)}
expectedColStart := strings.Index(buf, "§")
buf = strings.Replace(buf, "§", "", 1)
expectedColEnd := strings.Index(buf, "§")
buf = strings.Replace(buf, "§", "", 1)

documents := lsplocal.NewDocumentStore()
fileURI := testDocumentTemplateURI
rootUri := uri.File("/")

d := lsp.DidOpenTextDocumentParams{
TextDocument: lsp.TextDocumentItem{
URI: fileURI,
Text: buf,
},
}
documents.DidOpen(&d, util.DefaultConfig)
h := &langHandler{
chartStore: charts.NewChartStore(rootUri, charts.NewChart),
documents: documents,
}

locations, err := h.Definition(context.TODO(), &lsp.DefinitionParams{
TextDocumentPositionParams: lsp.TextDocumentPositionParams{
TextDocument: lsp.TextDocumentIdentifier{URI: fileURI},
Position: pos,
},
})

assert.NoError(t, err)

assert.Contains(t, locations, lsp.Location{
URI: testDocumentTemplateURI,
Range: lsp.Range{
Start: lsp.Position{
Line: 0,
Character: uint32(expectedColStart),
},
End: lsp.Position{
Line: 0,
Character: uint32(expectedColEnd),
},
},
})
})
}
}

0 comments on commit 581b71b

Please sign in to comment.