Debian Codegraph @

util/sprig/strings_test.go

raw
1 package sprig
2
3 import (
4 "encoding/base32"
5 "encoding/base64"
6 "fmt"
7 "strings"
8 "testing"
9
10 "github.com/stretchr/testify/assert"
11 )
12
13 func TestSubstr(t *testing.T) {
14 tpl := `{{"fooo" | substr 0 3 }}`
15 if err := runt(tpl, "foo"); err != nil {
16 t.Error(err)
17 }
18 }
19
20 func TestSubstr_shorterString(t *testing.T) {
21 tpl := `{{"foo" | substr 0 10 }}`
22 if err := runt(tpl, "foo"); err != nil {
23 t.Error(err)
24 }
25 }
26
27 func TestTrunc(t *testing.T) {
28 tpl := `{{ "foooooo" | trunc 3 }}`
29 if err := runt(tpl, "foo"); err != nil {
30 t.Error(err)
31 }
32 tpl = `{{ "baaaaaar" | trunc -3 }}`
33 if err := runt(tpl, "aar"); err != nil {
34 t.Error(err)
35 }
36 tpl = `{{ "baaaaaar" | trunc -999 }}`
37 if err := runt(tpl, "baaaaaar"); err != nil {
38 t.Error(err)
39 }
40 tpl = `{{ "baaaaaz" | trunc 0 }}`
41 if err := runt(tpl, ""); err != nil {
42 t.Error(err)
43 }
44 }
45
46 func TestQuote(t *testing.T) {
47 tpl := `{{quote "a" "b" "c"}}`
48 if err := runt(tpl, `"a" "b" "c"`); err != nil {
49 t.Error(err)
50 }
51 tpl = `{{quote "\"a\"" "b" "c"}}`
52 if err := runt(tpl, `"\"a\"" "b" "c"`); err != nil {
53 t.Error(err)
54 }
55 tpl = `{{quote 1 2 3 }}`
56 if err := runt(tpl, `"1" "2" "3"`); err != nil {
57 t.Error(err)
58 }
59 tpl = `{{ .value | quote }}`
60 values := map[string]any{"value": nil}
61 if err := runtv(tpl, ``, values); err != nil {
62 t.Error(err)
63 }
64 }
65 func TestSquote(t *testing.T) {
66 tpl := `{{squote "a" "b" "c"}}`
67 if err := runt(tpl, `'a' 'b' 'c'`); err != nil {
68 t.Error(err)
69 }
70 tpl = `{{squote 1 2 3 }}`
71 if err := runt(tpl, `'1' '2' '3'`); err != nil {
72 t.Error(err)
73 }
74 tpl = `{{ .value | squote }}`
75 values := map[string]any{"value": nil}
76 if err := runtv(tpl, ``, values); err != nil {
77 t.Error(err)
78 }
79 }
80
81 func TestContains(t *testing.T) {
82 // Mainly, we're just verifying the paramater order swap.
83 tests := []string{
84 `{{if contains "cat" "fair catch"}}1{{end}}`,
85 `{{if hasPrefix "cat" "catch"}}1{{end}}`,
86 `{{if hasSuffix "cat" "ducat"}}1{{end}}`,
87 }
88 for _, tt := range tests {
89 if err := runt(tt, "1"); err != nil {
90 t.Error(err)
91 }
92 }
93 }
94
95 func TestTrim(t *testing.T) {
96 tests := []string{
97 `{{trim " 5.00 "}}`,
98 `{{trimAll "$" "$5.00$"}}`,
99 `{{trimPrefix "$" "$5.00"}}`,
100 `{{trimSuffix "$" "5.00$"}}`,
101 }
102 for _, tt := range tests {
103 if err := runt(tt, "5.00"); err != nil {
104 t.Error(err)
105 }
106 }
107 }
108
109 func TestSplit(t *testing.T) {
110 tpl := `{{$v := "foo$bar$baz" | split "$"}}{{$v._0}}`
111 if err := runt(tpl, "foo"); err != nil {
112 t.Error(err)
113 }
114 }
115
116 func TestSplitn(t *testing.T) {
117 tpl := `{{$v := "foo$bar$baz" | splitn "$" 2}}{{$v._0}}`
118 if err := runt(tpl, "foo"); err != nil {
119 t.Error(err)
120 }
121 }
122
123 func TestToString(t *testing.T) {
124 tpl := `{{ toString 1 | kindOf }}`
125 assert.NoError(t, runt(tpl, "string"))
126 }
127
128 func TestToStrings(t *testing.T) {
129 tpl := `{{ $s := list 1 2 3 | toStrings }}{{ index $s 1 | kindOf }}`
130 assert.NoError(t, runt(tpl, "string"))
131 tpl = `{{ list 1 .value 2 | toStrings }}`
132 values := map[string]any{"value": nil}
133 if err := runtv(tpl, `[1 2]`, values); err != nil {
134 t.Error(err)
135 }
136 }
137
138 func TestJoin(t *testing.T) {
139 assert.NoError(t, runt(`{{ tuple "a" "b" "c" | join "-" }}`, "a-b-c"))
140 assert.NoError(t, runt(`{{ tuple 1 2 3 | join "-" }}`, "1-2-3"))
141 assert.NoError(t, runtv(`{{ join "-" .V }}`, "a-b-c", map[string]any{"V": []string{"a", "b", "c"}}))
142 assert.NoError(t, runtv(`{{ join "-" .V }}`, "abc", map[string]any{"V": "abc"}))
143 assert.NoError(t, runtv(`{{ join "-" .V }}`, "1-2-3", map[string]any{"V": []int{1, 2, 3}}))
144 assert.NoError(t, runtv(`{{ join "-" .value }}`, "1-2", map[string]any{"value": []any{"1", nil, "2"}}))
145 }
146
147 func TestSortAlpha(t *testing.T) {
148 // Named `append` in the function map
149 tests := map[string]string{
150 `{{ list "c" "a" "b" | sortAlpha | join "" }}`: "abc",
151 `{{ list 2 1 4 3 | sortAlpha | join "" }}`: "1234",
152 }
153 for tpl, expect := range tests {
154 assert.NoError(t, runt(tpl, expect))
155 }
156 }
157 func TestBase64EncodeDecode(t *testing.T) {
158 magicWord := "coffee"
159 expect := base64.StdEncoding.EncodeToString([]byte(magicWord))
160
161 if expect == magicWord {
162 t.Fatal("Encoder doesn't work.")
163 }
164
165 tpl := `{{b64enc "coffee"}}`
166 if err := runt(tpl, expect); err != nil {
167 t.Error(err)
168 }
169 tpl = fmt.Sprintf("{{b64dec %q}}", expect)
170 if err := runt(tpl, magicWord); err != nil {
171 t.Error(err)
172 }
173 }
174 func TestBase32EncodeDecode(t *testing.T) {
175 magicWord := "coffee"
176 expect := base32.StdEncoding.EncodeToString([]byte(magicWord))
177
178 if expect == magicWord {
179 t.Fatal("Encoder doesn't work.")
180 }
181
182 tpl := `{{b32enc "coffee"}}`
183 if err := runt(tpl, expect); err != nil {
184 t.Error(err)
185 }
186 tpl = fmt.Sprintf("{{b32dec %q}}", expect)
187 if err := runt(tpl, magicWord); err != nil {
188 t.Error(err)
189 }
190 }
191
192 func TestCat(t *testing.T) {
193 tpl := `{{$b := "b"}}{{"c" | cat "a" $b}}`
194 if err := runt(tpl, "a b c"); err != nil {
195 t.Error(err)
196 }
197 tpl = `{{ .value | cat "a" "b"}}`
198 values := map[string]any{"value": nil}
199 if err := runtv(tpl, "a b", values); err != nil {
200 t.Error(err)
201 }
202 }
203
204 func TestIndent(t *testing.T) {
205 tpl := `{{indent 4 "a\nb\nc"}}`
206 if err := runt(tpl, " a\n b\n c"); err != nil {
207 t.Error(err)
208 }
209 }
210
211 func TestNindent(t *testing.T) {
212 tpl := `{{nindent 4 "a\nb\nc"}}`
213 if err := runt(tpl, "\n a\n b\n c"); err != nil {
214 t.Error(err)
215 }
216 }
217
218 func TestIndentLimit(t *testing.T) {
219 // Indentation beyond a sane width is an amplification attempt: indent allocates
220 // spaces * lines bytes in a single uninterruptible call
221 if err := runt(`{{indent 100 "a"}}`, strings.Repeat(" ", 100)+"a"); err != nil {
222 t.Error(err)
223 }
224 for _, tpl := range []string{
225 `{{indent 101 "a"}}`,
226 `{{nindent 101 "a"}}`,
227 `{{indent 1000000000 "a"}}`,
228 } {
229 if _, err := runRaw(tpl, nil); err == nil {
230 t.Errorf("expected %s to be rejected", tpl)
231 } else if !strings.Contains(err.Error(), "exceeds limit") {
232 t.Errorf("expected limit error for %s, got: %v", tpl, err)
233 }
234 }
235 }
236
237 func TestReplace(t *testing.T) {
238 tpl := `{{"I Am Henry VIII" | replace " " "-"}}`
239 if err := runt(tpl, "I-Am-Henry-VIII"); err != nil {
240 t.Error(err)
241 }
242 }
243
244 func TestPlural(t *testing.T) {
245 tpl := `{{$num := len "two"}}{{$num}} {{$num | plural "1 char" "chars"}}`
246 if err := runt(tpl, "3 chars"); err != nil {
247 t.Error(err)
248 }
249 tpl = `{{len "t" | plural "cheese" "%d chars"}}`
250 if err := runt(tpl, "cheese"); err != nil {
251 t.Error(err)
252 }
253 }