Use render! in tests to make debugging test failures easier.

This commit is contained in:
Dylan Thacker-Smith
2014-03-20 12:04:17 -04:00
parent e190bbba9e
commit a5990042ff
16 changed files with 215 additions and 242 deletions
+3 -3
View File
@@ -93,9 +93,9 @@ class BlankTest < Test::Unit::TestCase
def test_include_is_blank def test_include_is_blank
Liquid::Template.file_system = BlankTestFileSystem.new Liquid::Template.file_system = BlankTestFileSystem.new
assert_equal "foobar"*(N+1), Template.parse(wrap("{% include 'foobar' %}")).render() assert_template_result "foobar"*(N+1), wrap("{% include 'foobar' %}")
assert_equal " foobar "*(N+1), Template.parse(wrap("{% include ' foobar ' %}")).render() assert_template_result " foobar "*(N+1), wrap("{% include ' foobar ' %}")
assert_equal " ", Template.parse(" {% include ' ' %} ").render() assert_template_result " "*(N+1), wrap(" {% include ' ' %} ")
end end
def test_case_is_blank def test_case_is_blank
+2 -2
View File
@@ -19,7 +19,7 @@ class CaptureTest < Test::Unit::TestCase
{{var}} {{var}}
END_TEMPLATE END_TEMPLATE
template = Template.parse(template_source) template = Template.parse(template_source)
rendered = template.render rendered = template.render!
assert_equal "test-string", rendered.gsub(/\s/, '') assert_equal "test-string", rendered.gsub(/\s/, '')
end end
@@ -34,7 +34,7 @@ class CaptureTest < Test::Unit::TestCase
{{ first }}-{{ second }} {{ first }}-{{ second }}
END_TEMPLATE END_TEMPLATE
template = Template.parse(template_source) template = Template.parse(template_source)
rendered = template.render rendered = template.render!
assert_equal "3-3", rendered.gsub(/\s/, '') assert_equal "3-3", rendered.gsub(/\s/, '')
end end
end # CaptureTest end # CaptureTest
+2 -2
View File
@@ -176,8 +176,8 @@ class ContextTest < Test::Unit::TestCase
end end
Template.register_filter(global) Template.register_filter(global)
assert_equal 'Global test', Template.parse("{{'test' | notice }}").render assert_equal 'Global test', Template.parse("{{'test' | notice }}").render!
assert_equal 'Local test', Template.parse("{{'test' | notice }}").render({}, :filters => [local]) assert_equal 'Local test', Template.parse("{{'test' | notice }}").render!({}, :filters => [local])
end end
def test_only_intended_filters_make_it_there def test_only_intended_filters_make_it_there
+50 -50
View File
@@ -106,140 +106,140 @@ class DropsTest < Test::Unit::TestCase
def test_product_drop def test_product_drop
assert_nothing_raised do assert_nothing_raised do
tpl = Liquid::Template.parse( ' ' ) tpl = Liquid::Template.parse( ' ' )
tpl.render('product' => ProductDrop.new) tpl.render!('product' => ProductDrop.new)
end end
end end
def test_drop_does_only_respond_to_whitelisted_methods def test_drop_does_only_respond_to_whitelisted_methods
assert_equal "", Liquid::Template.parse("{{ product.inspect }}").render('product' => ProductDrop.new) assert_equal "", Liquid::Template.parse("{{ product.inspect }}").render!('product' => ProductDrop.new)
assert_equal "", Liquid::Template.parse("{{ product.pretty_inspect }}").render('product' => ProductDrop.new) assert_equal "", Liquid::Template.parse("{{ product.pretty_inspect }}").render!('product' => ProductDrop.new)
assert_equal "", Liquid::Template.parse("{{ product.whatever }}").render('product' => ProductDrop.new) assert_equal "", Liquid::Template.parse("{{ product.whatever }}").render!('product' => ProductDrop.new)
assert_equal "", Liquid::Template.parse('{{ product | map: "inspect" }}').render('product' => ProductDrop.new) assert_equal "", Liquid::Template.parse('{{ product | map: "inspect" }}').render!('product' => ProductDrop.new)
assert_equal "", Liquid::Template.parse('{{ product | map: "pretty_inspect" }}').render('product' => ProductDrop.new) assert_equal "", Liquid::Template.parse('{{ product | map: "pretty_inspect" }}').render!('product' => ProductDrop.new)
assert_equal "", Liquid::Template.parse('{{ product | map: "whatever" }}').render('product' => ProductDrop.new) assert_equal "", Liquid::Template.parse('{{ product | map: "whatever" }}').render!('product' => ProductDrop.new)
end end
def test_drops_respond_to_to_liquid def test_drops_respond_to_to_liquid
assert_equal "text1", Liquid::Template.parse("{{ product.to_liquid.texts.text }}").render('product' => ProductDrop.new) assert_equal "text1", Liquid::Template.parse("{{ product.to_liquid.texts.text }}").render!('product' => ProductDrop.new)
assert_equal "text1", Liquid::Template.parse('{{ product | map: "to_liquid" | map: "texts" | map: "text" }}').render('product' => ProductDrop.new) assert_equal "text1", Liquid::Template.parse('{{ product | map: "to_liquid" | map: "texts" | map: "text" }}').render!('product' => ProductDrop.new)
end end
def test_text_drop def test_text_drop
output = Liquid::Template.parse( ' {{ product.texts.text }} ' ).render('product' => ProductDrop.new) output = Liquid::Template.parse( ' {{ product.texts.text }} ' ).render!('product' => ProductDrop.new)
assert_equal ' text1 ', output assert_equal ' text1 ', output
end end
def test_unknown_method def test_unknown_method
output = Liquid::Template.parse( ' {{ product.catchall.unknown }} ' ).render('product' => ProductDrop.new) output = Liquid::Template.parse( ' {{ product.catchall.unknown }} ' ).render!('product' => ProductDrop.new)
assert_equal ' method: unknown ', output assert_equal ' method: unknown ', output
end end
def test_integer_argument_drop def test_integer_argument_drop
output = Liquid::Template.parse( ' {{ product.catchall[8] }} ' ).render('product' => ProductDrop.new) output = Liquid::Template.parse( ' {{ product.catchall[8] }} ' ).render!('product' => ProductDrop.new)
assert_equal ' method: 8 ', output assert_equal ' method: 8 ', output
end end
def test_text_array_drop def test_text_array_drop
output = Liquid::Template.parse( '{% for text in product.texts.array %} {{text}} {% endfor %}' ).render('product' => ProductDrop.new) output = Liquid::Template.parse( '{% for text in product.texts.array %} {{text}} {% endfor %}' ).render!('product' => ProductDrop.new)
assert_equal ' text1 text2 ', output assert_equal ' text1 text2 ', output
end end
def test_context_drop def test_context_drop
output = Liquid::Template.parse( ' {{ context.bar }} ' ).render('context' => ContextDrop.new, 'bar' => "carrot") output = Liquid::Template.parse( ' {{ context.bar }} ' ).render!('context' => ContextDrop.new, 'bar' => "carrot")
assert_equal ' carrot ', output assert_equal ' carrot ', output
end end
def test_nested_context_drop def test_nested_context_drop
output = Liquid::Template.parse( ' {{ product.context.foo }} ' ).render('product' => ProductDrop.new, 'foo' => "monkey") output = Liquid::Template.parse( ' {{ product.context.foo }} ' ).render!('product' => ProductDrop.new, 'foo' => "monkey")
assert_equal ' monkey ', output assert_equal ' monkey ', output
end end
def test_protected def test_protected
output = Liquid::Template.parse( ' {{ product.callmenot }} ' ).render('product' => ProductDrop.new) output = Liquid::Template.parse( ' {{ product.callmenot }} ' ).render!('product' => ProductDrop.new)
assert_equal ' ', output assert_equal ' ', output
end end
def test_object_methods_not_allowed def test_object_methods_not_allowed
[:dup, :clone, :singleton_class, :eval, :class_eval, :inspect].each do |method| [:dup, :clone, :singleton_class, :eval, :class_eval, :inspect].each do |method|
output = Liquid::Template.parse(" {{ product.#{method} }} ").render('product' => ProductDrop.new) output = Liquid::Template.parse(" {{ product.#{method} }} ").render!('product' => ProductDrop.new)
assert_equal ' ', output assert_equal ' ', output
end end
end end
def test_scope def test_scope
assert_equal '1', Liquid::Template.parse( '{{ context.scopes }}' ).render('context' => ContextDrop.new) assert_equal '1', Liquid::Template.parse( '{{ context.scopes }}' ).render!('context' => ContextDrop.new)
assert_equal '2', Liquid::Template.parse( '{%for i in dummy%}{{ context.scopes }}{%endfor%}' ).render('context' => ContextDrop.new, 'dummy' => [1]) assert_equal '2', Liquid::Template.parse( '{%for i in dummy%}{{ context.scopes }}{%endfor%}' ).render!('context' => ContextDrop.new, 'dummy' => [1])
assert_equal '3', Liquid::Template.parse( '{%for i in dummy%}{%for i in dummy%}{{ context.scopes }}{%endfor%}{%endfor%}' ).render('context' => ContextDrop.new, 'dummy' => [1]) assert_equal '3', Liquid::Template.parse( '{%for i in dummy%}{%for i in dummy%}{{ context.scopes }}{%endfor%}{%endfor%}' ).render!('context' => ContextDrop.new, 'dummy' => [1])
end end
def test_scope_though_proc def test_scope_though_proc
assert_equal '1', Liquid::Template.parse( '{{ s }}' ).render('context' => ContextDrop.new, 's' => Proc.new{|c| c['context.scopes'] }) assert_equal '1', Liquid::Template.parse( '{{ s }}' ).render!('context' => ContextDrop.new, 's' => Proc.new{|c| c['context.scopes'] })
assert_equal '2', Liquid::Template.parse( '{%for i in dummy%}{{ s }}{%endfor%}' ).render('context' => ContextDrop.new, 's' => Proc.new{|c| c['context.scopes'] }, 'dummy' => [1]) assert_equal '2', Liquid::Template.parse( '{%for i in dummy%}{{ s }}{%endfor%}' ).render!('context' => ContextDrop.new, 's' => Proc.new{|c| c['context.scopes'] }, 'dummy' => [1])
assert_equal '3', Liquid::Template.parse( '{%for i in dummy%}{%for i in dummy%}{{ s }}{%endfor%}{%endfor%}' ).render('context' => ContextDrop.new, 's' => Proc.new{|c| c['context.scopes'] }, 'dummy' => [1]) assert_equal '3', Liquid::Template.parse( '{%for i in dummy%}{%for i in dummy%}{{ s }}{%endfor%}{%endfor%}' ).render!('context' => ContextDrop.new, 's' => Proc.new{|c| c['context.scopes'] }, 'dummy' => [1])
end end
def test_scope_with_assigns def test_scope_with_assigns
assert_equal 'variable', Liquid::Template.parse( '{% assign a = "variable"%}{{a}}' ).render('context' => ContextDrop.new) assert_equal 'variable', Liquid::Template.parse( '{% assign a = "variable"%}{{a}}' ).render!('context' => ContextDrop.new)
assert_equal 'variable', Liquid::Template.parse( '{% assign a = "variable"%}{%for i in dummy%}{{a}}{%endfor%}' ).render('context' => ContextDrop.new, 'dummy' => [1]) assert_equal 'variable', Liquid::Template.parse( '{% assign a = "variable"%}{%for i in dummy%}{{a}}{%endfor%}' ).render!('context' => ContextDrop.new, 'dummy' => [1])
assert_equal 'test', Liquid::Template.parse( '{% assign header_gif = "test"%}{{header_gif}}' ).render('context' => ContextDrop.new) assert_equal 'test', Liquid::Template.parse( '{% assign header_gif = "test"%}{{header_gif}}' ).render!('context' => ContextDrop.new)
assert_equal 'test', Liquid::Template.parse( "{% assign header_gif = 'test'%}{{header_gif}}" ).render('context' => ContextDrop.new) assert_equal 'test', Liquid::Template.parse( "{% assign header_gif = 'test'%}{{header_gif}}" ).render!('context' => ContextDrop.new)
end end
def test_scope_from_tags def test_scope_from_tags
assert_equal '1', Liquid::Template.parse( '{% for i in context.scopes_as_array %}{{i}}{% endfor %}' ).render('context' => ContextDrop.new, 'dummy' => [1]) assert_equal '1', Liquid::Template.parse( '{% for i in context.scopes_as_array %}{{i}}{% endfor %}' ).render!('context' => ContextDrop.new, 'dummy' => [1])
assert_equal '12', Liquid::Template.parse( '{%for a in dummy%}{% for i in context.scopes_as_array %}{{i}}{% endfor %}{% endfor %}' ).render('context' => ContextDrop.new, 'dummy' => [1]) assert_equal '12', Liquid::Template.parse( '{%for a in dummy%}{% for i in context.scopes_as_array %}{{i}}{% endfor %}{% endfor %}' ).render!('context' => ContextDrop.new, 'dummy' => [1])
assert_equal '123', Liquid::Template.parse( '{%for a in dummy%}{%for a in dummy%}{% for i in context.scopes_as_array %}{{i}}{% endfor %}{% endfor %}{% endfor %}' ).render('context' => ContextDrop.new, 'dummy' => [1]) assert_equal '123', Liquid::Template.parse( '{%for a in dummy%}{%for a in dummy%}{% for i in context.scopes_as_array %}{{i}}{% endfor %}{% endfor %}{% endfor %}' ).render!('context' => ContextDrop.new, 'dummy' => [1])
end end
def test_access_context_from_drop 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]) assert_equal '123', Liquid::Template.parse( '{%for a in dummy%}{{ context.loop_pos }}{% endfor %}' ).render!('context' => ContextDrop.new, 'dummy' => [1,2,3])
end end
def test_enumerable_drop def test_enumerable_drop
assert_equal '123', Liquid::Template.parse( '{% for c in collection %}{{c}}{% endfor %}').render('collection' => EnumerableDrop.new) assert_equal '123', Liquid::Template.parse( '{% for c in collection %}{{c}}{% endfor %}').render!('collection' => EnumerableDrop.new)
end end
def test_enumerable_drop_size def test_enumerable_drop_size
assert_equal '3', Liquid::Template.parse( '{{collection.size}}').render('collection' => EnumerableDrop.new) assert_equal '3', Liquid::Template.parse( '{{collection.size}}').render!('collection' => EnumerableDrop.new)
end end
def test_enumerable_drop_will_invoke_before_method_for_clashing_method_names def test_enumerable_drop_will_invoke_before_method_for_clashing_method_names
["select", "each", "map", "cycle"].each do |method| ["select", "each", "map", "cycle"].each do |method|
assert_equal method.to_s, Liquid::Template.parse("{{collection.#{method}}}").render('collection' => EnumerableDrop.new) assert_equal method.to_s, Liquid::Template.parse("{{collection.#{method}}}").render!('collection' => EnumerableDrop.new)
assert_equal method.to_s, Liquid::Template.parse("{{collection[\"#{method}\"]}}").render('collection' => EnumerableDrop.new) assert_equal method.to_s, Liquid::Template.parse("{{collection[\"#{method}\"]}}").render!('collection' => EnumerableDrop.new)
assert_equal method.to_s, Liquid::Template.parse("{{collection.#{method}}}").render('collection' => RealEnumerableDrop.new) assert_equal method.to_s, Liquid::Template.parse("{{collection.#{method}}}").render!('collection' => RealEnumerableDrop.new)
assert_equal method.to_s, Liquid::Template.parse("{{collection[\"#{method}\"]}}").render('collection' => RealEnumerableDrop.new) assert_equal method.to_s, Liquid::Template.parse("{{collection[\"#{method}\"]}}").render!('collection' => RealEnumerableDrop.new)
end end
end end
def test_some_enumerable_methods_still_get_invoked def test_some_enumerable_methods_still_get_invoked
[ :count, :max ].each do |method| [ :count, :max ].each do |method|
assert_equal "3", Liquid::Template.parse("{{collection.#{method}}}").render('collection' => RealEnumerableDrop.new) assert_equal "3", Liquid::Template.parse("{{collection.#{method}}}").render!('collection' => RealEnumerableDrop.new)
assert_equal "3", Liquid::Template.parse("{{collection[\"#{method}\"]}}").render('collection' => RealEnumerableDrop.new) assert_equal "3", Liquid::Template.parse("{{collection[\"#{method}\"]}}").render!('collection' => RealEnumerableDrop.new)
assert_equal "3", Liquid::Template.parse("{{collection.#{method}}}").render('collection' => EnumerableDrop.new) assert_equal "3", Liquid::Template.parse("{{collection.#{method}}}").render!('collection' => EnumerableDrop.new)
assert_equal "3", Liquid::Template.parse("{{collection[\"#{method}\"]}}").render('collection' => EnumerableDrop.new) assert_equal "3", Liquid::Template.parse("{{collection[\"#{method}\"]}}").render!('collection' => EnumerableDrop.new)
end end
assert_equal "yes", Liquid::Template.parse("{% if collection contains 3 %}yes{% endif %}").render('collection' => RealEnumerableDrop.new) assert_equal "yes", Liquid::Template.parse("{% if collection contains 3 %}yes{% endif %}").render!('collection' => RealEnumerableDrop.new)
[ :min, :first ].each do |method| [ :min, :first ].each do |method|
assert_equal "1", Liquid::Template.parse("{{collection.#{method}}}").render('collection' => RealEnumerableDrop.new) assert_equal "1", Liquid::Template.parse("{{collection.#{method}}}").render!('collection' => RealEnumerableDrop.new)
assert_equal "1", Liquid::Template.parse("{{collection[\"#{method}\"]}}").render('collection' => RealEnumerableDrop.new) assert_equal "1", Liquid::Template.parse("{{collection[\"#{method}\"]}}").render!('collection' => RealEnumerableDrop.new)
assert_equal "1", Liquid::Template.parse("{{collection.#{method}}}").render('collection' => EnumerableDrop.new) assert_equal "1", Liquid::Template.parse("{{collection.#{method}}}").render!('collection' => EnumerableDrop.new)
assert_equal "1", Liquid::Template.parse("{{collection[\"#{method}\"]}}").render('collection' => EnumerableDrop.new) assert_equal "1", Liquid::Template.parse("{{collection[\"#{method}\"]}}").render!('collection' => EnumerableDrop.new)
end end
end end
def test_empty_string_value_access def test_empty_string_value_access
assert_equal '', Liquid::Template.parse('{{ product[value] }}').render('product' => ProductDrop.new, 'value' => '') assert_equal '', Liquid::Template.parse('{{ product[value] }}').render!('product' => ProductDrop.new, 'value' => '')
end end
def test_nil_value_access def test_nil_value_access
assert_equal '', Liquid::Template.parse('{{ product[value] }}').render('product' => ProductDrop.new, 'value' => nil) assert_equal '', Liquid::Template.parse('{{ product[value] }}').render!('product' => ProductDrop.new, 'value' => nil)
end end
def test_default_to_s_on_drops def test_default_to_s_on_drops
assert_equal 'ProductDrop', Liquid::Template.parse("{{ product }}").render('product' => ProductDrop.new) assert_equal 'ProductDrop', Liquid::Template.parse("{{ product }}").render!('product' => ProductDrop.new)
assert_equal 'EnumerableDrop', Liquid::Template.parse('{{ collection }}').render('collection' => EnumerableDrop.new) assert_equal 'EnumerableDrop', Liquid::Template.parse('{{ collection }}').render!('collection' => EnumerableDrop.new)
end end
end # DropsTest end # DropsTest
+5 -5
View File
@@ -113,13 +113,13 @@ class FiltersInTemplate < Test::Unit::TestCase
def test_local_global def test_local_global
Template.register_filter(MoneyFilter) Template.register_filter(MoneyFilter)
assert_equal " 1000$ ", Template.parse("{{1000 | money}}").render(nil, nil) assert_equal " 1000$ ", Template.parse("{{1000 | money}}").render!(nil, nil)
assert_equal " 1000$ CAD ", Template.parse("{{1000 | money}}").render(nil, :filters => CanadianMoneyFilter) assert_equal " 1000$ CAD ", Template.parse("{{1000 | money}}").render!(nil, :filters => CanadianMoneyFilter)
assert_equal " 1000$ CAD ", Template.parse("{{1000 | money}}").render(nil, :filters => [CanadianMoneyFilter]) assert_equal " 1000$ CAD ", Template.parse("{{1000 | money}}").render!(nil, :filters => [CanadianMoneyFilter])
end end
def test_local_filter_with_deprecated_syntax def test_local_filter_with_deprecated_syntax
assert_equal " 1000$ CAD ", Template.parse("{{1000 | money}}").render(nil, CanadianMoneyFilter) assert_equal " 1000$ CAD ", Template.parse("{{1000 | money}}").render!(nil, CanadianMoneyFilter)
assert_equal " 1000$ CAD ", Template.parse("{{1000 | money}}").render(nil, [CanadianMoneyFilter]) assert_equal " 1000$ CAD ", Template.parse("{{1000 | money}}").render!(nil, [CanadianMoneyFilter])
end end
end # FiltersTest end # FiltersTest
+6 -6
View File
@@ -77,11 +77,11 @@ class ModuleExTest < Test::Unit::TestCase
end end
def test_should_use_regular_objects_as_drops def test_should_use_regular_objects_as_drops
assert_equal 'allowedA', Liquid::Template.parse("{{ a.allowedA }}").render('a'=>@a) assert_template_result 'allowedA', "{{ a.allowedA }}", 'a'=>@a
assert_equal 'allowedB', Liquid::Template.parse("{{ a.chainedB.allowedB }}").render('a'=>@a) assert_template_result 'allowedB', "{{ a.chainedB.allowedB }}", 'a'=>@a
assert_equal 'allowedC', Liquid::Template.parse("{{ a.chainedB.chainedC.allowedC }}").render('a'=>@a) assert_template_result 'allowedC', "{{ a.chainedB.chainedC.allowedC }}", 'a'=>@a
assert_equal 'another_allowedC', Liquid::Template.parse("{{ a.chainedB.chainedC.another_allowedC }}").render('a'=>@a) assert_template_result 'another_allowedC', "{{ a.chainedB.chainedC.another_allowedC }}", 'a'=>@a
assert_equal '', Liquid::Template.parse("{{ a.restricted }}").render('a'=>@a) assert_template_result '', "{{ a.restricted }}", 'a'=>@a
assert_equal '', Liquid::Template.parse("{{ a.unknown }}").render('a'=>@a) assert_template_result '', "{{ a.unknown }}", 'a'=>@a
end end
end # ModuleExTest end # ModuleExTest
+11 -11
View File
@@ -41,76 +41,76 @@ class OutputTest < Test::Unit::TestCase
text = %| {{best_cars}} | text = %| {{best_cars}} |
expected = %| bmw | expected = %| bmw |
assert_equal expected, Template.parse(text).render(@assigns) assert_equal expected, Template.parse(text).render!(@assigns)
end end
def test_variable_traversing def test_variable_traversing
text = %| {{car.bmw}} {{car.gm}} {{car.bmw}} | text = %| {{car.bmw}} {{car.gm}} {{car.bmw}} |
expected = %| good bad good | expected = %| good bad good |
assert_equal expected, Template.parse(text).render(@assigns) assert_equal expected, Template.parse(text).render!(@assigns)
end end
def test_variable_piping def test_variable_piping
text = %( {{ car.gm | make_funny }} ) text = %( {{ car.gm | make_funny }} )
expected = %| LOL | expected = %| LOL |
assert_equal expected, Template.parse(text).render(@assigns, :filters => [FunnyFilter]) assert_equal expected, Template.parse(text).render!(@assigns, :filters => [FunnyFilter])
end end
def test_variable_piping_with_input def test_variable_piping_with_input
text = %( {{ car.gm | cite_funny }} ) text = %( {{ car.gm | cite_funny }} )
expected = %| LOL: bad | expected = %| LOL: bad |
assert_equal expected, Template.parse(text).render(@assigns, :filters => [FunnyFilter]) assert_equal expected, Template.parse(text).render!(@assigns, :filters => [FunnyFilter])
end end
def test_variable_piping_with_args def test_variable_piping_with_args
text = %! {{ car.gm | add_smiley : ':-(' }} ! text = %! {{ car.gm | add_smiley : ':-(' }} !
expected = %| bad :-( | expected = %| bad :-( |
assert_equal expected, Template.parse(text).render(@assigns, :filters => [FunnyFilter]) assert_equal expected, Template.parse(text).render!(@assigns, :filters => [FunnyFilter])
end end
def test_variable_piping_with_no_args def test_variable_piping_with_no_args
text = %! {{ car.gm | add_smiley }} ! text = %! {{ car.gm | add_smiley }} !
expected = %| bad :-) | expected = %| bad :-) |
assert_equal expected, Template.parse(text).render(@assigns, :filters => [FunnyFilter]) assert_equal expected, Template.parse(text).render!(@assigns, :filters => [FunnyFilter])
end end
def test_multiple_variable_piping_with_args def test_multiple_variable_piping_with_args
text = %! {{ car.gm | add_smiley : ':-(' | add_smiley : ':-('}} ! text = %! {{ car.gm | add_smiley : ':-(' | add_smiley : ':-('}} !
expected = %| bad :-( :-( | expected = %| bad :-( :-( |
assert_equal expected, Template.parse(text).render(@assigns, :filters => [FunnyFilter]) assert_equal expected, Template.parse(text).render!(@assigns, :filters => [FunnyFilter])
end end
def test_variable_piping_with_multiple_args def test_variable_piping_with_multiple_args
text = %! {{ car.gm | add_tag : 'span', 'bar'}} ! text = %! {{ car.gm | add_tag : 'span', 'bar'}} !
expected = %| <span id="bar">bad</span> | expected = %| <span id="bar">bad</span> |
assert_equal expected, Template.parse(text).render(@assigns, :filters => [FunnyFilter]) assert_equal expected, Template.parse(text).render!(@assigns, :filters => [FunnyFilter])
end end
def test_variable_piping_with_variable_args def test_variable_piping_with_variable_args
text = %! {{ car.gm | add_tag : 'span', car.bmw}} ! text = %! {{ car.gm | add_tag : 'span', car.bmw}} !
expected = %| <span id="good">bad</span> | expected = %| <span id="good">bad</span> |
assert_equal expected, Template.parse(text).render(@assigns, :filters => [FunnyFilter]) assert_equal expected, Template.parse(text).render!(@assigns, :filters => [FunnyFilter])
end end
def test_multiple_pipings def test_multiple_pipings
text = %( {{ best_cars | cite_funny | paragraph }} ) text = %( {{ best_cars | cite_funny | paragraph }} )
expected = %| <p>LOL: bmw</p> | expected = %| <p>LOL: bmw</p> |
assert_equal expected, Template.parse(text).render(@assigns, :filters => [FunnyFilter]) assert_equal expected, Template.parse(text).render!(@assigns, :filters => [FunnyFilter])
end end
def test_link_to def test_link_to
text = %( {{ 'Typo' | link_to: 'http://typo.leetsoft.com' }} ) text = %( {{ 'Typo' | link_to: 'http://typo.leetsoft.com' }} )
expected = %| <a href="http://typo.leetsoft.com">Typo</a> | expected = %| <a href="http://typo.leetsoft.com">Typo</a> |
assert_equal expected, Template.parse(text).render(@assigns, :filters => [FunnyFilter]) assert_equal expected, Template.parse(text).render!(@assigns, :filters => [FunnyFilter])
end end
end # OutputTest end # OutputTest
+1 -1
View File
@@ -7,7 +7,7 @@ class ParsingQuirksTest < Test::Unit::TestCase
text = %| div { font-weight: bold; } | text = %| div { font-weight: bold; } |
template = Template.parse(text) template = Template.parse(text)
assert_equal text, template.render assert_equal text, template.render!
assert_equal [String], template.root.nodelist.collect {|i| i.class} assert_equal [String], template.root.nodelist.collect {|i| i.class}
end end
+5 -5
View File
@@ -13,14 +13,14 @@ class SecurityTest < Test::Unit::TestCase
text = %( {{ '1+1' | instance_eval }} ) text = %( {{ '1+1' | instance_eval }} )
expected = %| 1+1 | expected = %| 1+1 |
assert_equal expected, Template.parse(text).render(@assigns) assert_equal expected, Template.parse(text).render!(@assigns)
end end
def test_no_existing_instance_eval def test_no_existing_instance_eval
text = %( {{ '1+1' | __instance_eval__ }} ) text = %( {{ '1+1' | __instance_eval__ }} )
expected = %| 1+1 | expected = %| 1+1 |
assert_equal expected, Template.parse(text).render(@assigns) assert_equal expected, Template.parse(text).render!(@assigns)
end end
@@ -28,7 +28,7 @@ class SecurityTest < Test::Unit::TestCase
text = %( {{ '1+1' | instance_eval }} ) text = %( {{ '1+1' | instance_eval }} )
expected = %| 1+1 | expected = %| 1+1 |
assert_equal expected, Template.parse(text).render(@assigns) assert_equal expected, Template.parse(text).render!(@assigns)
end end
@@ -36,7 +36,7 @@ class SecurityTest < Test::Unit::TestCase
text = %( {{ '1+1' | add_one | instance_eval }} ) text = %( {{ '1+1' | add_one | instance_eval }} )
expected = %| 1+1 + 1 | expected = %| 1+1 + 1 |
assert_equal expected, Template.parse(text).render(@assigns, :filters => SecurityFilter) assert_equal expected, Template.parse(text).render!(@assigns, :filters => SecurityFilter)
end end
def test_does_not_add_filters_to_symbol_table def test_does_not_add_filters_to_symbol_table
@@ -47,7 +47,7 @@ class SecurityTest < Test::Unit::TestCase
template = Template.parse(test) template = Template.parse(test)
assert_equal [], (Symbol.all_symbols - current_symbols) assert_equal [], (Symbol.all_symbols - current_symbols)
template.render template.render!
assert_equal [], (Symbol.all_symbols - current_symbols) assert_equal [], (Symbol.all_symbols - current_symbols)
end end
+10 -9
View File
@@ -126,37 +126,38 @@ class StandardFiltersTest < Test::Unit::TestCase
end end
def test_map_doesnt_call_arbitrary_stuff def test_map_doesnt_call_arbitrary_stuff
assert_equal "", Liquid::Template.parse('{{ "foo" | map: "__id__" }}').render assert_template_result "", '{{ "foo" | map: "__id__" }}'
assert_equal "", Liquid::Template.parse('{{ "foo" | map: "inspect" }}').render assert_template_result "", '{{ "foo" | map: "inspect" }}'
end end
def test_map_calls_to_liquid def test_map_calls_to_liquid
t = TestThing.new t = TestThing.new
assert_equal "woot: 1", Liquid::Template.parse('{{ foo | map: "whatever" }}').render("foo" => [t]) assert_template_result "woot: 1", '{{ foo | map: "whatever" }}', "foo" => [t]
end end
def test_map_on_hashes def test_map_on_hashes
assert_equal "4217", Liquid::Template.parse('{{ thing | map: "foo" | map: "bar" }}').render("thing" => { "foo" => [ { "bar" => 42 }, { "bar" => 17 } ] }) assert_template_result "4217", '{{ thing | map: "foo" | map: "bar" }}',
"thing" => { "foo" => [ { "bar" => 42 }, { "bar" => 17 } ] }
end end
def test_sort_calls_to_liquid def test_sort_calls_to_liquid
t = TestThing.new t = TestThing.new
assert_equal "woot: 1", Liquid::Template.parse('{{ foo | sort: "whatever" }}').render("foo" => [t]) assert_template_result "woot: 1", '{{ foo | sort: "whatever" }}', "foo" => [t]
end end
def test_map_over_proc def test_map_over_proc
drop = TestDrop.new drop = TestDrop.new
p = Proc.new{ drop } p = Proc.new{ drop }
templ = '{{ procs | map: "test" }}' templ = '{{ procs | map: "test" }}'
assert_equal "testfoo", Liquid::Template.parse(templ).render("procs" => [p]) assert_template_result "testfoo", templ, "procs" => [p]
end end
def test_map_works_on_enumerables def test_map_works_on_enumerables
assert_equal "123", Liquid::Template.parse('{{ foo | map: "foo" }}').render!("foo" => TestEnumerable.new) assert_template_result "123", '{{ foo | map: "foo" }}', "foo" => TestEnumerable.new
end end
def test_sort_works_on_enumerables def test_sort_works_on_enumerables
assert_equal "213", Liquid::Template.parse('{{ foo | sort: "bar" | map: "foo" }}').render!("foo" => TestEnumerable.new) assert_template_result "213", '{{ foo | sort: "bar" | map: "foo" }}', "foo" => TestEnumerable.new
end end
def test_date def test_date
@@ -257,7 +258,7 @@ class StandardFiltersTest < Test::Unit::TestCase
assert_template_result "4", "{{ 14 | divided_by:3 }}" assert_template_result "4", "{{ 14 | divided_by:3 }}"
assert_template_result "5", "{{ 15 | divided_by:3 }}" assert_template_result "5", "{{ 15 | divided_by:3 }}"
assert_template_result "Liquid error: divided by 0", "{{ 5 | divided_by:0 }}" assert_equal "Liquid error: divided by 0", Template.parse("{{ 5 | divided_by:0 }}").render
assert_template_result "0.5", "{{ 2.0 | divided_by:4 }}" assert_template_result "0.5", "{{ 2.0 | divided_by:4 }}"
end end
+29 -35
View File
@@ -78,57 +78,52 @@ class IncludeTagTest < Test::Unit::TestCase
def test_include_tag_looks_for_file_system_in_registers_first def test_include_tag_looks_for_file_system_in_registers_first
assert_equal 'from OtherFileSystem', assert_equal 'from OtherFileSystem',
Template.parse("{% include 'pick_a_source' %}").render({}, :registers => {:file_system => OtherFileSystem.new}) Template.parse("{% include 'pick_a_source' %}").render!({}, :registers => {:file_system => OtherFileSystem.new})
end end
def test_include_tag_with def test_include_tag_with
assert_equal "Product: Draft 151cm ", assert_template_result "Product: Draft 151cm ",
Template.parse("{% include 'product' with products[0] %}").render( "products" => [ {'title' => 'Draft 151cm'}, {'title' => 'Element 155cm'} ] ) "{% include 'product' with products[0] %}", "products" => [ {'title' => 'Draft 151cm'}, {'title' => 'Element 155cm'} ]
end end
def test_include_tag_with_default_name def test_include_tag_with_default_name
assert_equal "Product: Draft 151cm ", assert_template_result "Product: Draft 151cm ",
Template.parse("{% include 'product' %}").render( "product" => {'title' => 'Draft 151cm'} ) "{% include 'product' %}", "product" => {'title' => 'Draft 151cm'}
end end
def test_include_tag_for def test_include_tag_for
assert_template_result "Product: Draft 151cm Product: Element 155cm ",
assert_equal "Product: Draft 151cm Product: Element 155cm ", "{% include 'product' for products %}", "products" => [ {'title' => 'Draft 151cm'}, {'title' => 'Element 155cm'} ]
Template.parse("{% include 'product' for products %}").render( "products" => [ {'title' => 'Draft 151cm'}, {'title' => 'Element 155cm'} ] )
end end
def test_include_tag_with_local_variables def test_include_tag_with_local_variables
assert_equal "Locale: test123 ", assert_template_result "Locale: test123 ", "{% include 'locale_variables' echo1: 'test123' %}"
Template.parse("{% include 'locale_variables' echo1: 'test123' %}").render
end end
def test_include_tag_with_multiple_local_variables def test_include_tag_with_multiple_local_variables
assert_equal "Locale: test123 test321", assert_template_result "Locale: test123 test321",
Template.parse("{% include 'locale_variables' echo1: 'test123', echo2: 'test321' %}").render "{% include 'locale_variables' echo1: 'test123', echo2: 'test321' %}"
end end
def test_include_tag_with_multiple_local_variables_from_context def test_include_tag_with_multiple_local_variables_from_context
assert_equal "Locale: test123 test321", assert_template_result "Locale: test123 test321",
Template.parse("{% include 'locale_variables' echo1: echo1, echo2: more_echos.echo2 %}").render('echo1' => 'test123', 'more_echos' => { "echo2" => 'test321'}) "{% include 'locale_variables' echo1: echo1, echo2: more_echos.echo2 %}",
'echo1' => 'test123', 'more_echos' => { "echo2" => 'test321'}
end end
def test_nested_include_tag def test_nested_include_tag
assert_equal "body body_detail", assert_template_result "body body_detail", "{% include 'body' %}"
Template.parse("{% include 'body' %}").render
assert_equal "header body body_detail footer", assert_template_result "header body body_detail footer", "{% include 'nested_template' %}"
Template.parse("{% include 'nested_template' %}").render
end end
def test_nested_include_with_variable def test_nested_include_with_variable
assert_template_result "Product: Draft 151cm details ",
"{% include 'nested_product_template' with product %}", "product" => {"title" => 'Draft 151cm'}
assert_equal "Product: Draft 151cm details ", assert_template_result "Product: Draft 151cm details Product: Element 155cm details ",
Template.parse("{% include 'nested_product_template' with product %}").render("product" => {"title" => 'Draft 151cm'}) "{% include 'nested_product_template' for products %}", "products" => [{"title" => 'Draft 151cm'}, {"title" => 'Element 155cm'}]
assert_equal "Product: Draft 151cm details Product: Element 155cm details ",
Template.parse("{% include 'nested_product_template' for products %}").render("products" => [{"title" => 'Draft 151cm'}, {"title" => 'Element 155cm'}])
end end
def test_recursively_included_template_does_not_produce_endless_loop def test_recursively_included_template_does_not_produce_endless_loop
@@ -160,34 +155,33 @@ class IncludeTagTest < Test::Unit::TestCase
end end
def test_dynamically_choosen_template def test_dynamically_choosen_template
assert_template_result "Test123", "{% include template %}", "template" => 'Test123'
assert_template_result "Test321", "{% include template %}", "template" => 'Test321'
assert_equal "Test123", Template.parse("{% include template %}").render("template" => 'Test123') assert_template_result "Product: Draft 151cm ", "{% include template for product %}",
assert_equal "Test321", Template.parse("{% include template %}").render("template" => 'Test321') "template" => 'product', 'product' => { 'title' => 'Draft 151cm'}
assert_equal "Product: Draft 151cm ", Template.parse("{% include template for product %}").render("template" => 'product', 'product' => { 'title' => 'Draft 151cm'})
end end
def test_include_tag_caches_second_read_of_same_partial def test_include_tag_caches_second_read_of_same_partial
file_system = CountingFileSystem.new file_system = CountingFileSystem.new
assert_equal 'from CountingFileSystemfrom CountingFileSystem', assert_equal 'from CountingFileSystemfrom CountingFileSystem',
Template.parse("{% include 'pick_a_source' %}{% include 'pick_a_source' %}").render({}, :registers => {:file_system => file_system}) Template.parse("{% include 'pick_a_source' %}{% include 'pick_a_source' %}").render!({}, :registers => {:file_system => file_system})
assert_equal 1, file_system.count assert_equal 1, file_system.count
end end
def test_include_tag_doesnt_cache_partials_across_renders def test_include_tag_doesnt_cache_partials_across_renders
file_system = CountingFileSystem.new file_system = CountingFileSystem.new
assert_equal 'from CountingFileSystem', assert_equal 'from CountingFileSystem',
Template.parse("{% include 'pick_a_source' %}").render({}, :registers => {:file_system => file_system}) Template.parse("{% include 'pick_a_source' %}").render!({}, :registers => {:file_system => file_system})
assert_equal 1, file_system.count assert_equal 1, file_system.count
assert_equal 'from CountingFileSystem', assert_equal 'from CountingFileSystem',
Template.parse("{% include 'pick_a_source' %}").render({}, :registers => {:file_system => file_system}) Template.parse("{% include 'pick_a_source' %}").render!({}, :registers => {:file_system => file_system})
assert_equal 2, file_system.count assert_equal 2, file_system.count
end end
def test_include_tag_within_if_statement def test_include_tag_within_if_statement
assert_equal "foo_if_true", assert_template_result "foo_if_true", "{% if true %}{% include 'foo_if_true' %}{% endif %}"
Template.parse("{% if true %}{% include 'foo_if_true' %}{% endif %}").render
end end
def test_custom_include_tag def test_custom_include_tag
@@ -195,7 +189,7 @@ class IncludeTagTest < Test::Unit::TestCase
Liquid::Template.tags['include'] = CustomInclude Liquid::Template.tags['include'] = CustomInclude
begin begin
assert_equal "custom_foo", assert_equal "custom_foo",
Template.parse("{% include 'custom_foo' %}").render Template.parse("{% include 'custom_foo' %}").render!
ensure ensure
Liquid::Template.tags['include'] = original_tag Liquid::Template.tags['include'] = original_tag
end end
@@ -206,7 +200,7 @@ class IncludeTagTest < Test::Unit::TestCase
Liquid::Template.tags['include'] = CustomInclude Liquid::Template.tags['include'] = CustomInclude
begin begin
assert_equal "custom_foo_if_true", assert_equal "custom_foo_if_true",
Template.parse("{% if true %}{% include 'custom_foo_if_true' %}{% endif %}").render Template.parse("{% if true %}{% include 'custom_foo_if_true' %}{% endif %}").render!
ensure ensure
Liquid::Template.tags['include'] = original_tag Liquid::Template.tags['include'] = original_tag
end end
+8 -9
View File
@@ -180,11 +180,11 @@ class StandardTagTest < Test::Unit::TestCase
# 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)
assert_equal "menswear", template.render("collection" => {'handle' => 'menswear-jackets'}) assert_equal "menswear", template.render!("collection" => {'handle' => 'menswear-jackets'})
assert_equal "menswear", template.render("collection" => {'handle' => 'menswear-t-shirts'}) assert_equal "menswear", template.render!("collection" => {'handle' => 'menswear-t-shirts'})
assert_equal "womenswear", template.render("collection" => {'handle' => 'x'}) assert_equal "womenswear", template.render!("collection" => {'handle' => 'x'})
assert_equal "womenswear", template.render("collection" => {'handle' => 'y'}) assert_equal "womenswear", template.render!("collection" => {'handle' => 'y'})
assert_equal "womenswear", template.render("collection" => {'handle' => 'z'}) assert_equal "womenswear", template.render!("collection" => {'handle' => 'z'})
end end
def test_case_when_or def test_case_when_or
@@ -218,7 +218,7 @@ class StandardTagTest < Test::Unit::TestCase
end end
def test_assign def test_assign
assert_equal 'variable', Liquid::Template.parse( '{% assign a = "variable"%}{{a}}').render assert_template_result 'variable', '{% assign a = "variable"%}{{a}}'
end end
def test_assign_unassigned def test_assign_unassigned
@@ -227,12 +227,11 @@ class StandardTagTest < Test::Unit::TestCase
end end
def test_assign_an_empty_string def test_assign_an_empty_string
assert_equal '', Liquid::Template.parse( '{% assign a = ""%}{{a}}' ).render assert_template_result '', '{% assign a = ""%}{{a}}'
end end
def test_assign_is_global def test_assign_is_global
assert_equal 'variable', assert_template_result 'variable', '{%for i in (1..2) %}{% assign a = "variable"%}{% endfor %}{{a}}'
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
+42 -63
View File
@@ -4,93 +4,78 @@ class StatementsTest < Test::Unit::TestCase
include Liquid include Liquid
def test_true_eql_true def test_true_eql_true
text = %| {% if true == true %} true {% else %} false {% endif %} | text = ' {% if true == true %} true {% else %} false {% endif %} '
expected = %| true | assert_template_result ' true ', text
assert_equal expected, Template.parse(text).render
end end
def test_true_not_eql_true def test_true_not_eql_true
text = %| {% if true != true %} true {% else %} false {% endif %} | text = ' {% if true != true %} true {% else %} false {% endif %} '
expected = %| false | assert_template_result ' false ', text
assert_equal expected, Template.parse(text).render
end end
def test_true_lq_true def test_true_lq_true
text = %| {% if 0 > 0 %} true {% else %} false {% endif %} | text = ' {% if 0 > 0 %} true {% else %} false {% endif %} '
expected = %| false | assert_template_result ' false ', text
assert_equal expected, Template.parse(text).render
end end
def test_one_lq_zero def test_one_lq_zero
text = %| {% if 1 > 0 %} true {% else %} false {% endif %} | text = ' {% if 1 > 0 %} true {% else %} false {% endif %} '
expected = %| true | assert_template_result ' true ', text
assert_equal expected, Template.parse(text).render
end end
def test_zero_lq_one def test_zero_lq_one
text = %| {% if 0 < 1 %} true {% else %} false {% endif %} | text = ' {% if 0 < 1 %} true {% else %} false {% endif %} '
expected = %| true | assert_template_result ' true ', text
assert_equal expected, Template.parse(text).render
end end
def test_zero_lq_or_equal_one def test_zero_lq_or_equal_one
text = %| {% if 0 <= 0 %} true {% else %} false {% endif %} | text = ' {% if 0 <= 0 %} true {% else %} false {% endif %} '
expected = %| true | assert_template_result ' true ', text
assert_equal expected, Template.parse(text).render
end end
def test_zero_lq_or_equal_one_involving_nil def test_zero_lq_or_equal_one_involving_nil
text = %| {% if null <= 0 %} true {% else %} false {% endif %} | text = ' {% if null <= 0 %} true {% else %} false {% endif %} '
expected = %| false | assert_template_result ' false ', text
assert_equal expected, Template.parse(text).render
text = %| {% if 0 <= null %} true {% else %} false {% endif %} | text = ' {% if 0 <= null %} true {% else %} false {% endif %} '
expected = %| false | assert_template_result ' false ', text
assert_equal expected, Template.parse(text).render
end end
def test_zero_lqq_or_equal_one def test_zero_lqq_or_equal_one
text = %| {% if 0 >= 0 %} true {% else %} false {% endif %} | text = ' {% if 0 >= 0 %} true {% else %} false {% endif %} '
expected = %| true | assert_template_result ' true ', text
assert_equal expected, Template.parse(text).render
end end
def test_strings def test_strings
text = %| {% if 'test' == 'test' %} true {% else %} false {% endif %} | text = " {% if 'test' == 'test' %} true {% else %} false {% endif %} "
expected = %| true | assert_template_result ' true ', text
assert_equal expected, Template.parse(text).render
end end
def test_strings_not_equal def test_strings_not_equal
text = %| {% if 'test' != 'test' %} true {% else %} false {% endif %} | text = " {% if 'test' != 'test' %} true {% else %} false {% endif %} "
expected = %| false | assert_template_result ' false ', text
assert_equal expected, Template.parse(text).render
end end
def test_var_strings_equal def test_var_strings_equal
text = %| {% if var == "hello there!" %} true {% else %} false {% endif %} | text = ' {% if var == "hello there!" %} true {% else %} false {% endif %} '
expected = %| true | assert_template_result ' true ', text, 'var' => 'hello there!'
assert_equal expected, Template.parse(text).render('var' => 'hello there!')
end end
def test_var_strings_are_not_equal def test_var_strings_are_not_equal
text = %| {% if "hello there!" == var %} true {% else %} false {% endif %} | text = ' {% if "hello there!" == var %} true {% else %} false {% endif %} '
expected = %| true | assert_template_result ' true ', text, 'var' => 'hello there!'
assert_equal expected, Template.parse(text).render('var' => 'hello there!')
end end
def test_var_and_long_string_are_equal def test_var_and_long_string_are_equal
text = %| {% if var == 'hello there!' %} true {% else %} false {% endif %} | text = " {% if var == 'hello there!' %} true {% else %} false {% endif %} "
expected = %| true | assert_template_result ' true ', text, 'var' => 'hello there!'
assert_equal expected, Template.parse(text).render('var' => 'hello there!')
end end
def test_var_and_long_string_are_equal_backwards def test_var_and_long_string_are_equal_backwards
text = %| {% if 'hello there!' == var %} true {% else %} false {% endif %} | text = " {% if 'hello there!' == var %} true {% else %} false {% endif %} "
expected = %| true | assert_template_result ' true ', text, 'var' => 'hello there!'
assert_equal expected, Template.parse(text).render('var' => 'hello there!')
end end
#def test_is_nil #def test_is_nil
@@ -101,34 +86,28 @@ class StatementsTest < Test::Unit::TestCase
#end #end
def test_is_collection_empty def test_is_collection_empty
text = %| {% if array == empty %} true {% else %} false {% endif %} | text = ' {% if array == empty %} true {% else %} false {% endif %} '
expected = %| true | assert_template_result ' true ', text, 'array' => []
assert_equal expected, Template.parse(text).render('array' => [])
end end
def test_is_not_collection_empty def test_is_not_collection_empty
text = %| {% if array == empty %} true {% else %} false {% endif %} | text = ' {% if array == empty %} true {% else %} false {% endif %} '
expected = %| false | assert_template_result ' false ', text, 'array' => [1,2,3]
assert_equal expected, Template.parse(text).render('array' => [1,2,3])
end end
def test_nil def test_nil
text = %| {% if var == nil %} true {% else %} false {% endif %} | text = ' {% if var == nil %} true {% else %} false {% endif %} '
expected = %| true | assert_template_result ' true ', text, 'var' => nil
assert_equal expected, Template.parse(text).render('var' => nil)
text = %| {% if var == null %} true {% else %} false {% endif %} | text = ' {% if var == null %} true {% else %} false {% endif %} '
expected = %| true | assert_template_result ' true ', text, 'var' => nil
assert_equal expected, Template.parse(text).render('var' => nil)
end end
def test_not_nil def test_not_nil
text = %| {% if var != nil %} true {% else %} false {% endif %} | text = ' {% if var != nil %} true {% else %} false {% endif %} '
expected = %| true | assert_template_result ' true ', text, 'var' => 1
assert_equal expected, Template.parse(text).render('var' => 1 )
text = %| {% if var != null %} true {% else %} false {% endif %} | text = ' {% if var != null %} true {% else %} false {% endif %} '
expected = %| true | assert_template_result ' true ', text, 'var' => 1
assert_equal expected, Template.parse(text).render('var' => 1 )
end end
end # StatementsTest end # StatementsTest
+24 -24
View File
@@ -47,57 +47,57 @@ class TemplateTest < Test::Unit::TestCase
def test_instance_assigns_persist_on_same_template_object_between_parses def test_instance_assigns_persist_on_same_template_object_between_parses
t = Template.new t = Template.new
assert_equal 'from instance assigns', t.parse("{% assign foo = 'from instance assigns' %}{{ foo }}").render assert_equal 'from instance assigns', t.parse("{% assign foo = 'from instance assigns' %}{{ foo }}").render!
assert_equal 'from instance assigns', t.parse("{{ foo }}").render assert_equal 'from instance assigns', t.parse("{{ foo }}").render!
end end
def test_instance_assigns_persist_on_same_template_parsing_between_renders def test_instance_assigns_persist_on_same_template_parsing_between_renders
t = Template.new.parse("{{ foo }}{% assign foo = 'foo' %}{{ foo }}") t = Template.new.parse("{{ foo }}{% assign foo = 'foo' %}{{ foo }}")
assert_equal 'foo', t.render assert_equal 'foo', t.render!
assert_equal 'foofoo', t.render assert_equal 'foofoo', t.render!
end end
def test_custom_assigns_do_not_persist_on_same_template def test_custom_assigns_do_not_persist_on_same_template
t = Template.new t = Template.new
assert_equal 'from custom assigns', t.parse("{{ foo }}").render('foo' => 'from custom assigns') assert_equal 'from custom assigns', t.parse("{{ foo }}").render!('foo' => 'from custom assigns')
assert_equal '', t.parse("{{ foo }}").render assert_equal '', t.parse("{{ foo }}").render!
end end
def test_custom_assigns_squash_instance_assigns def test_custom_assigns_squash_instance_assigns
t = Template.new t = Template.new
assert_equal 'from instance assigns', t.parse("{% assign foo = 'from instance assigns' %}{{ foo }}").render assert_equal 'from instance assigns', t.parse("{% assign foo = 'from instance assigns' %}{{ foo }}").render!
assert_equal 'from custom assigns', t.parse("{{ foo }}").render('foo' => 'from custom assigns') assert_equal 'from custom assigns', t.parse("{{ foo }}").render!('foo' => 'from custom assigns')
end end
def test_persistent_assigns_squash_instance_assigns def test_persistent_assigns_squash_instance_assigns
t = Template.new t = Template.new
assert_equal 'from instance assigns', t.parse("{% assign foo = 'from instance assigns' %}{{ foo }}").render assert_equal 'from instance assigns', t.parse("{% assign foo = 'from instance assigns' %}{{ foo }}").render!
t.assigns['foo'] = 'from persistent assigns' t.assigns['foo'] = 'from persistent assigns'
assert_equal 'from persistent assigns', t.parse("{{ foo }}").render assert_equal 'from persistent assigns', t.parse("{{ foo }}").render!
end end
def test_lambda_is_called_once_from_persistent_assigns_over_multiple_parses_and_renders def test_lambda_is_called_once_from_persistent_assigns_over_multiple_parses_and_renders
t = Template.new t = Template.new
t.assigns['number'] = lambda { @global ||= 0; @global += 1 } t.assigns['number'] = lambda { @global ||= 0; @global += 1 }
assert_equal '1', t.parse("{{number}}").render assert_equal '1', t.parse("{{number}}").render!
assert_equal '1', t.parse("{{number}}").render assert_equal '1', t.parse("{{number}}").render!
assert_equal '1', t.render assert_equal '1', t.render!
@global = nil @global = nil
end end
def test_lambda_is_called_once_from_custom_assigns_over_multiple_parses_and_renders def test_lambda_is_called_once_from_custom_assigns_over_multiple_parses_and_renders
t = Template.new t = Template.new
assigns = {'number' => lambda { @global ||= 0; @global += 1 }} assigns = {'number' => lambda { @global ||= 0; @global += 1 }}
assert_equal '1', t.parse("{{number}}").render(assigns) assert_equal '1', t.parse("{{number}}").render!(assigns)
assert_equal '1', t.parse("{{number}}").render(assigns) assert_equal '1', t.parse("{{number}}").render!(assigns)
assert_equal '1', t.render(assigns) assert_equal '1', t.render!(assigns)
@global = nil @global = nil
end end
def test_resource_limits_works_with_custom_length_method def test_resource_limits_works_with_custom_length_method
t = Template.parse("{% assign foo = bar %}") t = Template.parse("{% assign foo = bar %}")
t.resource_limits = { :render_length_limit => 42 } t.resource_limits = { :render_length_limit => 42 }
assert_equal "", t.render("bar" => SomethingWithLength.new) assert_equal "", t.render!("bar" => SomethingWithLength.new)
end end
def test_resource_limits_render_length def test_resource_limits_render_length
@@ -106,7 +106,7 @@ class TemplateTest < Test::Unit::TestCase
assert_equal "Liquid error: Memory limits exceeded", t.render() assert_equal "Liquid error: Memory limits exceeded", t.render()
assert t.resource_limits[:reached] assert t.resource_limits[:reached]
t.resource_limits = { :render_length_limit => 10 } t.resource_limits = { :render_length_limit => 10 }
assert_equal "0123456789", t.render() assert_equal "0123456789", t.render!()
assert_not_nil t.resource_limits[:render_length_current] assert_not_nil t.resource_limits[:render_length_current]
end end
@@ -120,7 +120,7 @@ class TemplateTest < Test::Unit::TestCase
assert_equal "Liquid error: Memory limits exceeded", t.render() assert_equal "Liquid error: Memory limits exceeded", t.render()
assert t.resource_limits[:reached] assert t.resource_limits[:reached]
t.resource_limits = { :render_score_limit => 200 } t.resource_limits = { :render_score_limit => 200 }
assert_equal (" foo " * 100), t.render() assert_equal (" foo " * 100), t.render!()
assert_not_nil t.resource_limits[:render_score_current] assert_not_nil t.resource_limits[:render_score_current]
end end
@@ -130,7 +130,7 @@ class TemplateTest < Test::Unit::TestCase
assert_equal "Liquid error: Memory limits exceeded", t.render() assert_equal "Liquid error: Memory limits exceeded", t.render()
assert t.resource_limits[:reached] assert t.resource_limits[:reached]
t.resource_limits = { :assign_score_limit => 2 } t.resource_limits = { :assign_score_limit => 2 }
assert_equal "", t.render() assert_equal "", t.render!()
assert_not_nil t.resource_limits[:assign_score_current] assert_not_nil t.resource_limits[:assign_score_current]
end end
@@ -143,7 +143,7 @@ class TemplateTest < Test::Unit::TestCase
def test_resource_limits_hash_in_template_gets_updated_even_if_no_limits_are_set def test_resource_limits_hash_in_template_gets_updated_even_if_no_limits_are_set
t = Template.parse("{% for a in (1..100) %} {% assign foo = 1 %} {% endfor %}") t = Template.parse("{% for a in (1..100) %} {% assign foo = 1 %} {% endfor %}")
t.render() t.render!()
assert t.resource_limits[:assign_score_current] > 0 assert t.resource_limits[:assign_score_current] > 0
assert t.resource_limits[:render_score_current] > 0 assert t.resource_limits[:render_score_current] > 0
assert t.resource_limits[:render_length_current] > 0 assert t.resource_limits[:render_length_current] > 0
@@ -153,9 +153,9 @@ class TemplateTest < Test::Unit::TestCase
t = Template.new t = Template.new
t.registers['lulz'] = 'haha' t.registers['lulz'] = 'haha'
drop = TemplateContextDrop.new drop = TemplateContextDrop.new
assert_equal 'fizzbuzz', t.parse('{{foo}}').render(drop) assert_equal 'fizzbuzz', t.parse('{{foo}}').render!(drop)
assert_equal 'bar', t.parse('{{bar}}').render(drop) assert_equal 'bar', t.parse('{{bar}}').render!(drop)
assert_equal 'haha', t.parse("{{baz}}").render(drop) assert_equal 'haha', t.parse("{{baz}}").render!(drop)
end end
def test_sets_default_localization_in_document def test_sets_default_localization_in_document
+15 -15
View File
@@ -141,49 +141,49 @@ class VariableResolutionTest < Test::Unit::TestCase
def test_simple_variable def test_simple_variable
template = Template.parse(%|{{test}}|) template = Template.parse(%|{{test}}|)
assert_equal 'worked', template.render('test' => 'worked') assert_equal 'worked', template.render!('test' => 'worked')
assert_equal 'worked wonderfully', template.render('test' => 'worked wonderfully') assert_equal 'worked wonderfully', template.render!('test' => 'worked wonderfully')
end end
def test_simple_with_whitespaces def test_simple_with_whitespaces
template = Template.parse(%| {{ test }} |) template = Template.parse(%| {{ test }} |)
assert_equal ' worked ', template.render('test' => 'worked') assert_equal ' worked ', template.render!('test' => 'worked')
assert_equal ' worked wonderfully ', template.render('test' => 'worked wonderfully') assert_equal ' worked wonderfully ', template.render!('test' => 'worked wonderfully')
end end
def test_ignore_unknown def test_ignore_unknown
template = Template.parse(%|{{ test }}|) template = Template.parse(%|{{ test }}|)
assert_equal '', template.render assert_equal '', template.render!
end end
def test_hash_scoping def test_hash_scoping
template = Template.parse(%|{{ test.test }}|) template = Template.parse(%|{{ test.test }}|)
assert_equal 'worked', template.render('test' => {'test' => 'worked'}) assert_equal 'worked', template.render!('test' => {'test' => 'worked'})
end end
def test_preset_assigns def test_preset_assigns
template = Template.parse(%|{{ test }}|) template = Template.parse(%|{{ test }}|)
template.assigns['test'] = 'worked' template.assigns['test'] = 'worked'
assert_equal 'worked', template.render assert_equal 'worked', template.render!
end end
def test_reuse_parsed_template def test_reuse_parsed_template
template = Template.parse(%|{{ greeting }} {{ name }}|) template = Template.parse(%|{{ greeting }} {{ name }}|)
template.assigns['greeting'] = 'Goodbye' template.assigns['greeting'] = 'Goodbye'
assert_equal 'Hello Tobi', template.render('greeting' => 'Hello', 'name' => 'Tobi') assert_equal 'Hello Tobi', template.render!('greeting' => 'Hello', 'name' => 'Tobi')
assert_equal 'Hello ', template.render('greeting' => 'Hello', 'unknown' => 'Tobi') assert_equal 'Hello ', template.render!('greeting' => 'Hello', 'unknown' => 'Tobi')
assert_equal 'Hello Brian', template.render('greeting' => 'Hello', 'name' => 'Brian') assert_equal 'Hello Brian', template.render!('greeting' => 'Hello', 'name' => 'Brian')
assert_equal 'Goodbye Brian', template.render('name' => 'Brian') assert_equal 'Goodbye Brian', template.render!('name' => 'Brian')
assert_equal({'greeting'=>'Goodbye'}, template.assigns) assert_equal({'greeting'=>'Goodbye'}, template.assigns)
end end
def test_assigns_not_polluted_from_template def test_assigns_not_polluted_from_template
template = Template.parse(%|{{ test }}{% assign test = 'bar' %}{{ test }}|) template = Template.parse(%|{{ test }}{% assign test = 'bar' %}{{ test }}|)
template.assigns['test'] = 'baz' template.assigns['test'] = 'baz'
assert_equal 'bazbar', template.render assert_equal 'bazbar', template.render!
assert_equal 'bazbar', template.render assert_equal 'bazbar', template.render!
assert_equal 'foobar', template.render('test' => 'foo') assert_equal 'foobar', template.render!('test' => 'foo')
assert_equal 'bazbar', template.render assert_equal 'bazbar', template.render!
end end
def test_hash_with_default_proc def test_hash_with_default_proc
+2 -2
View File
@@ -26,13 +26,13 @@ module Test
include Liquid include Liquid
def assert_template_result(expected, template, assigns = {}, message = nil) def assert_template_result(expected, template, assigns = {}, message = nil)
assert_equal expected, Template.parse(template).render(assigns) assert_equal expected, Template.parse(template).render!(assigns)
end end
def assert_template_result_matches(expected, template, assigns = {}, message = nil) def assert_template_result_matches(expected, template, assigns = {}, message = nil)
return assert_template_result(expected, template, assigns, message) unless expected.is_a? Regexp return assert_template_result(expected, template, assigns, message) unless expected.is_a? Regexp
assert_match expected, Template.parse(template).render(assigns) assert_match expected, Template.parse(template).render!(assigns)
end end
def assert_match_syntax_error(match, template, registers = {}) def assert_match_syntax_error(match, template, registers = {})