Debian Codegraph @

strings_test.go

raw
1 package sprig
2
3 import (
4 "encoding/base32"
5 "encoding/base64"
6 "fmt"
7 "testing"
8 "unicode/utf8"
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]interface{}{"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]interface{}{"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]interface{}{"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]interface{}{"V": []string{"a", "b", "c"}}))
142 assert.NoError(t, runtv(`{{ join "-" .V }}`, "abc", map[string]interface{}{"V": "abc"}))
143 assert.NoError(t, runtv(`{{ join "-" .V }}`, "1-2-3", map[string]interface{}{"V": []int{1, 2, 3}}))
144 assert.NoError(t, runtv(`{{ join "-" .value }}`, "1-2", map[string]interface{}{"value": []interface{}{"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 TestGoutils(t *testing.T) {
193 tests := map[string]string{
194 `{{abbrev 5 "hello world"}}`: "he...",
195 `{{abbrevboth 5 10 "1234 5678 9123"}}`: "...5678...",
196 `{{nospace "h e l l o "}}`: "hello",
197 `{{untitle "First Try"}}`: "first try", //https://youtu.be/44-RsrF_V_w
198 `{{initials "First Try"}}`: "FT",
199 `{{wrap 5 "Hello World"}}`: "Hello\nWorld",
200 `{{wrapWith 5 "\t" "Hello World"}}`: "Hello\tWorld",
201 }
202 for k, v := range tests {
203 t.Log(k)
204 if err := runt(k, v); err != nil {
205 t.Errorf("Error on tpl %q: %s", k, err)
206 }
207 }
208 }
209
210 func TestRandomString(t *testing.T) {
211 // Random strings are now using Masterminds/goutils's cryptographically secure random string functions
212 // by default. Consequently, these tests now have no predictable character sequence. No checks for exact
213 // string output are necessary.
214
215 // {{randAlphaNum 5}} should yield five random characters
216 if x, _ := runRaw(`{{randAlphaNum 5}}`, nil); utf8.RuneCountInString(x) != 5 {
217 t.Errorf("String should be 5 characters; string was %v characters", utf8.RuneCountInString(x))
218 }
219
220 // {{randAlpha 5}} should yield five random characters
221 if x, _ := runRaw(`{{randAlpha 5}}`, nil); utf8.RuneCountInString(x) != 5 {
222 t.Errorf("String should be 5 characters; string was %v characters", utf8.RuneCountInString(x))
223 }
224
225 // {{randAscii 5}} should yield five random characters
226 if x, _ := runRaw(`{{randAscii 5}}`, nil); utf8.RuneCountInString(x) != 5 {
227 t.Errorf("String should be 5 characters; string was %v characters", utf8.RuneCountInString(x))
228 }
229
230 // {{randNumeric 5}} should yield five random characters
231 if x, _ := runRaw(`{{randNumeric 5}}`, nil); utf8.RuneCountInString(x) != 5 {
232 t.Errorf("String should be 5 characters; string was %v characters", utf8.RuneCountInString(x))
233 }
234 }
235
236 func TestCat(t *testing.T) {
237 tpl := `{{$b := "b"}}{{"c" | cat "a" $b}}`
238 if err := runt(tpl, "a b c"); err != nil {
239 t.Error(err)
240 }
241 tpl = `{{ .value | cat "a" "b"}}`
242 values := map[string]interface{}{"value": nil}
243 if err := runtv(tpl, "a b", values); err != nil {
244 t.Error(err)
245 }
246 }
247
248 func TestIndent(t *testing.T) {
249 tpl := `{{indent 4 "a\nb\nc"}}`
250 if err := runt(tpl, " a\n b\n c"); err != nil {
251 t.Error(err)
252 }
253 }
254
255 func TestNindent(t *testing.T) {
256 tpl := `{{nindent 4 "a\nb\nc"}}`
257 if err := runt(tpl, "\n a\n b\n c"); err != nil {
258 t.Error(err)
259 }
260 }
261
262 func TestReplace(t *testing.T) {
263 tpl := `{{"I Am Henry VIII" | replace " " "-"}}`
264 if err := runt(tpl, "I-Am-Henry-VIII"); err != nil {
265 t.Error(err)
266 }
267 }
268
269 func TestPlural(t *testing.T) {
270 tpl := `{{$num := len "two"}}{{$num}} {{$num | plural "1 char" "chars"}}`
271 if err := runt(tpl, "3 chars"); err != nil {
272 t.Error(err)
273 }
274 tpl = `{{len "t" | plural "cheese" "%d chars"}}`
275 if err := runt(tpl, "cheese"); err != nil {
276 t.Error(err)
277 }
278 }