Skip to content

Commit

Permalink
test(hover): test many hover cases
Browse files Browse the repository at this point in the history
  • Loading branch information
qvalentin committed Mar 17, 2024
1 parent e655520 commit 3aafb82
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 3 deletions.
1 change: 1 addition & 0 deletions internal/adapter/yamlls/diagnostics_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ func TestYamllsDiagnosticsIntegration(t *testing.T) {
}

func TestYamllsDiagnosticsIntegrationWithSchema(t *testing.T) {
t.Parallel()
diagnosticsChan := make(chan lsp.PublishDiagnosticsParams)

config := util.DefaultConfig.YamllsConfiguration
Expand Down
5 changes: 2 additions & 3 deletions internal/adapter/yamlls/integration_test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ func openFile(t *testing.T, documents *lsplocal.DocumentStore, path string, yaml
Text: string(content),
},
}
documents.DidOpen(&d, util.DefaultConfig)
tree := lsplocal.ParseAst(nil, d.TextDocument.Text)
yamllsConnector.DocumentDidOpen(tree, d)
doc, err := documents.DidOpen(&d, util.DefaultConfig)
yamllsConnector.DocumentDidOpen(doc.Ast, d)
}
127 changes: 127 additions & 0 deletions internal/handler/hover_main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
package handler

import (
"context"
"fmt"
"os"
"path/filepath"
"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"
lsp "go.lsp.dev/protocol"
"go.lsp.dev/uri"
)

func TestHoverMain(t *testing.T) {
testCases := []struct {
desc string
position lsp.Position
expected string
expectedError error
}{
{
desc: "Test hover on function",
position: lsp.Position{
Line: 7,
Character: 10,
},
expected: "negate the boolean value of $x",
expectedError: nil,
},
{
desc: "Test hover on .Values",
position: lsp.Position{
Line: 25,
Character: 18,
},
expected: "The values made available through values.yaml, --set and -f.",
expectedError: nil,
},
{
desc: "Test hover on empty array in .Values",
position: lsp.Position{
Line: 25,
Character: 28,
},
expected: fmt.Sprintf("### %s\n%s\n\n", filepath.Join("..", "..", "testdata", "example", "values.yaml"), "[]"),
expectedError: nil,
},
{
desc: "Test hover on .Chart metadata",
position: lsp.Position{
Line: 33,
Character: 28,
},
expected: "Name of the chart\n\nexample\n",
expectedError: nil,
},
{
desc: "Test hover on dot",
position: lsp.Position{
Line: 17,
Character: 19,
},
expected: fmt.Sprintf("### %s\n%s\n\n\n", filepath.Join("..", "..", "testdata", "example", "values.yaml"), "{}"),
expectedError: nil,
},
{
desc: "Test hover on .Files function",
position: lsp.Position{
Line: 68,
Character: 24,
},
expected: "Returns a list of files whose names match the given shell glob pattern.\n",
expectedError: nil,
},
{
desc: "Test hover on yaml text",
position: lsp.Position{
Line: 0,
Character: 0,
},
expected: "",
expectedError: nil,
},
}
for _, tt := range testCases {
t.Run(tt.desc, func(t *testing.T) {
documents := lsplocal.NewDocumentStore()

path := "../../testdata/example/templates/deployment.yaml"
fileURI := uri.File(path)

content, err := os.ReadFile(path)
if err != nil {
t.Fatal("Could not read test file", err)
}
d := lsp.DidOpenTextDocumentParams{
TextDocument: lsp.TextDocumentItem{
URI: fileURI,
LanguageID: "",
Version: 0,
Text: string(content),
},
}
documents.DidOpen(&d, util.DefaultConfig)
h := &langHandler{
chartStore: charts.NewChartStore(uri.File("."), charts.NewChart),
documents: documents,
yamllsConnector: &yamlls.Connector{},
}
result, err := h.Hover(context.Background(), &lsp.HoverParams{
TextDocumentPositionParams: lsp.TextDocumentPositionParams{
TextDocument: lsp.TextDocumentIdentifier{
URI: fileURI,
},
Position: tt.position,
},
})
assert.Equal(t, tt.expectedError, err)
assert.Equal(t, tt.expected, result.Contents.Value)
})
}
}
2 changes: 2 additions & 0 deletions testdata/example/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,6 @@ spec:
{{- with .Values.tolerations }}
tolerations:
{{- toYaml . | nindent 8 }}
{{- if $.Files.Glob "files/dags/*.py" }}
{{- end }}
{{- end }}

0 comments on commit 3aafb82

Please sign in to comment.