Add rigid parser to tablerow tag

This commit is contained in:
Guilherme Carreiro
2025-10-27 16:33:31 +01:00
committed by Guilherme Carreiro
parent 5248025439
commit 5ec3008b37
4 changed files with 320 additions and 6 deletions
+257
View File
@@ -255,4 +255,261 @@ class TableRowTest < Minitest::Test
template,
)
end
def test_tablerow_with_cols_attribute_in_rigid_mode
template = <<~LIQUID.chomp
{% tablerow i in (1..6) cols: 3 %}{{ i }}{% endtablerow %}
LIQUID
expected = <<~OUTPUT
<tr class="row1">
<td class="col1">1</td><td class="col2">2</td><td class="col3">3</td></tr>
<tr class="row2"><td class="col1">4</td><td class="col2">5</td><td class="col3">6</td></tr>
OUTPUT
result = Template.parse(template, environment: rigid_environment).render
assert_equal(expected, result)
end
def test_tablerow_with_limit_attribute_in_rigid_mode
template = <<~LIQUID.chomp
{% tablerow i in (1..10) limit: 3 %}{{ i }}{% endtablerow %}
LIQUID
expected = <<~OUTPUT
<tr class="row1">
<td class="col1">1</td><td class="col2">2</td><td class="col3">3</td></tr>
OUTPUT
result = Template.parse(template, environment: rigid_environment).render
assert_equal(expected, result)
end
def test_tablerow_with_offset_attribute_in_rigid_mode
template = <<~LIQUID.chomp
{% tablerow i in (1..5) offset: 2 %}{{ i }}{% endtablerow %}
LIQUID
expected = <<~OUTPUT
<tr class="row1">
<td class="col1">3</td><td class="col2">4</td><td class="col3">5</td></tr>
OUTPUT
result = Template.parse(template, environment: rigid_environment).render
assert_equal(expected, result)
end
def test_tablerow_with_range_attribute_in_rigid_mode
template = <<~LIQUID.chomp
{% tablerow i in (1..3) range: (1..10) %}{{ i }}{% endtablerow %}
LIQUID
expected = <<~OUTPUT
<tr class="row1">
<td class="col1">1</td><td class="col2">2</td><td class="col3">3</td></tr>
OUTPUT
result = Template.parse(template, environment: rigid_environment).render
assert_equal(expected, result)
end
def test_tablerow_with_multiple_attributes_in_rigid_mode
template = <<~LIQUID.chomp
{% tablerow i in (1..10) cols: 2, limit: 4, offset: 1 %}{{ i }}{% endtablerow %}
LIQUID
expected = <<~OUTPUT
<tr class="row1">
<td class="col1">2</td><td class="col2">3</td></tr>
<tr class="row2"><td class="col1">4</td><td class="col2">5</td></tr>
OUTPUT
result = Template.parse(template, environment: rigid_environment).render
assert_equal(expected, result)
end
def test_tablerow_with_variable_collection_in_rigid_mode
template = <<~LIQUID.chomp
{% tablerow n in numbers cols: 2 %}{{ n }}{% endtablerow %}
LIQUID
expected = <<~OUTPUT
<tr class="row1">
<td class="col1">1</td><td class="col2">2</td></tr>
<tr class="row2"><td class="col1">3</td><td class="col2">4</td></tr>
OUTPUT
result = Template.parse(template, environment: rigid_environment).render('numbers' => [1, 2, 3, 4])
assert_equal(expected, result)
end
def test_tablerow_with_dotted_access_in_rigid_mode
template = <<~LIQUID.chomp
{% tablerow n in obj.numbers cols: 2 %}{{ n }}{% endtablerow %}
LIQUID
expected = <<~OUTPUT
<tr class="row1">
<td class="col1">1</td><td class="col2">2</td></tr>
<tr class="row2"><td class="col1">3</td><td class="col2">4</td></tr>
OUTPUT
result = Template.parse(template, environment: rigid_environment).render('obj' => { 'numbers' => [1, 2, 3, 4] })
assert_equal(expected, result)
end
def test_tablerow_with_bracketed_access_in_rigid_mode
template = <<~LIQUID.chomp
{% tablerow n in obj["numbers"] cols: 2 %}{{ n }}{% endtablerow %}
LIQUID
expected = <<~OUTPUT
<tr class="row1">
<td class="col1">10</td><td class="col2">20</td></tr>
OUTPUT
result = Template.parse(template, environment: rigid_environment).render('obj' => { 'numbers' => [10, 20] })
assert_equal(expected, result)
end
def test_tablerow_without_attributes_in_rigid_mode
template = <<~LIQUID.chomp
{% tablerow i in (1..3) %}{{ i }}{% endtablerow %}
LIQUID
expected = <<~OUTPUT
<tr class="row1">
<td class="col1">1</td><td class="col2">2</td><td class="col3">3</td></tr>
OUTPUT
result = Template.parse(template, environment: rigid_environment).render
assert_equal(expected, result)
end
def test_tablerow_with_trailing_comma_in_rigid_mode
template = <<~LIQUID.chomp
{% tablerow i in (1..3) cols: 2, %}{{ i }}{% endtablerow %}
LIQUID
expected = <<~OUTPUT
<tr class="row1">
<td class="col1">1</td><td class="col2">2</td></tr>
<tr class="row2"><td class="col1">3</td></tr>
OUTPUT
result = Template.parse(template, environment: rigid_environment).render
assert_equal(expected, result)
end
def test_tablerow_with_invalid_attribute_name_in_rigid_mode
template = '{% tablerow i in (1..10) invalid_attr: 5 %}{{ i }}{% endtablerow %}'
error = assert_raises(SyntaxError) do
Template.parse(template, environment: rigid_environment)
end
assert_equal("Liquid syntax error: Invalid attribute 'invalid_attr' in tablerow loop. Valid attributes are cols, limit, offset, and range in \"i in (1..10) invalid_attr: 5\"", error.message)
end
def test_tablerow_with_invalid_expression_in_limit_in_rigid_mode
template = '{% tablerow i in (1..10) limit: foo=>bar %}{{ i }}{% endtablerow %}'
error = assert_raises(SyntaxError) do
Template.parse(template, environment: rigid_environment)
end
assert_equal("Liquid syntax error: Unexpected character = in \"i in (1..10) limit: foo=>bar\"", error.message)
end
def test_tablerow_with_invalid_expression_in_offset_in_rigid_mode
template = '{% tablerow i in (1..10) offset: foo=>bar %}{{ i }}{% endtablerow %}'
error = assert_raises(SyntaxError) do
Template.parse(template, environment: rigid_environment)
end
assert_equal("Liquid syntax error: Unexpected character = in \"i in (1..10) offset: foo=>bar\"", error.message)
end
def test_tablerow_with_invalid_expression_in_cols_in_rigid_mode
template = '{% tablerow i in (1..10) cols: foo=>bar %}{{ i }}{% endtablerow %}'
error = assert_raises(SyntaxError) do
Template.parse(template, environment: rigid_environment)
end
assert_equal("Liquid syntax error: Unexpected character = in \"i in (1..10) cols: foo=>bar\"", error.message)
end
def test_tablerow_with_invalid_expression_in_range_in_rigid_mode
template = '{% tablerow i in (1..10) range: foo=>bar %}{{ i }}{% endtablerow %}'
error = assert_raises(SyntaxError) do
Template.parse(template, environment: rigid_environment)
end
assert_equal("Liquid syntax error: Unexpected character = in \"i in (1..10) range: foo=>bar\"", error.message)
end
def test_tablerow_without_in_keyword_in_rigid_mode
template = '{% tablerow i (1..10) %}{{ i }}{% endtablerow %}'
error = assert_raises(SyntaxError) do
Template.parse(template, environment: rigid_environment)
end
assert_equal("Liquid syntax error: For loops require an 'in' clause in \"i (1..10)\"", error.message)
end
def test_tablerow_with_multiple_invalid_attributes_reports_first_in_rigid_mode
template = '{% tablerow i in (1..10) invalid1: 5, invalid2: 10 %}{{ i }}{% endtablerow %}'
error = assert_raises(SyntaxError) do
Template.parse(template, environment: rigid_environment)
end
assert_equal("Liquid syntax error: Invalid attribute 'invalid1' in tablerow loop. Valid attributes are cols, limit, offset, and range in \"i in (1..10) invalid1: 5, invalid2: 10\"", error.message)
end
def test_tablerow_with_empty_collection_in_rigid_mode
template = <<~LIQUID.chomp
{% tablerow i in empty_array cols: 2 %}{{ i }}{% endtablerow %}
LIQUID
expected = <<~OUTPUT
<tr class="row1">
</tr>
OUTPUT
result = Template.parse(template, environment: rigid_environment).render('empty_array' => [])
assert_equal(expected, result)
end
def test_tablerow_lax_mode_still_accepts_invalid_attributes
template = <<~LIQUID.chomp
{% tablerow i in (1..3) invalid_attr: 5 %}{{ i }}{% endtablerow %}
LIQUID
expected = <<~OUTPUT
<tr class="row1">
<td class="col1">1</td><td class="col2">2</td><td class="col3">3</td></tr>
OUTPUT
result = Template.parse(template, environment: lax_environment).render
assert_equal(expected, result)
end
def test_tablerow_strict_mode_still_accepts_invalid_attributes
template = <<~LIQUID.chomp
{% tablerow i in (1..3) invalid_attr: 5 %}{{ i }}{% endtablerow %}
LIQUID
expected = <<~OUTPUT
<tr class="row1">
<td class="col1">1</td><td class="col2">2</td><td class="col3">3</td></tr>
OUTPUT
result = Template.parse(template, environment: strict_environment).render
assert_equal(expected, result)
end
private
def rigid_environment
Environment.build(error_mode: :rigid)
end
def strict_environment
Environment.build(error_mode: :strict)
end
def lax_environment
Environment.build(error_mode: :lax)
end
end
+24 -6
View File
@@ -6,10 +6,10 @@ class RigidModeUnitTest < Minitest::Test
include Liquid
def test_tablerow_limit_with_invalid_expression
skip
template = <<~LIQUID
{% tablerow i in (1..10) limit: foo=>bar %}{{ i }}{% endtablerow %}
{% tablerow i in (1..10) limit: foo=>bar %}
{{ i }}
{% endtablerow %}
LIQUID
refute_nil(lax_parse(template))
@@ -22,10 +22,10 @@ class RigidModeUnitTest < Minitest::Test
end
def test_tablerow_offset_with_invalid_expression
skip
template = <<~LIQUID
{% tablerow i in (1..10) offset: foo=>bar %}{{ i }}{% endtablerow %}
{% tablerow i in (1..10) offset: foo=>bar %}
{{ i }}
{% endtablerow %}
LIQUID
refute_nil(lax_parse(template))
@@ -37,6 +37,24 @@ class RigidModeUnitTest < Minitest::Test
assert_match(/Unexpected character =/, error.message)
end
def test_tablerow_with_invalid_attribute
template = <<~LIQUID
{% tablerow i in (1..10) invalid_attr: 5 %}
{{ i }}
{% endtablerow %}
LIQUID
refute_nil(lax_parse(template))
refute_nil(strict_parse(template))
error = assert_raises(SyntaxError) do
rigid_parse(template)
end
assert_match(/Invalid attribute 'invalid_attr'/, error.message)
assert_match(/Valid attributes are cols, limit, offset, and range/, error.message)
end
def test_cycle_name_with_invalid_expression
template = <<~LIQUID
{% for i in (1..3) %}