mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-15 08:50:45 -07:00
Change behaviour of arithmetic filters to cast arguments to numbers
This commit is contained in:
@@ -185,22 +185,35 @@ module Liquid
|
|||||||
|
|
||||||
# addition
|
# addition
|
||||||
def plus(input, operand)
|
def plus(input, operand)
|
||||||
input + operand if input.respond_to?('+')
|
to_number(input) + to_number(operand)
|
||||||
end
|
end
|
||||||
|
|
||||||
# subtraction
|
# subtraction
|
||||||
def minus(input, operand)
|
def minus(input, operand)
|
||||||
input - operand if input.respond_to?('-')
|
to_number(input) - to_number(operand)
|
||||||
end
|
end
|
||||||
|
|
||||||
# multiplication
|
# multiplication
|
||||||
def times(input, operand)
|
def times(input, operand)
|
||||||
input * operand if input.respond_to?('*')
|
to_number(input) * to_number(operand)
|
||||||
end
|
end
|
||||||
|
|
||||||
# division
|
# division
|
||||||
def divided_by(input, operand)
|
def divided_by(input, operand)
|
||||||
input / operand if input.respond_to?('/')
|
to_number(input) / to_number(operand)
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def to_number(obj)
|
||||||
|
case obj
|
||||||
|
when Numeric
|
||||||
|
obj
|
||||||
|
when String
|
||||||
|
(obj.strip =~ /^\d+\.\d+$/) ? obj.to_f : obj.to_i
|
||||||
|
else
|
||||||
|
0
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -131,16 +131,27 @@ class StandardFiltersTest < Test::Unit::TestCase
|
|||||||
|
|
||||||
def test_plus
|
def test_plus
|
||||||
assert_template_result "2", "{{ 1 | plus:1 }}"
|
assert_template_result "2", "{{ 1 | plus:1 }}"
|
||||||
assert_template_result "11", "{{ '1' | plus:'1' }}"
|
assert_template_result "2.0", "{{ '1' | plus:'1.0' }}"
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_minus
|
def test_minus
|
||||||
assert_template_result "4", "{{ input | minus:operand }}", 'input' => 5, 'operand' => 1
|
assert_template_result "4", "{{ input | minus:operand }}", 'input' => 5, 'operand' => 1
|
||||||
|
assert_template_result "2.3", "{{ '4.3' | minus:'2' }}"
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_times
|
def test_times
|
||||||
assert_template_result "12", "{{ 3 | times:4 }}"
|
assert_template_result "12", "{{ 3 | times:4 }}"
|
||||||
assert_template_result "foofoofoofoo", "{{ 'foo' | times:4 }}"
|
assert_template_result "0", "{{ 'foo' | times:4 }}"
|
||||||
|
assert_template_result "6.3", "{{ '2.1' | times:3 }}"
|
||||||
|
assert_template_result "6", "{{ '2.1' | times:3 | replace: '.','-' | plus:0}}"
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_divided_by
|
||||||
|
assert_template_result "4", "{{ 12 | divided_by:3 }}"
|
||||||
|
assert_template_result "4", "{{ 14 | divided_by:3 }}"
|
||||||
|
assert_template_result "4.66666666666667", "{{ 14 | divided_by:'3.0' }}"
|
||||||
|
assert_template_result "5", "{{ 15 | divided_by:3 }}"
|
||||||
|
assert_template_result "Liquid error: divided by 0", "{{ 5 | divided_by:0 }}"
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_append
|
def test_append
|
||||||
@@ -155,10 +166,8 @@ class StandardFiltersTest < Test::Unit::TestCase
|
|||||||
assert_template_result('abc',"{{ a | prepend: b}}",assigns)
|
assert_template_result('abc',"{{ a | prepend: b}}",assigns)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_divided_by
|
def test_cannot_access_private_methods
|
||||||
assert_template_result "4", "{{ 12 | divided_by:3 }}"
|
assert_template_result('a',"{{ 'a' | to_number }}")
|
||||||
assert_template_result "4", "{{ 14 | divided_by:3 }}"
|
|
||||||
assert_template_result "5", "{{ 15 | divided_by:3 }}"
|
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user