Debian Codegraph @

dict_test.go

raw
1 package sprig
2
3 import (
4 "strings"
5 "testing"
6
7 "github.com/stretchr/testify/assert"
8 )
9
10 func TestDict(t *testing.T) {
11 tpl := `{{$d := dict 1 2 "three" "four" 5}}{{range $k, $v := $d}}{{$k}}{{$v}}{{end}}`
12 out, err := runRaw(tpl, nil)
13 if err != nil {
14 t.Error(err)
15 }
16 if len(out) != 12 {
17 t.Errorf("Expected length 12, got %d", len(out))
18 }
19 // dict does not guarantee ordering because it is backed by a map.
20 if !strings.Contains(out, "12") {
21 t.Error("Expected grouping 12")
22 }
23 if !strings.Contains(out, "threefour") {
24 t.Error("Expected grouping threefour")
25 }
26 if !strings.Contains(out, "5") {
27 t.Error("Expected 5")
28 }
29 tpl = `{{$t := dict "I" "shot" "the" "albatross"}}{{$t.the}} {{$t.I}}`
30 if err := runt(tpl, "albatross shot"); err != nil {
31 t.Error(err)
32 }
33 }
34
35 func TestUnset(t *testing.T) {
36 tpl := `{{- $d := dict "one" 1 "two" 222222 -}}
37 {{- $_ := unset $d "two" -}}
38 {{- range $k, $v := $d}}{{$k}}{{$v}}{{- end -}}
39 `
40
41 expect := "one1"
42 if err := runt(tpl, expect); err != nil {
43 t.Error(err)
44 }
45 }
46 func TestHasKey(t *testing.T) {
47 tpl := `{{- $d := dict "one" 1 "two" 222222 -}}
48 {{- if hasKey $d "one" -}}1{{- end -}}
49 `
50
51 expect := "1"
52 if err := runt(tpl, expect); err != nil {
53 t.Error(err)
54 }
55 }
56
57 func TestPluck(t *testing.T) {
58 tpl := `
59 {{- $d := dict "one" 1 "two" 222222 -}}
60 {{- $d2 := dict "one" 1 "two" 33333 -}}
61 {{- $d3 := dict "one" 1 -}}
62 {{- $d4 := dict "one" 1 "two" 4444 -}}
63 {{- pluck "two" $d $d2 $d3 $d4 -}}
64 `
65
66 expect := "[222222 33333 4444]"
67 if err := runt(tpl, expect); err != nil {
68 t.Error(err)
69 }
70 }
71
72 func TestKeys(t *testing.T) {
73 tests := map[string]string{
74 `{{ dict "foo" 1 "bar" 2 | keys | sortAlpha }}`: "[bar foo]",
75 `{{ dict | keys }}`: "[]",
76 `{{ keys (dict "foo" 1) (dict "bar" 2) (dict "bar" 3) | uniq | sortAlpha }}`: "[bar foo]",
77 }
78 for tpl, expect := range tests {
79 if err := runt(tpl, expect); err != nil {
80 t.Error(err)
81 }
82 }
83 }
84
85 func TestPick(t *testing.T) {
86 tests := map[string]string{
87 `{{- $d := dict "one" 1 "two" 222222 }}{{ pick $d "two" | len -}}`: "1",
88 `{{- $d := dict "one" 1 "two" 222222 }}{{ pick $d "two" -}}`: "map[two:222222]",
89 `{{- $d := dict "one" 1 "two" 222222 }}{{ pick $d "one" "two" | len -}}`: "2",
90 `{{- $d := dict "one" 1 "two" 222222 }}{{ pick $d "one" "two" "three" | len -}}`: "2",
91 `{{- $d := dict }}{{ pick $d "two" | len -}}`: "0",
92 }
93 for tpl, expect := range tests {
94 if err := runt(tpl, expect); err != nil {
95 t.Error(err)
96 }
97 }
98 }
99 func TestOmit(t *testing.T) {
100 tests := map[string]string{
101 `{{- $d := dict "one" 1 "two" 222222 }}{{ omit $d "one" | len -}}`: "1",
102 `{{- $d := dict "one" 1 "two" 222222 }}{{ omit $d "one" -}}`: "map[two:222222]",
103 `{{- $d := dict "one" 1 "two" 222222 }}{{ omit $d "one" "two" | len -}}`: "0",
104 `{{- $d := dict "one" 1 "two" 222222 }}{{ omit $d "two" "three" | len -}}`: "1",
105 `{{- $d := dict }}{{ omit $d "two" | len -}}`: "0",
106 }
107 for tpl, expect := range tests {
108 if err := runt(tpl, expect); err != nil {
109 t.Error(err)
110 }
111 }
112 }
113
114 func TestGet(t *testing.T) {
115 tests := map[string]string{
116 `{{- $d := dict "one" 1 }}{{ get $d "one" -}}`: "1",
117 `{{- $d := dict "one" 1 "two" "2" }}{{ get $d "two" -}}`: "2",
118 `{{- $d := dict }}{{ get $d "two" -}}`: "",
119 }
120 for tpl, expect := range tests {
121 if err := runt(tpl, expect); err != nil {
122 t.Error(err)
123 }
124 }
125 }
126
127 func TestSet(t *testing.T) {
128 tpl := `{{- $d := dict "one" 1 "two" 222222 -}}
129 {{- $_ := set $d "two" 2 -}}
130 {{- $_ := set $d "three" 3 -}}
131 {{- if hasKey $d "one" -}}{{$d.one}}{{- end -}}
132 {{- if hasKey $d "two" -}}{{$d.two}}{{- end -}}
133 {{- if hasKey $d "three" -}}{{$d.three}}{{- end -}}
134 `
135
136 expect := "123"
137 if err := runt(tpl, expect); err != nil {
138 t.Error(err)
139 }
140 }
141
142 func TestMerge(t *testing.T) {
143 dict := map[string]interface{}{
144 "src2": map[string]interface{}{
145 "h": 10,
146 "i": "i",
147 "j": "j",
148 },
149 "src1": map[string]interface{}{
150 "a": 1,
151 "b": 2,
152 "d": map[string]interface{}{
153 "e": "four",
154 },
155 "g": []int{6, 7},
156 "i": "aye",
157 "j": "jay",
158 "k": map[string]interface{}{
159 "l": false,
160 },
161 },
162 "dst": map[string]interface{}{
163 "a": "one",
164 "c": 3,
165 "d": map[string]interface{}{
166 "f": 5,
167 },
168 "g": []int{8, 9},
169 "i": "eye",
170 "k": map[string]interface{}{
171 "l": true,
172 },
173 },
174 }
175 tpl := `{{merge .dst .src1 .src2}}`
176 _, err := runRaw(tpl, dict)
177 if err != nil {
178 t.Error(err)
179 }
180 expected := map[string]interface{}{
181 "a": "one", // key overridden
182 "b": 2, // merged from src1
183 "c": 3, // merged from dst
184 "d": map[string]interface{}{ // deep merge
185 "e": "four",
186 "f": 5,
187 },
188 "g": []int{8, 9}, // overridden - arrays are not merged
189 "h": 10, // merged from src2
190 "i": "eye", // overridden twice
191 "j": "jay", // overridden and merged
192 "k": map[string]interface{}{
193 "l": true, // overridden
194 },
195 }
196 assert.Equal(t, expected, dict["dst"])
197 }
198
199 func TestMergeOverwrite(t *testing.T) {
200 dict := map[string]interface{}{
201 "src2": map[string]interface{}{
202 "h": 10,
203 "i": "i",
204 "j": "j",
205 },
206 "src1": map[string]interface{}{
207 "a": 1,
208 "b": 2,
209 "d": map[string]interface{}{
210 "e": "four",
211 },
212 "g": []int{6, 7},
213 "i": "aye",
214 "j": "jay",
215 "k": map[string]interface{}{
216 "l": false,
217 },
218 },
219 "dst": map[string]interface{}{
220 "a": "one",
221 "c": 3,
222 "d": map[string]interface{}{
223 "f": 5,
224 },
225 "g": []int{8, 9},
226 "i": "eye",
227 "k": map[string]interface{}{
228 "l": true,
229 },
230 },
231 }
232 tpl := `{{mergeOverwrite .dst .src1 .src2}}`
233 _, err := runRaw(tpl, dict)
234 if err != nil {
235 t.Error(err)
236 }
237 expected := map[string]interface{}{
238 "a": 1, // key overwritten from src1
239 "b": 2, // merged from src1
240 "c": 3, // merged from dst
241 "d": map[string]interface{}{ // deep merge
242 "e": "four",
243 "f": 5,
244 },
245 "g": []int{6, 7}, // overwritten src1 wins
246 "h": 10, // merged from src2
247 "i": "i", // overwritten twice src2 wins
248 "j": "j", // overwritten twice src2 wins
249 "k": map[string]interface{}{ // deep merge
250 "l": false, // overwritten src1 wins
251 },
252 }
253 assert.Equal(t, expected, dict["dst"])
254 }
255
256 func TestValues(t *testing.T) {
257 tests := map[string]string{
258 `{{- $d := dict "a" 1 "b" 2 }}{{ values $d | sortAlpha | join "," }}`: "1,2",
259 `{{- $d := dict "a" "first" "b" 2 }}{{ values $d | sortAlpha | join "," }}`: "2,first",
260 }
261
262 for tpl, expect := range tests {
263 if err := runt(tpl, expect); err != nil {
264 t.Error(err)
265 }
266 }
267 }
268
269 func TestDeepCopy(t *testing.T) {
270 tests := map[string]string{
271 `{{- $d := dict "a" 1 "b" 2 | deepCopy }}{{ values $d | sortAlpha | join "," }}`: "1,2",
272 `{{- $d := dict "a" 1 "b" 2 | deepCopy }}{{ keys $d | sortAlpha | join "," }}`: "a,b",
273 `{{- $one := dict "foo" (dict "bar" "baz") "qux" true -}}{{ deepCopy $one }}`: "map[foo:map[bar:baz] qux:true]",
274 }
275
276 for tpl, expect := range tests {
277 if err := runt(tpl, expect); err != nil {
278 t.Error(err)
279 }
280 }
281 }
282
283 func TestMustDeepCopy(t *testing.T) {
284 tests := map[string]string{
285 `{{- $d := dict "a" 1 "b" 2 | mustDeepCopy }}{{ values $d | sortAlpha | join "," }}`: "1,2",
286 `{{- $d := dict "a" 1 "b" 2 | mustDeepCopy }}{{ keys $d | sortAlpha | join "," }}`: "a,b",
287 `{{- $one := dict "foo" (dict "bar" "baz") "qux" true -}}{{ mustDeepCopy $one }}`: "map[foo:map[bar:baz] qux:true]",
288 }
289
290 for tpl, expect := range tests {
291 if err := runt(tpl, expect); err != nil {
292 t.Error(err)
293 }
294 }
295 }
296
297 func TestDig(t *testing.T) {
298 tests := map[string]string{
299 `{{- $d := dict "a" (dict "b" (dict "c" 1)) }}{{ dig "a" "b" "c" "" $d }}`: "1",
300 `{{- $d := dict "a" (dict "b" (dict "c" 1)) }}{{ dig "a" "b" "z" "2" $d }}`: "2",
301 `{{ dict "a" 1 | dig "a" "" }}`: "1",
302 `{{ dict "a" 1 | dig "z" "2" }}`: "2",
303 }
304
305 for tpl, expect := range tests {
306 if err := runt(tpl, expect); err != nil {
307 t.Error(err)
308 }
309 }
310 }