forked from Masterminds/sprig
-
Notifications
You must be signed in to change notification settings - Fork 0
/
list_test.go
135 lines (123 loc) · 3.61 KB
/
list_test.go
1
2
3
4
5
6
7
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
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package sprig
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestTuple(t *testing.T) {
tpl := `{{$t := tuple 1 "a" "foo"}}{{index $t 2}}{{index $t 0 }}{{index $t 1}}`
if err := runt(tpl, "foo1a"); err != nil {
t.Error(err)
}
}
func TestList(t *testing.T) {
tpl := `{{$t := list 1 "a" "foo"}}{{index $t 2}}{{index $t 0 }}{{index $t 1}}`
if err := runt(tpl, "foo1a"); err != nil {
t.Error(err)
}
}
func TestPush(t *testing.T) {
// Named `append` in the function map
tests := map[string]string{
`{{ $t := tuple 1 2 3 }}{{ append $t 4 | len }}`: "4",
`{{ $t := tuple 1 2 3 4 }}{{ append $t 5 | join "-" }}`: "1-2-3-4-5",
}
for tpl, expect := range tests {
assert.NoError(t, runt(tpl, expect))
}
}
func TestPrepend(t *testing.T) {
tests := map[string]string{
`{{ $t := tuple 1 2 3 }}{{ prepend $t 0 | len }}`: "4",
`{{ $t := tuple 1 2 3 4 }}{{ prepend $t 0 | join "-" }}`: "0-1-2-3-4",
}
for tpl, expect := range tests {
assert.NoError(t, runt(tpl, expect))
}
}
func TestFirst(t *testing.T) {
tests := map[string]string{
`{{ list 1 2 3 | first }}`: "1",
`{{ list | first }}`: "<no value>",
}
for tpl, expect := range tests {
assert.NoError(t, runt(tpl, expect))
}
}
func TestLast(t *testing.T) {
tests := map[string]string{
`{{ list 1 2 3 | last }}`: "3",
`{{ list | last }}`: "<no value>",
}
for tpl, expect := range tests {
assert.NoError(t, runt(tpl, expect))
}
}
func TestInitial(t *testing.T) {
tests := map[string]string{
`{{ list 1 2 3 | initial | len }}`: "2",
`{{ list 1 2 3 | initial | last }}`: "2",
`{{ list 1 2 3 | initial | first }}`: "1",
`{{ list | initial }}`: "[]",
}
for tpl, expect := range tests {
assert.NoError(t, runt(tpl, expect))
}
}
func TestRest(t *testing.T) {
tests := map[string]string{
`{{ list 1 2 3 | rest | len }}`: "2",
`{{ list 1 2 3 | rest | last }}`: "3",
`{{ list 1 2 3 | rest | first }}`: "2",
`{{ list | rest }}`: "[]",
}
for tpl, expect := range tests {
assert.NoError(t, runt(tpl, expect))
}
}
func TestReverse(t *testing.T) {
tests := map[string]string{
`{{ list 1 2 3 | reverse | first }}`: "3",
`{{ list 1 2 3 | reverse | rest | first }}`: "2",
`{{ list 1 2 3 | reverse | last }}`: "1",
`{{ list 1 2 3 4 | reverse }}`: "[4 3 2 1]",
`{{ list 1 | reverse }}`: "[1]",
`{{ list | reverse }}`: "[]",
}
for tpl, expect := range tests {
assert.NoError(t, runt(tpl, expect))
}
}
func TestUniq(t *testing.T) {
tests := map[string]string{
`{{ list 1 2 3 4 | uniq }}`: `[1 2 3 4]`,
`{{ list "a" "b" "c" "d" | uniq }}`: `[a b c d]`,
`{{ list 1 1 1 1 2 2 2 2 | uniq }}`: `[1 2]`,
`{{ list "foo" 1 1 1 1 "foo" "foo" | uniq }}`: `[foo 1]`,
`{{ list | uniq }}`: `[]`,
}
for tpl, expect := range tests {
assert.NoError(t, runt(tpl, expect))
}
}
func TestWithout(t *testing.T) {
tests := map[string]string{
`{{ without (list 1 2 3 4) 1 }}`: `[2 3 4]`,
`{{ without (list "a" "b" "c" "d") "a" }}`: `[b c d]`,
`{{ without (list 1 1 1 1 2) 1 }}`: `[2]`,
`{{ without (list) 1 }}`: `[]`,
`{{ without (list 1 2 3) }}`: `[1 2 3]`,
`{{ without list }}`: `[]`,
}
for tpl, expect := range tests {
assert.NoError(t, runt(tpl, expect))
}
}
func TestHas(t *testing.T) {
tests := map[string]string{
`{{ list 1 2 3 | has 1 }}`: `true`,
`{{ list 1 2 3 | has 4 }}`: `false`,
}
for tpl, expect := range tests {
assert.NoError(t, runt(tpl, expect))
}
}