Added filters for basic arithmetic

This commit is contained in:
James MacAulay
2008-10-15 11:02:25 -04:00
parent 3bfde37a53
commit 69bc84b777
2 changed files with 36 additions and 0 deletions
+20
View File
@@ -155,6 +155,26 @@ module Liquid
array.last if array.respond_to?(:last)
end
# addition
def plus(input, operand)
input + operand if input.respond_to?('+')
end
# subtraction
def minus(input, operand)
input - operand if input.respond_to?('-')
end
# multiplication
def times(input, operand)
input * operand if input.respond_to?('*')
end
# division
def divided_by(input, operand)
input / operand if input.respond_to?('/')
end
end
Template.register_filter(StandardFilters)
+16
View File
@@ -118,9 +118,25 @@ class StandardFiltersTest < Test::Unit::TestCase
assert_template_result "a<br />\nb<br />\nc", "{{ source | newline_to_br }}", 'source' => "a\nb\nc"
end
def test_plus
assert_template_result "2", "{{ 1 | plus:1 }}"
assert_template_result "11", "{{ '1' | plus:'1' }}"
end
def test_minus
assert_template_result "4", "{{ input | minus:operand }}", 'input' => 5, 'operand' => 1
end
def test_times
assert_template_result "12", "{{ 3 | times:4 }}"
assert_template_result "foofoofoofoo", "{{ 'foo' | times:4 }}"
end
def test_divided_by
assert_template_result "4", "{{ 12 | divided_by:3 }}"
assert_template_result "4", "{{ 14 | divided_by:3 }}"
assert_template_result "5", "{{ 15 | divided_by:3 }}"
end
end