mirror of
https://github.com/Shopify/liquid.git
synced 2026-10-03 09:05:13 -07:00
Streamline new tests for sum filter
This commit is contained in:
@@ -895,6 +895,7 @@ module Liquid
|
|||||||
result = InputIterator.new(values_for_sum, context).sum do |item|
|
result = InputIterator.new(values_for_sum, context).sum do |item|
|
||||||
Utils.to_number(item)
|
Utils.to_number(item)
|
||||||
end
|
end
|
||||||
|
|
||||||
result.is_a?(BigDecimal) ? result.to_f : result
|
result.is_a?(BigDecimal) ? result.to_f : result
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -946,21 +946,6 @@ class StandardFiltersTest < Minitest::Test
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_sum_with_floats
|
|
||||||
input = [0.1, 0.2, 0.3]
|
|
||||||
assert_equal(0.6, @filters.sum(input))
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_sum_with_negative_float
|
|
||||||
input = [0.1, 0.2, -0.3]
|
|
||||||
assert_equal(0.0, @filters.sum(input))
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_sum_with_float_strings
|
|
||||||
input = [0.1, "0.2", "0.3"]
|
|
||||||
assert_equal(0.6, @filters.sum(input))
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_sum_with_nested_arrays
|
def test_sum_with_nested_arrays
|
||||||
input = [1, [2, [3, 4]]]
|
input = [1, [2, [3, 4]]]
|
||||||
|
|
||||||
@@ -1009,36 +994,40 @@ class StandardFiltersTest < Minitest::Test
|
|||||||
assert(t.foo > 0)
|
assert(t.foo > 0)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_render_sum_of_floats
|
def test_sum_of_floats
|
||||||
assert_equal(Liquid::Template.parse('{{ foo | sum }}').render("foo" => [0.1, 0.2, 0.3]), "0.6")
|
input = [0.1, 0.2, 0.3]
|
||||||
|
assert_equal(0.6, @filters.sum(input))
|
||||||
|
assert_template_result("0.6", "{{ input | sum }}", { "input" => input })
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_render_sum_of_negative_floats
|
def test_sum_of_negative_floats
|
||||||
assert_equal(Liquid::Template.parse('{{ foo | sum }}').render("foo" => [0.1, -0.2, 0.3]), "0.2")
|
input = [0.1, 0.2, -0.3]
|
||||||
|
assert_equal(0.0, @filters.sum(input))
|
||||||
|
assert_template_result("0.0", "{{ input | sum }}", { "input" => input })
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_render_sum_negative_float_result
|
def test_sum_with_float_strings
|
||||||
assert_equal(Liquid::Template.parse('{{ foo | sum }}').render("foo" => [0.1, -0.2, -0.3]), "-0.4")
|
input = [0.1, "0.2", "0.3"]
|
||||||
|
assert_equal(0.6, @filters.sum(input))
|
||||||
|
assert_template_result("0.6", "{{ input | sum }}", { "input" => input })
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_render_sum_float_zero_result
|
def test_sum_resulting_in_negative_float
|
||||||
assert_equal(Liquid::Template.parse('{{ foo | sum }}').render("foo" => [0.1, 0.2, -0.3]), "0.0")
|
input = [0.1, -0.2, -0.3]
|
||||||
end
|
assert_equal(-0.4, @filters.sum(input))
|
||||||
|
assert_template_result("-0.4", "{{ input | sum }}", { "input" => input })
|
||||||
def test_render_sum_with_floats_and_indexable_map_values
|
|
||||||
input = [{ "quantity" => 1 }, { "quantity" => 0.2, "weight" => -0.3 }, { "weight" => 0.4 }]
|
|
||||||
assert_equal(Liquid::Template.parse('{{ foo | sum }}').render("foo" => input), "0")
|
|
||||||
assert_equal(Liquid::Template.parse('{{ foo | sum: "quantity" }}').render("foo" => input), "1.2")
|
|
||||||
assert_equal(Liquid::Template.parse('{{ foo | sum: "weight" }}').render("foo" => input), "0.1")
|
|
||||||
assert_equal(Liquid::Template.parse('{{ foo | sum: "subtotal" }}').render("foo" => input), "0")
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_sum_with_floats_and_indexable_map_values
|
def test_sum_with_floats_and_indexable_map_values
|
||||||
input = [{ "quantity" => 1 }, { "quantity" => 0.2, "weight" => -0.3 }, { "weight" => 0.4 }]
|
input = [{ "quantity" => 1 }, { "quantity" => 0.2, "weight" => -0.3 }, { "weight" => 0.4 }]
|
||||||
assert_equal(0, @filters.sum(input))
|
assert_equal(0.0, @filters.sum(input))
|
||||||
assert_equal(1.2, @filters.sum(input, "quantity"))
|
assert_equal(1.2, @filters.sum(input, "quantity"))
|
||||||
assert_equal(0.1, @filters.sum(input, "weight"))
|
assert_equal(0.1, @filters.sum(input, "weight"))
|
||||||
assert_equal(0, @filters.sum(input, "subtotal"))
|
assert_equal(0.0, @filters.sum(input, "subtotal"))
|
||||||
|
assert_template_result("0", "{{ input | sum }}", { "input" => input })
|
||||||
|
assert_template_result("1.2", "{{ input | sum: 'quantity' }}", { "input" => input })
|
||||||
|
assert_template_result("0.1", "{{ input | sum: 'weight' }}", { "input" => input })
|
||||||
|
assert_template_result("0", "{{ input | sum: 'subtotal' }}", { "input" => input })
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|||||||
Reference in New Issue
Block a user