add more legacy tests

This commit is contained in:
Florian Weingarten
2014-07-24 00:33:39 +00:00
parent b7b243a13d
commit 0b45ffeada
2 changed files with 33 additions and 12 deletions

View File

@@ -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

View File

@@ -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]])