Skip to content

Commit

Permalink
decoder: Test semantic tokens for List
Browse files Browse the repository at this point in the history
  • Loading branch information
dbanck committed Mar 7, 2023
1 parent c911a74 commit 28c9886
Showing 1 changed file with 251 additions and 0 deletions.
251 changes: 251 additions & 0 deletions decoder/expr_list_semtok_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,251 @@
package decoder

import (
"context"
"fmt"
"testing"

"github.com/google/go-cmp/cmp"
"github.com/hashicorp/hcl-lang/lang"
"github.com/hashicorp/hcl-lang/schema"
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hclsyntax"
)

func TestSemanticTokens_exprList(t *testing.T) {
testCases := []struct {
testName string
attrSchema map[string]*schema.AttributeSchema
cfg string
expectedSemanticTokens []lang.SemanticToken
}{
{
"empty list without element",
map[string]*schema.AttributeSchema{
"attr": {
Constraint: schema.List{},
},
},
`attr = []`,
[]lang.SemanticToken{
{
Type: lang.TokenAttrName,
Modifiers: lang.SemanticTokenModifiers{},
Range: hcl.Range{
Filename: "test.tf",
Start: hcl.Pos{Line: 1, Column: 1, Byte: 0},
End: hcl.Pos{Line: 1, Column: 5, Byte: 4},
},
},
},
},
{
"empty list with element",
map[string]*schema.AttributeSchema{
"attr": {
Constraint: schema.List{
Elem: schema.Keyword{
Keyword: "keyword",
},
},
},
},
`attr = []`,
[]lang.SemanticToken{
{
Type: lang.TokenAttrName,
Modifiers: lang.SemanticTokenModifiers{},
Range: hcl.Range{
Filename: "test.tf",
Start: hcl.Pos{Line: 1, Column: 1, Byte: 0},
End: hcl.Pos{Line: 1, Column: 5, Byte: 4},
},
},
},
},
{
"single element list",
map[string]*schema.AttributeSchema{
"attr": {
Constraint: schema.List{
Elem: schema.Keyword{
Keyword: "keyword",
},
},
},
},
`attr = [keyword]`,
[]lang.SemanticToken{
{
Type: lang.TokenAttrName,
Modifiers: lang.SemanticTokenModifiers{},
Range: hcl.Range{
Filename: "test.tf",
Start: hcl.Pos{Line: 1, Column: 1, Byte: 0},
End: hcl.Pos{Line: 1, Column: 5, Byte: 4},
},
},
{
Type: lang.TokenKeyword,
Modifiers: lang.SemanticTokenModifiers{},
Range: hcl.Range{
Filename: "test.tf",
Start: hcl.Pos{Line: 1, Column: 9, Byte: 8},
End: hcl.Pos{Line: 1, Column: 16, Byte: 15},
},
},
},
},
{
"single element multi-line list",
map[string]*schema.AttributeSchema{
"attr": {
Constraint: schema.List{
Elem: schema.Keyword{
Keyword: "keyword",
},
},
},
},
`attr = [
keyword,
]`,
[]lang.SemanticToken{
{
Type: lang.TokenAttrName,
Modifiers: lang.SemanticTokenModifiers{},
Range: hcl.Range{
Filename: "test.tf",
Start: hcl.Pos{Line: 1, Column: 1, Byte: 0},
End: hcl.Pos{Line: 1, Column: 5, Byte: 4},
},
},
{
Type: lang.TokenKeyword,
Modifiers: lang.SemanticTokenModifiers{},
Range: hcl.Range{
Filename: "test.tf",
Start: hcl.Pos{Line: 2, Column: 3, Byte: 11},
End: hcl.Pos{Line: 2, Column: 10, Byte: 18},
},
},
},
},
{
"multi-element multi-line list",
map[string]*schema.AttributeSchema{
"attr": {
Constraint: schema.List{
Elem: schema.Keyword{
Keyword: "keyword",
},
},
},
},
`attr = [
keyword,
keyword,
]`,
[]lang.SemanticToken{
{
Type: lang.TokenAttrName,
Modifiers: lang.SemanticTokenModifiers{},
Range: hcl.Range{
Filename: "test.tf",
Start: hcl.Pos{Line: 1, Column: 1, Byte: 0},
End: hcl.Pos{Line: 1, Column: 5, Byte: 4},
},
},
{
Type: lang.TokenKeyword,
Modifiers: lang.SemanticTokenModifiers{},
Range: hcl.Range{
Filename: "test.tf",
Start: hcl.Pos{Line: 2, Column: 3, Byte: 11},
End: hcl.Pos{Line: 2, Column: 10, Byte: 18},
},
},
{
Type: lang.TokenKeyword,
Modifiers: lang.SemanticTokenModifiers{},
Range: hcl.Range{
Filename: "test.tf",
Start: hcl.Pos{Line: 3, Column: 3, Byte: 22},
End: hcl.Pos{Line: 3, Column: 10, Byte: 29},
},
},
},
},
{
"multi-element multi-line list with invalid element",
map[string]*schema.AttributeSchema{
"attr": {
Constraint: schema.List{
Elem: schema.Keyword{
Keyword: "keyword",
},
},
},
},
`attr = [
keyword,
invalid,
keyword,
]`,
[]lang.SemanticToken{
{
Type: lang.TokenAttrName,
Modifiers: lang.SemanticTokenModifiers{},
Range: hcl.Range{
Filename: "test.tf",
Start: hcl.Pos{Line: 1, Column: 1, Byte: 0},
End: hcl.Pos{Line: 1, Column: 5, Byte: 4},
},
},
{
Type: lang.TokenKeyword,
Modifiers: lang.SemanticTokenModifiers{},
Range: hcl.Range{
Filename: "test.tf",
Start: hcl.Pos{Line: 2, Column: 3, Byte: 11},
End: hcl.Pos{Line: 2, Column: 10, Byte: 18},
},
},
{
Type: lang.TokenKeyword,
Modifiers: lang.SemanticTokenModifiers{},
Range: hcl.Range{
Filename: "test.tf",
Start: hcl.Pos{Line: 4, Column: 3, Byte: 33},
End: hcl.Pos{Line: 4, Column: 10, Byte: 40},
},
},
},
},
}
for i, tc := range testCases {
t.Run(fmt.Sprintf("%d-%s", i, tc.testName), func(t *testing.T) {
bodySchema := &schema.BodySchema{
Attributes: tc.attrSchema,
}

f, _ := hclsyntax.ParseConfig([]byte(tc.cfg), "test.tf", hcl.InitialPos)
d := testPathDecoder(t, &PathContext{
Schema: bodySchema,
Files: map[string]*hcl.File{
"test.tf": f,
},
})

ctx := context.Background()
tokens, err := d.SemanticTokensInFile(ctx, "test.tf")
if err != nil {
t.Fatal(err)
}

if diff := cmp.Diff(tc.expectedSemanticTokens, tokens); diff != "" {
t.Fatalf("unexpected tokens: %s", diff)
}
})
}
}

0 comments on commit 28c9886

Please sign in to comment.