mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-19 19:00:39 -07:00
Instrument usage of bug with iteration of String with offset or 0 limit (#1667)
This commit is contained in:
+5
-1
@@ -16,7 +16,11 @@ module Liquid
|
|||||||
|
|
||||||
# Maintains Ruby 1.8.7 String#each behaviour on 1.9
|
# Maintains Ruby 1.8.7 String#each behaviour on 1.9
|
||||||
if collection.is_a?(String)
|
if collection.is_a?(String)
|
||||||
return collection.empty? ? [] : [collection]
|
return [] if collection.empty?
|
||||||
|
if from > 0 || to == 0
|
||||||
|
Usage.increment("string_slice_bug")
|
||||||
|
end
|
||||||
|
return [collection]
|
||||||
end
|
end
|
||||||
return [] unless collection.respond_to?(:each)
|
return [] unless collection.respond_to?(:each)
|
||||||
|
|
||||||
|
|||||||
@@ -12,4 +12,34 @@ class ForTagUnitTest < Minitest::Test
|
|||||||
template = Liquid::Template.parse('{% for item in items %}FOR{% else %}ELSE{% endfor %}')
|
template = Liquid::Template.parse('{% for item in items %}FOR{% else %}ELSE{% endfor %}')
|
||||||
assert_equal(['FOR', 'ELSE'], template.root.nodelist[0].nodelist.map(&:nodelist).flatten)
|
assert_equal(['FOR', 'ELSE'], template.root.nodelist[0].nodelist.map(&:nodelist).flatten)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_for_string_slice_bug_usage
|
||||||
|
template = Liquid::Template.parse("{% for x in str, offset: 1 %}{{ x }},{% endfor %}")
|
||||||
|
assert_usage("string_slice_bug") do
|
||||||
|
assert_equal("abc,", template.render({ "str" => "abc" }))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_for_string_0_limit_usage
|
||||||
|
template = Liquid::Template.parse("{% for x in str, limit: 0 %}{{ x }},{% endfor %}")
|
||||||
|
assert_usage("string_slice_bug") do
|
||||||
|
assert_equal("abc,", template.render({ "str" => "abc" }))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_for_string_no_slice_usage
|
||||||
|
template = Liquid::Template.parse("{% for x in str, offset: 0, limit: 1 %}{{ x }},{% endfor %}")
|
||||||
|
assert_usage("string_slice_bug", times: 0) do
|
||||||
|
assert_equal("abc,", template.render({ "str" => "abc" }))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def assert_usage(name, times: 1, &block)
|
||||||
|
count = 0
|
||||||
|
result = Liquid::Usage.stub(:increment, ->(n) { count += 1 if n == name }, &block)
|
||||||
|
assert_equal(times, count)
|
||||||
|
result
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user