From 2c5d2be1936b3631aaba0246a23ec880cf1e158d Mon Sep 17 00:00:00 2001 From: James Prior Date: Fri, 30 Jun 2023 17:05:35 +0100 Subject: [PATCH 1/3] Don't render `sum` filter results in scientific notation. --- lib/liquid/standardfilters.rb | 3 ++- test/integration/standard_filter_test.rb | 31 ++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/lib/liquid/standardfilters.rb b/lib/liquid/standardfilters.rb index 661235a9d..6f27a87a0 100644 --- a/lib/liquid/standardfilters.rb +++ b/lib/liquid/standardfilters.rb @@ -892,9 +892,10 @@ def sum(input, property = nil) raise_property_error(property) end - InputIterator.new(values_for_sum, context).sum do |item| + result = InputIterator.new(values_for_sum, context).sum do |item| Utils.to_number(item) end + result.is_a?(BigDecimal) ? result.to_f : result end private diff --git a/test/integration/standard_filter_test.rb b/test/integration/standard_filter_test.rb index b2405f7f8..ba2790a5d 100644 --- a/test/integration/standard_filter_test.rb +++ b/test/integration/standard_filter_test.rb @@ -946,6 +946,21 @@ def test_sum_with_numeric_strings 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 input = [1, [2, [3, 4]]] @@ -994,6 +1009,22 @@ def test_sum_with_property_calls_to_liquid_on_property_values assert(t.foo > 0) end + def test_render_sum_of_floats + assert_equal(Liquid::Template.parse('{{ foo | sum }}').render("foo" => [0.1, 0.2, 0.3]), "0.6") + end + + def test_render_sum_of_negative_floats + assert_equal(Liquid::Template.parse('{{ foo | sum }}').render("foo" => [0.1, -0.2, 0.3]), "0.2") + end + + def test_render_sum_negative_float_result + assert_equal(Liquid::Template.parse('{{ foo | sum }}').render("foo" => [0.1, -0.2, -0.3]), "-0.4") + end + + def test_render_sum_float_zero_result + assert_equal(Liquid::Template.parse('{{ foo | sum }}').render("foo" => [0.1, 0.2, -0.3]), "0.0") + end + private def with_timezone(tz) From de6d15a73e651035db49e5ec778b350c3aef5abe Mon Sep 17 00:00:00 2001 From: James Prior Date: Tue, 25 Jul 2023 17:41:42 +0100 Subject: [PATCH 2/3] Test sum floats from properties. --- test/integration/standard_filter_test.rb | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/test/integration/standard_filter_test.rb b/test/integration/standard_filter_test.rb index ba2790a5d..87c0aedf6 100644 --- a/test/integration/standard_filter_test.rb +++ b/test/integration/standard_filter_test.rb @@ -1025,6 +1025,22 @@ def test_render_sum_float_zero_result assert_equal(Liquid::Template.parse('{{ foo | sum }}').render("foo" => [0.1, 0.2, -0.3]), "0.0") end + 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 + + def test_sum_with_floats_and_indexable_map_values + input = [{ "quantity" => 1 }, { "quantity" => 0.2, "weight" => -0.3 }, { "weight" => 0.4 }] + assert_equal(0, @filters.sum(input)) + assert_equal(1.2, @filters.sum(input, "quantity")) + assert_equal(0.1, @filters.sum(input, "weight")) + assert_equal(0, @filters.sum(input, "subtotal")) + end + private def with_timezone(tz) From 75e7725f57342a3a174748bfe37f4623eb3674f6 Mon Sep 17 00:00:00 2001 From: Adam Klingbaum Date: Tue, 8 Aug 2023 16:15:48 +0000 Subject: [PATCH 3/3] Streamline new tests for `sum` filter --- lib/liquid/standardfilters.rb | 1 + test/integration/standard_filter_test.rb | 55 ++++++++++-------------- 2 files changed, 23 insertions(+), 33 deletions(-) diff --git a/lib/liquid/standardfilters.rb b/lib/liquid/standardfilters.rb index 6f27a87a0..0fcd55e58 100644 --- a/lib/liquid/standardfilters.rb +++ b/lib/liquid/standardfilters.rb @@ -895,6 +895,7 @@ def sum(input, property = nil) result = InputIterator.new(values_for_sum, context).sum do |item| Utils.to_number(item) end + result.is_a?(BigDecimal) ? result.to_f : result end diff --git a/test/integration/standard_filter_test.rb b/test/integration/standard_filter_test.rb index 87c0aedf6..e945ad253 100644 --- a/test/integration/standard_filter_test.rb +++ b/test/integration/standard_filter_test.rb @@ -946,21 +946,6 @@ def test_sum_with_numeric_strings 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 input = [1, [2, [3, 4]]] @@ -1009,36 +994,40 @@ def test_sum_with_property_calls_to_liquid_on_property_values assert(t.foo > 0) end - def test_render_sum_of_floats - assert_equal(Liquid::Template.parse('{{ foo | sum }}').render("foo" => [0.1, 0.2, 0.3]), "0.6") - end - - def test_render_sum_of_negative_floats - assert_equal(Liquid::Template.parse('{{ foo | sum }}').render("foo" => [0.1, -0.2, 0.3]), "0.2") + def test_sum_of_floats + input = [0.1, 0.2, 0.3] + assert_equal(0.6, @filters.sum(input)) + assert_template_result("0.6", "{{ input | sum }}", { "input" => input }) end - def test_render_sum_negative_float_result - assert_equal(Liquid::Template.parse('{{ foo | sum }}').render("foo" => [0.1, -0.2, -0.3]), "-0.4") + def test_sum_of_negative_floats + input = [0.1, 0.2, -0.3] + assert_equal(0.0, @filters.sum(input)) + assert_template_result("0.0", "{{ input | sum }}", { "input" => input }) end - def test_render_sum_float_zero_result - assert_equal(Liquid::Template.parse('{{ foo | sum }}').render("foo" => [0.1, 0.2, -0.3]), "0.0") + def test_sum_with_float_strings + input = [0.1, "0.2", "0.3"] + assert_equal(0.6, @filters.sum(input)) + assert_template_result("0.6", "{{ input | sum }}", { "input" => input }) end - 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") + def test_sum_resulting_in_negative_float + input = [0.1, -0.2, -0.3] + assert_equal(-0.4, @filters.sum(input)) + assert_template_result("-0.4", "{{ input | sum }}", { "input" => input }) end def test_sum_with_floats_and_indexable_map_values 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(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 private