Skip to content

Commit a86a9e1

Browse files
committed
test expression extension in a sub block
1 parent 9f4bbb7 commit a86a9e1

File tree

1 file changed

+231
-0
lines changed

1 file changed

+231
-0
lines changed

decoder/semantic_tokens_test.go

Lines changed: 231 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1404,3 +1404,234 @@ resource "vault_auth_backend" "blah" {
14041404
t.Fatalf("unexpected tokens: %s", diff)
14051405
}
14061406
}
1407+
1408+
func TestDecoder_SemanticTokensInFile_extensions_countIndexInSubBlock(t *testing.T) {
1409+
bodySchema := &schema.BodySchema{
1410+
Blocks: map[string]*schema.BlockSchema{
1411+
"resource": {
1412+
Body: &schema.BodySchema{
1413+
Extensions: &schema.BodyExtensions{
1414+
Count: true,
1415+
},
1416+
Attributes: map[string]*schema.AttributeSchema{
1417+
"count": {
1418+
Expr: schema.LiteralTypeOnly(cty.Number),
1419+
},
1420+
},
1421+
Blocks: map[string]*schema.BlockSchema{
1422+
"block": {
1423+
Body: &schema.BodySchema{
1424+
Attributes: map[string]*schema.AttributeSchema{
1425+
"attr": {
1426+
Expr: schema.LiteralTypeOnly(cty.Number),
1427+
},
1428+
},
1429+
},
1430+
},
1431+
},
1432+
},
1433+
Labels: []*schema.LabelSchema{
1434+
{
1435+
Name: "type",
1436+
IsDepKey: true,
1437+
SemanticTokenModifiers: lang.SemanticTokenModifiers{
1438+
lang.TokenModifierDependent,
1439+
},
1440+
},
1441+
{Name: "name"},
1442+
},
1443+
},
1444+
},
1445+
}
1446+
1447+
testCfg := []byte(`
1448+
resource "foobar" "name" {
1449+
count = 1
1450+
block {
1451+
attr = count.index
1452+
}
1453+
}
1454+
`)
1455+
1456+
f, pDiags := hclsyntax.ParseConfig(testCfg, "test.tf", hcl.InitialPos)
1457+
if len(pDiags) > 0 {
1458+
t.Fatal(pDiags)
1459+
}
1460+
1461+
d := testPathDecoder(t, &PathContext{
1462+
Schema: bodySchema,
1463+
Files: map[string]*hcl.File{
1464+
"test.tf": f,
1465+
},
1466+
})
1467+
1468+
ctx := context.Background()
1469+
1470+
tokens, err := d.SemanticTokensInFile(ctx, "test.tf")
1471+
if err != nil {
1472+
t.Fatal(err)
1473+
}
1474+
1475+
expectedTokens := []lang.SemanticToken{
1476+
{ // resource
1477+
Type: lang.TokenBlockType,
1478+
Modifiers: []lang.SemanticTokenModifier{},
1479+
Range: hcl.Range{
1480+
Filename: "test.tf",
1481+
Start: hcl.Pos{
1482+
Line: 2,
1483+
Column: 1,
1484+
Byte: 1,
1485+
},
1486+
End: hcl.Pos{
1487+
Line: 2,
1488+
Column: 9,
1489+
Byte: 9,
1490+
},
1491+
},
1492+
},
1493+
{ // foobar
1494+
Type: lang.TokenBlockLabel,
1495+
Modifiers: []lang.SemanticTokenModifier{
1496+
lang.TokenModifierDependent,
1497+
},
1498+
Range: hcl.Range{
1499+
Filename: "test.tf",
1500+
Start: hcl.Pos{
1501+
Line: 2,
1502+
Column: 10,
1503+
Byte: 10,
1504+
},
1505+
End: hcl.Pos{
1506+
Line: 2,
1507+
Column: 18,
1508+
Byte: 18,
1509+
},
1510+
},
1511+
},
1512+
{ // name
1513+
Type: lang.TokenBlockLabel,
1514+
Modifiers: []lang.SemanticTokenModifier{},
1515+
Range: hcl.Range{
1516+
Filename: "test.tf",
1517+
Start: hcl.Pos{
1518+
Line: 2,
1519+
Column: 19,
1520+
Byte: 19,
1521+
},
1522+
End: hcl.Pos{
1523+
Line: 2,
1524+
Column: 25,
1525+
Byte: 25,
1526+
},
1527+
},
1528+
},
1529+
{ // count
1530+
Type: lang.TokenAttrName,
1531+
Modifiers: lang.SemanticTokenModifiers{},
1532+
Range: hcl.Range{
1533+
Filename: "test.tf",
1534+
Start: hcl.Pos{
1535+
Line: 3,
1536+
Column: 2,
1537+
Byte: 29,
1538+
},
1539+
End: hcl.Pos{
1540+
Line: 3,
1541+
Column: 7,
1542+
Byte: 34,
1543+
},
1544+
},
1545+
},
1546+
{ // 1 number
1547+
Type: lang.TokenNumber,
1548+
Modifiers: lang.SemanticTokenModifiers{},
1549+
Range: hcl.Range{
1550+
Filename: "test.tf",
1551+
Start: hcl.Pos{
1552+
Line: 3,
1553+
Column: 10,
1554+
Byte: 37,
1555+
},
1556+
End: hcl.Pos{
1557+
Line: 3,
1558+
Column: 11,
1559+
Byte: 38,
1560+
},
1561+
},
1562+
},
1563+
{ // block
1564+
Type: lang.TokenBlockType,
1565+
Modifiers: lang.SemanticTokenModifiers{},
1566+
Range: hcl.Range{
1567+
Filename: "test.tf",
1568+
Start: hcl.Pos{
1569+
Line: 4,
1570+
Column: 2,
1571+
Byte: 40,
1572+
},
1573+
End: hcl.Pos{
1574+
Line: 4,
1575+
Column: 7,
1576+
Byte: 45,
1577+
},
1578+
},
1579+
},
1580+
{ // attr
1581+
Type: lang.TokenAttrName,
1582+
Modifiers: lang.SemanticTokenModifiers{},
1583+
Range: hcl.Range{
1584+
Filename: "test.tf",
1585+
Start: hcl.Pos{
1586+
Line: 5,
1587+
Column: 3,
1588+
Byte: 50,
1589+
},
1590+
End: hcl.Pos{
1591+
Line: 5,
1592+
Column: 7,
1593+
Byte: 54,
1594+
},
1595+
},
1596+
},
1597+
{ // count
1598+
Type: lang.TokenTraversalStep,
1599+
Modifiers: lang.SemanticTokenModifiers{},
1600+
Range: hcl.Range{
1601+
Filename: "test.tf",
1602+
Start: hcl.Pos{
1603+
Line: 5,
1604+
Column: 10,
1605+
Byte: 57,
1606+
},
1607+
End: hcl.Pos{
1608+
Line: 5,
1609+
Column: 15,
1610+
Byte: 62,
1611+
},
1612+
},
1613+
},
1614+
{ // index
1615+
Type: lang.TokenTraversalStep,
1616+
Modifiers: lang.SemanticTokenModifiers{},
1617+
Range: hcl.Range{
1618+
Filename: "test.tf",
1619+
Start: hcl.Pos{
1620+
Line: 5,
1621+
Column: 16,
1622+
Byte: 63,
1623+
},
1624+
End: hcl.Pos{
1625+
Line: 5,
1626+
Column: 22,
1627+
Byte: 69,
1628+
},
1629+
},
1630+
},
1631+
}
1632+
1633+
diff := cmp.Diff(expectedTokens, tokens)
1634+
if diff != "" {
1635+
t.Fatalf("unexpected tokens: %s", diff)
1636+
}
1637+
}

0 commit comments

Comments
 (0)