Changed implementation of For in such a way that it only depends on the existence of a each method. This allows drops to simply implement each for enumeration

This commit is contained in:
Tobias Lütke
2008-05-08 12:30:48 -04:00
parent 7f58cbf82d
commit 4c0cfae0b7
9 changed files with 94 additions and 47 deletions

View File

@@ -59,6 +59,16 @@ class ProductDrop < Liquid::Drop
def callmenot
"protected"
end
end
class EnumerableDrop < Liquid::Drop
include Enumerable
def each
yield 1
yield 2
yield 3
end
end
@@ -132,6 +142,10 @@ class DropsTest < Test::Unit::TestCase
def test_access_context_from_drop
assert_equal '123', Liquid::Template.parse( '{%for a in dummy%}{{ context.loop_pos }}{% endfor %}' ).render('context' => ContextDrop.new, 'dummy' => [1,2,3])
end
def test_enumerable_drop
assert_equal '123', Liquid::Template.parse( '{% for c in collection %}{{c}}{% endfor %}').render('collection' => EnumerableDrop.new)
end

View File

@@ -57,8 +57,23 @@ class ErrorHandlingTest < Test::Unit::TestCase
end
end
def test_missing_endtag
assert_nothing_raised do
template = Liquid::Template.parse(' {% for a in b %} ... ')
assert_equal ' Liquid error: Unknown operator =! ', template.render
assert_equal 1, template.errors.size
assert_equal Liquid::SyntaxError, template.errors.first.class
end
end
def test_unrecognized_operator
assert_nothing_raised do

View File

@@ -96,9 +96,10 @@ class IncludeTagTest < Test::Unit::TestCase
end
Liquid::Template.file_system = infinite_file_system.new
assert_match /-{552}Liquid error: stack level too deep$/,
Template.parse("{% include 'loop' %}").render
assert_raise(Liquid::StackLevelError) do
Template.parse("{% include 'loop' %}").render!
end
end

View File

@@ -104,11 +104,14 @@ HERE
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