2020-05-11 20:17:36 +02:00
|
|
|
package hcl2
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/hashicorp/hcl/v2"
|
2021-03-17 14:20:05 +01:00
|
|
|
"github.com/pulumi/pulumi/pkg/v3/codegen/hcl2/model"
|
|
|
|
"github.com/pulumi/pulumi/pkg/v3/codegen/hcl2/syntax"
|
|
|
|
"github.com/pulumi/pulumi/pkg/v3/codegen/schema"
|
2020-05-11 20:17:36 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestRewriteConversions(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
input, output string
|
|
|
|
to model.Type
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
input: `"1" + 2`,
|
2020-05-29 12:38:53 +02:00
|
|
|
output: `1 + 2`,
|
2020-05-11 20:17:36 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
input: `{a: "b"}`,
|
|
|
|
output: `{a: "b"}`,
|
|
|
|
to: model.NewObjectType(map[string]model.Type{
|
|
|
|
"a": model.StringType,
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: `{a: "b"}`,
|
|
|
|
output: `{a: "b"}`,
|
|
|
|
to: model.InputType(model.NewObjectType(map[string]model.Type{
|
|
|
|
"a": model.StringType,
|
|
|
|
})),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: `{a: "b"}`,
|
|
|
|
output: `__convert({a: "b"})`,
|
|
|
|
to: model.NewObjectType(map[string]model.Type{
|
|
|
|
"a": model.StringType,
|
|
|
|
}, &schema.ObjectType{}),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: `{a: "b"}`,
|
|
|
|
output: `__convert({a: "b"})`,
|
|
|
|
to: model.InputType(model.NewObjectType(map[string]model.Type{
|
|
|
|
"a": model.StringType,
|
|
|
|
}, &schema.ObjectType{})),
|
|
|
|
},
|
2020-05-13 17:25:26 +02:00
|
|
|
{
|
|
|
|
input: `{a: "1" + 2}`,
|
2020-05-29 12:38:53 +02:00
|
|
|
output: `{a: 1 + 2}`,
|
2020-05-13 17:25:26 +02:00
|
|
|
to: model.NewObjectType(map[string]model.Type{
|
|
|
|
"a": model.NumberType,
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: `[{a: "b"}]`,
|
2020-05-21 19:23:33 +02:00
|
|
|
output: "__convert([\n __convert({a: \"b\"})])",
|
2020-05-13 17:25:26 +02:00
|
|
|
to: model.NewListType(model.NewObjectType(map[string]model.Type{
|
|
|
|
"a": model.StringType,
|
|
|
|
}, &schema.ObjectType{})),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: `[for v in ["b"]: {a: v}]`,
|
|
|
|
output: `[for v in ["b"]: __convert( {a: v})]`,
|
|
|
|
to: model.NewListType(model.NewObjectType(map[string]model.Type{
|
|
|
|
"a": model.StringType,
|
|
|
|
}, &schema.ObjectType{})),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: `true ? {a: "b"} : {a: "c"}`,
|
|
|
|
output: `true ? __convert( {a: "b"}) : __convert( {a: "c"})`,
|
|
|
|
to: model.NewObjectType(map[string]model.Type{
|
|
|
|
"a": model.StringType,
|
|
|
|
}, &schema.ObjectType{}),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: `!"true"`,
|
2020-05-29 12:38:53 +02:00
|
|
|
output: `!true`,
|
2020-05-13 17:25:26 +02:00
|
|
|
to: model.BoolType,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: `["a"][i]`,
|
|
|
|
output: `["a"][__convert(i)]`,
|
|
|
|
to: model.StringType,
|
|
|
|
},
|
2020-06-02 21:00:35 +02:00
|
|
|
{
|
|
|
|
input: `42`,
|
|
|
|
output: `__convert(42)`,
|
|
|
|
to: model.IntType,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: `"42"`,
|
|
|
|
output: `__convert(42)`,
|
|
|
|
to: model.IntType,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: `{a: 42}`,
|
|
|
|
output: `{a: __convert( 42)}`,
|
|
|
|
to: model.NewObjectType(map[string]model.Type{
|
|
|
|
"a": model.IntType,
|
|
|
|
}),
|
|
|
|
},
|
2020-05-11 20:17:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
scope := model.NewRootScope(syntax.None)
|
2020-05-13 17:25:26 +02:00
|
|
|
scope.Define("i", &model.Variable{
|
|
|
|
Name: "i",
|
|
|
|
VariableType: model.StringType,
|
|
|
|
})
|
2020-05-11 20:17:36 +02:00
|
|
|
for _, c := range cases {
|
|
|
|
expr, diags := model.BindExpressionText(c.input, scope, hcl.Pos{})
|
|
|
|
assert.Len(t, diags, 0)
|
|
|
|
|
|
|
|
to := c.to
|
|
|
|
if to == nil {
|
|
|
|
to = expr.Type()
|
|
|
|
}
|
|
|
|
expr = RewriteConversions(expr, to)
|
|
|
|
assert.Equal(t, c.output, fmt.Sprintf("%v", expr))
|
|
|
|
}
|
|
|
|
}
|
2020-05-13 17:25:26 +02:00
|
|
|
|
|
|
|
func TestRewriteConversionsAfterApply(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
input, output string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
input: `f({id: v.id})`,
|
|
|
|
output: `__apply(v,eval(v, f(__convert({id: v.id}))))`,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
scope := model.NewRootScope(syntax.None)
|
|
|
|
scope.DefineFunction("f", model.NewFunction(model.StaticFunctionSignature{
|
|
|
|
Parameters: []model.Parameter{{
|
|
|
|
Name: "args",
|
|
|
|
Type: model.NewObjectType(map[string]model.Type{
|
|
|
|
"id": model.StringType,
|
|
|
|
}, &schema.ObjectType{}),
|
|
|
|
}},
|
|
|
|
ReturnType: model.DynamicType,
|
|
|
|
}))
|
|
|
|
scope.Define("v", &model.Variable{
|
|
|
|
Name: "v",
|
|
|
|
VariableType: model.NewOutputType(model.NewObjectType(map[string]model.Type{
|
|
|
|
"id": model.StringType,
|
|
|
|
})),
|
|
|
|
})
|
|
|
|
|
|
|
|
for _, c := range cases {
|
|
|
|
expr, diags := model.BindExpressionText(c.input, scope, hcl.Pos{})
|
|
|
|
assert.Len(t, diags, 0)
|
|
|
|
|
|
|
|
expr, _ = RewriteApplies(expr, nameInfo(0), false)
|
|
|
|
expr = RewriteConversions(expr, expr.Type())
|
|
|
|
assert.Equal(t, c.output, fmt.Sprintf("%v", expr))
|
|
|
|
}
|
|
|
|
}
|