mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-12 23:40:45 -07:00
add more legacy tests
This commit is contained in:
@@ -92,7 +92,7 @@ module Liquid
|
|||||||
|
|
||||||
# Join elements of the array with certain character between them
|
# Join elements of the array with certain character between them
|
||||||
def join(input, glue = ' '.freeze)
|
def join(input, glue = ' '.freeze)
|
||||||
Array(input).join(glue)
|
InputIterator.new(input).join(glue)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Sort elements of the array
|
# Sort elements of the array
|
||||||
@@ -110,7 +110,7 @@ module Liquid
|
|||||||
|
|
||||||
# Reverse the elements of an array
|
# Reverse the elements of an array
|
||||||
def reverse(input)
|
def reverse(input)
|
||||||
ary = Array(input)
|
ary = InputIterator.new(input)
|
||||||
ary.reverse
|
ary.reverse
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -309,8 +309,7 @@ module Liquid
|
|||||||
include Enumerable
|
include Enumerable
|
||||||
|
|
||||||
def initialize(input)
|
def initialize(input)
|
||||||
@input =
|
@input = if input.is_a?(Array)
|
||||||
if input.is_a?(Array)
|
|
||||||
input.flatten
|
input.flatten
|
||||||
elsif input.is_a?(Hash)
|
elsif input.is_a?(Hash)
|
||||||
[input]
|
[input]
|
||||||
@@ -321,6 +320,14 @@ module Liquid
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def join(glue)
|
||||||
|
to_a.join(glue)
|
||||||
|
end
|
||||||
|
|
||||||
|
def reverse
|
||||||
|
reverse_each.to_a
|
||||||
|
end
|
||||||
|
|
||||||
def each
|
def each
|
||||||
@input.each do |e|
|
@input.each do |e|
|
||||||
yield(e.respond_to?(:to_liquid) ? e.to_liquid : e)
|
yield(e.respond_to?(:to_liquid) ? e.to_liquid : e)
|
||||||
|
|||||||
@@ -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")
|
assert_equal [{"a" => 1}, {"a" => 2}, {"a" => 3}, {"a" => 4}], @filters.sort([{"a" => 4}, {"a" => 3}, {"a" => 1}, {"a" => 2}], "a")
|
||||||
end
|
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
|
def test_numerical_vs_lexicographical_sort
|
||||||
assert_equal [2, 10], @filters.sort([10, 2])
|
assert_equal [2, 10], @filters.sort([10, 2])
|
||||||
assert_equal [{"a" => 2}, {"a" => 10}], @filters.sort([{"a" => 10}, {"a" => 2}], "a")
|
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])
|
assert_equal [4,3,2,1], @filters.reverse([1,2,3,4])
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_legacy_reverse_hash
|
||||||
|
assert_equal [{a:1, b:2}], @filters.reverse(a:1, b:2)
|
||||||
|
end
|
||||||
|
|
||||||
def test_map
|
def test_map
|
||||||
assert_equal [1,2,3,4], @filters.map([{"a" => 1}, {"a" => 2}, {"a" => 3}, {"a" => 4}], 'a')
|
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' }}",
|
assert_template_result 'abc', "{{ ary | map:'foo' | map:'bar' }}",
|
||||||
@@ -149,6 +157,12 @@ class StandardFiltersTest < Test::Unit::TestCase
|
|||||||
"thing" => { "foo" => [ { "bar" => 42 }, { "bar" => 17 } ] }
|
"thing" => { "foo" => [ { "bar" => 42 }, { "bar" => 17 } ] }
|
||||||
end
|
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
|
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])
|
||||||
assert_equal [1,2,3,4], @filters.flatten([[1,2,3,4]])
|
assert_equal [1,2,3,4], @filters.flatten([[1,2,3,4]])
|
||||||
|
|||||||
Reference in New Issue
Block a user