-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
decoder: Test semantic tokens for List
- Loading branch information
Showing
1 changed file
with
251 additions
and
0 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,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) | ||
} | ||
}) | ||
} | ||
} |