Ruby 1.9.1 bugfixes

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