Merge pull request #49 from hashicorp/t-hclsyntax-fix

hclsyntax: Add test for 'func()[idx]' syntax
This commit is contained in:
Radek Simko 2018-09-21 18:02:03 +01:00 committed by GitHub
commit ffb8e97eb7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 148 additions and 6 deletions

View File

@ -8,6 +8,10 @@ import (
"github.com/zclconf/go-cty/cty"
)
func init() {
deep.MaxDepth = 999
}
func TestParseConfig(t *testing.T) {
tests := []struct {
input string
@ -1393,8 +1397,8 @@ block "valid" {}
Val: cty.StringVal("Hello\n"),
SrcRange: hcl.Range{
Start: hcl.Pos{Line: 1, Column: 6, Byte: 5},
End: hcl.Pos{Line: 1, Column: 12, Byte: 11},
Start: hcl.Pos{Line: 2, Column: 1, Byte: 10},
End: hcl.Pos{Line: 3, Column: 1, Byte: 16},
},
},
},
@ -1426,8 +1430,8 @@ block "valid" {}
Val: cty.StringVal("Hi"),
SrcRange: hcl.Range{
Start: hcl.Pos{Line: 4, Column: 24, Byte: 5},
End: hcl.Pos{Line: 4, Column: 9, Byte: 28},
Start: hcl.Pos{Line: 4, Column: 6, Byte: 25},
End: hcl.Pos{Line: 4, Column: 8, Byte: 27},
},
},
},
@ -1550,8 +1554,8 @@ block "valid" {}
Name: "baz",
SrcRange: hcl.Range{
Start: hcl.Pos{Line: 1, Column: 8, Byte: 7},
End: hcl.Pos{Line: 1, Column: 12, Byte: 11},
Start: hcl.Pos{Line: 1, Column: 12, Byte: 11},
End: hcl.Pos{Line: 1, Column: 16, Byte: 15},
},
},
},
@ -1952,6 +1956,140 @@ block "valid" {}
},
},
},
{
"a = sort(data.first.ref.attr)[count.index]\n",
0,
&Body{
Attributes: Attributes{
"a": {
Name: "a",
Expr: &IndexExpr{
Collection: &FunctionCallExpr{
Name: "sort",
Args: []Expression{
&ScopeTraversalExpr{
Traversal: hcl.Traversal{
hcl.TraverseRoot{
Name: "data",
SrcRange: hcl.Range{
Filename: "",
Start: hcl.Pos{Line: 1, Column: 10, Byte: 9},
End: hcl.Pos{Line: 1, Column: 14, Byte: 13},
},
},
hcl.TraverseAttr{
Name: "first",
SrcRange: hcl.Range{
Filename: "",
Start: hcl.Pos{Line: 1, Column: 14, Byte: 13},
End: hcl.Pos{Line: 1, Column: 20, Byte: 19},
},
},
hcl.TraverseAttr{
Name: "ref",
SrcRange: hcl.Range{
Filename: "",
Start: hcl.Pos{Line: 1, Column: 20, Byte: 19},
End: hcl.Pos{Line: 1, Column: 24, Byte: 23},
},
},
hcl.TraverseAttr{
Name: "attr",
SrcRange: hcl.Range{
Filename: "",
Start: hcl.Pos{Line: 1, Column: 24, Byte: 23},
End: hcl.Pos{Line: 1, Column: 29, Byte: 28},
},
},
},
SrcRange: hcl.Range{
Filename: "",
Start: hcl.Pos{Line: 1, Column: 10, Byte: 9},
End: hcl.Pos{Line: 1, Column: 29, Byte: 28},
},
},
},
ExpandFinal: false,
NameRange: hcl.Range{
Filename: "",
Start: hcl.Pos{Line: 1, Column: 5, Byte: 4},
End: hcl.Pos{Line: 1, Column: 9, Byte: 8},
},
OpenParenRange: hcl.Range{
Filename: "",
Start: hcl.Pos{Line: 1, Column: 9, Byte: 8},
End: hcl.Pos{Line: 1, Column: 10, Byte: 9},
},
CloseParenRange: hcl.Range{
Filename: "",
Start: hcl.Pos{Line: 1, Column: 29, Byte: 28},
End: hcl.Pos{Line: 1, Column: 30, Byte: 29},
},
},
Key: &ScopeTraversalExpr{
Traversal: hcl.Traversal{
hcl.TraverseRoot{
Name: "count",
SrcRange: hcl.Range{
Filename: "",
Start: hcl.Pos{Line: 1, Column: 31, Byte: 30},
End: hcl.Pos{Line: 1, Column: 36, Byte: 35},
},
},
hcl.TraverseAttr{
Name: "index",
SrcRange: hcl.Range{
Filename: "",
Start: hcl.Pos{Line: 1, Column: 36, Byte: 35},
End: hcl.Pos{Line: 1, Column: 42, Byte: 41},
},
},
},
SrcRange: hcl.Range{
Filename: "",
Start: hcl.Pos{Line: 1, Column: 31, Byte: 30},
End: hcl.Pos{Line: 1, Column: 42, Byte: 41},
},
},
SrcRange: hcl.Range{
Filename: "",
Start: hcl.Pos{Line: 1, Column: 30, Byte: 29},
End: hcl.Pos{Line: 1, Column: 43, Byte: 42},
},
OpenRange: hcl.Range{
Filename: "",
Start: hcl.Pos{Line: 1, Column: 30, Byte: 29},
End: hcl.Pos{Line: 1, Column: 31, Byte: 30},
},
},
SrcRange: hcl.Range{
Filename: "",
Start: hcl.Pos{Line: 1, Column: 1, Byte: 0},
End: hcl.Pos{Line: 1, Column: 43, Byte: 42},
},
NameRange: hcl.Range{
Filename: "",
Start: hcl.Pos{Line: 1, Column: 1, Byte: 0},
End: hcl.Pos{Line: 1, Column: 2, Byte: 1},
},
EqualsRange: hcl.Range{
Filename: "",
Start: hcl.Pos{Line: 1, Column: 3, Byte: 2},
End: hcl.Pos{Line: 1, Column: 4, Byte: 3},
},
},
},
Blocks: Blocks{},
SrcRange: hcl.Range{
Start: hcl.Pos{Line: 1, Column: 1, Byte: 0},
End: hcl.Pos{Line: 2, Column: 1, Byte: 43},
},
EndRange: hcl.Range{
Start: hcl.Pos{Line: 2, Column: 1, Byte: 43},
End: hcl.Pos{Line: 2, Column: 1, Byte: 43},
},
},
},
}
for _, test := range tests {

View File

@ -8,6 +8,10 @@ import (
"github.com/hashicorp/hcl2/hcl"
)
func init() {
deep.MaxDepth = 999
}
func TestParse(t *testing.T) {
tests := []struct {
Input string