-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[TEST] integration tests for go to definition
- Loading branch information
Showing
2 changed files
with
115 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file was deleted.
Oops, something went wrong.