opentofu/internal/command/jsonconfig/expression_test.go
Martin Atkins 31349a9c3a Move configs/ to internal/configs/
This is part of a general effort to move all of Terraform's non-library
package surface under internal in order to reinforce that these are for
internal use within Terraform only.

If you were previously importing packages under this prefix into an
external codebase, you could pin to an earlier release tag as an interim
solution until you've make a plan to achieve the same functionality some
other way.
2021-05-17 14:09:07 -07:00

74 lines
1.4 KiB
Go

package jsonconfig
import (
"encoding/json"
"reflect"
"testing"
"github.com/zclconf/go-cty/cty"
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hclsyntax"
"github.com/hashicorp/terraform/internal/configs/configschema"
)
func TestMarshalExpressions(t *testing.T) {
tests := []struct {
Input hcl.Body
Schema *configschema.Block
Want expressions
}{
{
&hclsyntax.Body{
Attributes: hclsyntax.Attributes{
"foo": &hclsyntax.Attribute{
Expr: &hclsyntax.LiteralValueExpr{
Val: cty.StringVal("bar"),
},
},
},
},
&configschema.Block{
Attributes: map[string]*configschema.Attribute{
"foo": {
Type: cty.String,
Optional: true,
},
},
},
expressions{
"foo": expression{
ConstantValue: json.RawMessage([]byte(`"bar"`)),
References: []string(nil),
},
},
},
}
for _, test := range tests {
got := marshalExpressions(test.Input, test.Schema)
if !reflect.DeepEqual(got, test.Want) {
t.Fatalf("wrong result:\nGot: %#v\nWant: %#v\n", got, test.Want)
}
}
}
func TestMarshalExpression(t *testing.T) {
tests := []struct {
Input hcl.Expression
Want expression
}{
{
nil,
expression{},
},
}
for _, test := range tests {
got := marshalExpression(test.Input)
if !reflect.DeepEqual(got, test.Want) {
t.Fatalf("wrong result:\nGot: %#v\nWant: %#v\n", got, test.Want)
}
}
}