mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-24 21:56:05 +02:00
tpl: Add math.Ceil, Floor, and Round
Ceil and Floor are frontends for the stdlib math functions. The Round implementation is essentially the same thing except that the Go stdlib doesn't include a Round implementation in a stable release yet. I've included the Round function slated for Go 1.10. Fixes #3883
This commit is contained in:
committed by
Bjørn Erik Pedersen
parent
80c7ea60a0
commit
19c5910485
@@ -143,6 +143,72 @@ func TestDoArithmetic(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestCeil(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
ns := New()
|
||||
|
||||
for i, test := range []struct {
|
||||
x interface{}
|
||||
expect interface{}
|
||||
}{
|
||||
{0.1, 1.0},
|
||||
{0.5, 1.0},
|
||||
{1.1, 2.0},
|
||||
{1.5, 2.0},
|
||||
{-0.1, 0.0},
|
||||
{-0.5, 0.0},
|
||||
{-1.1, -1.0},
|
||||
{-1.5, -1.0},
|
||||
{"abc", false},
|
||||
} {
|
||||
errMsg := fmt.Sprintf("[%d] %v", i, test)
|
||||
|
||||
result, err := ns.Ceil(test.x)
|
||||
|
||||
if b, ok := test.expect.(bool); ok && !b {
|
||||
require.Error(t, err, errMsg)
|
||||
continue
|
||||
}
|
||||
|
||||
require.NoError(t, err, errMsg)
|
||||
assert.Equal(t, test.expect, result, errMsg)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFloor(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
ns := New()
|
||||
|
||||
for i, test := range []struct {
|
||||
x interface{}
|
||||
expect interface{}
|
||||
}{
|
||||
{0.1, 0.0},
|
||||
{0.5, 0.0},
|
||||
{1.1, 1.0},
|
||||
{1.5, 1.0},
|
||||
{-0.1, -1.0},
|
||||
{-0.5, -1.0},
|
||||
{-1.1, -2.0},
|
||||
{-1.5, -2.0},
|
||||
{"abc", false},
|
||||
} {
|
||||
errMsg := fmt.Sprintf("[%d] %v", i, test)
|
||||
|
||||
result, err := ns.Floor(test.x)
|
||||
|
||||
if b, ok := test.expect.(bool); ok && !b {
|
||||
require.Error(t, err, errMsg)
|
||||
continue
|
||||
}
|
||||
|
||||
require.NoError(t, err, errMsg)
|
||||
assert.Equal(t, test.expect, result, errMsg)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLog(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
@@ -255,3 +321,36 @@ func TestModBool(t *testing.T) {
|
||||
assert.Equal(t, test.expect, result, errMsg)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRound(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
ns := New()
|
||||
|
||||
for i, test := range []struct {
|
||||
x interface{}
|
||||
expect interface{}
|
||||
}{
|
||||
{0.1, 0.0},
|
||||
{0.5, 1.0},
|
||||
{1.1, 1.0},
|
||||
{1.5, 2.0},
|
||||
{-0.1, -0.0},
|
||||
{-0.5, -1.0},
|
||||
{-1.1, -1.0},
|
||||
{-1.5, -2.0},
|
||||
{"abc", false},
|
||||
} {
|
||||
errMsg := fmt.Sprintf("[%d] %v", i, test)
|
||||
|
||||
result, err := ns.Round(test.x)
|
||||
|
||||
if b, ok := test.expect.(bool); ok && !b {
|
||||
require.Error(t, err, errMsg)
|
||||
continue
|
||||
}
|
||||
|
||||
require.NoError(t, err, errMsg)
|
||||
assert.Equal(t, test.expect, result, errMsg)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user