diff --git a/lib/liquid/expression/comparison_expression.rb b/lib/liquid/expression/comparison_expression.rb index fb57fd20..5469c015 100644 --- a/lib/liquid/expression/comparison_expression.rb +++ b/lib/liquid/expression/comparison_expression.rb @@ -3,6 +3,9 @@ module Liquid class Expression class ComparisonExpression + # We can improve the resiliency of lax parsing by not expecting whitespace + # surrounding the operator (ie \s+ => \s*). + # However this is not in parity with existing lax parsing behavior. COMPARISON_REGEX = /\A\s*(.+?)\s+(==|!=|<>|<=|>=|<|>|contains)\s+(.+)\s*\z/ class << self diff --git a/test/unit/boolean_unit_test.rb b/test/unit/boolean_unit_test.rb index b63648b2..10dae113 100644 --- a/test/unit/boolean_unit_test.rb +++ b/test/unit/boolean_unit_test.rb @@ -4,7 +4,6 @@ require 'test_helper' class BooleanUnitTest < Minitest::Test include Liquid - def test_simple_boolean_comparison assert_parity("1 > 0", "true") assert_parity("1 < 0", "false") @@ -368,15 +367,11 @@ class BooleanUnitTest < Minitest::Test assert_equal("false", act_output) end - # TESTING TECHNICALLY INCORRECT BEHAVIOUR OF LIQUID-RUBY - # If liquid-vm fails this test, we should change it. def test_conditions_with_boolean_operators_without_whitespace_around_operator - # Define the Liquid template focusing on the selected attribute template = <<~LIQUID LIQUID - # Define the context for the template where the variant should be selected context = { "variant" => { "id" => 420, @@ -394,15 +389,24 @@ class BooleanUnitTest < Minitest::Test # # # However, the existing behaviour in liquid-ruby is that the whitespace is required around the boolean operator. - expected_output = <<~HTML + expected_lax_output = <<~HTML HTML - # Render the template with the context - actual_output = Liquid::Template.parse(template).render(context) + expected_strict_output = <<~HTML + + HTML - # Assert that the actual output matches the expected output - assert_equal(expected_output.delete("\n"), actual_output.delete("\n")) + # This bugged output only happens in lax mode. + prev_error_mode = Liquid::Environment.default.error_mode + Liquid::Environment.default.error_mode = :lax + actual_lax_output = Liquid::Template.parse(template).render(context) + Liquid::Environment.default.error_mode = prev_error_mode + + actual_strict_output = Liquid::Template.parse(template).render(context) + + assert_equal(expected_lax_output.delete("\n"), actual_lax_output.delete("\n")) + assert_equal(expected_strict_output.delete("\n"), actual_strict_output.delete("\n")) end # TESTING INCORRECT BEHAVIOUR OF LIQUID-RUBY