Skip to content

Commit 327c740

Browse files
add tests
1 parent ce3c10f commit 327c740

File tree

1 file changed

+157
-0
lines changed

1 file changed

+157
-0
lines changed
+157
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
package sdkv2
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
7+
"github.com/hashicorp/go-cty/cty"
8+
"github.com/stretchr/testify/require"
9+
)
10+
11+
func TestNilCase(t *testing.T) {
12+
t.Parallel()
13+
val := cty.NullVal(cty.String)
14+
result := objectFromCtyValue(val)
15+
require.Nil(t, result)
16+
}
17+
18+
func TestEncapsulatedTypeFail(t *testing.T) {
19+
t.Parallel()
20+
innerVal := "test"
21+
val := cty.CapsuleVal(cty.Capsule("name", reflect.TypeOf(innerVal)), &innerVal)
22+
require.Panics(t, func() {
23+
objectFromCtyValueInner(val)
24+
})
25+
}
26+
27+
func TestObjectFromCtyValue(t *testing.T) {
28+
t.Parallel()
29+
t.Run("Null", func(t *testing.T) {
30+
val := cty.NullVal(cty.String)
31+
result := objectFromCtyValueInner(val)
32+
require.Nil(t, result)
33+
})
34+
35+
t.Run("Unknown", func(t *testing.T) {
36+
val := cty.UnknownVal(cty.String)
37+
result := objectFromCtyValueInner(val)
38+
require.Equal(t, terraformUnknownVariableValue, result)
39+
})
40+
41+
t.Run("Dynamic Value", func(t *testing.T) {
42+
val := cty.DynamicVal
43+
result := objectFromCtyValueInner(val)
44+
require.Equal(t, terraformUnknownVariableValue, result)
45+
})
46+
47+
t.Run("String", func(t *testing.T) {
48+
val := cty.StringVal("test")
49+
result := objectFromCtyValueInner(val)
50+
require.Equal(t, "test", result)
51+
})
52+
53+
t.Run("Number", func(t *testing.T) {
54+
val := cty.NumberIntVal(42)
55+
result := objectFromCtyValueInner(val)
56+
require.Equal(t, "42", result)
57+
})
58+
59+
t.Run("Bool", func(t *testing.T) {
60+
val := cty.BoolVal(true)
61+
result := objectFromCtyValueInner(val)
62+
require.Equal(t, true, result)
63+
})
64+
65+
t.Run("List", func(t *testing.T) {
66+
val := cty.ListVal([]cty.Value{
67+
cty.StringVal("one"),
68+
cty.StringVal("two"),
69+
})
70+
result := objectFromCtyValueInner(val)
71+
expected := []interface{}{"one", "two"}
72+
73+
require.Equal(t, expected, result)
74+
})
75+
76+
t.Run("Set", func(t *testing.T) {
77+
val := cty.SetVal([]cty.Value{
78+
cty.StringVal("one"),
79+
cty.StringVal("two"),
80+
})
81+
result := objectFromCtyValueInner(val)
82+
// Note: sets might have non-deterministic order when converted to list
83+
elements := result.([]interface{})
84+
require.Equal(t, 2, len(elements))
85+
require.Contains(t, elements, "one")
86+
require.Contains(t, elements, "two")
87+
})
88+
89+
t.Run("Map", func(t *testing.T) {
90+
val := cty.MapVal(map[string]cty.Value{
91+
"key1": cty.StringVal("value1"),
92+
"key2": cty.StringVal("value2"),
93+
})
94+
result := objectFromCtyValue(val)
95+
expected := map[string]interface{}{
96+
"key1": "value1",
97+
"key2": "value2",
98+
}
99+
100+
require.Equal(t, expected, result)
101+
})
102+
103+
t.Run("Map with different types", func(t *testing.T) {
104+
val := cty.ObjectVal(map[string]cty.Value{
105+
"key1": cty.StringVal("value1"),
106+
"key2": cty.NumberIntVal(42),
107+
})
108+
result := objectFromCtyValue(val)
109+
expected := map[string]interface{}{
110+
"key1": "value1",
111+
"key2": "42",
112+
}
113+
114+
require.Equal(t, expected, result)
115+
})
116+
117+
t.Run("Object", func(t *testing.T) {
118+
val := cty.ObjectVal(map[string]cty.Value{
119+
"name": cty.StringVal("test"),
120+
"count": cty.NumberIntVal(5),
121+
"valid": cty.BoolVal(true),
122+
})
123+
124+
result := objectFromCtyValue(val)
125+
expected := map[string]interface{}{
126+
"name": "test",
127+
"count": "5",
128+
"valid": true,
129+
}
130+
131+
require.Equal(t, expected, result)
132+
})
133+
134+
t.Run("Nested", func(t *testing.T) {
135+
val := cty.ObjectVal(map[string]cty.Value{
136+
"name": cty.StringVal("parent"),
137+
"child": cty.ObjectVal(map[string]cty.Value{
138+
"name": cty.StringVal("child"),
139+
"list": cty.ListVal([]cty.Value{
140+
cty.StringVal("item1"),
141+
cty.StringVal("item2"),
142+
}),
143+
}),
144+
})
145+
146+
result := objectFromCtyValue(val)
147+
expected := map[string]interface{}{
148+
"name": "parent",
149+
"child": map[string]interface{}{
150+
"name": "child",
151+
"list": []interface{}{"item1", "item2"},
152+
},
153+
}
154+
155+
require.Equal(t, expected, result)
156+
})
157+
}

0 commit comments

Comments
 (0)