hcl/hclsyntax/walk_test.go
Martin Atkins f1f3985230 hclsyntax: Explicit AST node for parentheses
So far the expression parentheses syntax has been handled entirely in the
parser and has been totally invisible in the AST. That's fine for typical
expression evaluation, but over the years it's led to a few quirky
behaviors in less common situations where we've assumed that all
expressions are covered by the AST itself or by the source ranges that the
AST captures.

In particular, hclwrite assumes that all expressions will have source
ranges that cover their tokens, and it generates an incorrect physical
syntax tree when the AST doesn't uphold that.

After resisting through a few other similar bugs, this commit finally
introduces an explicit AST node for parentheses, which makes the
parentheses explicit in the AST and captures the larger source range that
includes the TokenOParen and the TokenCParen.

This means that parentheses will now be visible as a distinct node when
walking the AST, as reflected in the updated tests here. That may cause
downstream applications that traverse the tree to exhibit different
behaviors but we're not considering that as a "breaking change" because
the Walk function doesn't make any guarantees about the specific AST
shape.
2020-12-02 12:03:00 -08:00

182 lines
5.1 KiB
Go

package hclsyntax
import (
"fmt"
"reflect"
"testing"
"github.com/davecgh/go-spew/spew"
"github.com/go-test/deep"
"github.com/hashicorp/hcl/v2"
)
func TestWalk(t *testing.T) {
tests := []struct {
src string
want []testWalkCall
}{
{
`1`,
[]testWalkCall{
{testWalkEnter, "*hclsyntax.LiteralValueExpr"},
{testWalkExit, "*hclsyntax.LiteralValueExpr"},
},
},
{
`foo`,
[]testWalkCall{
{testWalkEnter, "*hclsyntax.ScopeTraversalExpr"},
{testWalkExit, "*hclsyntax.ScopeTraversalExpr"},
},
},
{
`1 + 1`,
[]testWalkCall{
{testWalkEnter, "*hclsyntax.BinaryOpExpr"},
{testWalkEnter, "*hclsyntax.LiteralValueExpr"},
{testWalkExit, "*hclsyntax.LiteralValueExpr"},
{testWalkEnter, "*hclsyntax.LiteralValueExpr"},
{testWalkExit, "*hclsyntax.LiteralValueExpr"},
{testWalkExit, "*hclsyntax.BinaryOpExpr"},
},
},
{
`(1 + 1)`,
[]testWalkCall{
{testWalkEnter, "*hclsyntax.ParenthesesExpr"},
{testWalkEnter, "*hclsyntax.BinaryOpExpr"},
{testWalkEnter, "*hclsyntax.LiteralValueExpr"},
{testWalkExit, "*hclsyntax.LiteralValueExpr"},
{testWalkEnter, "*hclsyntax.LiteralValueExpr"},
{testWalkExit, "*hclsyntax.LiteralValueExpr"},
{testWalkExit, "*hclsyntax.BinaryOpExpr"},
{testWalkExit, "*hclsyntax.ParenthesesExpr"},
},
},
{
`a[0]`,
[]testWalkCall{
// because the index is constant here, the index is absorbed into the traversal
{testWalkEnter, "*hclsyntax.ScopeTraversalExpr"},
{testWalkExit, "*hclsyntax.ScopeTraversalExpr"},
},
},
{
`0[foo]`, // semantically incorrect, but should still parse and be walkable
[]testWalkCall{
{testWalkEnter, "*hclsyntax.IndexExpr"},
{testWalkEnter, "*hclsyntax.LiteralValueExpr"},
{testWalkExit, "*hclsyntax.LiteralValueExpr"},
{testWalkEnter, "*hclsyntax.ScopeTraversalExpr"},
{testWalkExit, "*hclsyntax.ScopeTraversalExpr"},
{testWalkExit, "*hclsyntax.IndexExpr"},
},
},
{
`bar()`,
[]testWalkCall{
{testWalkEnter, "*hclsyntax.FunctionCallExpr"},
{testWalkExit, "*hclsyntax.FunctionCallExpr"},
},
},
{
`bar(1, a)`,
[]testWalkCall{
{testWalkEnter, "*hclsyntax.FunctionCallExpr"},
{testWalkEnter, "*hclsyntax.LiteralValueExpr"},
{testWalkExit, "*hclsyntax.LiteralValueExpr"},
{testWalkEnter, "*hclsyntax.ScopeTraversalExpr"},
{testWalkExit, "*hclsyntax.ScopeTraversalExpr"},
{testWalkExit, "*hclsyntax.FunctionCallExpr"},
},
},
{
`bar(1, a)[0]`,
[]testWalkCall{
{testWalkEnter, "*hclsyntax.RelativeTraversalExpr"},
{testWalkEnter, "*hclsyntax.FunctionCallExpr"},
{testWalkEnter, "*hclsyntax.LiteralValueExpr"},
{testWalkExit, "*hclsyntax.LiteralValueExpr"},
{testWalkEnter, "*hclsyntax.ScopeTraversalExpr"},
{testWalkExit, "*hclsyntax.ScopeTraversalExpr"},
{testWalkExit, "*hclsyntax.FunctionCallExpr"},
{testWalkExit, "*hclsyntax.RelativeTraversalExpr"},
},
},
{
`[for x in foo: x + 1 if x < 10]`,
[]testWalkCall{
{testWalkEnter, "*hclsyntax.ForExpr"},
{testWalkEnter, "*hclsyntax.ScopeTraversalExpr"},
{testWalkExit, "*hclsyntax.ScopeTraversalExpr"},
{testWalkEnter, "hclsyntax.ChildScope"},
{testWalkEnter, "*hclsyntax.BinaryOpExpr"},
{testWalkEnter, "*hclsyntax.ScopeTraversalExpr"},
{testWalkExit, "*hclsyntax.ScopeTraversalExpr"},
{testWalkEnter, "*hclsyntax.LiteralValueExpr"},
{testWalkExit, "*hclsyntax.LiteralValueExpr"},
{testWalkExit, "*hclsyntax.BinaryOpExpr"},
{testWalkExit, "hclsyntax.ChildScope"},
{testWalkEnter, "hclsyntax.ChildScope"},
{testWalkEnter, "*hclsyntax.BinaryOpExpr"},
{testWalkEnter, "*hclsyntax.ScopeTraversalExpr"},
{testWalkExit, "*hclsyntax.ScopeTraversalExpr"},
{testWalkEnter, "*hclsyntax.LiteralValueExpr"},
{testWalkExit, "*hclsyntax.LiteralValueExpr"},
{testWalkExit, "*hclsyntax.BinaryOpExpr"},
{testWalkExit, "hclsyntax.ChildScope"},
{testWalkExit, "*hclsyntax.ForExpr"},
},
},
}
for _, test := range tests {
t.Run(test.src, func(t *testing.T) {
expr, diags := ParseExpression([]byte(test.src), "", hcl.Pos{Line: 1, Column: 1})
if diags.HasErrors() {
t.Fatalf("failed to parse expression: %s", diags.Error())
}
w := testWalker{}
diags = Walk(expr, &w)
if diags.HasErrors() {
t.Fatalf("failed to walk: %s", diags.Error())
}
got := w.Calls
if !reflect.DeepEqual(got, test.want) {
t.Errorf("wrong calls\ngot: %swant: %s", spew.Sdump(got), spew.Sdump(test.want))
for _, problem := range deep.Equal(got, test.want) {
t.Errorf(problem)
}
}
})
}
}
type testWalkMethod int
const testWalkEnter testWalkMethod = 1
const testWalkExit testWalkMethod = 2
type testWalkCall struct {
Method testWalkMethod
NodeType string
}
type testWalker struct {
Calls []testWalkCall
}
func (w *testWalker) Enter(node Node) hcl.Diagnostics {
w.Calls = append(w.Calls, testWalkCall{testWalkEnter, fmt.Sprintf("%T", node)})
return nil
}
func (w *testWalker) Exit(node Node) hcl.Diagnostics {
w.Calls = append(w.Calls, testWalkCall{testWalkExit, fmt.Sprintf("%T", node)})
return nil
}