mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-19 10:52:48 -07:00
Merge pull request #570 from Shopify/fix-strict-conditions
Fix condition parse order in strict mode
This commit is contained in:
@@ -78,16 +78,16 @@ module Liquid
|
|||||||
|
|
||||||
def strict_parse(markup)
|
def strict_parse(markup)
|
||||||
p = Parser.new(markup)
|
p = Parser.new(markup)
|
||||||
|
condition = parse_binary_comparison(p)
|
||||||
condition = parse_comparison(p)
|
|
||||||
|
|
||||||
while op = (p.id?('and'.freeze) || p.id?('or'.freeze))
|
|
||||||
new_cond = parse_comparison(p)
|
|
||||||
new_cond.send(op, condition)
|
|
||||||
condition = new_cond
|
|
||||||
end
|
|
||||||
p.consume(:end_of_string)
|
p.consume(:end_of_string)
|
||||||
|
condition
|
||||||
|
end
|
||||||
|
|
||||||
|
def parse_binary_comparison(p)
|
||||||
|
condition = parse_comparison(p)
|
||||||
|
if op = (p.id?('and'.freeze) || p.id?('or'.freeze))
|
||||||
|
condition.send(op, parse_binary_comparison(p))
|
||||||
|
end
|
||||||
condition
|
condition
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -166,4 +166,25 @@ class IfElseTagTest < Minitest::Test
|
|||||||
assert_template_result('', %({% if 1 or throw or or 1 %}yes{% endif %}))
|
assert_template_result('', %({% if 1 or throw or or 1 %}yes{% endif %}))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_multiple_conditions
|
||||||
|
tpl = "{% if a or b and c %}true{% else %}false{% endif %}"
|
||||||
|
|
||||||
|
tests = {
|
||||||
|
[true, true, true] => true,
|
||||||
|
[true, true, false] => true,
|
||||||
|
[true, false, true] => true,
|
||||||
|
[true, false, false] => true,
|
||||||
|
[false, true, true] => true,
|
||||||
|
[false, true, false] => false,
|
||||||
|
[false, false, true] => false,
|
||||||
|
[false, false, false] => false,
|
||||||
|
}
|
||||||
|
|
||||||
|
tests.each do |vals, expected|
|
||||||
|
a, b, c = vals
|
||||||
|
assigns = { 'a' => a, 'b' => b, 'c' => c }
|
||||||
|
assert_template_result expected.to_s, tpl, assigns, assigns.to_s
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
+2
-2
@@ -32,13 +32,13 @@ module Minitest
|
|||||||
include Liquid
|
include Liquid
|
||||||
|
|
||||||
def assert_template_result(expected, template, assigns = {}, message = nil)
|
def assert_template_result(expected, template, assigns = {}, message = nil)
|
||||||
assert_equal expected, Template.parse(template).render!(assigns)
|
assert_equal expected, Template.parse(template).render!(assigns), message
|
||||||
end
|
end
|
||||||
|
|
||||||
def assert_template_result_matches(expected, template, assigns = {}, message = nil)
|
def assert_template_result_matches(expected, template, assigns = {}, message = nil)
|
||||||
return assert_template_result(expected, template, assigns, message) unless expected.is_a? Regexp
|
return assert_template_result(expected, template, assigns, message) unless expected.is_a? Regexp
|
||||||
|
|
||||||
assert_match expected, Template.parse(template).render!(assigns)
|
assert_match expected, Template.parse(template).render!(assigns), message
|
||||||
end
|
end
|
||||||
|
|
||||||
def assert_match_syntax_error(match, template, registers = {})
|
def assert_match_syntax_error(match, template, registers = {})
|
||||||
|
|||||||
Reference in New Issue
Block a user