diff --git a/lib/liquid/parser.rb b/lib/liquid/parser.rb index 00faefdce..daaadf6d2 100644 --- a/lib/liquid/parser.rb +++ b/lib/liquid/parser.rb @@ -60,12 +60,7 @@ def expression when :string, :number consume when :open_round - consume - first = expression - consume(:dotdot) - last = expression - consume(:close_round) - "(#{first}..#{last})" + consume_round_parentheses(token) else raise SyntaxError, "#{token} is not a valid expression" end @@ -79,13 +74,32 @@ def expression operator = consume(:boolean_operator) left = expr right = expression - - "#{left} #{operator} #{right}" + if look(:close_round) + "(#{left} #{operator} #{right})" + else + "#{left} #{operator} #{right}" + end else expr end end + def consume_round_parentheses(token) + consume + first = expression + dotdot_token = consume?(:dotdot) + if dotdot_token + last = expression + consume(:close_round) + "(#{first}..#{last})" + elsif look(:close_round) + consume(:close_round) + first + else + raise SyntaxError, "#{token} is not a valid expression" + end + end + def argument str = +"" # might be a keyword argument (identifier: expression) diff --git a/test/test_boolean_helper.rb b/test/test_boolean_helper.rb new file mode 100755 index 000000000..f0baec33d --- /dev/null +++ b/test/test_boolean_helper.rb @@ -0,0 +1,61 @@ +#!/usr/bin/env ruby +# frozen_string_literal: true + +module Minitest + module Assertions + include Liquid + def assert_with_lax_parsing(template, expected_output, context = {}) + prev_error_mode = Liquid::Environment.default.error_mode + Liquid::Environment.default.error_mode = :lax + + begin + actual_output = Liquid::Template.parse(template).render(context) + rescue StandardError => e + actual_output = e.message + ensure + Liquid::Environment.default.error_mode = prev_error_mode + end + + assert_equal(expected_output.strip, actual_output.strip) + end + + def assert_parity(liquid_expression, expected_result, args = {}) + assert_condition(liquid_expression, expected_result, args) + assert_expression(liquid_expression, expected_result, args) + end + + def assert_expression(liquid_expression, expected_result, args = {}) + assert_parity_scenario(:expression, "{{ #{liquid_expression} }}", expected_result, args) + end + + def assert_condition(liquid_condition, expected_result, args = {}) + assert_parity_scenario(:condition, "{% if #{liquid_condition} %}true{% else %}false{% endif %}", expected_result, args) + end + + def assert_parity_scenario(kind, template, exp_output, args = {}) + act_output = Liquid::Template.parse(template).render(args) + + assert_equal(exp_output, act_output, <<~ERROR_MESSAGE) + #{kind.to_s.capitalize} template failure: + --- + #{template} + --- + args: #{args.inspect} + ERROR_MESSAGE + end + end +end + +class LinkDrop < Liquid::Drop + attr_accessor :levels, :links, :title, :type, :url + + def initialize(levels: nil, links: nil, title: nil, type: nil, url: nil) + super() + + @levels = levels + @links = links + @title = title + @type = type + @url = url + end +end diff --git a/test/unit/boolean_precedence_unit_test.rb b/test/unit/boolean_precedence_unit_test.rb new file mode 100644 index 000000000..3c20f41ac --- /dev/null +++ b/test/unit/boolean_precedence_unit_test.rb @@ -0,0 +1,171 @@ +# frozen_string_literal: true + +require 'test_helper' +require 'test_boolean_helper' + +class BooleanPrecedenceUnitTest < Minitest::Test + include Liquid + + def test_basic_boolean_parenthesized_expressions + assert_parity("false and (false or true)", "false") + assert_parity("true and (false or true)", "true") + assert_parity("(true and false) or true", "true") + assert_parity("(false and true) or false", "false") + end + + def test_nested_boolean_parentheses + assert_parity("(false and (true or false)) or true", "true") + assert_parity("true and (false or (true and true))", "true") + assert_parity("(true and (false or false)) or false", "false") + end + + def test_multiple_operations_with_consistent_operators + assert_parity("(true and true) and (false or true)", "true") + assert_parity("(false or false) or (true and false)", "false") + end + + def test_parentheses_changing_default_precedence + # Default precedence: (true and false) or true + assert_parity("true and false or true", "true") + # With parentheses: true and (false or true) + assert_parity("true and (false or true)", "true") + + # Default precedence: false or (true and true) + assert_parity("false or true and true", "true") + # With parentheses: (false or true) and true + assert_parity("(false or true) and true", "true") + end + + def test_boolean_parentheses_with_variables + assert_parity("(a or b) and c", "true", { "a" => true, "b" => false, "c" => true }) + assert_parity("(a or b) and c", "false", { "a" => true, "b" => false, "c" => false }) + assert_parity("a and (b or c)", "true", { "a" => true, "b" => false, "c" => true }) + assert_parity("a and (b or c)", "false", { "a" => false, "b" => true, "c" => true }) + end + + def test_comparison_operators_inside_parentheses + assert_parity("(1 > 0) and (2 < 3)", "true") + assert_parity("(1 < 0) or (2 > 3)", "false") + assert_parity("true and (1 == 1)", "true") + assert_parity("false or (2 != 2)", "false") + end + + def test_complex_nested_boolean_expressions + assert_parity("((true and false) or (false and true)) or ((false or true) and (true or false))", "true") + assert_parity("((true and true) or (false and false)) and ((true or false) and (false or true))", "true") + end + + def test_not_operator_with_parentheses + # Testing how 'not' interacts with parentheses + assert_parity("not (true or false)", "false") + assert_parity("not (false and true)", "true") + assert_parity("(not false) and true", "true") + assert_parity("(not true) or false", "false") + assert_parity("not (not true)", "true") + end + + def test_nil_values_with_boolean_precedence + # How nil values interact with boolean expressions and parentheses + assert_parity("nil and (true or false)", "false") + assert_parity("(nil or true) and false", "false") + assert_parity("(nil and nil) or true", "true") + assert_parity("true and (nil or false)", "false") + end + + def test_mixed_primitive_types_with_parentheses + # Testing how different types interact in boolean expressions with parentheses + assert_parity("('' or 0) and true", "true") + assert_parity("(true and 'string') or false", "true") + assert_parity("(false or '') and 1", "false") + assert_parity("(nil or false) and 'text'", "false") + end + + def test_triple_operator_precedence + # Testing three different operators with different parenthesizing + assert_parity("true or false and true or false", "true") # default precedence + assert_parity("true or (false and true) or false", "true") + assert_parity("(true or false) and (true or false)", "true") + assert_parity("((true or false) and true) or false", "true") + assert_parity("true or (false and (true or false))", "true") + end + + def test_undefined_variables_with_parentheses + # How undefined variables behave with parentheses + assert_parity("(undefined_var or true) and false", "false") + assert_parity("true and (undefined_var or false)", "false") + assert_parity("(undefined_var and true) or true", "true") + assert_parity("false or (undefined_var and false)", "false") + end + + def test_comparison_chaining_with_parentheses + # Testing how comparison chains work with parentheses + assert_parity("(1 < 2) and (2 < 3) and (3 < 4)", "true") + assert_parity("(1 < 2) and ((2 > 3) or (3 < 4))", "true") + assert_parity( + "(a > b) or ((c < d) and (e == f))", + "true", + { "a" => 5, "b" => 3, "c" => 1, "d" => 2, "e" => 7, "f" => 7 }, + ) + assert_parity( + "(a > b) or ((c < d) and (e == f))", + "false", + { "a" => 3, "b" => 5, "c" => 2, "d" => 1, "e" => 7, "f" => 8 }, + ) + end + + def test_deeply_nested_expressions + # Testing very deep nesting to ensure parser handles it correctly + assert_parity("(((true and true) or (false and false)) and ((true or false) and (true)))", "true") + assert_parity( + "(((a or b) and c) or (d and (e or f)))", + "true", + { "a" => false, "b" => true, "c" => true, "d" => true, "e" => true, "f" => false }, + ) + end + + def test_malformed_parentheses + # Unbalanced parentheses - missing closing parenthesis + template = "{% if (true and false %}true{% else %}false{% endif %}" + assert_raises(Liquid::SyntaxError) { Liquid::Template.parse(template) } + + # Unbalanced parentheses - missing opening parenthesis + template = "{% if true and false) %}true{% else %}false{% endif %}" + assert_raises(Liquid::SyntaxError) { Liquid::Template.parse(template) } + + # Empty parentheses + template = "{% if () %}true{% else %}false{% endif %}" + assert_raises(Liquid::SyntaxError) { Liquid::Template.parse(template) } + + # Consecutive opening parentheses without operators + template = "{% if ((true) %}true{% else %}false{% endif %}" + assert_raises(Liquid::SyntaxError) { Liquid::Template.parse(template) } + + # Consecutive closing parentheses without proper opening + template = "{% if (true)) %}true{% else %}false{% endif %}" + assert_raises(Liquid::SyntaxError) { Liquid::Template.parse(template) } + + # Parentheses with missing operand + template = "{% if (and true) %}true{% else %}false{% endif %}" + assert_raises(Liquid::SyntaxError) { Liquid::Template.parse(template) } + + # Operator followed immediately by closing parenthesis + template = "{% if (true and) %}true{% else %}false{% endif %}" + assert_raises(Liquid::SyntaxError) { Liquid::Template.parse(template) } + + # Nested malformed parentheses + template = "{% if (true and (false or true) %}true{% else %}false{% endif %}" + assert_raises(Liquid::SyntaxError) { Liquid::Template.parse(template) } + + # Double parentheses with no content between them + template = "{% if true and (()) %}true{% else %}false{% endif %}" + assert_raises(Liquid::SyntaxError) { Liquid::Template.parse(template) } + + # Misplaced parentheses around operators + template = "{% if true (and) false %}true{% else %}false{% endif %}" + assert_raises(Liquid::SyntaxError) { Liquid::Template.parse(template) } + + # Parentheses at wrong position in expression + template = "{% if true) and (false %}true{% else %}false{% endif %}" + assert_raises(Liquid::SyntaxError) { Liquid::Template.parse(template) } + end +end diff --git a/test/unit/boolean_unit_test.rb b/test/unit/boolean_unit_test.rb index dabf65094..72eca878b 100644 --- a/test/unit/boolean_unit_test.rb +++ b/test/unit/boolean_unit_test.rb @@ -1,6 +1,7 @@ # frozen_string_literal: true require 'test_helper' +require 'test_boolean_helper' class BooleanUnitTest < Minitest::Test include Liquid @@ -487,60 +488,4 @@ def test_assign_boolean_expression_to_variable template = Liquid::Template.parse("{% assign is_preview_mode = content_for_header contains 'foo' or content_for_header contains 'bar' %}{{ is_preview_mode }}") assert_equal("false", template.render(context)) end - - private - - def assert_with_lax_parsing(template, expected_output, context = {}) - prev_error_mode = Liquid::Environment.default.error_mode - Liquid::Environment.default.error_mode = :lax - - begin - actual_output = Liquid::Template.parse(template).render(context) - rescue StandardError => e - actual_output = e.message - ensure - Liquid::Environment.default.error_mode = prev_error_mode - end - - assert_equal(expected_output.strip, actual_output.strip) - end - - def assert_parity(liquid_expression, expected_result, args = {}) - assert_condition(liquid_expression, expected_result, args) - assert_expression(liquid_expression, expected_result, args) - end - - def assert_expression(liquid_expression, expected_result, args = {}) - assert_parity_scenario(:expression, "{{ #{liquid_expression} }}", expected_result, args) - end - - def assert_condition(liquid_condition, expected_result, args = {}) - assert_parity_scenario(:condition, "{% if #{liquid_condition} %}true{% else %}false{% endif %}", expected_result, args) - end - - def assert_parity_scenario(kind, template, exp_output, args = {}) - act_output = Liquid::Template.parse(template).render(args) - - assert_equal(exp_output, act_output, <<~ERROR_MESSAGE) - #{kind.to_s.capitalize} template failure: - --- - #{template} - --- - args: #{args.inspect} - ERROR_MESSAGE - end - - class LinkDrop < Liquid::Drop - attr_accessor :levels, :links, :title, :type, :url - - def initialize(levels: nil, links: nil, title: nil, type: nil, url: nil) - super() - - @levels = levels - @links = links - @title = title - @type = type - @url = url - end - end end diff --git a/test/unit/expression/logical_expression_test.rb b/test/unit/expression/logical_expression_test.rb new file mode 100644 index 000000000..2bfe4ba15 --- /dev/null +++ b/test/unit/expression/logical_expression_test.rb @@ -0,0 +1,219 @@ +# frozen_string_literal: true + +require 'test_helper' +require 'test_boolean_helper' + +class LogicalExpressionTest < Minitest::Test + include Liquid + + def setup + @ss = StringScanner.new("") + @cache = {} + end + + def test_logical_detection + assert(Expression::LogicalExpression.logical?("foo and bar")) + assert(Expression::LogicalExpression.logical?("foo or bar")) + assert(Expression::LogicalExpression.logical?("true and false")) + assert(Expression::LogicalExpression.logical?("1 or 0")) + + refute(Expression::LogicalExpression.logical?("foo")) + refute(Expression::LogicalExpression.logical?("1 == 1")) + refute(Expression::LogicalExpression.logical?("a contains b")) + refute(Expression::LogicalExpression.logical?("not foo")) + end + + def test_parenthesized_logical_detection + assert(Expression::LogicalExpression.logical?("a and (b or c)")) + assert(Expression::LogicalExpression.logical?("(a or b) and c")) + end + + def test_boolean_operator_detection + assert(Expression::LogicalExpression.boolean_operator?("and")) + assert(Expression::LogicalExpression.boolean_operator?("or")) + + refute(Expression::LogicalExpression.boolean_operator?("not")) + refute(Expression::LogicalExpression.boolean_operator?("==")) + refute(Expression::LogicalExpression.boolean_operator?("contains")) + refute(Expression::LogicalExpression.boolean_operator?("foo")) + end + + def test_basic_parsing + result = Expression::LogicalExpression.parse("true and false", @ss, @cache) + assert_instance_of(Condition, result) + + result = Expression::LogicalExpression.parse("a or b", @ss, @cache) + assert_instance_of(Condition, result) + end + + def test_parsing_with_different_expressions + # Test with simple variable expressions + result = Expression::LogicalExpression.parse("var1 and var2", @ss, @cache) + assert_instance_of(Condition, result) + + # Test with comparison expressions + result = Expression::LogicalExpression.parse("a == 1 and b != 2", @ss, @cache) + assert_instance_of(Condition, result) + end + + def test_parsing_complex_expressions + # Test with nested logical expressions + result = Expression::LogicalExpression.parse("a and b or c", @ss, @cache) + assert_instance_of(Condition, result) + + result = Expression::LogicalExpression.parse("a or b and c", @ss, @cache) + assert_instance_of(Condition, result) + end + + def test_parsing_parenthesized_expressions + result = Expression::LogicalExpression.parse("(a and b) or c", @ss, @cache) + assert_instance_of(Condition, result) + + result = Expression::LogicalExpression.parse("a and (b or c)", @ss, @cache) + assert_instance_of(Condition, result) + + # Test with complex expressions + result = Expression::LogicalExpression.parse("(a or b) and (c or d)", @ss, @cache) + assert_instance_of(Condition, result) + end + + def test_evaluation_of_parsed_expressions + context = Liquid::Context.new( + "a" => true, + "b" => false, + "c" => true, + "d" => false, + ) + + # Test simple logical expressions + expr = Expression::LogicalExpression.parse("a and c", @ss, @cache) + assert_equal(true, expr.evaluate(context)) + + expr = Expression::LogicalExpression.parse("a and b", @ss, @cache) + assert_equal(false, expr.evaluate(context)) + + expr = Expression::LogicalExpression.parse("b or c", @ss, @cache) + assert_equal(true, expr.evaluate(context)) + + expr = Expression::LogicalExpression.parse("b or d", @ss, @cache) + assert_equal(false, expr.evaluate(context)) + end + + def test_evaluation_of_complex_expressions + context = Liquid::Context.new( + "a" => true, + "b" => false, + "c" => true, + "d" => false, + ) + + # Test complex logical expressions + expr = Expression::LogicalExpression.parse("a and b or c", @ss, @cache) + assert_equal(true, expr.evaluate(context)) + end + + def test_evaluation_of_parenthesized_expressions + context = Liquid::Context.new( + "a" => true, + "b" => false, + "c" => true, + "d" => false, + ) + + expr = Expression::LogicalExpression.parse("a and (b or d)", @ss, @cache) + assert_equal(false, expr.evaluate(context)) + + expr = Expression::LogicalExpression.parse("(a or b) and (c or d)", @ss, @cache) + assert_equal(true, expr.evaluate(context)) + + expr = Expression::LogicalExpression.parse("(a or b) and (b or d)", @ss, @cache) + assert_equal(false, expr.evaluate(context)) + end + + def test_precedence_rules + context = Liquid::Context.new( + "a" => true, + "b" => false, + "c" => true, + ) + + # Test precedence rules (AND has higher precedence than OR) + # This should be interpreted as: a and (b or c) + expr1 = Expression::LogicalExpression.parse("a and b or c", @ss, @cache) + assert_equal(true, expr1.evaluate(context)) + + # Change context to make the expressions evaluate differently + context = Liquid::Context.new( + "a" => false, + "b" => false, + "c" => true, + ) + + # With these values, "a and (b or c)" would be false + expr1 = Expression::LogicalExpression.parse("a and b or c", @ss, @cache) + assert_equal(false, expr1.evaluate(context)) + end + + def test_precedence_with_parentheses + context = Liquid::Context.new( + "a" => true, + "b" => false, + "c" => true, + ) + + # This should be interpreted as: (a and b) or c + expr2 = Expression::LogicalExpression.parse("(a and b) or c", @ss, @cache) + assert_equal(true, expr2.evaluate(context)) + + # Change context to make the expressions evaluate differently + context = Liquid::Context.new( + "a" => false, + "b" => false, + "c" => true, + ) + + # But "(a and b) or c" would be true + expr2 = Expression::LogicalExpression.parse("(a and b) or c", @ss, @cache) + assert_equal(true, expr2.evaluate(context)) + end + + def test_integration_with_if_tag + # Test that our expressions work properly in actual templates + assert_template_result("true", "{% if true and true %}true{% else %}false{% endif %}") + assert_template_result("false", "{% if true and false %}true{% else %}false{% endif %}") + assert_template_result("true", "{% if false or true %}true{% else %}false{% endif %}") + assert_template_result("false", "{% if false or false %}true{% else %}false{% endif %}") + end + + def test_integration_with_parenthesized_if_tag + # Test with parenthesized expressions + assert_template_result("true", "{% if (true and false) or true %}true{% else %}false{% endif %}") + assert_template_result("false", "{% if true and (false or false) %}true{% else %}false{% endif %}") + assert_template_result("true", "{% if true and (false or true) %}true{% else %}false{% endif %}") + end + + def test_integration_with_variables + # Test with variables + template = "{% if a and b %}true{% else %}false{% endif %}" + assert_template_result("true", template, { "a" => true, "b" => true }) + assert_template_result("false", template, { "a" => true, "b" => false }) + + template = "{% if a or b %}true{% else %}false{% endif %}" + assert_template_result("true", template, { "a" => true, "b" => false }) + assert_template_result("false", template, { "a" => false, "b" => false }) + end + + def test_integration_with_parenthesized_variables + # Test with parenthesized expressions + template = "{% if (a and b) or c %}true{% else %}false{% endif %}" + assert_template_result("true", template, { "a" => true, "b" => true, "c" => false }) + assert_template_result("true", template, { "a" => false, "b" => false, "c" => true }) + assert_template_result("false", template, { "a" => false, "b" => false, "c" => false }) + + template = "{% if a and (b or c) %}true{% else %}false{% endif %}" + assert_template_result("true", template, { "a" => true, "b" => true, "c" => false }) + assert_template_result("true", template, { "a" => true, "b" => false, "c" => true }) + assert_template_result("false", template, { "a" => true, "b" => false, "c" => false }) + assert_template_result("false", template, { "a" => false, "b" => true, "c" => true }) + end +end diff --git a/test/unit/range_unit_test.rb b/test/unit/range_unit_test.rb new file mode 100644 index 000000000..98275707f --- /dev/null +++ b/test/unit/range_unit_test.rb @@ -0,0 +1,135 @@ +# frozen_string_literal: true + +require 'test_helper' + +class RangeUnitTest < Minitest::Test + include Liquid + + def test_basic_range_creation + assert_template_result("1 2 3 4 5", "{% for i in (1..5) %}{{ i }} {% endfor %}") + end + + def test_range_with_variables + assert_template_result("3 4 5", "{% assign start = 3 %}{% for i in (start..5) %}{{ i }} {% endfor %}") + assert_template_result("1 2 3", "{% assign end = 3 %}{% for i in (1..end) %}{{ i }} {% endfor %}") + assert_template_result("2 3 4", "{% assign start = 2 %}{% assign end = 4 %}{% for i in (start..end) %}{{ i }} {% endfor %}") + end + + def test_range_with_whitespace + assert_template_result("1 2 3", "{% for i in ( 1 .. 3 ) %}{{ i }} {% endfor %}") + assert_template_result("1 2 3", "{% for i in (1 .. 3) %}{{ i }} {% endfor %}") + end + + def test_range_with_expressions + assert_template_result("3 4 5", "{% assign x = 1 %}{% assign start = x | plus: 2 %}{% for i in (start..5) %}{{ i }} {% endfor %}") + assert_template_result("1 2 3", "{% assign x = 2 %}{% assign end = x | plus: 1 %}{% for i in (1..end) %}{{ i }} {% endfor %}") + end + + def test_range_with_literals_in_iteration + assert_template_result("1 2 3 4 5", "{% for i in (1..5) %}{{ i }} {% endfor %}") + end + + def test_range_size_and_first_last + assert_template_result("5", "{{ (1..5) | size }}") + assert_template_result("1", "{{ (1..5) | first }}") + assert_template_result("5", "{{ (1..5) | last }}") + end + + def test_empty_ranges + assert_template_result("", "{% for i in (5..1) %}{{ i }}{% endfor %}") + end + + def test_ranges_in_conditionals + assert_template_result("yes", "{% if 3 >= (1..5) %}no{% else %}yes{% endif %}") + assert_template_result("yes", "{% if (1..5) contains 3 %}yes{% else %}no{% endif %}") + assert_template_result("no", "{% if (1..5) contains 6 %}yes{% else %}no{% endif %}") + end + + def test_range_with_negative_numbers + assert_template_result("-3 -2 -1 0", "{% for i in (-3..0) %}{{ i }} {% endfor %}") + end + + def test_range_with_floats + # Liquid doesn't support float ranges, should either error or not iterate + template = "{% for i in (1.5..3.5) %}{{ i }} {% endfor %}" + # Floats are rounded down to the nearest integer + assert_template_result("1 2 3", template) + end + + # def test_ranges_with_calculated_endpoints + # assert_template_result( + # "3 4 5", + # "{% assign start = 1 %}{% assign end = 7 %}{% for i in (start | plus: 2 .. end | minus: 2) %}{{ i }} {% endfor %}", + # ) + # end + + def test_malformed_ranges + # Missing start value + assert_raises(Liquid::SyntaxError) { Liquid::Template.parse("{% for i in (..5) %}{{ i }}{% endfor %}") } + # Missing end value + assert_raises(Liquid::SyntaxError) { Liquid::Template.parse("{% for i in (1..) %}{{ i }}{% endfor %}") } + # Missing both values + assert_raises(Liquid::SyntaxError) { Liquid::Template.parse("{% for i in (..) %}{{ i }}{% endfor %}") } + # Wrong syntax (no parentheses) + assert_raises(Liquid::SyntaxError) { Liquid::Template.parse("{% for i in 1..5 %}{{ i }}{% endfor %}") } + # Unbalanced parentheses + assert_raises(Liquid::SyntaxError) { Liquid::Template.parse("{% for i in (1..5 %}{{ i }}{% endfor %}") } + # Invalid characters in range + assert_raises(Liquid::SyntaxError) { Liquid::Template.parse("{% for i in (#..@) %}{{ i }}{% endfor %}") } + # Invalid range + assert_raises(Liquid::SyntaxError) { Liquid::Template.parse("{% assign start = 1 %}{% assign end = 7 %}{% for i in (start | plus: 2 .. end | minus: 2) %}{{ i }} {% endfor %}") } + end + + def test_ranges_with_strings_and_variables + assert_template_result( + "3 4 5", + "{% assign range = (3..5) %}{% for i in range %}{{ i }} {% endfor %}", + ) + assert_template_result( + "4 5 6", + "{% assign start = 4 %}{% assign range = (start..6) %}{% for i in range %}{{ i }} {% endfor %}", + ) + end + + def test_ranges_with_limit_and_offset + assert_template_result( + "2 3", + "{% for i in (1..5) limit:2 offset:1 %}{{ i }} {% endfor %}", + ) + assert_template_result( + "3 4 5", + "{% for i in (1..5) offset:2 %}{{ i }} {% endfor %}", + ) + assert_template_result( + "1 2", + "{% for i in (1..5) limit:2 %}{{ i }} {% endfor %}", + ) + end + + def test_reversed_ranges + assert_template_result( + "5 4 3 2 1", + "{% for i in (1..5) reversed %}{{ i }} {% endfor %}", + ) + end + + def test_variable_ranges_with_reversed + assert_template_result( + "4 3 2 1", + "{% assign num = 4 %}{% for i in (1..num) reversed %}{{ i }} {% endfor %}", + ) + end + + def test_assigned_ranges_with_reversed + assert_template_result( + "5 4 3 2 1", + "{% assign range = (1..5) %}{% for i in range reversed %}{{ i }} {% endfor %}", + ) + end + + private + + def assert_template_result(expected, template, assigns = {}) + assert_equal(expected, Liquid::Template.parse(template).render!(assigns).strip) + end +end