mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-15 00:40:40 -07:00
committed by
Tobias Lütke
parent
37580976db
commit
8d27864845
+30
-33
@@ -3,7 +3,7 @@ module Liquid
|
||||
#
|
||||
# Example:
|
||||
#
|
||||
# c = Condition.new('1', '==', '1')
|
||||
# c = Condition.new('1', '==', '1')
|
||||
# c.evaluate #=> true
|
||||
#
|
||||
class Condition #:nodoc:
|
||||
@@ -17,33 +17,33 @@ module Liquid
|
||||
'<=' => :<=,
|
||||
'contains' => lambda { |cond, left, right| left.include?(right) },
|
||||
}
|
||||
|
||||
|
||||
def self.operators
|
||||
@@operators
|
||||
end
|
||||
|
||||
attr_reader :attachment
|
||||
attr_accessor :left, :operator, :right
|
||||
|
||||
|
||||
def initialize(left = nil, operator = nil, right = nil)
|
||||
@left, @operator, @right = left, operator, right
|
||||
@child_relation = nil
|
||||
@child_condition = nil
|
||||
end
|
||||
|
||||
|
||||
def evaluate(context = Context.new)
|
||||
result = interpret_condition(left, right, operator, context)
|
||||
|
||||
result = interpret_condition(left, right, operator, context)
|
||||
|
||||
case @child_relation
|
||||
when :or
|
||||
when :or
|
||||
result || @child_condition.evaluate(context)
|
||||
when :and
|
||||
when :and
|
||||
result && @child_condition.evaluate(context)
|
||||
else
|
||||
result
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
def or(condition)
|
||||
@child_relation, @child_condition = :or, condition
|
||||
end
|
||||
@@ -51,25 +51,25 @@ module Liquid
|
||||
def and(condition)
|
||||
@child_relation, @child_condition = :and, condition
|
||||
end
|
||||
|
||||
|
||||
def attach(attachment)
|
||||
@attachment = attachment
|
||||
end
|
||||
|
||||
|
||||
def else?
|
||||
false
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
def inspect
|
||||
"#<Condition #{[@left, @operator, @right].compact.join(' ')}>"
|
||||
end
|
||||
|
||||
|
||||
private
|
||||
|
||||
|
||||
def equal_variables(left, right)
|
||||
if left.is_a?(Symbol)
|
||||
if right.respond_to?(left)
|
||||
return right.send(left.to_s)
|
||||
return right.send(left.to_s)
|
||||
else
|
||||
return nil
|
||||
end
|
||||
@@ -77,47 +77,44 @@ module Liquid
|
||||
|
||||
if right.is_a?(Symbol)
|
||||
if left.respond_to?(right)
|
||||
return left.send(right.to_s)
|
||||
return left.send(right.to_s)
|
||||
else
|
||||
return nil
|
||||
end
|
||||
end
|
||||
|
||||
left == right
|
||||
end
|
||||
left == right
|
||||
end
|
||||
|
||||
def interpret_condition(left, right, op, context)
|
||||
|
||||
# If the operator is empty this means that the decision statement is just
|
||||
# a single variable. We can just poll this variable from the context and
|
||||
# If the operator is empty this means that the decision statement is just
|
||||
# a single variable. We can just poll this variable from the context and
|
||||
# return this as the result.
|
||||
return context[left] if op == nil
|
||||
return context[left] if op == nil
|
||||
|
||||
left, right = context[left], context[right]
|
||||
|
||||
|
||||
operation = self.class.operators[op] || raise(ArgumentError.new("Unknown operator #{op}"))
|
||||
|
||||
if operation.respond_to?(:call)
|
||||
operation.call(self, left, right)
|
||||
elsif left.respond_to?(operation) and right.respond_to?(operation)
|
||||
elsif left.respond_to?(operation) and right.respond_to?(operation)
|
||||
left.send(operation, right)
|
||||
else
|
||||
nil
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
class ElseCondition < Condition
|
||||
|
||||
def else?
|
||||
def else?
|
||||
true
|
||||
end
|
||||
|
||||
|
||||
def evaluate(context)
|
||||
true
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
@@ -224,7 +224,7 @@ module Liquid
|
||||
|
||||
object
|
||||
end
|
||||
|
||||
|
||||
def filtered_variable(markup)
|
||||
Variable.new(markup).render(self)
|
||||
end
|
||||
|
||||
+18
-17
@@ -1,9 +1,9 @@
|
||||
module Liquid
|
||||
|
||||
|
||||
# A drop in liquid is a class which allows you to to export DOM like things to liquid
|
||||
# Methods of drops are callable.
|
||||
# The main use for liquid drops is the implement lazy loaded objects.
|
||||
# If you would like to make data available to the web designers which you don't want loaded unless needed then
|
||||
# Methods of drops are callable.
|
||||
# The main use for liquid drops is the implement lazy loaded objects.
|
||||
# If you would like to make data available to the web designers which you don't want loaded unless needed then
|
||||
# a drop is a great way to do that
|
||||
#
|
||||
# Example:
|
||||
@@ -13,38 +13,39 @@ module Liquid
|
||||
# Shop.current.products.find(:all, :order => 'sales', :limit => 10 )
|
||||
# end
|
||||
# end
|
||||
#
|
||||
# tmpl = Liquid::Template.parse( ' {% for product in product.top_sales %} {{ product.name }} {%endfor%} ' )
|
||||
# tmpl.render('product' => ProductDrop.new ) # will invoke top_sales query.
|
||||
#
|
||||
# Your drop can either implement the methods sans any parameters or implement the before_method(name) method which is a
|
||||
# tmpl = Liquid::Template.parse( ' {% for product in product.top_sales %} {{ product.name }} {%endfor%} ' )
|
||||
# tmpl.render('product' => ProductDrop.new ) # will invoke top_sales query.
|
||||
#
|
||||
# Your drop can either implement the methods sans any parameters or implement the before_method(name) method which is a
|
||||
# catch all
|
||||
class Drop
|
||||
attr_writer :context
|
||||
|
||||
# Catch all for the method
|
||||
# Catch all for the method
|
||||
def before_method(method)
|
||||
nil
|
||||
end
|
||||
|
||||
|
||||
# called by liquid to invoke a drop
|
||||
def invoke_drop(method)
|
||||
if self.class.public_instance_methods.include?(method.to_s)
|
||||
send(method.to_sym)
|
||||
else
|
||||
def invoke_drop(method)
|
||||
# for backward compatibility with Ruby 1.8
|
||||
methods = self.class.public_instance_methods.map { |m| m.to_s }
|
||||
if methods.include?(method.to_s)
|
||||
send(method.to_sym)
|
||||
else
|
||||
before_method(method)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
def has_key?(name)
|
||||
true
|
||||
end
|
||||
|
||||
|
||||
def to_liquid
|
||||
self
|
||||
end
|
||||
|
||||
alias :[] :invoke_drop
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
+10
-11
@@ -190,9 +190,10 @@ class ContextTest < Test::Unit::TestCase
|
||||
end
|
||||
|
||||
context = Context.new(@template)
|
||||
methods = context.strainer.methods
|
||||
methods_before = context.strainer.methods.map { |method| method.to_s }
|
||||
context.add_filters(filter)
|
||||
assert_equal (methods + ['hi']).sort, context.strainer.methods.sort
|
||||
methods_after = context.strainer.methods.map { |method| method.to_s }
|
||||
assert_equal (methods_before + ["hi"]).sort, methods_after.sort
|
||||
end
|
||||
|
||||
def test_add_item_in_outer_scope
|
||||
@@ -289,11 +290,9 @@ class ContextTest < Test::Unit::TestCase
|
||||
end
|
||||
|
||||
def test_access_hashes_with_hash_notation
|
||||
|
||||
@context['products'] = {'count' => 5, 'tags' => ['deepsnow', 'freestyle'] }
|
||||
@context['product'] = {'variants' => [ {'title' => 'draft151cm'}, {'title' => 'element151cm'} ]}
|
||||
|
||||
|
||||
assert_equal 5, @context['products["count"]']
|
||||
assert_equal 'deepsnow', @context['products["tags"][0]']
|
||||
assert_equal 'deepsnow', @context['products["tags"].first']
|
||||
@@ -417,25 +416,25 @@ class ContextTest < Test::Unit::TestCase
|
||||
end
|
||||
|
||||
def test_lambda_as_variable
|
||||
@context['dynamic'] = lambda { 'Hello' }
|
||||
@context['dynamic'] = proc { 'Hello' }
|
||||
|
||||
assert_equal 'Hello', @context['dynamic']
|
||||
end
|
||||
|
||||
def test_nested_lambda_as_variable
|
||||
@context['dynamic'] = { "lambda" => lambda { 'Hello' } }
|
||||
@context['dynamic'] = { "lambda" => proc { 'Hello' } }
|
||||
|
||||
assert_equal 'Hello', @context['dynamic.lambda']
|
||||
end
|
||||
|
||||
def test_array_containing_lambda_as_variable
|
||||
@context['dynamic'] = [1,2, lambda { 'Hello' } ,4,5]
|
||||
@context['dynamic'] = [1,2, proc { 'Hello' } ,4,5]
|
||||
|
||||
assert_equal 'Hello', @context['dynamic[2]']
|
||||
end
|
||||
|
||||
def test_lambda_is_called_once
|
||||
@context['callcount'] = lambda { @global ||= 0; @global += 1; @global.to_s }
|
||||
@context['callcount'] = proc { @global ||= 0; @global += 1; @global.to_s }
|
||||
|
||||
assert_equal '1', @context['callcount']
|
||||
assert_equal '1', @context['callcount']
|
||||
@@ -445,7 +444,7 @@ class ContextTest < Test::Unit::TestCase
|
||||
end
|
||||
|
||||
def test_nested_lambda_is_called_once
|
||||
@context['callcount'] = { "lambda" => lambda { @global ||= 0; @global += 1; @global.to_s } }
|
||||
@context['callcount'] = { "lambda" => proc { @global ||= 0; @global += 1; @global.to_s } }
|
||||
|
||||
assert_equal '1', @context['callcount.lambda']
|
||||
assert_equal '1', @context['callcount.lambda']
|
||||
@@ -455,7 +454,7 @@ class ContextTest < Test::Unit::TestCase
|
||||
end
|
||||
|
||||
def test_lambda_in_array_is_called_once
|
||||
@context['callcount'] = [1,2, lambda { @global ||= 0; @global += 1; @global.to_s } ,4,5]
|
||||
@context['callcount'] = [1,2, proc { @global ||= 0; @global += 1; @global.to_s } ,4,5]
|
||||
|
||||
assert_equal '1', @context['callcount[2]']
|
||||
assert_equal '1', @context['callcount[2]']
|
||||
@@ -467,7 +466,7 @@ class ContextTest < Test::Unit::TestCase
|
||||
def test_access_to_context_from_proc
|
||||
@context.registers[:magic] = 345392
|
||||
|
||||
@context['magic'] = lambda { @context.registers[:magic] }
|
||||
@context['magic'] = proc { @context.registers[:magic] }
|
||||
|
||||
assert_equal 345392, @context['magic']
|
||||
end
|
||||
|
||||
+116
-116
@@ -3,14 +3,14 @@ require File.dirname(__FILE__) + '/helper'
|
||||
|
||||
class StandardTagTest < Test::Unit::TestCase
|
||||
include Liquid
|
||||
|
||||
|
||||
def test_tag
|
||||
|
||||
|
||||
def test_tag
|
||||
tag = Tag.new('tag', [], [])
|
||||
assert_equal 'liquid::tag', tag.name
|
||||
assert_equal '', tag.render(Context.new)
|
||||
assert_equal 'liquid::tag', tag.name
|
||||
assert_equal '', tag.render(Context.new)
|
||||
end
|
||||
|
||||
|
||||
def test_no_transform
|
||||
assert_template_result('this text should come out of the template without change...',
|
||||
'this text should come out of the template without change...')
|
||||
@@ -18,60 +18,60 @@ class StandardTagTest < Test::Unit::TestCase
|
||||
assert_template_result('<blah>','<blah>')
|
||||
assert_template_result('|,.:','|,.:')
|
||||
assert_template_result('','')
|
||||
|
||||
|
||||
text = %|this shouldnt see any transformation either but has multiple lines
|
||||
as you can clearly see here ...|
|
||||
assert_template_result(text,text)
|
||||
end
|
||||
|
||||
|
||||
def test_has_a_block_which_does_nothing
|
||||
assert_template_result(%|the comment block should be removed .. right?|,
|
||||
%|the comment block should be removed {%comment%} be gone.. {%endcomment%} .. right?|)
|
||||
|
||||
|
||||
assert_template_result('','{%comment%}{%endcomment%}')
|
||||
assert_template_result('','{%comment%}{% endcomment %}')
|
||||
assert_template_result('','{% comment %}{%endcomment%}')
|
||||
assert_template_result('','{% comment %}{% endcomment %}')
|
||||
assert_template_result('','{%comment%}comment{%endcomment%}')
|
||||
assert_template_result('','{% comment %}comment{% endcomment %}')
|
||||
|
||||
|
||||
assert_template_result('foobar','foo{%comment%}comment{%endcomment%}bar')
|
||||
assert_template_result('foobar','foo{% comment %}comment{% endcomment %}bar')
|
||||
assert_template_result('foobar','foo{%comment%} comment {%endcomment%}bar')
|
||||
assert_template_result('foobar','foo{% comment %} comment {% endcomment %}bar')
|
||||
|
||||
|
||||
assert_template_result('foo bar','foo {%comment%} {%endcomment%} bar')
|
||||
assert_template_result('foo bar','foo {%comment%}comment{%endcomment%} bar')
|
||||
assert_template_result('foo bar','foo {%comment%} comment {%endcomment%} bar')
|
||||
|
||||
|
||||
assert_template_result('foobar','foo{%comment%}
|
||||
{%endcomment%}bar')
|
||||
end
|
||||
|
||||
|
||||
def test_for
|
||||
assert_template_result(' yo yo yo yo ','{%for item in array%} yo {%endfor%}','array' => [1,2,3,4])
|
||||
assert_template_result('yoyo','{%for item in array%}yo{%endfor%}','array' => [1,2])
|
||||
assert_template_result(' yo ','{%for item in array%} yo {%endfor%}','array' => [1])
|
||||
assert_template_result('','{%for item in array%}{%endfor%}','array' => [1,2])
|
||||
expected = <<HERE
|
||||
|
||||
|
||||
yo
|
||||
|
||||
|
||||
yo
|
||||
|
||||
|
||||
yo
|
||||
|
||||
|
||||
HERE
|
||||
template = <<HERE
|
||||
{%for item in array%}
|
||||
{%for item in array%}
|
||||
yo
|
||||
{%endfor%}
|
||||
{%endfor%}
|
||||
HERE
|
||||
assert_template_result(expected,template,'array' => [1,2,3])
|
||||
end
|
||||
|
||||
|
||||
def test_for_with_range
|
||||
assert_template_result(' 1 2 3 ','{%for item in (1..3) %} {{item}} {%endfor%}')
|
||||
assert_template_result(' 1 2 3 ','{%for item in (1..3) %} {{item}} {%endfor%}')
|
||||
end
|
||||
|
||||
def test_for_with_variable
|
||||
@@ -82,7 +82,7 @@ HERE
|
||||
assert_template_result('a b c','{%for item in array%}{{item}}{%endfor%}','array' => ['a',' ','b',' ','c'])
|
||||
assert_template_result('abc','{%for item in array%}{{item}}{%endfor%}','array' => ['a','','b','','c'])
|
||||
end
|
||||
|
||||
|
||||
def test_for_helpers
|
||||
assigns = {'array' => [1,2,3] }
|
||||
assert_template_result(' 1/3 2/3 3/3 ','{%for item in array%} {{forloop.index}}/{{forloop.length}} {%endfor%}',assigns)
|
||||
@@ -93,41 +93,41 @@ HERE
|
||||
assert_template_result(' true false false ','{%for item in array%} {{forloop.first}} {%endfor%}',assigns)
|
||||
assert_template_result(' false false true ','{%for item in array%} {{forloop.last}} {%endfor%}',assigns)
|
||||
end
|
||||
|
||||
|
||||
def test_for_and_if
|
||||
assigns = {'array' => [1,2,3] }
|
||||
assert_template_result('+--', '{%for item in array%}{% if forloop.first %}+{% else %}-{% endif %}{%endfor%}', assigns)
|
||||
end
|
||||
|
||||
|
||||
def test_for_with_filtered_expressions
|
||||
assert_template_result('abc','{% for letter in letters|sort %}{{ letter }}{% endfor %}', 'letters' => %w{c b a})
|
||||
end
|
||||
|
||||
|
||||
def test_limiting
|
||||
assigns = {'array' => [1,2,3,4,5,6,7,8,9,0]}
|
||||
assert_template_result('12','{%for i in array limit:2 %}{{ i }}{%endfor%}',assigns)
|
||||
assert_template_result('1234','{%for i in array limit:4 %}{{ i }}{%endfor%}',assigns)
|
||||
assert_template_result('3456','{%for i in array limit:4 offset:2 %}{{ i }}{%endfor%}',assigns)
|
||||
assert_template_result('3456','{%for i in array limit: 4 offset: 2 %}{{ i }}{%endfor%}',assigns)
|
||||
assert_template_result('3456','{%for i in array limit: 4 offset: 2 %}{{ i }}{%endfor%}',assigns)
|
||||
end
|
||||
|
||||
|
||||
def test_dynamic_variable_limiting
|
||||
assigns = {'array' => [1,2,3,4,5,6,7,8,9,0]}
|
||||
assigns['limit'] = 2
|
||||
assigns['offset'] = 2
|
||||
assert_template_result('34','{%for i in array limit: limit offset: offset %}{{ i }}{%endfor%}',assigns)
|
||||
assert_template_result('34','{%for i in array limit: limit offset: offset %}{{ i }}{%endfor%}',assigns)
|
||||
end
|
||||
|
||||
|
||||
def test_nested_for
|
||||
assigns = {'array' => [[1,2],[3,4],[5,6]] }
|
||||
assert_template_result('123456','{%for item in array%}{%for i in item%}{{ i }}{%endfor%}{%endfor%}',assigns)
|
||||
end
|
||||
|
||||
|
||||
def test_offset_only
|
||||
assigns = {'array' => [1,2,3,4,5,6,7,8,9,0]}
|
||||
assert_template_result('890','{%for i in array offset:7 %}{{ i }}{%endfor%}',assigns)
|
||||
end
|
||||
|
||||
|
||||
def test_pause_resume
|
||||
assigns = {'array' => {'items' => [1,2,3,4,5,6,7,8,9,0]}}
|
||||
markup = <<-MKUP
|
||||
@@ -146,7 +146,7 @@ HERE
|
||||
XPCTD
|
||||
assert_template_result(expected,markup,assigns)
|
||||
end
|
||||
|
||||
|
||||
def test_pause_resume_limit
|
||||
assigns = {'array' => {'items' => [1,2,3,4,5,6,7,8,9,0]}}
|
||||
markup = <<-MKUP
|
||||
@@ -165,7 +165,7 @@ HERE
|
||||
XPCTD
|
||||
assert_template_result(expected,markup,assigns)
|
||||
end
|
||||
|
||||
|
||||
def test_pause_resume_BIG_limit
|
||||
assigns = {'array' => {'items' => [1,2,3,4,5,6,7,8,9,0]}}
|
||||
markup = <<-MKUP
|
||||
@@ -184,8 +184,8 @@ HERE
|
||||
XPCTD
|
||||
assert_template_result(expected,markup,assigns)
|
||||
end
|
||||
|
||||
|
||||
|
||||
|
||||
def test_pause_resume_BIG_offset
|
||||
assigns = {'array' => {'items' => [1,2,3,4,5,6,7,8,9,0]}}
|
||||
markup = %q({%for i in array.items limit:3 %}{{i}}{%endfor%}
|
||||
@@ -200,19 +200,19 @@ HERE
|
||||
)
|
||||
assert_template_result(expected,markup,assigns)
|
||||
end
|
||||
|
||||
|
||||
def test_assign
|
||||
assigns = {'var' => 'content' }
|
||||
assert_template_result('var2: var2:content','var2:{{var2}} {%assign var2 = var%} var2:{{var2}}',assigns)
|
||||
|
||||
|
||||
end
|
||||
|
||||
def test_hyphenated_assign
|
||||
assigns = {'a-b' => '1' }
|
||||
assert_template_result('a-b:1 a-b:2','a-b:{{a-b}} {%assign a-b = 2 %}a-b:{{a-b}}',assigns)
|
||||
|
||||
|
||||
end
|
||||
|
||||
|
||||
def test_assign_with_colon_and_spaces
|
||||
assigns = {'var' => {'a:b c' => {'paged' => '1' }}}
|
||||
assert_template_result('var2: 1','{%assign var2 = var["a:b c"].paged %}var2: {{var2}}',assigns)
|
||||
@@ -230,62 +230,62 @@ HERE
|
||||
end
|
||||
|
||||
def test_case
|
||||
assigns = {'condition' => 2 }
|
||||
assigns = {'condition' => 2 }
|
||||
assert_template_result(' its 2 ','{% case condition %}{% when 1 %} its 1 {% when 2 %} its 2 {% endcase %}', assigns)
|
||||
|
||||
assigns = {'condition' => 1 }
|
||||
assert_template_result(' its 1 ','{% case condition %}{% when 1 %} its 1 {% when 2 %} its 2 {% endcase %}', assigns)
|
||||
|
||||
assigns = {'condition' => 3 }
|
||||
assert_template_result('','{% case condition %}{% when 1 %} its 1 {% when 2 %} its 2 {% endcase %}', assigns)
|
||||
assert_template_result('','{% case condition %}{% when 1 %} its 1 {% when 2 %} its 2 {% endcase %}', assigns)
|
||||
|
||||
assigns = {'condition' => "string here" }
|
||||
assert_template_result(' hit ','{% case condition %}{% when "string here" %} hit {% endcase %}', assigns)
|
||||
assert_template_result(' hit ','{% case condition %}{% when "string here" %} hit {% endcase %}', assigns)
|
||||
|
||||
assigns = {'condition' => "bad string here" }
|
||||
assert_template_result('','{% case condition %}{% when "string here" %} hit {% endcase %}', assigns)
|
||||
assert_template_result('','{% case condition %}{% when "string here" %} hit {% endcase %}', assigns)
|
||||
end
|
||||
|
||||
|
||||
def test_case_with_else
|
||||
|
||||
assigns = {'condition' => 5 }
|
||||
assert_template_result(' hit ','{% case condition %}{% when 5 %} hit {% else %} else {% endcase %}', assigns)
|
||||
assert_template_result(' hit ','{% case condition %}{% when 5 %} hit {% else %} else {% endcase %}', assigns)
|
||||
|
||||
assigns = {'condition' => 6 }
|
||||
assert_template_result(' else ','{% case condition %}{% when 5 %} hit {% else %} else {% endcase %}', assigns)
|
||||
|
||||
assert_template_result(' else ','{% case condition %}{% when 5 %} hit {% else %} else {% endcase %}', assigns)
|
||||
|
||||
assigns = {'condition' => 6 }
|
||||
assert_template_result(' else ','{% case condition %} {% when 5 %} hit {% else %} else {% endcase %}', assigns)
|
||||
|
||||
assert_template_result(' else ','{% case condition %} {% when 5 %} hit {% else %} else {% endcase %}', assigns)
|
||||
|
||||
|
||||
end
|
||||
|
||||
|
||||
def test_case_on_size
|
||||
assert_template_result('', '{% case a.size %}{% when 1 %}1{% when 2 %}2{% endcase %}', 'a' => [])
|
||||
assert_template_result('1' , '{% case a.size %}{% when 1 %}1{% when 2 %}2{% endcase %}', 'a' => [1])
|
||||
assert_template_result('2' , '{% case a.size %}{% when 1 %}1{% when 2 %}2{% endcase %}', 'a' => [1, 1])
|
||||
assert_template_result('', '{% case a.size %}{% when 1 %}1{% when 2 %}2{% endcase %}', 'a' => [1, 1, 1])
|
||||
assert_template_result('', '{% case a.size %}{% when 1 %}1{% when 2 %}2{% endcase %}', 'a' => [1, 1, 1, 1])
|
||||
assert_template_result('', '{% case a.size %}{% when 1 %}1{% when 2 %}2{% endcase %}', 'a' => [1, 1, 1, 1, 1])
|
||||
end
|
||||
|
||||
assert_template_result('', '{% case a.size %}{% when 1 %}1{% when 2 %}2{% endcase %}', 'a' => [])
|
||||
assert_template_result('1' , '{% case a.size %}{% when 1 %}1{% when 2 %}2{% endcase %}', 'a' => [1])
|
||||
assert_template_result('2' , '{% case a.size %}{% when 1 %}1{% when 2 %}2{% endcase %}', 'a' => [1, 1])
|
||||
assert_template_result('', '{% case a.size %}{% when 1 %}1{% when 2 %}2{% endcase %}', 'a' => [1, 1, 1])
|
||||
assert_template_result('', '{% case a.size %}{% when 1 %}1{% when 2 %}2{% endcase %}', 'a' => [1, 1, 1, 1])
|
||||
assert_template_result('', '{% case a.size %}{% when 1 %}1{% when 2 %}2{% endcase %}', 'a' => [1, 1, 1, 1, 1])
|
||||
end
|
||||
|
||||
def test_case_on_size_with_else
|
||||
assert_template_result('else', '{% case a.size %}{% when 1 %}1{% when 2 %}2{% else %}else{% endcase %}', 'a' => [])
|
||||
assert_template_result('1', '{% case a.size %}{% when 1 %}1{% when 2 %}2{% else %}else{% endcase %}', 'a' => [1])
|
||||
assert_template_result('2', '{% case a.size %}{% when 1 %}1{% when 2 %}2{% else %}else{% endcase %}', 'a' => [1, 1])
|
||||
assert_template_result('else', '{% case a.size %}{% when 1 %}1{% when 2 %}2{% else %}else{% endcase %}', 'a' => [1, 1, 1])
|
||||
assert_template_result('else', '{% case a.size %}{% when 1 %}1{% when 2 %}2{% else %}else{% endcase %}', 'a' => [1, 1, 1, 1])
|
||||
assert_template_result('else', '{% case a.size %}{% when 1 %}1{% when 2 %}2{% else %}else{% endcase %}', 'a' => [1, 1, 1, 1, 1])
|
||||
assert_template_result('else', '{% case a.size %}{% when 1 %}1{% when 2 %}2{% else %}else{% endcase %}', 'a' => [])
|
||||
assert_template_result('1', '{% case a.size %}{% when 1 %}1{% when 2 %}2{% else %}else{% endcase %}', 'a' => [1])
|
||||
assert_template_result('2', '{% case a.size %}{% when 1 %}1{% when 2 %}2{% else %}else{% endcase %}', 'a' => [1, 1])
|
||||
assert_template_result('else', '{% case a.size %}{% when 1 %}1{% when 2 %}2{% else %}else{% endcase %}', 'a' => [1, 1, 1])
|
||||
assert_template_result('else', '{% case a.size %}{% when 1 %}1{% when 2 %}2{% else %}else{% endcase %}', 'a' => [1, 1, 1, 1])
|
||||
assert_template_result('else', '{% case a.size %}{% when 1 %}1{% when 2 %}2{% else %}else{% endcase %}', 'a' => [1, 1, 1, 1, 1])
|
||||
end
|
||||
|
||||
|
||||
def test_case_on_length_with_else
|
||||
assert_template_result('else', '{% case a.empty? %}{% when true %}true{% when false %}false{% else %}else{% endcase %}', {})
|
||||
assert_template_result('false', '{% case false %}{% when true %}true{% when false %}false{% else %}else{% endcase %}', {})
|
||||
assert_template_result('true', '{% case true %}{% when true %}true{% when false %}false{% else %}else{% endcase %}', {})
|
||||
assert_template_result('else', '{% case NULL %}{% when true %}true{% when false %}false{% else %}else{% endcase %}', {})
|
||||
assert_template_result('else', '{% case a.empty? %}{% when true %}true{% when false %}false{% else %}else{% endcase %}', {})
|
||||
assert_template_result('false', '{% case false %}{% when true %}true{% when false %}false{% else %}else{% endcase %}', {})
|
||||
assert_template_result('true', '{% case true %}{% when true %}true{% when false %}false{% else %}else{% endcase %}', {})
|
||||
assert_template_result('else', '{% case NULL %}{% when true %}true{% when false %}false{% else %}else{% endcase %}', {})
|
||||
end
|
||||
|
||||
def test_assign_from_case
|
||||
|
||||
def test_assign_from_case
|
||||
# Example from the shopify forums
|
||||
code = %q({% case collection.handle %}{% when 'menswear-jackets' %}{% assign ptitle = 'menswear' %}{% when 'menswear-t-shirts' %}{% assign ptitle = 'menswear' %}{% else %}{% assign ptitle = 'womenswear' %}{% endcase %}{{ ptitle }})
|
||||
template = Liquid::Template.parse(code)
|
||||
@@ -303,12 +303,12 @@ HERE
|
||||
assert_template_result(' its 1 or 2 or 3 ', code, {'condition' => 3 })
|
||||
assert_template_result(' its 4 ', code, {'condition' => 4 })
|
||||
assert_template_result('', code, {'condition' => 5 })
|
||||
|
||||
|
||||
code = '{% case condition %}{% when 1 or "string" or null %} its 1 or 2 or 3 {% when 4 %} its 4 {% endcase %}'
|
||||
assert_template_result(' its 1 or 2 or 3 ', code, {'condition' => 1 })
|
||||
assert_template_result(' its 1 or 2 or 3 ', code, {'condition' => 'string' })
|
||||
assert_template_result(' its 1 or 2 or 3 ', code, {'condition' => nil })
|
||||
assert_template_result('', code, {'condition' => 'something else' })
|
||||
assert_template_result('', code, {'condition' => 'something else' })
|
||||
end
|
||||
|
||||
def test_case_when_comma
|
||||
@@ -318,82 +318,82 @@ HERE
|
||||
assert_template_result(' its 1 or 2 or 3 ', code, {'condition' => 3 })
|
||||
assert_template_result(' its 4 ', code, {'condition' => 4 })
|
||||
assert_template_result('', code, {'condition' => 5 })
|
||||
|
||||
|
||||
code = '{% case condition %}{% when 1, "string", null %} its 1 or 2 or 3 {% when 4 %} its 4 {% endcase %}'
|
||||
assert_template_result(' its 1 or 2 or 3 ', code, {'condition' => 1 })
|
||||
assert_template_result(' its 1 or 2 or 3 ', code, {'condition' => 'string' })
|
||||
assert_template_result(' its 1 or 2 or 3 ', code, {'condition' => nil })
|
||||
assert_template_result('', code, {'condition' => 'something else' })
|
||||
assert_template_result('', code, {'condition' => 'something else' })
|
||||
end
|
||||
|
||||
|
||||
def test_assign
|
||||
assert_equal 'variable', Liquid::Template.parse( '{% assign a = "variable"%}{{a}}' ).render
|
||||
assert_equal 'variable', Liquid::Template.parse( '{% assign a = "variable"%}{{a}}' ).render
|
||||
end
|
||||
|
||||
|
||||
def test_assign_is_global
|
||||
assert_equal 'variable', Liquid::Template.parse( '{%for i in (1..2) %}{% assign a = "variable"%}{% endfor %}{{a}}' ).render
|
||||
end
|
||||
|
||||
assert_equal 'variable', Liquid::Template.parse( '{%for i in (1..2) %}{% assign a = "variable"%}{% endfor %}{{a}}' ).render
|
||||
end
|
||||
|
||||
def test_case_detects_bad_syntax
|
||||
assert_raise(SyntaxError) do
|
||||
assert_template_result('', '{% case false %}{% when %}true{% endcase %}', {})
|
||||
assert_template_result('', '{% case false %}{% when %}true{% endcase %}', {})
|
||||
end
|
||||
|
||||
assert_raise(SyntaxError) do
|
||||
assert_template_result('', '{% case false %}{% huh %}true{% endcase %}', {})
|
||||
assert_template_result('', '{% case false %}{% huh %}true{% endcase %}', {})
|
||||
end
|
||||
|
||||
|
||||
end
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
def test_cycle
|
||||
|
||||
assert_template_result('one','{%cycle "one", "two"%}')
|
||||
assert_template_result('one two','{%cycle "one", "two"%} {%cycle "one", "two"%}')
|
||||
|
||||
assert_template_result('one two one','{%cycle "one", "two"%} {%cycle "one", "two"%} {%cycle "one", "two"%}')
|
||||
|
||||
assert_template_result('text-align: left text-align: right','{%cycle "text-align: left", "text-align: right" %} {%cycle "text-align: left", "text-align: right"%}')
|
||||
|
||||
assert_template_result('one','{%cycle "one", "two"%}')
|
||||
assert_template_result('one two','{%cycle "one", "two"%} {%cycle "one", "two"%}')
|
||||
|
||||
assert_template_result('one two one','{%cycle "one", "two"%} {%cycle "one", "two"%} {%cycle "one", "two"%}')
|
||||
|
||||
assert_template_result('text-align: left text-align: right','{%cycle "text-align: left", "text-align: right" %} {%cycle "text-align: left", "text-align: right"%}')
|
||||
|
||||
end
|
||||
|
||||
|
||||
def test_multiple_cycles
|
||||
assert_template_result('1 2 1 1 2 3 1','{%cycle 1,2%} {%cycle 1,2%} {%cycle 1,2%} {%cycle 1,2,3%} {%cycle 1,2,3%} {%cycle 1,2,3%} {%cycle 1,2,3%}')
|
||||
assert_template_result('1 2 1 1 2 3 1','{%cycle 1,2%} {%cycle 1,2%} {%cycle 1,2%} {%cycle 1,2,3%} {%cycle 1,2,3%} {%cycle 1,2,3%} {%cycle 1,2,3%}')
|
||||
end
|
||||
|
||||
|
||||
def test_multiple_named_cycles
|
||||
assert_template_result('one one two two one one','{%cycle 1: "one", "two" %} {%cycle 2: "one", "two" %} {%cycle 1: "one", "two" %} {%cycle 2: "one", "two" %} {%cycle 1: "one", "two" %} {%cycle 2: "one", "two" %}')
|
||||
assert_template_result('one one two two one one','{%cycle 1: "one", "two" %} {%cycle 2: "one", "two" %} {%cycle 1: "one", "two" %} {%cycle 2: "one", "two" %} {%cycle 1: "one", "two" %} {%cycle 2: "one", "two" %}')
|
||||
end
|
||||
|
||||
|
||||
def test_multiple_named_cycles_with_names_from_context
|
||||
assigns = {"var1" => 1, "var2" => 2 }
|
||||
assert_template_result('one one two two one one','{%cycle var1: "one", "two" %} {%cycle var2: "one", "two" %} {%cycle var1: "one", "two" %} {%cycle var2: "one", "two" %} {%cycle var1: "one", "two" %} {%cycle var2: "one", "two" %}', assigns)
|
||||
assert_template_result('one one two two one one','{%cycle var1: "one", "two" %} {%cycle var2: "one", "two" %} {%cycle var1: "one", "two" %} {%cycle var2: "one", "two" %} {%cycle var1: "one", "two" %} {%cycle var2: "one", "two" %}', assigns)
|
||||
end
|
||||
|
||||
|
||||
def test_size_of_array
|
||||
assigns = {"array" => [1,2,3,4]}
|
||||
assert_template_result('array has 4 elements', "array has {{ array.size }} elements", assigns)
|
||||
assert_template_result('array has 4 elements', "array has {{ array.size }} elements", assigns)
|
||||
end
|
||||
|
||||
def test_size_of_hash
|
||||
assigns = {"hash" => {:a => 1, :b => 2, :c=> 3, :d => 4}}
|
||||
assert_template_result('hash has 4 elements', "hash has {{ hash.size }} elements", assigns)
|
||||
end
|
||||
|
||||
def test_illegal_symbols
|
||||
assert_template_result('', '{% if true == empty %}?{% endif %}', {})
|
||||
assert_template_result('', '{% if true == null %}?{% endif %}', {})
|
||||
assert_template_result('', '{% if empty == true %}?{% endif %}', {})
|
||||
assert_template_result('', '{% if null == true %}?{% endif %}', {})
|
||||
end
|
||||
|
||||
def test_for_reversed
|
||||
assigns = {'array' => [ 1, 2, 3] }
|
||||
assert_template_result('321','{%for item in array reversed %}{{item}}{%endfor%}',assigns)
|
||||
assert_template_result('hash has 4 elements', "hash has {{ hash.size }} elements", assigns)
|
||||
end
|
||||
|
||||
|
||||
def test_illegal_symbols
|
||||
assert_template_result('', '{% if true == empty %}?{% endif %}', {})
|
||||
assert_template_result('', '{% if true == null %}?{% endif %}', {})
|
||||
assert_template_result('', '{% if empty == true %}?{% endif %}', {})
|
||||
assert_template_result('', '{% if null == true %}?{% endif %}', {})
|
||||
end
|
||||
|
||||
def test_for_reversed
|
||||
assigns = {'array' => [ 1, 2, 3] }
|
||||
assert_template_result('321','{%for item in array reversed %}{{item}}{%endfor%}',assigns)
|
||||
end
|
||||
|
||||
|
||||
def test_ifchanged
|
||||
assigns = {'array' => [ 1, 1, 2, 2, 3, 3] }
|
||||
assert_template_result('123','{%for item in array%}{%ifchanged%}{{item}}{% endifchanged %}{%endfor%}',assigns)
|
||||
|
||||
Reference in New Issue
Block a user