1 package sprig
2
3 import (
4 "fmt"
5 "strconv"
6 "testing"
7
8 "github.com/stretchr/testify/assert"
9 )
10
11 func TestUntil(t *testing.T) {
12 tests := map[string]string{
13 `{{range $i, $e := until 5}}{{$i}}{{$e}}{{end}}`: "0011223344",
14 `{{range $i, $e := until -5}}{{$i}}{{$e}} {{end}}`: "00 1-1 2-2 3-3 4-4 ",
15 }
16 for tpl, expect := range tests {
17 if err := runt(tpl, expect); err != nil {
18 t.Error(err)
19 }
20 }
21 }
22 func TestUntilStep(t *testing.T) {
23 tests := map[string]string{
24 `{{range $i, $e := untilStep 0 5 1}}{{$i}}{{$e}}{{end}}`: "0011223344",
25 `{{range $i, $e := untilStep 3 6 1}}{{$i}}{{$e}}{{end}}`: "031425",
26 `{{range $i, $e := untilStep 0 -10 -2}}{{$i}}{{$e}} {{end}}`: "00 1-2 2-4 3-6 4-8 ",
27 `{{range $i, $e := untilStep 3 0 1}}{{$i}}{{$e}}{{end}}`: "",
28 `{{range $i, $e := untilStep 3 99 0}}{{$i}}{{$e}}{{end}}`: "",
29 `{{range $i, $e := untilStep 3 99 -1}}{{$i}}{{$e}}{{end}}`: "",
30 `{{range $i, $e := untilStep 3 0 0}}{{$i}}{{$e}}{{end}}`: "",
31 }
32 for tpl, expect := range tests {
33 if err := runt(tpl, expect); err != nil {
34 t.Error(err)
35 }
36 }
37
38 }
39 func TestBiggest(t *testing.T) {
40 tpl := `{{ biggest 1 2 3 345 5 6 7}}`
41 if err := runt(tpl, `345`); err != nil {
42 t.Error(err)
43 }
44
45 tpl = `{{ max 345}}`
46 if err := runt(tpl, `345`); err != nil {
47 t.Error(err)
48 }
49 }
50 func TestMaxf(t *testing.T) {
51 tpl := `{{ maxf 1 2 3 345.7 5 6 7}}`
52 if err := runt(tpl, `345.7`); err != nil {
53 t.Error(err)
54 }
55
56 tpl = `{{ max 345 }}`
57 if err := runt(tpl, `345`); err != nil {
58 t.Error(err)
59 }
60 }
61 func TestMin(t *testing.T) {
62 tpl := `{{ min 1 2 3 345 5 6 7}}`
63 if err := runt(tpl, `1`); err != nil {
64 t.Error(err)
65 }
66
67 tpl = `{{ min 345}}`
68 if err := runt(tpl, `345`); err != nil {
69 t.Error(err)
70 }
71 }
72
73 func TestMinf(t *testing.T) {
74 tpl := `{{ minf 1.4 2 3 345.6 5 6 7}}`
75 if err := runt(tpl, `1.4`); err != nil {
76 t.Error(err)
77 }
78
79 tpl = `{{ minf 345 }}`
80 if err := runt(tpl, `345`); err != nil {
81 t.Error(err)
82 }
83 }
84
85 func TestToFloat64(t *testing.T) {
86 target := float64(102)
87 if target != toFloat64(int8(102)) {
88 t.Errorf("Expected 102")
89 }
90 if target != toFloat64(int(102)) {
91 t.Errorf("Expected 102")
92 }
93 if target != toFloat64(int32(102)) {
94 t.Errorf("Expected 102")
95 }
96 if target != toFloat64(int16(102)) {
97 t.Errorf("Expected 102")
98 }
99 if target != toFloat64(int64(102)) {
100 t.Errorf("Expected 102")
101 }
102 if target != toFloat64("102") {
103 t.Errorf("Expected 102")
104 }
105 if 0 != toFloat64("frankie") {
106 t.Errorf("Expected 0")
107 }
108 if target != toFloat64(uint16(102)) {
109 t.Errorf("Expected 102")
110 }
111 if target != toFloat64(uint64(102)) {
112 t.Errorf("Expected 102")
113 }
114 if 102.1234 != toFloat64(float64(102.1234)) {
115 t.Errorf("Expected 102.1234")
116 }
117 if 1 != toFloat64(true) {
118 t.Errorf("Expected 102")
119 }
120 }
121 func TestToInt64(t *testing.T) {
122 target := int64(102)
123 if target != toInt64(int8(102)) {
124 t.Errorf("Expected 102")
125 }
126 if target != toInt64(int(102)) {
127 t.Errorf("Expected 102")
128 }
129 if target != toInt64(int32(102)) {
130 t.Errorf("Expected 102")
131 }
132 if target != toInt64(int16(102)) {
133 t.Errorf("Expected 102")
134 }
135 if target != toInt64(int64(102)) {
136 t.Errorf("Expected 102")
137 }
138 if target != toInt64("102") {
139 t.Errorf("Expected 102")
140 }
141 if 0 != toInt64("frankie") {
142 t.Errorf("Expected 0")
143 }
144 if target != toInt64(uint16(102)) {
145 t.Errorf("Expected 102")
146 }
147 if target != toInt64(uint64(102)) {
148 t.Errorf("Expected 102")
149 }
150 if target != toInt64(float64(102.1234)) {
151 t.Errorf("Expected 102")
152 }
153 if 1 != toInt64(true) {
154 t.Errorf("Expected 102")
155 }
156 }
157
158 func TestToInt(t *testing.T) {
159 target := int(102)
160 if target != toInt(int8(102)) {
161 t.Errorf("Expected 102")
162 }
163 if target != toInt(int(102)) {
164 t.Errorf("Expected 102")
165 }
166 if target != toInt(int32(102)) {
167 t.Errorf("Expected 102")
168 }
169 if target != toInt(int16(102)) {
170 t.Errorf("Expected 102")
171 }
172 if target != toInt(int64(102)) {
173 t.Errorf("Expected 102")
174 }
175 if target != toInt("102") {
176 t.Errorf("Expected 102")
177 }
178 if 0 != toInt("frankie") {
179 t.Errorf("Expected 0")
180 }
181 if target != toInt(uint16(102)) {
182 t.Errorf("Expected 102")
183 }
184 if target != toInt(uint64(102)) {
185 t.Errorf("Expected 102")
186 }
187 if target != toInt(float64(102.1234)) {
188 t.Errorf("Expected 102")
189 }
190 if 1 != toInt(true) {
191 t.Errorf("Expected 102")
192 }
193 }
194
195 func TestToDecimal(t *testing.T) {
196 tests := map[interface{}]int64{
197 "777": 511,
198 777: 511,
199 770: 504,
200 755: 493,
201 }
202
203 for input, expectedResult := range tests {
204 result := toDecimal(input)
205 if result != expectedResult {
206 t.Errorf("Expected %v but got %v", expectedResult, result)
207 }
208 }
209 }
210
211 func TestAdd1(t *testing.T) {
212 tpl := `{{ 3 | add1 }}`
213 if err := runt(tpl, `4`); err != nil {
214 t.Error(err)
215 }
216 }
217
218 func TestAdd(t *testing.T) {
219 tpl := `{{ 3 | add 1 2}}`
220 if err := runt(tpl, `6`); err != nil {
221 t.Error(err)
222 }
223 }
224
225 func TestDiv(t *testing.T) {
226 tpl := `{{ 4 | div 5 }}`
227 if err := runt(tpl, `1`); err != nil {
228 t.Error(err)
229 }
230 }
231
232 func TestMul(t *testing.T) {
233 tpl := `{{ 1 | mul "2" 3 "4"}}`
234 if err := runt(tpl, `24`); err != nil {
235 t.Error(err)
236 }
237 }
238
239 func TestSub(t *testing.T) {
240 tpl := `{{ 3 | sub 14 }}`
241 if err := runt(tpl, `11`); err != nil {
242 t.Error(err)
243 }
244 }
245
246 func TestCeil(t *testing.T) {
247 assert.Equal(t, 123.0, ceil(123))
248 assert.Equal(t, 123.0, ceil("123"))
249 assert.Equal(t, 124.0, ceil(123.01))
250 assert.Equal(t, 124.0, ceil("123.01"))
251 }
252
253 func TestFloor(t *testing.T) {
254 assert.Equal(t, 123.0, floor(123))
255 assert.Equal(t, 123.0, floor("123"))
256 assert.Equal(t, 123.0, floor(123.9999))
257 assert.Equal(t, 123.0, floor("123.9999"))
258 }
259
260 func TestRound(t *testing.T) {
261 assert.Equal(t, 123.556, round(123.5555, 3))
262 assert.Equal(t, 123.556, round("123.55555", 3))
263 assert.Equal(t, 124.0, round(123.500001, 0))
264 assert.Equal(t, 123.0, round(123.49999999, 0))
265 assert.Equal(t, 123.23, round(123.2329999, 2, .3))
266 assert.Equal(t, 123.24, round(123.233, 2, .3))
267 }
268
269 func TestRandomInt(t *testing.T) {
270 var tests = []struct {
271 min int
272 max int
273 }{
274 {10, 11},
275 {10, 13},
276 {0, 1},
277 {5, 50},
278 }
279 for _, v := range tests {
280 x, _ := runRaw(fmt.Sprintf(`{{ randInt %d %d }}`, v.min, v.max), nil)
281 r, err := strconv.Atoi(x)
282 assert.NoError(t, err)
283 assert.True(t, func(min, max, r int) bool {
284 return r >= v.min && r < v.max
285 }(v.min, v.max, r))
286 }
287 }
288
289 func TestSeq(t *testing.T) {
290 tests := map[string]string{
291 `{{seq 0 1 3}}`: "0 1 2 3",
292 `{{seq 0 3 10}}`: "0 3 6 9",
293 `{{seq 3 3 2}}`: "",
294 `{{seq 3 -3 2}}`: "3",
295 `{{seq}}`: "",
296 `{{seq 0 4}}`: "0 1 2 3 4",
297 `{{seq 5}}`: "1 2 3 4 5",
298 `{{seq -5}}`: "1 0 -1 -2 -3 -4 -5",
299 `{{seq 0}}`: "1 0",
300 `{{seq 0 1 2 3}}`: "",
301 `{{seq 0 -4}}`: "0 -1 -2 -3 -4",
302 }
303 for tpl, expect := range tests {
304 if err := runt(tpl, expect); err != nil {
305 t.Error(err)
306 }
307 }
308 }
309
View as plain text