215 lines
4.4 KiB
Go
215 lines
4.4 KiB
Go
|
package jsonplan
|
||
|
|
||
|
import (
|
||
|
"reflect"
|
||
|
"testing"
|
||
|
|
||
|
"github.com/zclconf/go-cty/cty"
|
||
|
)
|
||
|
|
||
|
func TestOmitUnknowns(t *testing.T) {
|
||
|
tests := []struct {
|
||
|
Input cty.Value
|
||
|
Want cty.Value
|
||
|
}{
|
||
|
{
|
||
|
cty.StringVal("hello"),
|
||
|
cty.StringVal("hello"),
|
||
|
},
|
||
|
{
|
||
|
cty.NullVal(cty.String),
|
||
|
cty.NullVal(cty.String),
|
||
|
},
|
||
|
{
|
||
|
cty.UnknownVal(cty.String),
|
||
|
cty.NilVal,
|
||
|
},
|
||
|
{
|
||
|
cty.ListValEmpty(cty.String),
|
||
|
cty.ListValEmpty(cty.String),
|
||
|
},
|
||
|
{
|
||
|
cty.ListVal([]cty.Value{cty.StringVal("hello")}),
|
||
|
cty.ListVal([]cty.Value{cty.StringVal("hello")}),
|
||
|
},
|
||
|
{
|
||
|
cty.ListVal([]cty.Value{cty.NullVal(cty.String)}),
|
||
|
cty.ListVal([]cty.Value{cty.NullVal(cty.String)}),
|
||
|
},
|
||
|
{
|
||
|
cty.ListVal([]cty.Value{cty.UnknownVal(cty.String)}),
|
||
|
cty.ListVal([]cty.Value{cty.NullVal(cty.String)}),
|
||
|
},
|
||
|
{
|
||
|
cty.ListVal([]cty.Value{cty.StringVal("hello")}),
|
||
|
cty.ListVal([]cty.Value{cty.StringVal("hello")}),
|
||
|
},
|
||
|
//
|
||
|
{
|
||
|
cty.ListVal([]cty.Value{
|
||
|
cty.StringVal("hello"),
|
||
|
cty.UnknownVal(cty.String)}),
|
||
|
cty.ListVal([]cty.Value{
|
||
|
cty.StringVal("hello"),
|
||
|
cty.NullVal(cty.String),
|
||
|
}),
|
||
|
},
|
||
|
{
|
||
|
cty.MapVal(map[string]cty.Value{
|
||
|
"hello": cty.True,
|
||
|
"world": cty.UnknownVal(cty.Bool),
|
||
|
}),
|
||
|
cty.MapVal(map[string]cty.Value{
|
||
|
"hello": cty.True,
|
||
|
}),
|
||
|
},
|
||
|
{
|
||
|
cty.SetVal([]cty.Value{
|
||
|
cty.StringVal("dev"),
|
||
|
cty.StringVal("foo"),
|
||
|
cty.StringVal("stg"),
|
||
|
cty.UnknownVal(cty.String),
|
||
|
}),
|
||
|
cty.SetVal([]cty.Value{
|
||
|
cty.StringVal("dev"),
|
||
|
cty.StringVal("foo"),
|
||
|
cty.StringVal("stg"),
|
||
|
}),
|
||
|
},
|
||
|
}
|
||
|
|
||
|
for _, test := range tests {
|
||
|
got := omitUnknowns(test.Input)
|
||
|
if !reflect.DeepEqual(got, test.Want) {
|
||
|
t.Errorf(
|
||
|
"wrong result\ninput: %#v\ngot: %#v\nwant: %#v",
|
||
|
test.Input, got, test.Want,
|
||
|
)
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func TestUnknownAsBool(t *testing.T) {
|
||
|
tests := []struct {
|
||
|
Input cty.Value
|
||
|
Want cty.Value
|
||
|
}{
|
||
|
{
|
||
|
cty.StringVal("hello"),
|
||
|
cty.False,
|
||
|
},
|
||
|
{
|
||
|
cty.NullVal(cty.String),
|
||
|
cty.False,
|
||
|
},
|
||
|
{
|
||
|
cty.UnknownVal(cty.String),
|
||
|
cty.True,
|
||
|
},
|
||
|
|
||
|
{
|
||
|
cty.NullVal(cty.DynamicPseudoType),
|
||
|
cty.False,
|
||
|
},
|
||
|
{
|
||
|
cty.NullVal(cty.Object(map[string]cty.Type{"test": cty.String})),
|
||
|
cty.False,
|
||
|
},
|
||
|
{
|
||
|
cty.DynamicVal,
|
||
|
cty.True,
|
||
|
},
|
||
|
|
||
|
{
|
||
|
cty.ListValEmpty(cty.String),
|
||
|
cty.False,
|
||
|
},
|
||
|
{
|
||
|
cty.ListVal([]cty.Value{cty.StringVal("hello")}),
|
||
|
cty.ListVal([]cty.Value{cty.False}),
|
||
|
},
|
||
|
{
|
||
|
cty.ListVal([]cty.Value{cty.NullVal(cty.String)}),
|
||
|
cty.ListVal([]cty.Value{cty.False}),
|
||
|
},
|
||
|
{
|
||
|
cty.ListVal([]cty.Value{cty.UnknownVal(cty.String)}),
|
||
|
cty.ListVal([]cty.Value{cty.True}),
|
||
|
},
|
||
|
{
|
||
|
cty.SetValEmpty(cty.String),
|
||
|
cty.False,
|
||
|
},
|
||
|
{
|
||
|
cty.SetVal([]cty.Value{cty.StringVal("hello")}),
|
||
|
cty.SetVal([]cty.Value{cty.False}),
|
||
|
},
|
||
|
{
|
||
|
cty.SetVal([]cty.Value{cty.NullVal(cty.String)}),
|
||
|
cty.SetVal([]cty.Value{cty.False}),
|
||
|
},
|
||
|
{
|
||
|
cty.SetVal([]cty.Value{cty.UnknownVal(cty.String)}),
|
||
|
cty.SetVal([]cty.Value{cty.True}),
|
||
|
},
|
||
|
{
|
||
|
cty.EmptyTupleVal,
|
||
|
cty.False,
|
||
|
},
|
||
|
{
|
||
|
cty.TupleVal([]cty.Value{cty.StringVal("hello")}),
|
||
|
cty.TupleVal([]cty.Value{cty.False}),
|
||
|
},
|
||
|
{
|
||
|
cty.TupleVal([]cty.Value{cty.NullVal(cty.String)}),
|
||
|
cty.TupleVal([]cty.Value{cty.False}),
|
||
|
},
|
||
|
{
|
||
|
cty.TupleVal([]cty.Value{cty.UnknownVal(cty.String)}),
|
||
|
cty.TupleVal([]cty.Value{cty.True}),
|
||
|
},
|
||
|
{
|
||
|
cty.MapValEmpty(cty.String),
|
||
|
cty.False,
|
||
|
},
|
||
|
{
|
||
|
cty.MapVal(map[string]cty.Value{"greeting": cty.StringVal("hello")}),
|
||
|
cty.MapVal(map[string]cty.Value{"greeting": cty.False}),
|
||
|
},
|
||
|
{
|
||
|
cty.MapVal(map[string]cty.Value{"greeting": cty.NullVal(cty.String)}),
|
||
|
cty.MapVal(map[string]cty.Value{"greeting": cty.False}),
|
||
|
},
|
||
|
{
|
||
|
cty.MapVal(map[string]cty.Value{"greeting": cty.UnknownVal(cty.String)}),
|
||
|
cty.MapVal(map[string]cty.Value{"greeting": cty.True}),
|
||
|
},
|
||
|
{
|
||
|
cty.EmptyObjectVal,
|
||
|
cty.False,
|
||
|
},
|
||
|
{
|
||
|
cty.ObjectVal(map[string]cty.Value{"greeting": cty.StringVal("hello")}),
|
||
|
cty.ObjectVal(map[string]cty.Value{"greeting": cty.False}),
|
||
|
},
|
||
|
{
|
||
|
cty.ObjectVal(map[string]cty.Value{"greeting": cty.NullVal(cty.String)}),
|
||
|
cty.ObjectVal(map[string]cty.Value{"greeting": cty.False}),
|
||
|
},
|
||
|
{
|
||
|
cty.ObjectVal(map[string]cty.Value{"greeting": cty.UnknownVal(cty.String)}),
|
||
|
cty.ObjectVal(map[string]cty.Value{"greeting": cty.True}),
|
||
|
},
|
||
|
}
|
||
|
|
||
|
for _, test := range tests {
|
||
|
got := unknownAsBool(test.Input)
|
||
|
if !reflect.DeepEqual(got, test.Want) {
|
||
|
t.Errorf(
|
||
|
"wrong result\ninput: %#v\ngot: %#v\nwant: %#v",
|
||
|
test.Input, got, test.Want,
|
||
|
)
|
||
|
}
|
||
|
}
|
||
|
}
|