From 0b45ffeadabb6728ef98d772a5e2bbf85464b4e2 Mon Sep 17 00:00:00 2001 From: Florian Weingarten Date: Thu, 24 Jul 2014 00:33:39 +0000 Subject: [PATCH] add more legacy tests --- lib/liquid/standardfilters.rb | 31 +++++++++++++++--------- test/integration/standard_filter_test.rb | 14 +++++++++++ 2 files changed, 33 insertions(+), 12 deletions(-) diff --git a/lib/liquid/standardfilters.rb b/lib/liquid/standardfilters.rb index 5818acc..7dd75d6 100644 --- a/lib/liquid/standardfilters.rb +++ b/lib/liquid/standardfilters.rb @@ -92,7 +92,7 @@ module Liquid # Join elements of the array with certain character between them def join(input, glue = ' '.freeze) - Array(input).join(glue) + InputIterator.new(input).join(glue) end # Sort elements of the array @@ -110,7 +110,7 @@ module Liquid # Reverse the elements of an array def reverse(input) - ary = Array(input) + ary = InputIterator.new(input) ary.reverse end @@ -309,16 +309,23 @@ module Liquid include Enumerable def initialize(input) - @input = - if input.is_a?(Array) - input.flatten - elsif input.is_a?(Hash) - [input] - elsif input.is_a?(Enumerable) - input - else - Array(input) - end + @input = if input.is_a?(Array) + input.flatten + elsif input.is_a?(Hash) + [input] + elsif input.is_a?(Enumerable) + input + else + Array(input) + end + end + + def join(glue) + to_a.join(glue) + end + + def reverse + reverse_each.to_a end def each diff --git a/test/integration/standard_filter_test.rb b/test/integration/standard_filter_test.rb index 50f93c6..a869a0c 100644 --- a/test/integration/standard_filter_test.rb +++ b/test/integration/standard_filter_test.rb @@ -117,6 +117,10 @@ class StandardFiltersTest < Test::Unit::TestCase assert_equal [{"a" => 1}, {"a" => 2}, {"a" => 3}, {"a" => 4}], @filters.sort([{"a" => 4}, {"a" => 3}, {"a" => 1}, {"a" => 2}], "a") end + def test_legacy_sort_hash + assert_equal [{a:1, b:2}], @filters.sort({a:1, b:2}) + end + def test_numerical_vs_lexicographical_sort assert_equal [2, 10], @filters.sort([10, 2]) assert_equal [{"a" => 2}, {"a" => 10}], @filters.sort([{"a" => 10}, {"a" => 2}], "a") @@ -128,6 +132,10 @@ class StandardFiltersTest < Test::Unit::TestCase assert_equal [4,3,2,1], @filters.reverse([1,2,3,4]) end + def test_legacy_reverse_hash + assert_equal [{a:1, b:2}], @filters.reverse(a:1, b:2) + 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' }}", @@ -149,6 +157,12 @@ class StandardFiltersTest < Test::Unit::TestCase "thing" => { "foo" => [ { "bar" => 42 }, { "bar" => 17 } ] } end + def test_legacy_map_on_hashes_with_dynamic_key + template = "{% assign key = 'foo' %}{{ thing | map: key | map: 'bar' }}" + hash = { "foo" => { "bar" => 42 } } + assert_template_result "42", template, "thing" => hash + end + def test_flatten assert_equal [1,2,3,4], @filters.flatten([1,2,3,4]) assert_equal [1,2,3,4], @filters.flatten([[1,2,3,4]])