Skip to content

Commit

Permalink
test: tdt for symbol table template context
Browse files Browse the repository at this point in the history
  • Loading branch information
qvalentin committed Jan 10, 2025
1 parent 53aec3d commit 2c5d440
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 14 deletions.
12 changes: 12 additions & 0 deletions internal/lsp/ast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"strings"
"testing"

"github.com/mrjosh/helm-ls/internal/util"
sitter "github.com/smacker/go-tree-sitter"
"github.com/stretchr/testify/assert"
"go.lsp.dev/protocol"
Expand Down Expand Up @@ -100,3 +101,14 @@ func getPositionForMarkedTestLine(buf string) (protocol.Position, string) {
pos := protocol.Position{Line: 0, Character: uint32(col)}
return pos, buf
}

func getRangeForMarkedTestLine(template string) (sitter.Range, string) {
pos0, template := getPositionForMarkedTestLine(template)
pos1, template := getPositionForMarkedTestLine(template)
return sitter.Range{
StartPoint: util.PositionToPoint(pos0),
EndPoint: util.PositionToPoint(pos1),
StartByte: pos0.Character,
EndByte: pos1.Character,
}, template
}
113 changes: 99 additions & 14 deletions internal/lsp/symbol_table_template_context_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package lsp

import (
"errors"
"testing"

"github.com/mrjosh/helm-ls/internal/util"

Check failure on line 7 in internal/lsp/symbol_table_template_context_test.go

View workflow job for this annotation

GitHub Actions / tests (1.22.4, ubuntu-latest)

"github.com/mrjosh/helm-ls/internal/util" imported and not used

Check failure on line 7 in internal/lsp/symbol_table_template_context_test.go

View workflow job for this annotation

GitHub Actions / tests (1.22.4, ubuntu-latest)

"github.com/mrjosh/helm-ls/internal/util" imported and not used

Check failure on line 7 in internal/lsp/symbol_table_template_context_test.go

View workflow job for this annotation

GitHub Actions / tests (1.22.4, ubuntu-latest)

"github.com/mrjosh/helm-ls/internal/util" imported and not used

Check failure on line 7 in internal/lsp/symbol_table_template_context_test.go

View workflow job for this annotation

GitHub Actions / lint (1.22.7, ubuntu-latest)

"github.com/mrjosh/helm-ls/internal/util" imported and not used (typecheck)

Check failure on line 7 in internal/lsp/symbol_table_template_context_test.go

View workflow job for this annotation

GitHub Actions / tests (1.22.4, macos-latest)

"github.com/mrjosh/helm-ls/internal/util" imported and not used

Check failure on line 7 in internal/lsp/symbol_table_template_context_test.go

View workflow job for this annotation

GitHub Actions / tests (1.22.4, macos-latest)

"github.com/mrjosh/helm-ls/internal/util" imported and not used

Check failure on line 7 in internal/lsp/symbol_table_template_context_test.go

View workflow job for this annotation

GitHub Actions / tests (1.22.4, macos-latest)

"github.com/mrjosh/helm-ls/internal/util" imported and not used
Expand Down Expand Up @@ -46,27 +47,111 @@ func TestTemplateContextVisitor(t *testing.T) {

func TestGetTemplateContextFromSymbolTable(t *testing.T) {
testCases := []struct {
desc string
template string
err error
expected TemplateContext
desc string
templateWithRangeMark string
expected TemplateContext
err error
}{
{
desc: "Selects simple selector expression correctly",
template: `{{ .Values.test }}`,
expected: TemplateContext{"Values", "test"},
desc: "Selects end of simple selector expression correctly",
templateWithRangeMark: `{{ .Values.^test^ }}`,
expected: TemplateContext{"Values", "test"},
},
{
desc: "Selects start of simple selector expression correctly",
templateWithRangeMark: `{{ .^Values^.test }}`,
expected: TemplateContext{"Values"},
},
{
desc: "Selects dot of simple selector expression correctly",
templateWithRangeMark: `{{ .Values^.^test }}`,
expected: TemplateContext{"Values", ""},
},
{
desc: "Selects starting dot of simple selector expression correctly",
templateWithRangeMark: `{{ ^.^Values.test }}`,
expected: TemplateContext{""},
},
{
desc: "Selects dot of unfinished selector expression correctly",
templateWithRangeMark: `{{ .Values.test^.^ }}`,
expected: TemplateContext{"Values", "test", ""},
},
{
desc: "Selects starting dot of unfinished selector expression correctly",
templateWithRangeMark: `{{ ^.^Values.test. }}`,
expected: TemplateContext{""},
},
{
desc: "Selects middle dot of unfinished selector expression correctly",
templateWithRangeMark: `{{ .Values^.^test. }}`,
expected: TemplateContext{"Values", ""},
},
{
desc: "Selects dot correctly",
templateWithRangeMark: ` {{ ^.^ }}`, // This is not consistent with dot in selector expression, but it's o.k.
expected: TemplateContext{},
},
{
desc: "Selects dot in with correctly",
templateWithRangeMark: `{{ with .Values.test }} {{ ^.^ }} {{ end }}`,
expected: TemplateContext{"Values", "test"},
},
{
desc: "Selects selector expression in with correctly",
templateWithRangeMark: `{{ with .Values.test }} {{ .^something^ }} {{ end }}`,
expected: TemplateContext{"Values", "test", "something"},
},
{
desc: "Selects unfinished selector expression in with correctly",
templateWithRangeMark: `{{ with .Values.test }} {{ .something^.^ }} {{ end }}`,
expected: TemplateContext{"Values", "test", "something", ""},
},
{
desc: "Selects start dot of unfinished selector expression in with correctly",
templateWithRangeMark: `{{ with .Values.test }} {{ ^.^something. }} {{ end }}`,
expected: TemplateContext{"Values", "test", ""},
},
{
desc: "Selects root selector expression in with correctly",
templateWithRangeMark: `{{ with .Values.test }} {{ $.Values.something^.^ }} {{ end }}`,
expected: TemplateContext{"Values", "something", ""},
},
{
desc: "Selects dot in range correctly",
templateWithRangeMark: `{{ range .Values.test }} {{ ^.^ }} {{ end }}`,
expected: TemplateContext{"Values", "test[]"},
},
{
desc: "Selects selector expression in range correctly",
templateWithRangeMark: `{{ range .Values.test }} {{ .^something^ }} {{ end }}`,
expected: TemplateContext{"Values", "test[]", "something"},
},
{
desc: "Selects unfinished selector expression in range correctly",
templateWithRangeMark: `{{ range .Values.test }} {{ .something^.^ }} {{ end }}`,
expected: TemplateContext{"Values", "test[]", "something", ""},
},
{
desc: "Selects root selector expression in range correctly",
templateWithRangeMark: `{{ range .Values.test }} {{ $.Values.something^.^ }} {{ end }}`,
expected: TemplateContext{"Values", "something", ""},
},
{
desc: "Selects variable selector expression in range correctly without variable visitor",
templateWithRangeMark: `{{ $x := .Values }} {{ range .Values.test }} {{ $x.something^.^ }} {{ end }}`,
expected: TemplateContext{"$x", "something", ""},
err: errors.New("variable $x not found"), // because we have no variable visitor in this test
},
}
for _, tC := range testCases {
t.Run(tC.desc, func(t *testing.T) {
pos, template := getPositionForMarkedTestLine(tC.template)
expectedRange, template := getRangeForMarkedTestLine(tC.templateWithRangeMark)
s := createSymbolTableWithContexts(template)
s.GetTemplateContext(sitter.Range{
StartPoint: sitter.Point{},
EndPoint: sitter.Point{},
StartByte: 0,
EndByte: 0,
}
result, err := s.GetTemplateContext(expectedRange)

assert.Equal(t, tC.err, err)
assert.Equal(t, tC.expected, result)
})
}
}
Expand Down

0 comments on commit 2c5d440

Please sign in to comment.