From c59cde9d1700e7e3c9e5e90e74a4bd28d5cf41c7 Mon Sep 17 00:00:00 2001 From: DBA Date: Sun, 22 Aug 2010 19:55:43 +0800 Subject: [PATCH] Code beautifier - standardfilters.rb - standard_filter_test.rb --- lib/liquid/standardfilters.rb | 128 ++++++++++++++++---------------- test/standard_filter_test.rb | 133 +++++++++++++++++----------------- 2 files changed, 130 insertions(+), 131 deletions(-) diff --git a/lib/liquid/standardfilters.rb b/lib/liquid/standardfilters.rb index c9e2f145..26f7c040 100644 --- a/lib/liquid/standardfilters.rb +++ b/lib/liquid/standardfilters.rb @@ -1,40 +1,40 @@ require 'cgi' module Liquid - + module StandardFilters - + # Return the size of an array or of an string def size(input) - + input.respond_to?(:size) ? input.size : 0 - end - + end + # convert a input string to DOWNCASE def downcase(input) input.to_s.downcase - end + end # convert a input string to UPCASE def upcase(input) input.to_s.upcase end - + # capitalize words in the input centence def capitalize(input) input.to_s.capitalize end - + def escape(input) CGI.escapeHTML(input) rescue input end - + def escape_once(input) ActionView::Helpers::TagHelper.escape_once(input) rescue input end - + alias_method :h, :escape - + # Truncate a string down to x characters def truncate(input, length = 50, truncate_string = "...") if input.nil? then return end @@ -48,19 +48,19 @@ module Liquid wordlist = input.to_s.split l = words.to_i - 1 l = 0 if l < 0 - wordlist.length > l ? wordlist[0..l].join(" ") + truncate_string : input + wordlist.length > l ? wordlist[0..l].join(" ") + truncate_string : input end - + def strip_html(input) input.to_s.gsub(//, '').gsub(/<.*?>/, '') - end - - # Remove all newlines from the string - def strip_newlines(input) - input.to_s.gsub(/\n/, '') end - - + + # Remove all newlines from the string + def strip_newlines(input) + input.to_s.gsub(/\n/, '') + end + + # Join elements of the array with certain character between them def join(input, glue = ' ') [input].flatten.join(glue) @@ -77,8 +77,8 @@ module Liquid elsif ary.first.respond_to?(property) ary.sort {|a,b| a.send(property) <=> b.send(property) } end - end - + end + # map/collect on a given property def map(input, property) ary = [input].flatten @@ -88,42 +88,42 @@ module Liquid ary.map {|e| e.send(property) } end end - + # Replace occurrences of a string with another def replace(input, string, replacement = '') input.to_s.gsub(string, replacement) end - + # Replace the first occurrences of a string with another def replace_first(input, string, replacement = '') input.to_s.sub(string, replacement) - end - + end + # remove a substring def remove(input, string) - input.to_s.gsub(string, '') + input.to_s.gsub(string, '') end - + # remove the first occurrences of a substring def remove_first(input, string) - input.to_s.sub(string, '') - end - + input.to_s.sub(string, '') + end + # add one string to another def append(input, string) input.to_s + string.to_s end - + # prepend a string to another def prepend(input, string) string.to_s + input.to_s end - + # Add
tags in front of all newlines in input string - def newline_to_br(input) - input.to_s.gsub(/\n/, "
\n") + def newline_to_br(input) + input.to_s.gsub(/\n/, "
\n") end - + # Reformat a date # # %a - The abbreviated weekday name (``Sun'') @@ -153,74 +153,74 @@ module Liquid # %Z - Time zone name # %% - Literal ``%'' character def date(input, format) - + if format.to_s.empty? return input.to_s end - + date = input.is_a?(String) ? Time.parse(input) : input - + if date.respond_to?(:strftime) date.strftime(format.to_s) else input end - rescue => e + rescue => e input end - - # Get the first element of the passed in array - # + + # Get the first element of the passed in array + # # Example: # {{ product.images | first | to_img }} - # + # def first(array) array.first if array.respond_to?(:first) end - # Get the last element of the passed in array - # + # Get the last element of the passed in array + # # Example: # {{ product.images | last | to_img }} - # + # def last(array) array.last if array.respond_to?(:last) end - + # addition def plus(input, operand) to_number(input) + to_number(operand) end - + # subtraction def minus(input, operand) to_number(input) - to_number(operand) end - + # multiplication def times(input, operand) to_number(input) * to_number(operand) end - + # division def divided_by(input, operand) 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 + + 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 - + Template.register_filter(StandardFilters) end diff --git a/test/standard_filter_test.rb b/test/standard_filter_test.rb index 35226e59..e3169829 100644 --- a/test/standard_filter_test.rb +++ b/test/standard_filter_test.rb @@ -9,148 +9,148 @@ end class StandardFiltersTest < Test::Unit::TestCase include Liquid - + def setup @filters = Filters.new end - + def test_size assert_equal 3, @filters.size([1,2,3]) assert_equal 0, @filters.size([]) assert_equal 0, @filters.size(nil) end - + def test_downcase assert_equal 'testing', @filters.downcase("Testing") assert_equal '', @filters.downcase(nil) end - + def test_upcase assert_equal 'TESTING', @filters.upcase("Testing") assert_equal '', @filters.upcase(nil) end - + def test_upcase assert_equal 'TESTING', @filters.upcase("Testing") assert_equal '', @filters.upcase(nil) end - + def test_truncate assert_equal '1234...', @filters.truncate('1234567890', 7) assert_equal '1234567890', @filters.truncate('1234567890', 20) assert_equal '...', @filters.truncate('1234567890', 0) assert_equal '1234567890', @filters.truncate('1234567890') end - + def test_escape assert_equal '<strong>', @filters.escape('') - assert_equal '<strong>', @filters.h('') + assert_equal '<strong>', @filters.h('') end - + def test_escape_once assert_equal '<strong>', @filters.escape_once(@filters.escape('')) end - + def test_truncatewords assert_equal 'one two three', @filters.truncatewords('one two three', 4) assert_equal 'one two...', @filters.truncatewords('one two three', 2) assert_equal 'one two three', @filters.truncatewords('one two three') - assert_equal 'Two small (13” x 5.5” x 10” high) baskets fit inside one large basket (13”...', @filters.truncatewords('Two small (13” x 5.5” x 10” high) baskets fit inside one large basket (13” x 16” x 10.5” high) with cover.', 15) + assert_equal 'Two small (13” x 5.5” x 10” high) baskets fit inside one large basket (13”...', @filters.truncatewords('Two small (13” x 5.5” x 10” high) baskets fit inside one large basket (13” x 16” x 10.5” high) with cover.', 15) end - + def test_strip_html - assert_equal 'test', @filters.strip_html("
test
") - assert_equal 'test', @filters.strip_html("
test
") - assert_equal '', @filters.strip_html("") - assert_equal '', @filters.strip_html(nil) + assert_equal 'test', @filters.strip_html("
test
") + assert_equal 'test', @filters.strip_html("
test
") + assert_equal '', @filters.strip_html("") + assert_equal '', @filters.strip_html(nil) end - + def test_join - assert_equal '1 2 3 4', @filters.join([1,2,3,4]) - assert_equal '1 - 2 - 3 - 4', @filters.join([1,2,3,4], ' - ') + assert_equal '1 2 3 4', @filters.join([1,2,3,4]) + assert_equal '1 - 2 - 3 - 4', @filters.join([1,2,3,4], ' - ') end - + def test_sort - assert_equal [1,2,3,4], @filters.sort([4,3,2,1]) + assert_equal [1,2,3,4], @filters.sort([4,3,2,1]) assert_equal [{"a" => 1}, {"a" => 2}, {"a" => 3}, {"a" => 4}], @filters.sort([{"a" => 4}, {"a" => 3}, {"a" => 1}, {"a" => 2}], "a") end - + def test_map assert_equal [1,2,3,4], @filters.map([{"a" => 1}, {"a" => 2}, {"a" => 3}, {"a" => 4}], 'a') assert_template_result 'abc', "{{ ary | map:'foo' | map:'bar' }}", 'ary' => [{'foo' => {'bar' => 'a'}}, {'foo' => {'bar' => 'b'}}, {'foo' => {'bar' => 'c'}}] end - + def test_date - assert_equal 'May', @filters.date(Time.parse("2006-05-05 10:00:00"), "%B") - assert_equal 'June', @filters.date(Time.parse("2006-06-05 10:00:00"), "%B") - assert_equal 'July', @filters.date(Time.parse("2006-07-05 10:00:00"), "%B") + assert_equal 'May', @filters.date(Time.parse("2006-05-05 10:00:00"), "%B") + assert_equal 'June', @filters.date(Time.parse("2006-06-05 10:00:00"), "%B") + assert_equal 'July', @filters.date(Time.parse("2006-07-05 10:00:00"), "%B") - assert_equal 'May', @filters.date("2006-05-05 10:00:00", "%B") - assert_equal 'June', @filters.date("2006-06-05 10:00:00", "%B") - assert_equal 'July', @filters.date("2006-07-05 10:00:00", "%B") + assert_equal 'May', @filters.date("2006-05-05 10:00:00", "%B") + assert_equal 'June', @filters.date("2006-06-05 10:00:00", "%B") + assert_equal 'July', @filters.date("2006-07-05 10:00:00", "%B") - assert_equal '2006-07-05 10:00:00', @filters.date("2006-07-05 10:00:00", "") - assert_equal '2006-07-05 10:00:00', @filters.date("2006-07-05 10:00:00", "") - assert_equal '2006-07-05 10:00:00', @filters.date("2006-07-05 10:00:00", "") - assert_equal '2006-07-05 10:00:00', @filters.date("2006-07-05 10:00:00", nil) + assert_equal '2006-07-05 10:00:00', @filters.date("2006-07-05 10:00:00", "") + assert_equal '2006-07-05 10:00:00', @filters.date("2006-07-05 10:00:00", "") + assert_equal '2006-07-05 10:00:00', @filters.date("2006-07-05 10:00:00", "") + assert_equal '2006-07-05 10:00:00', @filters.date("2006-07-05 10:00:00", nil) + + assert_equal '07/05/2006', @filters.date("2006-07-05 10:00:00", "%m/%d/%Y") - assert_equal '07/05/2006', @filters.date("2006-07-05 10:00:00", "%m/%d/%Y") - assert_equal "07/16/2004", @filters.date("Fri Jul 16 01:00:00 2004", "%m/%d/%Y") - - assert_equal nil, @filters.date(nil, "%B") + + assert_equal nil, @filters.date(nil, "%B") end - - + + def test_first_last - assert_equal 1, @filters.first([1,2,3]) - assert_equal 3, @filters.last([1,2,3]) - assert_equal nil, @filters.first([]) - assert_equal nil, @filters.last([]) + assert_equal 1, @filters.first([1,2,3]) + assert_equal 3, @filters.last([1,2,3]) + assert_equal nil, @filters.first([]) + assert_equal nil, @filters.last([]) end - + def test_replace - assert_equal 'b b b b', @filters.replace("a a a a", 'a', 'b') + assert_equal 'b b b b', @filters.replace("a a a a", 'a', 'b') assert_equal 'b a a a', @filters.replace_first("a a a a", 'a', 'b') - assert_template_result 'b a a a', "{{ 'a a a a' | replace_first: 'a', 'b' }}" + assert_template_result 'b a a a', "{{ 'a a a a' | replace_first: 'a', 'b' }}" end - + def test_remove - assert_equal ' ', @filters.remove("a a a a", 'a') - assert_equal 'a a a', @filters.remove_first("a a a a", 'a ') + assert_equal ' ', @filters.remove("a a a a", 'a') + assert_equal 'a a a', @filters.remove_first("a a a a", 'a ') assert_template_result 'a a a', "{{ 'a a a a' | remove_first: 'a ' }}" end - - def test_pipes_in_string_arguments + + def test_pipes_in_string_arguments assert_template_result 'foobar', "{{ 'foo|bar' | remove: '|' }}" end - + def test_strip_newlines assert_template_result 'abc', "{{ source | strip_newlines }}", 'source' => "a\nb\nc" end - + def test_newlines_to_br 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 "2.0", "{{ '1' | plus:'1.0' }}" end - + def test_minus assert_template_result "4", "{{ input | minus:operand }}", 'input' => 5, 'operand' => 1 assert_template_result "2.3", "{{ '4.3' | minus:'2' }}" end - + def test_times assert_template_result "12", "{{ 3 | 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 - + end + def test_divided_by assert_template_result "4", "{{ 12 | divided_by:3 }}" assert_template_result "4", "{{ 14 | divided_by:3 }}" @@ -158,22 +158,21 @@ class StandardFiltersTest < Test::Unit::TestCase assert_template_result "5", "{{ 15 | divided_by:3 }}" assert_template_result "Liquid error: divided by 0", "{{ 5 | divided_by:0 }}" end - + def test_append assigns = {'a' => 'bc', 'b' => 'd' } - assert_template_result('bcd',"{{ a | append: 'd'}}",assigns) - assert_template_result('bcd',"{{ a | append: b}}",assigns) + assert_template_result('bcd',"{{ a | append: 'd'}}",assigns) + assert_template_result('bcd',"{{ a | append: b}}",assigns) end - + def test_prepend assigns = {'a' => 'bc', 'b' => 'a' } - assert_template_result('abc',"{{ a | prepend: 'a'}}",assigns) - assert_template_result('abc',"{{ a | prepend: b}}",assigns) + assert_template_result('abc',"{{ a | prepend: 'a'}}",assigns) + assert_template_result('abc',"{{ a | prepend: b}}",assigns) end - + def test_cannot_access_private_methods assert_template_result('a',"{{ 'a' | to_number }}") end - -end +end