From 69bc84b7774f7404067c1d553535f61a462d7313 Mon Sep 17 00:00:00 2001 From: James MacAulay Date: Mon, 12 May 2008 17:11:42 -0400 Subject: [PATCH] Added filters for basic arithmetic --- lib/liquid/standardfilters.rb | 20 ++++++++++++++++++++ test/standard_filter_test.rb | 16 ++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/lib/liquid/standardfilters.rb b/lib/liquid/standardfilters.rb index 99a7078b..8aad420f 100644 --- a/lib/liquid/standardfilters.rb +++ b/lib/liquid/standardfilters.rb @@ -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) diff --git a/test/standard_filter_test.rb b/test/standard_filter_test.rb index 06106c37..2765fc45 100644 --- a/test/standard_filter_test.rb +++ b/test/standard_filter_test.rb @@ -118,9 +118,25 @@ class StandardFiltersTest < Test::Unit::TestCase assert_template_result "a
\nb
\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