1 package sprig
2
3 import (
4 "testing"
5
6 "github.com/stretchr/testify/assert"
7 )
8
9 func TestTuple(t *testing.T) {
10 tpl := `{{$t := tuple 1 "a" "foo"}}{{index $t 2}}{{index $t 0 }}{{index $t 1}}`
11 if err := runt(tpl, "foo1a"); err != nil {
12 t.Error(err)
13 }
14 }
15
16 func TestList(t *testing.T) {
17 tpl := `{{$t := list 1 "a" "foo"}}{{index $t 2}}{{index $t 0 }}{{index $t 1}}`
18 if err := runt(tpl, "foo1a"); err != nil {
19 t.Error(err)
20 }
21 }
22
23 func TestPush(t *testing.T) {
24
25 tests := map[string]string{
26 `{{ $t := tuple 1 2 3 }}{{ append $t 4 | len }}`: "4",
27 `{{ $t := tuple 1 2 3 4 }}{{ append $t 5 | join "-" }}`: "1-2-3-4-5",
28 `{{ $t := regexSplit "/" "foo/bar/baz" -1 }}{{ append $t "qux" | join "-" }}`: "foo-bar-baz-qux",
29 }
30 for tpl, expect := range tests {
31 assert.NoError(t, runt(tpl, expect))
32 }
33 }
34
35 func TestMustPush(t *testing.T) {
36
37 tests := map[string]string{
38 `{{ $t := tuple 1 2 3 }}{{ mustAppend $t 4 | len }}`: "4",
39 `{{ $t := tuple 1 2 3 4 }}{{ mustAppend $t 5 | join "-" }}`: "1-2-3-4-5",
40 `{{ $t := regexSplit "/" "foo/bar/baz" -1 }}{{ mustPush $t "qux" | join "-" }}`: "foo-bar-baz-qux",
41 }
42 for tpl, expect := range tests {
43 assert.NoError(t, runt(tpl, expect))
44 }
45 }
46
47 func TestChunk(t *testing.T) {
48 tests := map[string]string{
49 `{{ tuple 1 2 3 4 5 6 7 | chunk 3 | len }}`: "3",
50 `{{ tuple | chunk 3 | len }}`: "0",
51 `{{ range ( tuple 1 2 3 4 5 6 7 8 9 | chunk 3 ) }}{{. | join "-"}}|{{end}}`: "1-2-3|4-5-6|7-8-9|",
52 `{{ range ( tuple 1 2 3 4 5 6 7 8 | chunk 3 ) }}{{. | join "-"}}|{{end}}`: "1-2-3|4-5-6|7-8|",
53 `{{ range ( tuple 1 2 | chunk 3 ) }}{{. | join "-"}}|{{end}}`: "1-2|",
54 }
55 for tpl, expect := range tests {
56 assert.NoError(t, runt(tpl, expect))
57 }
58 }
59
60 func TestMustChunk(t *testing.T) {
61 tests := map[string]string{
62 `{{ tuple 1 2 3 4 5 6 7 | mustChunk 3 | len }}`: "3",
63 `{{ tuple | mustChunk 3 | len }}`: "0",
64 `{{ range ( tuple 1 2 3 4 5 6 7 8 9 | mustChunk 3 ) }}{{. | join "-"}}|{{end}}`: "1-2-3|4-5-6|7-8-9|",
65 `{{ range ( tuple 1 2 3 4 5 6 7 8 | mustChunk 3 ) }}{{. | join "-"}}|{{end}}`: "1-2-3|4-5-6|7-8|",
66 `{{ range ( tuple 1 2 | mustChunk 3 ) }}{{. | join "-"}}|{{end}}`: "1-2|",
67 }
68 for tpl, expect := range tests {
69 assert.NoError(t, runt(tpl, expect))
70 }
71 }
72
73 func TestPrepend(t *testing.T) {
74 tests := map[string]string{
75 `{{ $t := tuple 1 2 3 }}{{ prepend $t 0 | len }}`: "4",
76 `{{ $t := tuple 1 2 3 4 }}{{ prepend $t 0 | join "-" }}`: "0-1-2-3-4",
77 `{{ $t := regexSplit "/" "foo/bar/baz" -1 }}{{ prepend $t "qux" | join "-" }}`: "qux-foo-bar-baz",
78 }
79 for tpl, expect := range tests {
80 assert.NoError(t, runt(tpl, expect))
81 }
82 }
83
84 func TestMustPrepend(t *testing.T) {
85 tests := map[string]string{
86 `{{ $t := tuple 1 2 3 }}{{ mustPrepend $t 0 | len }}`: "4",
87 `{{ $t := tuple 1 2 3 4 }}{{ mustPrepend $t 0 | join "-" }}`: "0-1-2-3-4",
88 `{{ $t := regexSplit "/" "foo/bar/baz" -1 }}{{ mustPrepend $t "qux" | join "-" }}`: "qux-foo-bar-baz",
89 }
90 for tpl, expect := range tests {
91 assert.NoError(t, runt(tpl, expect))
92 }
93 }
94
95 func TestFirst(t *testing.T) {
96 tests := map[string]string{
97 `{{ list 1 2 3 | first }}`: "1",
98 `{{ list | first }}`: "<no value>",
99 `{{ regexSplit "/src/" "foo/src/bar" -1 | first }}`: "foo",
100 }
101 for tpl, expect := range tests {
102 assert.NoError(t, runt(tpl, expect))
103 }
104 }
105
106 func TestMustFirst(t *testing.T) {
107 tests := map[string]string{
108 `{{ list 1 2 3 | mustFirst }}`: "1",
109 `{{ list | mustFirst }}`: "<no value>",
110 `{{ regexSplit "/src/" "foo/src/bar" -1 | mustFirst }}`: "foo",
111 }
112 for tpl, expect := range tests {
113 assert.NoError(t, runt(tpl, expect))
114 }
115 }
116
117 func TestLast(t *testing.T) {
118 tests := map[string]string{
119 `{{ list 1 2 3 | last }}`: "3",
120 `{{ list | last }}`: "<no value>",
121 `{{ regexSplit "/src/" "foo/src/bar" -1 | last }}`: "bar",
122 }
123 for tpl, expect := range tests {
124 assert.NoError(t, runt(tpl, expect))
125 }
126 }
127
128 func TestMustLast(t *testing.T) {
129 tests := map[string]string{
130 `{{ list 1 2 3 | mustLast }}`: "3",
131 `{{ list | mustLast }}`: "<no value>",
132 `{{ regexSplit "/src/" "foo/src/bar" -1 | mustLast }}`: "bar",
133 }
134 for tpl, expect := range tests {
135 assert.NoError(t, runt(tpl, expect))
136 }
137 }
138
139 func TestInitial(t *testing.T) {
140 tests := map[string]string{
141 `{{ list 1 2 3 | initial | len }}`: "2",
142 `{{ list 1 2 3 | initial | last }}`: "2",
143 `{{ list 1 2 3 | initial | first }}`: "1",
144 `{{ list | initial }}`: "[]",
145 `{{ regexSplit "/" "foo/bar/baz" -1 | initial }}`: "[foo bar]",
146 }
147 for tpl, expect := range tests {
148 assert.NoError(t, runt(tpl, expect))
149 }
150 }
151
152 func TestMustInitial(t *testing.T) {
153 tests := map[string]string{
154 `{{ list 1 2 3 | mustInitial | len }}`: "2",
155 `{{ list 1 2 3 | mustInitial | last }}`: "2",
156 `{{ list 1 2 3 | mustInitial | first }}`: "1",
157 `{{ list | mustInitial }}`: "[]",
158 `{{ regexSplit "/" "foo/bar/baz" -1 | mustInitial }}`: "[foo bar]",
159 }
160 for tpl, expect := range tests {
161 assert.NoError(t, runt(tpl, expect))
162 }
163 }
164
165 func TestRest(t *testing.T) {
166 tests := map[string]string{
167 `{{ list 1 2 3 | rest | len }}`: "2",
168 `{{ list 1 2 3 | rest | last }}`: "3",
169 `{{ list 1 2 3 | rest | first }}`: "2",
170 `{{ list | rest }}`: "[]",
171 `{{ regexSplit "/" "foo/bar/baz" -1 | rest }}`: "[bar baz]",
172 }
173 for tpl, expect := range tests {
174 assert.NoError(t, runt(tpl, expect))
175 }
176 }
177
178 func TestMustRest(t *testing.T) {
179 tests := map[string]string{
180 `{{ list 1 2 3 | mustRest | len }}`: "2",
181 `{{ list 1 2 3 | mustRest | last }}`: "3",
182 `{{ list 1 2 3 | mustRest | first }}`: "2",
183 `{{ list | mustRest }}`: "[]",
184 `{{ regexSplit "/" "foo/bar/baz" -1 | mustRest }}`: "[bar baz]",
185 }
186 for tpl, expect := range tests {
187 assert.NoError(t, runt(tpl, expect))
188 }
189 }
190
191 func TestReverse(t *testing.T) {
192 tests := map[string]string{
193 `{{ list 1 2 3 | reverse | first }}`: "3",
194 `{{ list 1 2 3 | reverse | rest | first }}`: "2",
195 `{{ list 1 2 3 | reverse | last }}`: "1",
196 `{{ list 1 2 3 4 | reverse }}`: "[4 3 2 1]",
197 `{{ list 1 | reverse }}`: "[1]",
198 `{{ list | reverse }}`: "[]",
199 `{{ regexSplit "/" "foo/bar/baz" -1 | reverse }}`: "[baz bar foo]",
200 }
201 for tpl, expect := range tests {
202 assert.NoError(t, runt(tpl, expect))
203 }
204 }
205
206 func TestMustReverse(t *testing.T) {
207 tests := map[string]string{
208 `{{ list 1 2 3 | mustReverse | first }}`: "3",
209 `{{ list 1 2 3 | mustReverse | rest | first }}`: "2",
210 `{{ list 1 2 3 | mustReverse | last }}`: "1",
211 `{{ list 1 2 3 4 | mustReverse }}`: "[4 3 2 1]",
212 `{{ list 1 | mustReverse }}`: "[1]",
213 `{{ list | mustReverse }}`: "[]",
214 `{{ regexSplit "/" "foo/bar/baz" -1 | mustReverse }}`: "[baz bar foo]",
215 }
216 for tpl, expect := range tests {
217 assert.NoError(t, runt(tpl, expect))
218 }
219 }
220
221 func TestCompact(t *testing.T) {
222 tests := map[string]string{
223 `{{ list 1 0 "" "hello" | compact }}`: `[1 hello]`,
224 `{{ list "" "" | compact }}`: `[]`,
225 `{{ list | compact }}`: `[]`,
226 `{{ regexSplit "/" "foo//bar" -1 | compact }}`: "[foo bar]",
227 }
228 for tpl, expect := range tests {
229 assert.NoError(t, runt(tpl, expect))
230 }
231 }
232
233 func TestMustCompact(t *testing.T) {
234 tests := map[string]string{
235 `{{ list 1 0 "" "hello" | mustCompact }}`: `[1 hello]`,
236 `{{ list "" "" | mustCompact }}`: `[]`,
237 `{{ list | mustCompact }}`: `[]`,
238 `{{ regexSplit "/" "foo//bar" -1 | mustCompact }}`: "[foo bar]",
239 }
240 for tpl, expect := range tests {
241 assert.NoError(t, runt(tpl, expect))
242 }
243 }
244
245 func TestUniq(t *testing.T) {
246 tests := map[string]string{
247 `{{ list 1 2 3 4 | uniq }}`: `[1 2 3 4]`,
248 `{{ list "a" "b" "c" "d" | uniq }}`: `[a b c d]`,
249 `{{ list 1 1 1 1 2 2 2 2 | uniq }}`: `[1 2]`,
250 `{{ list "foo" 1 1 1 1 "foo" "foo" | uniq }}`: `[foo 1]`,
251 `{{ list | uniq }}`: `[]`,
252 `{{ regexSplit "/" "foo/foo/bar" -1 | uniq }}`: "[foo bar]",
253 }
254 for tpl, expect := range tests {
255 assert.NoError(t, runt(tpl, expect))
256 }
257 }
258
259 func TestMustUniq(t *testing.T) {
260 tests := map[string]string{
261 `{{ list 1 2 3 4 | mustUniq }}`: `[1 2 3 4]`,
262 `{{ list "a" "b" "c" "d" | mustUniq }}`: `[a b c d]`,
263 `{{ list 1 1 1 1 2 2 2 2 | mustUniq }}`: `[1 2]`,
264 `{{ list "foo" 1 1 1 1 "foo" "foo" | mustUniq }}`: `[foo 1]`,
265 `{{ list | mustUniq }}`: `[]`,
266 `{{ regexSplit "/" "foo/foo/bar" -1 | mustUniq }}`: "[foo bar]",
267 }
268 for tpl, expect := range tests {
269 assert.NoError(t, runt(tpl, expect))
270 }
271 }
272
273 func TestWithout(t *testing.T) {
274 tests := map[string]string{
275 `{{ without (list 1 2 3 4) 1 }}`: `[2 3 4]`,
276 `{{ without (list "a" "b" "c" "d") "a" }}`: `[b c d]`,
277 `{{ without (list 1 1 1 1 2) 1 }}`: `[2]`,
278 `{{ without (list) 1 }}`: `[]`,
279 `{{ without (list 1 2 3) }}`: `[1 2 3]`,
280 `{{ without list }}`: `[]`,
281 `{{ without (regexSplit "/" "foo/bar/baz" -1 ) "foo" }}`: "[bar baz]",
282 }
283 for tpl, expect := range tests {
284 assert.NoError(t, runt(tpl, expect))
285 }
286 }
287
288 func TestMustWithout(t *testing.T) {
289 tests := map[string]string{
290 `{{ mustWithout (list 1 2 3 4) 1 }}`: `[2 3 4]`,
291 `{{ mustWithout (list "a" "b" "c" "d") "a" }}`: `[b c d]`,
292 `{{ mustWithout (list 1 1 1 1 2) 1 }}`: `[2]`,
293 `{{ mustWithout (list) 1 }}`: `[]`,
294 `{{ mustWithout (list 1 2 3) }}`: `[1 2 3]`,
295 `{{ mustWithout list }}`: `[]`,
296 `{{ mustWithout (regexSplit "/" "foo/bar/baz" -1 ) "foo" }}`: "[bar baz]",
297 }
298 for tpl, expect := range tests {
299 assert.NoError(t, runt(tpl, expect))
300 }
301 }
302
303 func TestHas(t *testing.T) {
304 tests := map[string]string{
305 `{{ list 1 2 3 | has 1 }}`: `true`,
306 `{{ list 1 2 3 | has 4 }}`: `false`,
307 `{{ regexSplit "/" "foo/bar/baz" -1 | has "bar" }}`: `true`,
308 `{{ has "bar" nil }}`: `false`,
309 }
310 for tpl, expect := range tests {
311 assert.NoError(t, runt(tpl, expect))
312 }
313 }
314
315 func TestMustHas(t *testing.T) {
316 tests := map[string]string{
317 `{{ list 1 2 3 | mustHas 1 }}`: `true`,
318 `{{ list 1 2 3 | mustHas 4 }}`: `false`,
319 `{{ regexSplit "/" "foo/bar/baz" -1 | mustHas "bar" }}`: `true`,
320 `{{ mustHas "bar" nil }}`: `false`,
321 }
322 for tpl, expect := range tests {
323 assert.NoError(t, runt(tpl, expect))
324 }
325 }
326
327 func TestSlice(t *testing.T) {
328 tests := map[string]string{
329 `{{ slice (list 1 2 3) }}`: "[1 2 3]",
330 `{{ slice (list 1 2 3) 0 1 }}`: "[1]",
331 `{{ slice (list 1 2 3) 1 3 }}`: "[2 3]",
332 `{{ slice (list 1 2 3) 1 }}`: "[2 3]",
333 `{{ slice (regexSplit "/" "foo/bar/baz" -1) 1 2 }}`: "[bar]",
334 }
335 for tpl, expect := range tests {
336 assert.NoError(t, runt(tpl, expect))
337 }
338 }
339
340 func TestMustSlice(t *testing.T) {
341 tests := map[string]string{
342 `{{ mustSlice (list 1 2 3) }}`: "[1 2 3]",
343 `{{ mustSlice (list 1 2 3) 0 1 }}`: "[1]",
344 `{{ mustSlice (list 1 2 3) 1 3 }}`: "[2 3]",
345 `{{ mustSlice (list 1 2 3) 1 }}`: "[2 3]",
346 `{{ mustSlice (regexSplit "/" "foo/bar/baz" -1) 1 2 }}`: "[bar]",
347 }
348 for tpl, expect := range tests {
349 assert.NoError(t, runt(tpl, expect))
350 }
351 }
352
353 func TestConcat(t *testing.T) {
354 tests := map[string]string{
355 `{{ concat (list 1 2 3) }}`: "[1 2 3]",
356 `{{ concat (list 1 2 3) (list 4 5) }}`: "[1 2 3 4 5]",
357 `{{ concat (list 1 2 3) (list 4 5) (list) }}`: "[1 2 3 4 5]",
358 `{{ concat (list 1 2 3) (list 4 5) (list nil) }}`: "[1 2 3 4 5 <nil>]",
359 `{{ concat (list 1 2 3) (list 4 5) (list ( list "foo" ) ) }}`: "[1 2 3 4 5 [foo]]",
360 }
361 for tpl, expect := range tests {
362 assert.NoError(t, runt(tpl, expect))
363 }
364 }
365
View as plain text