mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-12 23:40:45 -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:
+40
-41
@@ -15,16 +15,15 @@ module Liquid
|
|||||||
SyntaxHelp = "Syntax Error in tag 'if' - Valid syntax: if [expression]"
|
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 = /(?:\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)
|
||||||
|
|
||||||
super
|
super
|
||||||
end
|
end
|
||||||
|
|
||||||
def unknown_tag(tag, markup, tokens)
|
def unknown_tag(tag, markup, tokens)
|
||||||
if ['elsif', 'else'].include?(tag)
|
if ['elsif', 'else'].include?(tag)
|
||||||
push_block(tag, markup)
|
push_block(tag, markup)
|
||||||
@@ -32,49 +31,49 @@ module Liquid
|
|||||||
super
|
super
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def render(context)
|
def render(context)
|
||||||
context.stack do
|
context.stack do
|
||||||
@blocks.each do |block|
|
@blocks.each do |block|
|
||||||
if block.evaluate(context)
|
if block.evaluate(context)
|
||||||
return render_all(block.attachment, context)
|
return render_all(block.attachment, context)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
''
|
''
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
|
||||||
|
|
||||||
def push_block(tag, markup)
|
|
||||||
block = if tag == 'else'
|
|
||||||
ElseCondition.new
|
|
||||||
else
|
|
||||||
|
|
||||||
expressions = markup.scan(ExpressionsAndOperators).reverse
|
|
||||||
raise(SyntaxError, SyntaxHelp) unless expressions.shift =~ Syntax
|
|
||||||
|
|
||||||
condition = Condition.new($1, $2, $3)
|
private
|
||||||
|
|
||||||
while not expressions.empty?
|
def push_block(tag, markup)
|
||||||
operator = expressions.shift
|
block = if tag == 'else'
|
||||||
|
ElseCondition.new
|
||||||
raise(SyntaxError, SyntaxHelp) unless expressions.shift.to_s =~ Syntax
|
else
|
||||||
|
|
||||||
new_condition = Condition.new($1, $2, $3)
|
expressions = markup.scan(ExpressionsAndOperators).reverse
|
||||||
new_condition.send(operator.to_sym, condition)
|
raise(SyntaxError, SyntaxHelp) unless expressions.shift =~ Syntax
|
||||||
condition = new_condition
|
|
||||||
end
|
condition = Condition.new($1, $2, $3)
|
||||||
|
|
||||||
condition
|
while not expressions.empty?
|
||||||
|
operator = expressions.shift
|
||||||
|
|
||||||
|
raise(SyntaxError, SyntaxHelp) unless expressions.shift.to_s =~ Syntax
|
||||||
|
|
||||||
|
new_condition = Condition.new($1, $2, $3)
|
||||||
|
new_condition.send(operator.to_sym, condition)
|
||||||
|
condition = new_condition
|
||||||
|
end
|
||||||
|
|
||||||
|
condition
|
||||||
|
end
|
||||||
|
|
||||||
|
@blocks.push(block)
|
||||||
|
@nodelist = block.attach(Array.new)
|
||||||
end
|
end
|
||||||
|
|
||||||
@blocks.push(block)
|
|
||||||
@nodelist = block.attach(Array.new)
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
Template.register_tag('if', If)
|
Template.register_tag('if', If)
|
||||||
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