Skip to content

Commit

Permalink
decoder: Test semtok for complex index expr
Browse files Browse the repository at this point in the history
  • Loading branch information
dbanck committed Jan 16, 2024
1 parent 6e9ccea commit 13836c4
Showing 1 changed file with 190 additions and 0 deletions.
190 changes: 190 additions & 0 deletions decoder/expr_any_index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,3 +370,193 @@ func TestHoverAtPos_exprAny_index(t *testing.T) {
})
}
}

func TestSemanticTokens_exprAny_index(t *testing.T) {
testCases := []struct {
testName string
attrSchema map[string]*schema.AttributeSchema
refOrigins reference.Origins
refTargets reference.Targets
cfg string
expectedSemanticTokens []lang.SemanticToken
}{
{
"simple conditional",
map[string]*schema.AttributeSchema{
"attr": {
Constraint: schema.AnyExpression{
OfType: cty.String,
},
},
},
reference.Origins{},
reference.Targets{},
`attr = true ? "t" : 422
`,
[]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.TokenBool,
Modifiers: lang.SemanticTokenModifiers{},
Range: hcl.Range{
Filename: "test.tf",
Start: hcl.Pos{Line: 1, Column: 8, Byte: 7},
End: hcl.Pos{Line: 1, Column: 12, Byte: 11},
},
},
{
Type: lang.TokenString,
Modifiers: lang.SemanticTokenModifiers{},
Range: hcl.Range{
Filename: "test.tf",
Start: hcl.Pos{Line: 1, Column: 15, Byte: 14},
End: hcl.Pos{Line: 1, Column: 18, Byte: 17},
},
},
{
Type: lang.TokenNumber,
Modifiers: lang.SemanticTokenModifiers{},
Range: hcl.Range{
Filename: "test.tf",
Start: hcl.Pos{Line: 1, Column: 21, Byte: 20},
End: hcl.Pos{Line: 1, Column: 24, Byte: 23},
},
},
},
},
{
"empty index",
map[string]*schema.AttributeSchema{
"attr": {
Constraint: schema.AnyExpression{
OfType: cty.String,
},
},
},
reference.Origins{},
reference.Targets{},
`attr = aws_instance.name.tags[local.name]
`,
[]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},
},
},
},
},
{
"local reference in index",
map[string]*schema.AttributeSchema{
"attr": {
Constraint: schema.AnyExpression{
OfType: cty.String,
},
},
},
reference.Origins{
reference.LocalOrigin{
Addr: lang.Address{
lang.RootStep{Name: "local"},
lang.AttrStep{Name: "name"},
},
Range: hcl.Range{
Filename: "test.tf",
Start: hcl.Pos{Line: 1, Column: 31, Byte: 30},
End: hcl.Pos{Line: 1, Column: 41, Byte: 40},
},
Constraints: reference.OriginConstraints{
{
OfType: cty.String,
},
},
},
},
reference.Targets{
{
Addr: lang.Address{
lang.RootStep{Name: "local"},
lang.AttrStep{Name: "name"},
},
RangePtr: &hcl.Range{
Filename: "variables.tf",
Start: hcl.Pos{Line: 2, Column: 1, Byte: 17},
End: hcl.Pos{Line: 2, Column: 3, Byte: 19},
},
Type: cty.String,
},
},
`attr = aws_instance.name.tags[local.name]
`,
[]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.TokenReferenceStep,
Modifiers: lang.SemanticTokenModifiers{},
Range: hcl.Range{
Filename: "test.tf",
Start: hcl.Pos{Line: 1, Column: 31, Byte: 30},
End: hcl.Pos{Line: 1, Column: 36, Byte: 35},
},
},
{
Type: lang.TokenReferenceStep,
Modifiers: lang.SemanticTokenModifiers{},
Range: hcl.Range{
Filename: "test.tf",
Start: hcl.Pos{Line: 1, Column: 37, Byte: 36},
End: hcl.Pos{Line: 1, Column: 41, 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,
},
ReferenceOrigins: tc.refOrigins,
ReferenceTargets: tc.refTargets,
})

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 13836c4

Please sign in to comment.