Covered changes with more tests, remove redundant cases, and the new with_error_mode(*modes)

Most of changes update this:
```
[:lax, :strict].each do |mode|
  with_error_mode(mode) do
    assert_template_result(...
```

to be this:
```
with_error_mode(:lax, :strict) do
  assert_template_result(...
```
This commit is contained in:
Guilherme Carreiro
2025-10-27 16:33:31 +01:00
committed by Guilherme Carreiro
parent a23c71e40b
commit 018442b7d1
10 changed files with 229 additions and 357 deletions
+31
View File
@@ -166,6 +166,37 @@ class ConditionUnitTest < Minitest::Test
assert_includes(err.lines.map(&:strip), expected)
end
def test_parse_expression_in_strict_mode
environment = Environment.build(error_mode: :strict)
parse_context = ParseContext.new(environment: environment)
result = Condition.parse_expression(parse_context, 'product.title')
assert_instance_of(VariableLookup, result)
assert_equal('product', result.name)
assert_equal(['title'], result.lookups)
end
def test_parse_expression_in_rigid_mode_raises_internal_error
environment = Environment.build(error_mode: :rigid)
parse_context = ParseContext.new(environment: environment)
error = assert_raises(Liquid::InternalError) do
Condition.parse_expression(parse_context, 'product.title')
end
assert_match(/unsafe parse_expression cannot be used in rigid mode/, error.message)
end
def test_parse_expression_with_safe_true_in_rigid_mode
environment = Environment.build(error_mode: :rigid)
parse_context = ParseContext.new(environment: environment)
result = Condition.parse_expression(parse_context, 'product.title', safe: true)
assert_instance_of(VariableLookup, result)
assert_equal('product', result.name)
assert_equal(['title'], result.lookups)
end
private
def assert_evaluates_true(left, op, right)
-191
View File
@@ -1,191 +0,0 @@
# frozen_string_literal: true
require 'test_helper'
class RigidModeUnitTest < Minitest::Test
include Liquid
def test_tablerow_limit_with_invalid_expression
template = <<~LIQUID
{% tablerow i in (1..10) limit: foo=>bar %}
{{ i }}
{% endtablerow %}
LIQUID
refute_nil(lax_parse(template))
refute_nil(strict_parse(template))
error = assert_raises(SyntaxError) do
rigid_parse(template)
end
assert_match(/Unexpected character =/, error.message)
end
def test_tablerow_offset_with_invalid_expression
template = <<~LIQUID
{% tablerow i in (1..10) offset: foo=>bar %}
{{ i }}
{% endtablerow %}
LIQUID
refute_nil(lax_parse(template))
refute_nil(strict_parse(template))
error = assert_raises(SyntaxError) do
rigid_parse(template)
end
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) %}
{% cycle foo=>bar: "a", "b" %}
{% endfor %}
LIQUID
refute_nil(lax_parse(template))
refute_nil(strict_parse(template))
error = assert_raises(SyntaxError) { rigid_parse(template) }
assert_match(/Unexpected character =/, error.message)
end
def test_cycle_variable_with_invalid_expression
template = <<~LIQUID
{% for i in (1..3) %}
{% cycle foo=>bar, "a", "b" %}
{% endfor %}
LIQUID
refute_nil(lax_parse(template))
refute_nil(strict_parse(template))
error = assert_raises(SyntaxError) { rigid_parse(template) }
assert_match(/Unexpected character =/, error.message)
end
def test_case_with_invalid_expression
template = <<~LIQUID
{% case foo=>bar %}
{% when 1 %}
one
{% endcase %}
LIQUID
refute_nil(lax_parse(template))
refute_nil(strict_parse(template))
error = assert_raises(SyntaxError) { rigid_parse(template) }
assert_match(/Unexpected character =/, error.message)
end
def test_include_template_with_invalid_expression
template = "{% include foo=>bar %}"
refute_nil(lax_parse(template))
refute_nil(strict_parse(template))
error = assert_raises(SyntaxError) { rigid_parse(template) }
assert_match(/Unexpected character =/, error.message)
end
def test_include_with_invalid_expression
template = '{% include "snippet" with foo=>bar %}'
refute_nil(lax_parse(template))
refute_nil(strict_parse(template))
error = assert_raises(SyntaxError) { rigid_parse(template) }
assert_match(/Unexpected character =/, error.message)
end
def test_include_attribute_with_invalid_expression
template = '{% include "snippet", key: foo=>bar %}'
refute_nil(lax_parse(template))
refute_nil(strict_parse(template))
error = assert_raises(SyntaxError) { rigid_parse(template) }
assert_match(/Unexpected character =/, error.message)
end
def test_render_with_invalid_expression
template = '{% render "snippet" with foo=>bar %}'
refute_nil(lax_parse(template))
refute_nil(strict_parse(template))
error = assert_raises(SyntaxError) { rigid_parse(template) }
assert_match(/Unexpected character =/, error.message)
end
def test_render_attribute_with_invalid_expression
template = '{% render "snippet", key: foo=>bar %}'
refute_nil(lax_parse(template))
refute_nil(strict_parse(template))
error = assert_raises(SyntaxError) { rigid_parse(template) }
assert_match(/Unexpected character =/, error.message)
end
def test_empty_expression_handling
ctx_rigid = ParseContext.new(environment: rigid)
assert_nil(ctx_rigid.parse_expression('', safe: true))
assert_nil(ctx_rigid.parse_expression(' ', safe: true))
end
private
def rigid_parse(source)
Template.parse(source, environment: rigid)
end
def strict_parse(source)
Template.parse(source, environment: strict)
end
def lax_parse(source)
Template.parse(source, environment: lax)
end
def lax
Environment.build(error_mode: :lax)
end
def rigid
Environment.build(error_mode: :rigid)
end
def strict
Environment.build(error_mode: :strict)
end
end
+12 -16
View File
@@ -20,8 +20,8 @@ class CaseTagUnitTest < Minitest::Test
{%- endcase -%}
LIQUID
[:lax, :strict].each do |mode|
with_error_mode(mode) { assert_template_result("one", template) }
with_error_mode(:lax, :strict) do
assert_template_result("one", template)
end
with_error_mode(:rigid) do
@@ -41,8 +41,8 @@ class CaseTagUnitTest < Minitest::Test
{%- endcase -%}
LIQUID
[:lax, :strict].each do |mode|
with_error_mode(mode) { assert_template_result("one", template) }
with_error_mode(:lax, :strict) do
assert_template_result("one", template)
end
with_error_mode(:rigid) do
@@ -62,8 +62,8 @@ class CaseTagUnitTest < Minitest::Test
{%- endcase -%}
LIQUID
[:lax, :strict, :rigid].each do |mode|
with_error_mode(mode) { assert_template_result("one", template) }
with_error_mode(:lax, :strict, :rigid) do
assert_template_result("one", template)
end
end
@@ -77,8 +77,8 @@ class CaseTagUnitTest < Minitest::Test
{%- endcase -%}
LIQUID
[:lax, :strict, :rigid].each do |mode|
with_error_mode(mode) { assert_template_result("one", template) }
with_error_mode(:lax, :strict, :rigid) do
assert_template_result("one", template)
end
end
@@ -93,10 +93,8 @@ class CaseTagUnitTest < Minitest::Test
LIQUID
assigns = { 'foo' => { 'bar' => 'baz' } }
[:lax, :strict].each do |mode|
with_error_mode(mode) do
assert_template_result("one", template, assigns)
end
with_error_mode(:lax, :strict) do
assert_template_result("one", template, assigns)
end
with_error_mode(:rigid) do
@@ -117,10 +115,8 @@ class CaseTagUnitTest < Minitest::Test
LIQUID
assigns = { 'foo' => { 'bar' => 'baz' } }
[:lax, :strict].each do |mode|
with_error_mode(mode) do
assert_template_result("one", template, assigns)
end
with_error_mode(:lax, :strict) do
assert_template_result("one", template, assigns)
end
with_error_mode(:rigid) do