1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
|
package stdlib
import (
"fmt"
"testing"
"github.com/zclconf/go-cty/cty"
)
func TestEqual(t *testing.T) {
tests := []struct {
A cty.Value
B cty.Value
Want cty.Value
}{
{
cty.NumberIntVal(1),
cty.NumberIntVal(2),
cty.False,
},
{
cty.NumberIntVal(2),
cty.NumberIntVal(2),
cty.True,
},
{
cty.NullVal(cty.Number),
cty.NullVal(cty.Number),
cty.True,
},
{
cty.NumberIntVal(2),
cty.NullVal(cty.Number),
cty.False,
},
{
cty.NumberIntVal(1),
cty.UnknownVal(cty.Number),
cty.UnknownVal(cty.Bool),
},
{
cty.UnknownVal(cty.Number),
cty.UnknownVal(cty.Number),
cty.UnknownVal(cty.Bool),
},
{
cty.NumberIntVal(1),
cty.DynamicVal,
cty.UnknownVal(cty.Bool),
},
{
cty.DynamicVal,
cty.DynamicVal,
cty.UnknownVal(cty.Bool),
},
}
for _, test := range tests {
t.Run(fmt.Sprintf("Equal(%#v,%#v)", test.A, test.B), func(t *testing.T) {
got, err := Equal(test.A, test.B)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
if !got.RawEquals(test.Want) {
t.Errorf("wrong result\ngot: %#v\nwant: %#v", got, test.Want)
}
})
}
}
func TestCoalesce(t *testing.T) {
tests := []struct {
Values []cty.Value
Want cty.Value
}{
{
[]cty.Value{cty.True},
cty.True,
},
{
[]cty.Value{cty.NullVal(cty.Bool), cty.True},
cty.True,
},
{
[]cty.Value{cty.NullVal(cty.Bool), cty.False},
cty.False,
},
{
[]cty.Value{cty.NullVal(cty.Bool), cty.False, cty.StringVal("hello")},
cty.StringVal("false"),
},
{
[]cty.Value{cty.True, cty.UnknownVal(cty.Bool)},
cty.True,
},
{
[]cty.Value{cty.UnknownVal(cty.Bool), cty.True},
cty.UnknownVal(cty.Bool),
},
{
[]cty.Value{cty.UnknownVal(cty.Bool), cty.StringVal("hello")},
cty.UnknownVal(cty.String),
},
{
[]cty.Value{cty.DynamicVal, cty.True},
cty.UnknownVal(cty.Bool),
},
{
[]cty.Value{cty.DynamicVal},
cty.DynamicVal,
},
}
for _, test := range tests {
t.Run(fmt.Sprintf("Coalesce(%#v...)", test.Values), func(t *testing.T) {
got, err := Coalesce(test.Values...)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
if !got.RawEquals(test.Want) {
t.Errorf("wrong result\ngot: %#v\nwant: %#v", got, test.Want)
}
})
}
}
|