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

raise invalid integer argument error from tablerow #1676

Merged
merged 2 commits into from
Jan 18, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 11 additions & 3 deletions lib/liquid/tags/table_row.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ def initialize(tag_name, markup, options)
def render_to_output_buffer(context, output)
(collection = context.evaluate(@collection_name)) || (return '')

from = @attributes.key?('offset') ? context.evaluate(@attributes['offset']).to_i : 0
to = @attributes.key?('limit') ? from + context.evaluate(@attributes['limit']).to_i : nil
from = @attributes.key?('offset') ? to_integer(context.evaluate(@attributes['offset'])) : 0
to = @attributes.key?('limit') ? from + to_integer(context.evaluate(@attributes['limit'])) : nil

collection = Utils.slice_collection(collection, from, to)
length = collection.length

cols = @attributes.key?('cols') ? context.evaluate(@attributes['cols']).to_i : length
cols = @attributes.key?('cols') ? to_integer(context.evaluate(@attributes['cols'])) : length

output << "<tr class=\"row1\">\n"
context.stack do
Expand Down Expand Up @@ -82,6 +82,14 @@ def children
super + @node.attributes.values + [@node.collection_name]
end
end

private

def to_integer(value)
value.to_i
rescue NoMethodError
raise Liquid::ArgumentError, "invalid integer"
end
end

Template.register_tag('tablerow', TableRow)
Expand Down
46 changes: 46 additions & 0 deletions test/integration/tags/table_row_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,32 @@ def test_cols_nil_constant_same_as_evaluated_nil_expression
{ "var" => nil })
end

def test_nil_limit_is_treated_as_zero
expect = "<tr class=\"row1\">\n" \
"</tr>\n"

assert_template_result(expect,
"{% tablerow i in (1..2) limit:nil %}{{ i }}{% endtablerow %}")

assert_template_result(expect,
"{% tablerow i in (1..2) limit:var %}{{ i }}{% endtablerow %}",
{ "var" => nil })
end

def test_nil_offset_is_treated_as_zero
expect = "<tr class=\"row1\">\n" \
"<td class=\"col1\">1:false</td>" \
"<td class=\"col2\">2:true</td>" \
"</tr>\n"

assert_template_result(expect,
"{% tablerow i in (1..2) offset:nil %}{{ i }}:{{ tablerowloop.col_last }}{% endtablerow %}")

assert_template_result(expect,
"{% tablerow i in (1..2) offset:var %}{{ i }}:{{ tablerowloop.col_last }}{% endtablerow %}",
{ "var" => nil })
end

def test_tablerow_loop_drop_attributes
template = <<~LIQUID.chomp
{% tablerow i in (1...2) %}
Expand Down Expand Up @@ -131,4 +157,24 @@ def test_tablerow_loop_drop_attributes

assert_template_result(expected_output, template)
end

def test_table_row_renders_correct_error_message_for_invalid_parameters
assert_template_result(
"Liquid error (line 1): invalid integer",
'{% tablerow n in (1...10) limit:true %} {{n}} {% endtablerow %}',
render_errors: true,
)

assert_template_result(
"Liquid error (line 1): invalid integer",
'{% tablerow n in (1...10) offset:true %} {{n}} {% endtablerow %}',
render_errors: true,
)

assert_template_result(
"Liquid error (line 1): invalid integer",
'{% tablerow n in (1...10) cols:true %} {{n}} {% endtablerow %}',
render_errors: true,
)
end
end