| 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 0 != toFloat64("frankie") { |
| 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 102.1234 != toFloat64(float64(102.1234)) { |
| 114 |
t.Errorf("Expected 102.1234") |
| 115 |
} |
| 116 |
if 1 != toFloat64(true) { |
| 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 0 != toInt64("frankie") { |
| 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 1 != toInt64(true) { |
| 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 0 != toInt("frankie") { |
| 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 1 != toInt(true) { |
| 190 |
t.Errorf("Expected 102") |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
func TestToDecimal(t *testing.T) { |
| 195 |
tests := map[interface{}]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 TestAdd1f(t *testing.T) { |
| 218 |
tpl := `{{ 3.4 | add1f }}` |
| 219 |
if err := runt(tpl, `4.4`); err != nil { |
| 220 |
t.Error(err) |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
func TestAdd(t *testing.T) { |
| 225 |
tpl := `{{ 3 | add 1 2}}` |
| 226 |
if err := runt(tpl, `6`); err != nil { |
| 227 |
t.Error(err) |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
func TestAddf(t *testing.T) { |
| 232 |
tpl := `{{ 3 | addf 1.5 2.2}}` |
| 233 |
if err := runt(tpl, `6.7`); err != nil { |
| 234 |
t.Error(err) |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
func TestDiv(t *testing.T) { |
| 239 |
tpl := `{{ 4 | div 5 }}` |
| 240 |
if err := runt(tpl, `1`); err != nil { |
| 241 |
t.Error(err) |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
func TestDivf(t *testing.T) { |
| 246 |
tpl := `{{ 2 | divf 5 4 }}` |
| 247 |
if err := runt(tpl, `0.625`); err != nil { |
| 248 |
t.Error(err) |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
func TestMul(t *testing.T) { |
| 253 |
tpl := `{{ 1 | mul "2" 3 "4"}}` |
| 254 |
if err := runt(tpl, `24`); err != nil { |
| 255 |
t.Error(err) |
| 256 |
} |
| 257 |
} |
| 258 |
|
| 259 |
func TestMulf(t *testing.T) { |
| 260 |
tpl := `{{ 1.2 | mulf "2.4" 10 "4"}}` |
| 261 |
if err := runt(tpl, `115.2`); err != nil { |
| 262 |
t.Error(err) |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
func TestSub(t *testing.T) { |
| 267 |
tpl := `{{ 3 | sub 14 }}` |
| 268 |
if err := runt(tpl, `11`); err != nil { |
| 269 |
t.Error(err) |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
func TestSubf(t *testing.T) { |
| 274 |
tpl := `{{ 3 | subf 4.5 1 }}` |
| 275 |
if err := runt(tpl, `0.5`); err != nil { |
| 276 |
t.Error(err) |
| 277 |
} |
| 278 |
} |
| 279 |
|
| 280 |
func TestCeil(t *testing.T) { |
| 281 |
assert.Equal(t, 123.0, ceil(123)) |
| 282 |
assert.Equal(t, 123.0, ceil("123")) |
| 283 |
assert.Equal(t, 124.0, ceil(123.01)) |
| 284 |
assert.Equal(t, 124.0, ceil("123.01")) |
| 285 |
} |
| 286 |
|
| 287 |
func TestFloor(t *testing.T) { |
| 288 |
assert.Equal(t, 123.0, floor(123)) |
| 289 |
assert.Equal(t, 123.0, floor("123")) |
| 290 |
assert.Equal(t, 123.0, floor(123.9999)) |
| 291 |
assert.Equal(t, 123.0, floor("123.9999")) |
| 292 |
} |
| 293 |
|
| 294 |
func TestRound(t *testing.T) { |
| •295 |
assert.InDelta(t, 123.556, round(123.5555, 3), 0.001) |
| 296 |
assert.Equal(t, 123.556, round("123.55555", 3)) |
| 297 |
assert.Equal(t, 124.0, round(123.500001, 0)) |
| 298 |
assert.Equal(t, 123.0, round(123.49999999, 0)) |
| 299 |
assert.Equal(t, 123.23, round(123.2329999, 2, .3)) |
| 300 |
assert.Equal(t, 123.24, round(123.233, 2, .3)) |
| 301 |
} |
| 302 |
|
| 303 |
func TestRandomInt(t *testing.T) { |
| 304 |
var tests = []struct { |
| 305 |
min int |
| 306 |
max int |
| 307 |
}{ |
| 308 |
{10, 11}, |
| 309 |
{10, 13}, |
| 310 |
{0, 1}, |
| 311 |
{5, 50}, |
| 312 |
} |
| 313 |
for _, v := range tests { |
| 314 |
x, _ := runRaw(fmt.Sprintf(`{{ randInt %d %d }}`, v.min, v.max), nil) |
| 315 |
r, err := strconv.Atoi(x) |
| 316 |
assert.NoError(t, err) |
| 317 |
assert.True(t, func(min, max, r int) bool { |
| 318 |
return r >= v.min && r < v.max |
| 319 |
}(v.min, v.max, r)) |
| 320 |
} |
| 321 |
} |
| 322 |
|
| 323 |
func TestSeq(t *testing.T) { |
| 324 |
tests := map[string]string{ |
| 325 |
`{{seq 0 1 3}}`: "0 1 2 3", |
| 326 |
`{{seq 0 3 10}}`: "0 3 6 9", |
| 327 |
`{{seq 3 3 2}}`: "", |
| 328 |
`{{seq 3 -3 2}}`: "3", |
| 329 |
`{{seq}}`: "", |
| 330 |
`{{seq 0 4}}`: "0 1 2 3 4", |
| 331 |
`{{seq 5}}`: "1 2 3 4 5", |
| 332 |
`{{seq -5}}`: "1 0 -1 -2 -3 -4 -5", |
| 333 |
`{{seq 0}}`: "1 0", |
| 334 |
`{{seq 0 1 2 3}}`: "", |
| 335 |
`{{seq 0 -4}}`: "0 -1 -2 -3 -4", |
| 336 |
} |
| 337 |
for tpl, expect := range tests { |
| 338 |
if err := runt(tpl, expect); err != nil { |
| 339 |
t.Error(err) |
| 340 |
} |
| 341 |
} |
| 342 |
} |