Skip to content

Commit

Permalink
[TEST] integration tests for go to definition
Browse files Browse the repository at this point in the history
  • Loading branch information
qvalentin committed Jul 28, 2023
1 parent 2d4c67f commit 2722199
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 7 deletions.
115 changes: 115 additions & 0 deletions internal/handler/definition_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package handler

import (
"context"
"fmt"
"reflect"
"testing"

lsplocal "github.com/mrjosh/helm-ls/internal/lsp"
gotemplate "github.com/mrjosh/helm-ls/internal/tree-sitter/gotemplate"
sitter "github.com/smacker/go-tree-sitter"
lsp "go.lsp.dev/protocol"
"go.lsp.dev/uri"
yamlv3 "gopkg.in/yaml.v3"
)

var testFileContent = `
{{ $variable := "text" }} # line 1
{{ $variable }} # line 2
{{ $someOther := "text" }}# line 4
{{ $variable }} # line 5
{{ range $index, $element := pipeline }}{{ $index }}{{ $element }}{{ end }} # line 7
{{ .Values.foo }} # line 8
`

var testDocumentTemplateURI = uri.URI("file:///test.yaml")
var testValuesURI = uri.URI("file:///values.yaml")
var valuesContent = `
foo: bar
something:
nested: false
`

func genericDefinitionTest(t *testing.T, position lsp.Position, expectedLocation lsp.Location, expectedError error) {
var node yamlv3.Node
var err = yamlv3.Unmarshal([]byte(valuesContent), &node)
if err != nil {
t.Fatal(err)
}
handler := &langHandler{
linterName: "helm-lint",
connPool: nil,
documents: nil,
values: make(map[string]interface{}),
valueNode: node,
projectFiles: ProjectFiles{
ValuesFile: "/values.yaml",
ChartFile: "",
},
}

parser := sitter.NewParser()
parser.SetLanguage(gotemplate.GetLanguage())
tree, _ := parser.ParseCtx(context.Background(), nil, []byte(testFileContent))
doc := &lsplocal.Document{
Content: testFileContent,
URI: testDocumentTemplateURI,
Ast: tree,
}

location, err := handler.definitionAstParsing(doc, position)

if err != nil && err.Error() != expectedError.Error() {
t.Errorf("expected %v, got %v", expectedError, err)
}

if reflect.DeepEqual(location, expectedLocation) == false {
t.Errorf("expected %v, got %v", expectedLocation, location)
}
}

func TestDefinitionVariable(t *testing.T) {
genericDefinitionTest(t, lsp.Position{Line: 2, Character: 8}, lsp.Location{
URI: testDocumentTemplateURI,
Range: lsp.Range{
Start: lsp.Position{
Line: 1,
Character: 3,
},
},
}, nil)
}

func TestDefinitionNotImplemented(t *testing.T) {
genericDefinitionTest(t, lsp.Position{Line: 1, Character: 1}, lsp.Location{
Range: lsp.Range{},
},
fmt.Errorf("Definition not implemented for node type %s", "{{"))
}

func TestDefinitionRange(t *testing.T) {
genericDefinitionTest(t, lsp.Position{Line: 7, Character: 60}, lsp.Location{
URI: testDocumentTemplateURI,
Range: lsp.Range{
Start: lsp.Position{
Line: 7,
Character: 17,
},
},
}, nil)
}

func TestDefinitionValue(t *testing.T) {
genericDefinitionTest(t, lsp.Position{Line: 8, Character: 13}, lsp.Location{
URI: testValuesURI,
Range: lsp.Range{
Start: lsp.Position{
Line: 1,
Character: 0,
},
},
}, nil)
}
7 changes: 0 additions & 7 deletions internal/handler/defintion_ast_test.go

This file was deleted.

0 comments on commit 2722199

Please sign in to comment.