hcl/hclpack/expression_test.go
Martin Atkins 5e07d8e1f9 hclpack: New package for wire representations of hcl bodies
In most applications it's possible to fully evaluate configuration at the
beginning and work only with resolved values after that, but in some
unusual cases it's necessary to split parsing and decoding between two
separate processes connected by a pipe or network connection.

hclpack is intended to provide compact wire formats for sending bodies
over the network such that they can be decoded and evaluated and get the
same results. This is not something that can happen fully automatically
because a hcl.Body is an abstract node rather than a physical construct,
and so access to the original source code is required to construct such
a representation, and to interpret any source ranges that emerged from
the final evaluation.
2018-11-10 09:36:26 -08:00

71 lines
1.4 KiB
Go

package hclpack
import (
"testing"
"github.com/hashicorp/hcl2/hcl"
"github.com/zclconf/go-cty/cty"
)
func TestExpressionValue(t *testing.T) {
tests := map[string]struct {
Expr *Expression
Ctx *hcl.EvalContext
Want cty.Value
}{
"simple literal expr": {
&Expression{
Source: []byte(`"hello"`),
SourceType: ExprNative,
},
nil,
cty.StringVal("hello"),
},
"simple literal template": {
&Expression{
Source: []byte(`hello ${5}`),
SourceType: ExprTemplate,
},
nil,
cty.StringVal("hello 5"),
},
"expr with variable": {
&Expression{
Source: []byte(`foo`),
SourceType: ExprNative,
},
&hcl.EvalContext{
Variables: map[string]cty.Value{
"foo": cty.StringVal("bar"),
},
},
cty.StringVal("bar"),
},
"template with variable": {
&Expression{
Source: []byte(`foo ${foo}`),
SourceType: ExprTemplate,
},
&hcl.EvalContext{
Variables: map[string]cty.Value{
"foo": cty.StringVal("bar"),
},
},
cty.StringVal("foo bar"),
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
got, diags := test.Expr.Value(test.Ctx)
for _, diag := range diags {
t.Errorf("unexpected diagnostic: %s", diag.Error())
}
if !test.Want.RawEquals(got) {
t.Errorf("wrong result\ngot: %#v\nwant: %#v", got, test.Want)
}
})
}
}