fixed conditions with strings containing "and"/"or"

This commit is contained in:
James MacAulay
2009-09-14 15:01:26 -04:00
parent d1d6febfc1
commit f42ce88456
4 changed files with 23 additions and 8 deletions
+4
View File
@@ -21,6 +21,10 @@ module Liquid
def self.operators
@@operators
end
def self.operator_regexp
Regexp.new(@@operators.keys.sort_by(&:length).reverse.map {|k| Regexp.escape(k)}.join('|'))
end
attr_reader :attachment
attr_accessor :left, :operator, :right
+5 -2
View File
@@ -13,7 +13,8 @@ module Liquid
#
class If < Block
SyntaxHelp = "Syntax Error in tag 'if' - Valid syntax: if [expression]"
Syntax = /(#{QuotedFragment})\s*([=!<>a-z_]+)?\s*(#{QuotedFragment})?/
Syntax = /(#{QuotedFragment})\s*(?:([=!<>a-z_]+)\s*(#{QuotedFragment}))?/
ExpressionsAndOperators = /and|or|#{QuotedFragment}\s*(?:(?!and|or)(?:[=!<>a-z_]+)\s*#{QuotedFragment})?/
def initialize(tag_name, markup, tokens)
@@ -50,7 +51,9 @@ module Liquid
ElseCondition.new
else
expressions = markup.split(/\b(and|or)\b/).reverse
# expressions = markup.split(/\b(and|or)\b/).reverse
expressions = markup.scan(ExpressionsAndOperators).flatten.compact.reverse
raise(SyntaxError, SyntaxHelp) unless expressions.shift =~ Syntax
condition = Condition.new($1, $2, $3)
+8
View File
@@ -36,6 +36,14 @@ class IfElseTest < Test::Unit::TestCase
assert_template_result('','{% if a == false or b == false %} YES {% endif %}', 'a' => true, 'b' => true)
end
def test_comparison_of_strings_containing_and_or_or
assert_nothing_raised do
awful_markup = "a == 'and' and b == 'or' and c == 'foo and bar' and d == 'bar or baz' and e == 'foo' and foo and bar"
assigns = {'a' => 'and', 'b' => 'or', 'c' => 'foo and bar', 'd' => 'bar or baz', 'e' => 'foo', 'foo' => true, 'bar' => true}
assert_template_result(' YES ',"{% if #{awful_markup} %} YES {% endif %}", assigns)
end
end
def test_if_and
assert_template_result(' YES ','{% if true and true %} YES {% endif %}')
assert_template_result('','{% if false and true %} YES {% endif %}')
+6 -6
View File
@@ -17,31 +17,31 @@ class StatementsTest < Test::Unit::TestCase
assert_equal expected, Template.parse(text).render
end
def test_true_lq_true
def test_zero_gt_zero
text = %| {% if 0 > 0 %} true {% else %} false {% endif %} |
expected = %| false |
assert_equal expected, Template.parse(text).render
end
def test_one_lq_zero
def test_one_gt_zero
text = %| {% if 1 > 0 %} true {% else %} false {% endif %} |
expected = %| true |
assert_equal expected, Template.parse(text).render
end
def test_zero_lq_one
def test_zero_lt_one
text = %| {% if 0 < 1 %} true {% else %} false {% endif %} |
expected = %| true |
assert_equal expected, Template.parse(text).render
end
def test_zero_lq_or_equal_one
def test_zero_lt_or_equal_zero
text = %| {% if 0 <= 0 %} true {% else %} false {% endif %} |
expected = %| true |
assert_equal expected, Template.parse(text).render
end
def test_zero_lq_or_equal_one_involving_nil
def test_zero_lt_or_equal_zero_involving_nil
text = %| {% if null <= 0 %} true {% else %} false {% endif %} |
expected = %| false |
assert_equal expected, Template.parse(text).render
@@ -52,7 +52,7 @@ class StatementsTest < Test::Unit::TestCase
assert_equal expected, Template.parse(text).render
end
def test_zero_lqq_or_equal_one
def test_zero_gt_or_equal_zero
text = %| {% if 0 >= 0 %} true {% else %} false {% endif %} |
expected = %| true |
assert_equal expected, Template.parse(text).render