mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-29 15:15:19 -07:00
Conditions
- added test to assert that conditions can contain conditions within its value (eg 'a-and-b') Tags - indented the if tag Tests - added ruby-debug to the test_helper - indented some tests
This commit is contained in:
+18
-19
@@ -17,7 +17,6 @@ module Liquid
|
|||||||
ExpressionsAndOperators = /(?:\b(?:and|or)\b|(?:\s*(?!\b(?:and|or)\b)(?:#{QuotedFragment}|\S+)\s*)+)/
|
ExpressionsAndOperators = /(?:\b(?:and|or)\b|(?:\s*(?!\b(?:and|or)\b)(?:#{QuotedFragment}|\S+)\s*)+)/
|
||||||
|
|
||||||
def initialize(tag_name, markup, tokens)
|
def initialize(tag_name, markup, tokens)
|
||||||
|
|
||||||
@blocks = []
|
@blocks = []
|
||||||
|
|
||||||
push_block('if', markup)
|
push_block('if', markup)
|
||||||
@@ -46,33 +45,33 @@ module Liquid
|
|||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def push_block(tag, markup)
|
def push_block(tag, markup)
|
||||||
block = if tag == 'else'
|
block = if tag == 'else'
|
||||||
ElseCondition.new
|
ElseCondition.new
|
||||||
else
|
else
|
||||||
|
|
||||||
expressions = markup.scan(ExpressionsAndOperators).reverse
|
expressions = markup.scan(ExpressionsAndOperators).reverse
|
||||||
raise(SyntaxError, SyntaxHelp) unless expressions.shift =~ Syntax
|
raise(SyntaxError, SyntaxHelp) unless expressions.shift =~ Syntax
|
||||||
|
|
||||||
condition = Condition.new($1, $2, $3)
|
condition = Condition.new($1, $2, $3)
|
||||||
|
|
||||||
while not expressions.empty?
|
while not expressions.empty?
|
||||||
operator = expressions.shift
|
operator = expressions.shift
|
||||||
|
|
||||||
raise(SyntaxError, SyntaxHelp) unless expressions.shift.to_s =~ Syntax
|
raise(SyntaxError, SyntaxHelp) unless expressions.shift.to_s =~ Syntax
|
||||||
|
|
||||||
new_condition = Condition.new($1, $2, $3)
|
new_condition = Condition.new($1, $2, $3)
|
||||||
new_condition.send(operator.to_sym, condition)
|
new_condition.send(operator.to_sym, condition)
|
||||||
condition = new_condition
|
condition = new_condition
|
||||||
|
end
|
||||||
|
|
||||||
|
condition
|
||||||
end
|
end
|
||||||
|
|
||||||
condition
|
@blocks.push(block)
|
||||||
|
@nodelist = block.attach(Array.new)
|
||||||
end
|
end
|
||||||
|
|
||||||
@blocks.push(block)
|
|
||||||
@nodelist = block.attach(Array.new)
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -48,16 +48,14 @@ class ConditionTest < Test::Unit::TestCase
|
|||||||
@context = Liquid::Context.new
|
@context = Liquid::Context.new
|
||||||
@context['array'] = [1,2,3,4,5]
|
@context['array'] = [1,2,3,4,5]
|
||||||
|
|
||||||
assert_evalutes_false "array", 'contains', '0'
|
assert_evalutes_false "array", 'contains', '0'
|
||||||
assert_evalutes_true "array", 'contains', '1'
|
assert_evalutes_true "array", 'contains', '1'
|
||||||
assert_evalutes_true "array", 'contains', '2'
|
assert_evalutes_true "array", 'contains', '2'
|
||||||
assert_evalutes_true "array", 'contains', '3'
|
assert_evalutes_true "array", 'contains', '3'
|
||||||
assert_evalutes_true "array", 'contains', '4'
|
assert_evalutes_true "array", 'contains', '4'
|
||||||
assert_evalutes_true "array", 'contains', '5'
|
assert_evalutes_true "array", 'contains', '5'
|
||||||
assert_evalutes_false "array", 'contains', '6'
|
assert_evalutes_false "array", 'contains', '6'
|
||||||
|
assert_evalutes_false "array", 'contains', '"1"'
|
||||||
assert_evalutes_false "array", 'contains', '"1"'
|
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_contains_returns_false_for_nil_operands
|
def test_contains_returns_false_for_nil_operands
|
||||||
@@ -94,17 +92,23 @@ class ConditionTest < Test::Unit::TestCase
|
|||||||
assert_equal false, condition.evaluate
|
assert_equal false, condition.evaluate
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
def test_should_allow_custom_proc_operator
|
def test_should_allow_custom_proc_operator
|
||||||
Condition.operators['starts_with'] = Proc.new { |cond, left, right| left =~ %r{^#{right}} }
|
Condition.operators['starts_with'] = Proc.new { |cond, left, right| left =~ %r{^#{right}} }
|
||||||
|
|
||||||
assert_evalutes_true "'bob'", 'starts_with', "'b'"
|
assert_evalutes_true "'bob'", 'starts_with', "'b'"
|
||||||
assert_evalutes_false "'bob'", 'starts_with', "'o'"
|
assert_evalutes_false "'bob'", 'starts_with', "'o'"
|
||||||
|
|
||||||
ensure
|
ensure
|
||||||
Condition.operators.delete 'starts_with'
|
Condition.operators.delete 'starts_with'
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_left_or_right_may_contain_operators
|
||||||
|
@context = Liquid::Context.new
|
||||||
|
@context['one'] = @context['another'] = "gnomeslab-and-or-liquid"
|
||||||
|
|
||||||
|
assert_evalutes_true "one", '==', "another"
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
def assert_evalutes_true(left, op, right)
|
def assert_evalutes_true(left, op, right)
|
||||||
assert Condition.new(left, op, right).evaluate(@context || Liquid::Context.new),
|
assert Condition.new(left, op, right).evaluate(@context || Liquid::Context.new),
|
||||||
|
|||||||
@@ -150,4 +150,10 @@ class IfElseTest < Test::Unit::TestCase
|
|||||||
ensure
|
ensure
|
||||||
Condition.operators.delete 'contains'
|
Condition.operators.delete 'contains'
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# def test_operators_are_ignored_unless_isolated
|
||||||
|
# Condition.operators['contains'] = :[]
|
||||||
|
#
|
||||||
|
# assert_template_result('yes', %({% if 'gnomeslab' == 'gnomeslab' %}yes{% endif %}))
|
||||||
|
# end
|
||||||
end # IfElseTest
|
end # IfElseTest
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ require 'test/unit'
|
|||||||
require 'test/unit/assertions'
|
require 'test/unit/assertions'
|
||||||
require 'caller'
|
require 'caller'
|
||||||
require 'breakpoint'
|
require 'breakpoint'
|
||||||
|
require 'ruby-debug'
|
||||||
require File.join File.dirname(__FILE__), '..', 'lib', 'liquid'
|
require File.join File.dirname(__FILE__), '..', 'lib', 'liquid'
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user