No longer test ParseContext directly on RigidModeUnitTest as

now the `safe: true` calls are considered safe

Test the entire template instead
This commit is contained in:
Guilherme Carreiro
2025-10-27 16:33:31 +01:00
committed by Guilherme Carreiro
parent b5fbad08c6
commit 0bc69b86b7
2 changed files with 85 additions and 62 deletions
+4 -2
View File
@@ -56,8 +56,10 @@ module Liquid
def parse_expression(markup, safe: false) def parse_expression(markup, safe: false)
# todo(guilherme): remove this once rigid mode is fully using safe_parse_expression # todo(guilherme): remove this once rigid mode is fully using safe_parse_expression
# raise Liquid::InternalError, "parse_expression is not supported in rigid mode" if !safe && @error_mode == :rigid if !safe && @error_mode == :rigid
puts("🚨 parse_expression used in rigid mode") if !safe && @error_mode == :rigid # raise Liquid::InternalError, "parse_expression is not supported in rigid mode"
puts("🚨 parse_expression used in rigid mode")
end
Expression.parse(markup, @string_scanner, @expression_cache) Expression.parse(markup, @string_scanner, @expression_cache)
end end
+81 -60
View File
@@ -6,57 +6,38 @@ class RigidModeUnitTest < Minitest::Test
include Liquid include Liquid
def test_direct_parse_expression_comparison def test_direct_parse_expression_comparison
skip("todo(guilherme): parse_context.safe_parse_expression in progress...")
test_cases = [ test_cases = [
'foo bar', '{{ foo bar }}',
'user.name first', '{{ user.name first }}',
'items[0] next', '{{ items[0] next }}',
'products[0].name extra', '{{ products[0].name extra }}',
] ]
test_cases.each do |expr| test_cases.each do |template|
ctx_strict = ParseContext.new(environment: strict_env) refute_nil(lax_parse(template))
result = ctx_strict.parse_expression(expr) assert_raises(SyntaxError) { strict_parse(template) }
refute_nil(result, "Strict mode should parse '#{expr}'") assert_raises(SyntaxError) { rigid_parse(template) }
ctx_rigid = ParseContext.new(environment: rigid_env)
error = assert_raises(SyntaxError) do
ctx_rigid.parse_expression(expr)
end
assert_match(/Expected end_of_string but found id/, error.message)
end end
end end
def test_comparison_strict_vs_rigid_with_space_separated_lookups def test_comparison_strict_vs_rigid_with_space_separated_lookups
skip("todo(guilherme): parse_context.safe_parse_expression in progress...") template = '{{ product title }}'
expr = 'product title' output = lax_parse(template).render({ 'product' => { 'title' => 'Snow' } })
assert_equal('{"title"=>"Snow"}', output)
ctx_lax = ParseContext.new(environment: lax_env) assert_raises(SyntaxError) { strict_parse(template) }
result_lax = ctx_lax.parse_expression(expr) assert_raises(SyntaxError) { rigid_parse(template) }
assert_equal('product', result_lax.name)
assert_equal(['title'], result_lax.lookups)
ctx_strict = ParseContext.new(environment: strict_env)
result_strict = ctx_strict.parse_expression(expr)
assert_equal('product', result_strict.name)
assert_equal(['title'], result_strict.lookups)
ctx_rigid = ParseContext.new(environment: rigid_env)
assert_raises(SyntaxError) do
ctx_rigid.parse_expression(expr)
end
end end
def test_tablerow_limit_with_invalid_expression def test_tablerow_limit_with_invalid_expression
skip("todo(guilherme): parse_context.safe_parse_expression in progress...") skip
template = <<~LIQUID template = <<~LIQUID
{% tablerow i in (1..10) limit: foo=>bar %}{{ i }}{% endtablerow %} {% tablerow i in (1..10) limit: foo=>bar %}{{ i }}{% endtablerow %}
LIQUID LIQUID
refute_nil(lax_parse(template))
refute_nil(strict_parse(template)) refute_nil(strict_parse(template))
error = assert_raises(SyntaxError) do error = assert_raises(SyntaxError) do
@@ -66,12 +47,13 @@ class RigidModeUnitTest < Minitest::Test
end end
def test_tablerow_offset_with_invalid_expression def test_tablerow_offset_with_invalid_expression
skip("todo(guilherme): parse_context.safe_parse_expression in progress...") skip
template = <<~LIQUID template = <<~LIQUID
{% tablerow i in (1..10) offset: foo=>bar %}{{ i }}{% endtablerow %} {% tablerow i in (1..10) offset: foo=>bar %}{{ i }}{% endtablerow %}
LIQUID LIQUID
refute_nil(lax_parse(template))
refute_nil(strict_parse(template)) refute_nil(strict_parse(template))
error = assert_raises(SyntaxError) do error = assert_raises(SyntaxError) do
@@ -87,6 +69,7 @@ class RigidModeUnitTest < Minitest::Test
{% endfor %} {% endfor %}
LIQUID LIQUID
refute_nil(lax_parse(template))
refute_nil(strict_parse(template)) refute_nil(strict_parse(template))
error = assert_raises(SyntaxError) do error = assert_raises(SyntaxError) do
@@ -102,6 +85,7 @@ class RigidModeUnitTest < Minitest::Test
{% endfor %} {% endfor %}
LIQUID LIQUID
refute_nil(lax_parse(template))
refute_nil(strict_parse(template)) refute_nil(strict_parse(template))
error = assert_raises(SyntaxError) do error = assert_raises(SyntaxError) do
@@ -118,6 +102,7 @@ class RigidModeUnitTest < Minitest::Test
{% endcase %} {% endcase %}
LIQUID LIQUID
refute_nil(lax_parse(template))
refute_nil(strict_parse(template)) refute_nil(strict_parse(template))
error = assert_raises(SyntaxError) do error = assert_raises(SyntaxError) do
@@ -129,6 +114,7 @@ class RigidModeUnitTest < Minitest::Test
def test_include_template_with_invalid_expression def test_include_template_with_invalid_expression
template = "{% include foo=>bar %}" template = "{% include foo=>bar %}"
refute_nil(lax_parse(template))
refute_nil(strict_parse(template)) refute_nil(strict_parse(template))
error = assert_raises(SyntaxError) do error = assert_raises(SyntaxError) do
@@ -140,6 +126,7 @@ class RigidModeUnitTest < Minitest::Test
def test_include_with_invalid_expression def test_include_with_invalid_expression
template = '{% include "snippet" with foo=>bar %}' template = '{% include "snippet" with foo=>bar %}'
refute_nil(lax_parse(template))
refute_nil(strict_parse(template)) refute_nil(strict_parse(template))
error = assert_raises(SyntaxError) do error = assert_raises(SyntaxError) do
@@ -151,6 +138,7 @@ class RigidModeUnitTest < Minitest::Test
def test_include_attribute_with_invalid_expression def test_include_attribute_with_invalid_expression
template = '{% include "snippet", key: foo=>bar %}' template = '{% include "snippet", key: foo=>bar %}'
refute_nil(lax_parse(template))
refute_nil(strict_parse(template)) refute_nil(strict_parse(template))
error = assert_raises(SyntaxError) do error = assert_raises(SyntaxError) do
@@ -162,6 +150,7 @@ class RigidModeUnitTest < Minitest::Test
def test_render_with_invalid_expression def test_render_with_invalid_expression
template = '{% render "snippet" with foo=>bar %}' template = '{% render "snippet" with foo=>bar %}'
refute_nil(lax_parse(template))
refute_nil(strict_parse(template)) refute_nil(strict_parse(template))
error = assert_raises(SyntaxError) do error = assert_raises(SyntaxError) do
@@ -173,6 +162,7 @@ class RigidModeUnitTest < Minitest::Test
def test_render_attribute_with_invalid_expression def test_render_attribute_with_invalid_expression
template = '{% render "snippet", key: foo=>bar %}' template = '{% render "snippet", key: foo=>bar %}'
refute_nil(lax_parse(template))
refute_nil(strict_parse(template)) refute_nil(strict_parse(template))
error = assert_raises(SyntaxError) do error = assert_raises(SyntaxError) do
@@ -193,9 +183,14 @@ class RigidModeUnitTest < Minitest::Test
} }
test_cases.each do |template_str, data| test_cases.each do |template_str, data|
t = rigid_parse(template_str) lax_result = lax_parse(template_str).render(data)
result = t.render(data) assert(lax_result.is_a?(String), "Lax mode should render '#{template_str}'")
assert(result.is_a?(String), "Should render successfully for '#{template_str}'")
strict_result = strict_parse(template_str).render(data)
assert(strict_result.is_a?(String), "Strict mode should render '#{template_str}'")
rigid_result = rigid_parse(template_str).render(data)
assert(rigid_result.is_a?(String), "Rigid mode should render '#{template_str}'")
end end
end end
@@ -204,9 +199,14 @@ class RigidModeUnitTest < Minitest::Test
{% for i in (1..3) %}{{ i }}{% endfor %} {% for i in (1..3) %}{{ i }}{% endfor %}
LIQUID LIQUID
t = rigid_parse(template) lax_result = lax_parse(template).render
result = t.render assert_equal("123\n", lax_result)
assert_equal("123\n", result)
strict_result = strict_parse(template).render
assert_equal("123\n", strict_result)
rigid_result = rigid_parse(template).render
assert_equal("123\n", rigid_result)
end end
def test_rigid_mode_with_variable_ranges def test_rigid_mode_with_variable_ranges
@@ -214,9 +214,16 @@ class RigidModeUnitTest < Minitest::Test
{% for i in (start..end) %}{{ i }}{% endfor %} {% for i in (start..end) %}{{ i }}{% endfor %}
LIQUID LIQUID
t = rigid_parse(template) data = { 'start' => 1, 'end' => 3 }
result = t.render({ 'start' => 1, 'end' => 3 })
assert_equal("123\n", result) lax_result = lax_parse(template).render(data)
assert_equal("123\n", lax_result)
strict_result = strict_parse(template).render(data)
assert_equal("123\n", strict_result)
rigid_result = rigid_parse(template).render(data)
assert_equal("123\n", rigid_result)
end end
def test_rigid_mode_valid_filters def test_rigid_mode_valid_filters
@@ -224,9 +231,14 @@ class RigidModeUnitTest < Minitest::Test
{{ "hello" | upcase | prepend: "Say: " }} {{ "hello" | upcase | prepend: "Say: " }}
LIQUID LIQUID
t = rigid_parse(template) lax_result = lax_parse(template).render
result = t.render assert_equal("Say: HELLO\n", lax_result)
assert_equal("Say: HELLO\n", result)
strict_result = strict_parse(template).render
assert_equal("Say: HELLO\n", strict_result)
rigid_result = rigid_parse(template).render
assert_equal("Say: HELLO\n", rigid_result)
end end
def test_rigid_mode_valid_filter_with_correct_variable_args def test_rigid_mode_valid_filter_with_correct_variable_args
@@ -234,39 +246,48 @@ class RigidModeUnitTest < Minitest::Test
{{ "hello" | append: world.name }} {{ "hello" | append: world.name }}
LIQUID LIQUID
t = rigid_parse(template) data = { 'world' => { 'name' => ' world' } }
result = t.render({ 'world' => { 'name' => ' world' } })
assert_equal("hello world\n", result) lax_result = lax_parse(template).render(data)
assert_equal("hello world\n", lax_result)
strict_result = strict_parse(template).render(data)
assert_equal("hello world\n", strict_result)
rigid_result = rigid_parse(template).render(data)
assert_equal("hello world\n", rigid_result)
end end
def test_empty_expression_handling def test_empty_expression_handling
ctx_rigid = ParseContext.new(environment: rigid_env) ctx_rigid = ParseContext.new(environment: rigid)
result = ctx_rigid.parse_expression('')
assert_nil(result)
result = ctx_rigid.parse_expression(' ') assert_nil(ctx_rigid.parse_expression('', safe: true))
assert_nil(result) assert_nil(ctx_rigid.parse_expression(' ', safe: true))
end end
private private
def rigid_parse(source) def rigid_parse(source)
Template.parse(source, environment: rigid_env) Template.parse(source, environment: rigid)
end end
def strict_parse(source) def strict_parse(source)
Template.parse(source, environment: strict_env) Template.parse(source, environment: strict)
end end
def lax_env def lax_parse(source)
Template.parse(source, environment: lax)
end
def lax
Environment.build(error_mode: :lax) Environment.build(error_mode: :lax)
end end
def rigid_env def rigid
Environment.build(error_mode: :rigid) Environment.build(error_mode: :rigid)
end end
def strict_env def strict
Environment.build(error_mode: :strict) Environment.build(error_mode: :strict)
end end
end end