-
Notifications
You must be signed in to change notification settings - Fork 0
/
value_generator_test.go
149 lines (129 loc) · 3.26 KB
/
value_generator_test.go
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package tfsig_test
import (
"fmt"
"testing"
"github.com/zclconf/go-cty/cty"
"github.com/yoanm/go-tfsig"
"github.com/yoanm/go-tfsig/testutils"
)
func TestNewValueGenerator(t *testing.T) {
t.Parallel()
basicStringValue := "basic_value"
customStringValue := "custom.my_var"
localVal := "local.my_local"
varVal := "var.my_var"
dataVal := "data.my_data.my_property"
boolVal := "true"
intVal := "1"
listVal := []string{basicStringValue, customStringValue, localVal, varVal, dataVal}
cases := map[string]struct {
value tfsig.ValueGenerator
goldenFile string
}{
"Default": {
tfsig.NewValueGenerator(),
"ValueGenerator.default",
},
"Enhanced": {
tfsig.NewValueGenerator("custom."),
"ValueGenerator.custom",
},
}
for tcname, tcase := range cases {
t.Run(
tcname,
func(t *testing.T) {
t.Parallel()
sig := tfsig.NewSignature("sig")
sig.SetElements(
tfsig.BodyElements{
tfsig.NewBodyAttribute("basic", *tcase.value.ToString(&basicStringValue)),
tfsig.NewBodyAttribute("custom", *tcase.value.ToString(&customStringValue)),
tfsig.NewBodyAttribute("local", *tcase.value.ToString(&localVal)),
tfsig.NewBodyAttribute("var", *tcase.value.ToString(&varVal)),
tfsig.NewBodyAttribute("data", *tcase.value.ToString(&dataVal)),
tfsig.NewBodyAttribute("bool", *tcase.value.ToBool(&boolVal)),
tfsig.NewBodyAttribute("number", *tcase.value.ToNumber(&intVal)),
tfsig.NewBodyAttribute("list_of_string", *tcase.value.ToStringList(&listVal)),
},
)
if err := testutils.EnsureBlockFileEqualsGoldenFile(sig.Build(), tcase.goldenFile); err != nil {
t.Errorf("Case \"%s\": %v", t.Name(), err)
}
},
)
}
}
func TestValueGenerator_nil(t *testing.T) {
t.Parallel()
valGen := tfsig.NewValueGenerator()
cases := map[string]struct {
fn func() *cty.Value
}{
"String": {
func() *cty.Value {
return valGen.ToString(nil)
},
},
"Bool": {
func() *cty.Value {
return valGen.ToBool(nil)
},
},
"Number": {
func() *cty.Value {
return valGen.ToNumber(nil)
},
},
"List of string": {
func() *cty.Value {
return valGen.ToStringList(nil)
},
},
"FromString": {
func() *cty.Value {
return valGen.FromString(nil, cty.List(cty.Bool))
},
},
}
for tcname, tcase := range cases {
t.Run(
tcname,
func(t *testing.T) {
t.Parallel()
if actual := tcase.fn(); actual != nil {
t.Errorf("wrong result for case %q: expected nil, got %v", t.Name(), actual)
}
},
)
}
}
func TestValueGenerator_panic(t *testing.T) {
t.Parallel()
valGen := tfsig.NewValueGenerator()
val := "a_value"
ctyType := cty.Map(cty.String)
expectedError := fmt.Sprintf("Unable to convert \"%s\" to a %s", val, ctyType.FriendlyName())
testutils.ExpectPanic(
t,
"Basic",
func() {
valGen.FromString(&val, ctyType)
},
expectedError,
)
}
func TestToIdent_nil(t *testing.T) {
t.Parallel()
valGen := tfsig.NewValueGenerator()
if actual := valGen.ToIdent(nil); actual != nil {
t.Errorf("wrong result: expected nil, got %v", actual)
}
}
func TestToIdentList_nil(t *testing.T) {
t.Parallel()
valGen := tfsig.NewValueGenerator()
if actual := valGen.ToIdentList(nil); actual != nil {
t.Errorf("wrong result: expected nil, got %v", actual)
}
}