From 2722199cd6d0a6c0af3b5c8b8dcd36d5e61d6905 Mon Sep 17 00:00:00 2001 From: qvalentin Date: Fri, 28 Jul 2023 19:38:22 +0200 Subject: [PATCH] [TEST] integration tests for go to definition --- internal/handler/definition_test.go | 115 +++++++++++++++++++++++++ internal/handler/defintion_ast_test.go | 7 -- 2 files changed, 115 insertions(+), 7 deletions(-) create mode 100644 internal/handler/definition_test.go delete mode 100644 internal/handler/defintion_ast_test.go diff --git a/internal/handler/definition_test.go b/internal/handler/definition_test.go new file mode 100644 index 00000000..17e05f4d --- /dev/null +++ b/internal/handler/definition_test.go @@ -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) +} diff --git a/internal/handler/defintion_ast_test.go b/internal/handler/defintion_ast_test.go deleted file mode 100644 index a31332fb..00000000 --- a/internal/handler/defintion_ast_test.go +++ /dev/null @@ -1,7 +0,0 @@ -package handler - -import "testing" - -func TestDefinitionAstParsingWithVariable(t *testing.T) { - -}