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