mirror of
https://github.com/danog/liquid.git
synced 2024-11-30 06:28:58 +01:00
Merge pull request #33 from Eun/master
floor and ceil should return integers
This commit is contained in:
commit
26998e1688
@ -75,8 +75,12 @@ func AddStandardFilters(fd FilterDictionary) { // nolint: gocyclo
|
|||||||
|
|
||||||
// number filters
|
// number filters
|
||||||
fd.AddFilter("abs", math.Abs)
|
fd.AddFilter("abs", math.Abs)
|
||||||
fd.AddFilter("ceil", math.Ceil)
|
fd.AddFilter("ceil", func(a float64) int {
|
||||||
fd.AddFilter("floor", math.Floor)
|
return int(math.Ceil(a))
|
||||||
|
})
|
||||||
|
fd.AddFilter("floor", func(a float64) int {
|
||||||
|
return int(math.Floor(a))
|
||||||
|
})
|
||||||
fd.AddFilter("modulo", math.Mod)
|
fd.AddFilter("modulo", math.Mod)
|
||||||
fd.AddFilter("minus", func(a, b float64) float64 {
|
fd.AddFilter("minus", func(a, b float64) float64 {
|
||||||
return a - b
|
return a - b
|
||||||
|
@ -143,8 +143,8 @@ var filterTests = []struct {
|
|||||||
{`"Tetsuro Takara" | url_encode`, "Tetsuro+Takara"},
|
{`"Tetsuro Takara" | url_encode`, "Tetsuro+Takara"},
|
||||||
|
|
||||||
// number filters
|
// number filters
|
||||||
{`-17 | abs`, 17},
|
{`-17 | abs`, 17.0},
|
||||||
{`4 | abs`, 4},
|
{`4 | abs`, 4.0},
|
||||||
{`"-19.86" | abs`, 19.86},
|
{`"-19.86" | abs`, 19.86},
|
||||||
|
|
||||||
{`1.2 | ceil`, 2},
|
{`1.2 | ceil`, 2},
|
||||||
@ -156,19 +156,19 @@ var filterTests = []struct {
|
|||||||
{`2.0 | floor`, 2},
|
{`2.0 | floor`, 2},
|
||||||
{`183.357 | floor`, 183},
|
{`183.357 | floor`, 183},
|
||||||
|
|
||||||
{`4 | plus: 2`, 6},
|
{`4 | plus: 2`, 6.0},
|
||||||
{`183.357 | plus: 12`, 195.357},
|
{`183.357 | plus: 12`, 195.357},
|
||||||
|
|
||||||
{`4 | minus: 2`, 2},
|
{`4 | minus: 2`, 2.0},
|
||||||
{`16 | minus: 4`, 12},
|
{`16 | minus: 4`, 12.0},
|
||||||
{`183.357 | minus: 12`, 171.357},
|
{`183.357 | minus: 12`, 171.357},
|
||||||
|
|
||||||
{`3 | times: 2`, 6},
|
{`3 | times: 2`, 6.0},
|
||||||
{`24 | times: 7`, 168},
|
{`24 | times: 7`, 168.0},
|
||||||
{`183.357 | times: 12`, 2200.284},
|
{`183.357 | times: 12`, 2200.284},
|
||||||
|
|
||||||
{`3 | modulo: 2`, 1},
|
{`3 | modulo: 2`, 1.0},
|
||||||
{`24 | modulo: 7`, 3},
|
{`24 | modulo: 7`, 3.0},
|
||||||
// {`183.357 | modulo: 12 | `, 3.357}, // TODO test suit use inexact
|
// {`183.357 | modulo: 12 | `, 3.357}, // TODO test suit use inexact
|
||||||
|
|
||||||
{`16 | divided_by: 4`, 4},
|
{`16 | divided_by: 4`, 4},
|
||||||
@ -177,8 +177,8 @@ var filterTests = []struct {
|
|||||||
{`20 | divided_by: 7.0`, 2.857142857142857},
|
{`20 | divided_by: 7.0`, 2.857142857142857},
|
||||||
{`20 | divided_by: 's'`, nil},
|
{`20 | divided_by: 's'`, nil},
|
||||||
|
|
||||||
{`1.2 | round`, 1},
|
{`1.2 | round`, 1.0},
|
||||||
{`2.7 | round`, 3},
|
{`2.7 | round`, 3.0},
|
||||||
{`183.357 | round: 2`, 183.36},
|
{`183.357 | round: 2`, 183.36},
|
||||||
|
|
||||||
// Jekyll extensions; added here for convenient testing
|
// Jekyll extensions; added here for convenient testing
|
||||||
@ -256,16 +256,7 @@ func TestFilters(t *testing.T) {
|
|||||||
t.Run(fmt.Sprintf("%02d", i+1), func(t *testing.T) {
|
t.Run(fmt.Sprintf("%02d", i+1), func(t *testing.T) {
|
||||||
actual, err := expressions.EvaluateString(test.in, context)
|
actual, err := expressions.EvaluateString(test.in, context)
|
||||||
require.NoErrorf(t, err, test.in)
|
require.NoErrorf(t, err, test.in)
|
||||||
expected := test.expected
|
require.Equalf(t, test.expected, actual, test.in)
|
||||||
switch v := actual.(type) {
|
|
||||||
case int:
|
|
||||||
actual = float64(v)
|
|
||||||
}
|
|
||||||
switch ex := expected.(type) {
|
|
||||||
case int:
|
|
||||||
expected = float64(ex)
|
|
||||||
}
|
|
||||||
require.Equalf(t, expected, actual, test.in)
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user