github.com/fighterlyt/hugo@v0.47.1/common/math/math_test.go (about) 1 // Copyright 2018 The Hugo Authors. All rights reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // http://www.apache.org/licenses/LICENSE-2.0 7 // 8 // Unless required by applicable law or agreed to in writing, software 9 // distributed under the License is distributed on an "AS IS" BASIS, 10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package math 15 16 import ( 17 "fmt" 18 "testing" 19 20 "github.com/alecthomas/assert" 21 "github.com/stretchr/testify/require" 22 ) 23 24 func TestDoArithmetic(t *testing.T) { 25 t.Parallel() 26 27 for i, test := range []struct { 28 a interface{} 29 b interface{} 30 op rune 31 expect interface{} 32 }{ 33 {3, 2, '+', int64(5)}, 34 {3, 2, '-', int64(1)}, 35 {3, 2, '*', int64(6)}, 36 {3, 2, '/', int64(1)}, 37 {3.0, 2, '+', float64(5)}, 38 {3.0, 2, '-', float64(1)}, 39 {3.0, 2, '*', float64(6)}, 40 {3.0, 2, '/', float64(1.5)}, 41 {3, 2.0, '+', float64(5)}, 42 {3, 2.0, '-', float64(1)}, 43 {3, 2.0, '*', float64(6)}, 44 {3, 2.0, '/', float64(1.5)}, 45 {3.0, 2.0, '+', float64(5)}, 46 {3.0, 2.0, '-', float64(1)}, 47 {3.0, 2.0, '*', float64(6)}, 48 {3.0, 2.0, '/', float64(1.5)}, 49 {uint(3), uint(2), '+', uint64(5)}, 50 {uint(3), uint(2), '-', uint64(1)}, 51 {uint(3), uint(2), '*', uint64(6)}, 52 {uint(3), uint(2), '/', uint64(1)}, 53 {uint(3), 2, '+', uint64(5)}, 54 {uint(3), 2, '-', uint64(1)}, 55 {uint(3), 2, '*', uint64(6)}, 56 {uint(3), 2, '/', uint64(1)}, 57 {3, uint(2), '+', uint64(5)}, 58 {3, uint(2), '-', uint64(1)}, 59 {3, uint(2), '*', uint64(6)}, 60 {3, uint(2), '/', uint64(1)}, 61 {uint(3), -2, '+', int64(1)}, 62 {uint(3), -2, '-', int64(5)}, 63 {uint(3), -2, '*', int64(-6)}, 64 {uint(3), -2, '/', int64(-1)}, 65 {-3, uint(2), '+', int64(-1)}, 66 {-3, uint(2), '-', int64(-5)}, 67 {-3, uint(2), '*', int64(-6)}, 68 {-3, uint(2), '/', int64(-1)}, 69 {uint(3), 2.0, '+', float64(5)}, 70 {uint(3), 2.0, '-', float64(1)}, 71 {uint(3), 2.0, '*', float64(6)}, 72 {uint(3), 2.0, '/', float64(1.5)}, 73 {3.0, uint(2), '+', float64(5)}, 74 {3.0, uint(2), '-', float64(1)}, 75 {3.0, uint(2), '*', float64(6)}, 76 {3.0, uint(2), '/', float64(1.5)}, 77 {0, 0, '+', 0}, 78 {0, 0, '-', 0}, 79 {0, 0, '*', 0}, 80 {"foo", "bar", '+', "foobar"}, 81 {3, 0, '/', false}, 82 {3.0, 0, '/', false}, 83 {3, 0.0, '/', false}, 84 {uint(3), uint(0), '/', false}, 85 {3, uint(0), '/', false}, 86 {-3, uint(0), '/', false}, 87 {uint(3), 0, '/', false}, 88 {3.0, uint(0), '/', false}, 89 {uint(3), 0.0, '/', false}, 90 {3, "foo", '+', false}, 91 {3.0, "foo", '+', false}, 92 {uint(3), "foo", '+', false}, 93 {"foo", 3, '+', false}, 94 {"foo", "bar", '-', false}, 95 {3, 2, '%', false}, 96 } { 97 errMsg := fmt.Sprintf("[%d] %v", i, test) 98 99 result, err := DoArithmetic(test.a, test.b, test.op) 100 101 if b, ok := test.expect.(bool); ok && !b { 102 require.Error(t, err, errMsg) 103 continue 104 } 105 106 require.NoError(t, err, errMsg) 107 assert.Equal(t, test.expect, result, errMsg) 108 } 109 }