Skip to content

Commit ec2c208

Browse files
add tests
1 parent ce3c10f commit ec2c208

File tree

1 file changed

+141
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)