Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "Instrument usage of bug with iteration of String with offset or 0 limit" #1677

Merged
merged 1 commit into from
Feb 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions lib/liquid/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,7 @@ def self.slice_collection_using_each(collection, from, to)

# Maintains Ruby 1.8.7 String#each behaviour on 1.9
if collection.is_a?(String)
return [] if collection.empty?
if from > 0 || to == 0
Usage.increment("string_slice_bug")
end
return [collection]
return collection.empty? ? [] : [collection]
end
return [] unless collection.respond_to?(:each)

Expand Down
30 changes: 0 additions & 30 deletions test/unit/tags/for_tag_unit_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,4 @@ def test_for_else_nodelist
template = Liquid::Template.parse('{% for item in items %}FOR{% else %}ELSE{% endfor %}')
assert_equal(['FOR', 'ELSE'], template.root.nodelist[0].nodelist.map(&:nodelist).flatten)
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