Debian Codegraph @

util/sprig/numeric_test.go

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