Newer
Older
package runner
import (
"testing"
"github.com/stretchr/testify/assert"
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
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v3"
)
func TestVariablesUnmarshal(t *testing.T) {
for _, tt := range []struct {
name string
yaml string
expected Variables
err string
}{
{"legacy", `- name: v1
value: value1
- name: v2
value: 54.3
- name: nested
value:
- attr1: attr1value
attr2: 2
- attr1: otherattr1value
attr2: 4
`,
Variables{
{Name: "v1", Value: "value1"},
{Name: "v2", Value: 54.3},
{Name: "nested", Value: []interface{}{
map[string]interface{}{
"attr1": "attr1value",
"attr2": 2,
},
map[string]interface{}{
"attr1": "otherattr1value",
"attr2": 4,
},
}},
},
"",
},
{"dict", `v1: value1
v2: 54.3
nested:
- attr1: attr1value
attr2: 2
- attr1: otherattr1value
attr2: 4
`,
Variables{
{Name: "v1", Value: "value1"},
{Name: "v2", Value: 54.3},
{Name: "nested", Value: []interface{}{
map[string]interface{}{
"attr1": "attr1value",
"attr2": 2,
},
map[string]interface{}{
"attr1": "otherattr1value",
"attr2": 4,
},
}},
},
"",
},
} {
t.Run(tt.name, func(t *testing.T) {
var actual Variables
err := yaml.Unmarshal([]byte(tt.yaml), &actual)
if tt.err != "" {
require.EqualError(t, err, tt.err)
} else {
require.NoError(t, err)
require.Equal(t, tt.expected, actual)
}
})
}
}
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
func TestLookupVariable(t *testing.T) {
variables := map[string]interface{}{
"string": "a string",
"int": 3,
"map": map[interface{}]interface{}{
"float": 12.3,
},
"list": []interface{}{
map[interface{}]interface{}{
"float": 12.3,
},
},
}
for _, tt := range []struct {
name string
expected interface{}
}{
{"string", "a string"},
{"int", 3},
{"map.float", 12.3},
{"list.0.float", 12.3},
{"list.2.float", nil},
{"list.-2.float", nil},
} {
t.Run(tt.name, func(t *testing.T) {
actual, ok := lookupVariable(variables, tt.name)
if tt.expected == nil {
assert.False(t, ok)
} else {
assert.True(t, ok)
assert.Equal(t, tt.expected, actual)
}
})
}
}
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
150
151
152
153
154
155
156
157
158
159
func TestSetVariable(t *testing.T) {
type V = map[string]interface{}
variables := func(setters ...func(V)) V {
ret := V{
"string": "a string",
"int": 3,
"map": map[interface{}]interface{}{
"float": 12.3,
},
"list": []interface{}{
map[interface{}]interface{}{
"float": 12.3,
},
},
}
for _, s := range setters {
s(ret)
}
return ret
}
for _, tt := range []struct {
name string
value interface{}
expected interface{}
}{
{"string", "new string", variables(func(v V) { v["string"] = "new string" })},
{"int", "not an int anymore", variables(func(v V) { v["int"] = "not an int anymore" })},
{"map.float", 13.0, variables(func(v V) { v["map"].(map[interface{}]interface{})["float"] = 13.0 })},
{"list.0.float", 15.0, variables(func(v V) { v["list"].([]interface{})[0].(map[interface{}]interface{})["float"] = 15.0 })},
{"list.2.float", nil, variables()},
{"list.-2.float", nil, variables()},
} {
t.Run(tt.name, func(t *testing.T) {
v := variables()
setVariable(v, strings.Split(tt.name, "."), tt.value)
assert.Equal(t, tt.expected, v)
})
}
}