From dedd1d3dc0c0724331c36f7ab34c083cf9533b19 Mon Sep 17 00:00:00 2001 From: Justin Li Date: Tue, 21 Oct 2014 12:09:26 -0400 Subject: [PATCH 1/3] Fix case where a variable name is falsy --- lib/liquid/variable.rb | 2 +- test/integration/tags/if_else_tag_test.rb | 1 + test/integration/variable_test.rb | 1 + 3 files changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/liquid/variable.rb b/lib/liquid/variable.rb index d2370781..7710cef0 100644 --- a/lib/liquid/variable.rb +++ b/lib/liquid/variable.rb @@ -83,7 +83,7 @@ module Liquid end def render(context) - return ''.freeze unless @name + return ''.freeze if @name.nil? @filters.inject(context.evaluate(@name)) do |output, (filter_name, filter_args, filter_kwargs)| filter_args = evaluate_filter_expressions(context, filter_args, filter_kwargs) output = context.invoke(filter_name, output, *filter_args) diff --git a/test/integration/tags/if_else_tag_test.rb b/test/integration/tags/if_else_tag_test.rb index 0b7ee0e9..26138359 100644 --- a/test/integration/tags/if_else_tag_test.rb +++ b/test/integration/tags/if_else_tag_test.rb @@ -8,6 +8,7 @@ class IfElseTagTest < Minitest::Test assert_template_result(' this text should go into the output ', ' {% if true %} this text should go into the output {% endif %} ') assert_template_result(' you rock ?','{% if false %} you suck {% endif %} {% if true %} you rock {% endif %}?') + assert_template_result(' NO ','{% assign v = false %}{% if v %} YES {% else %} NO {% endif %}') end def test_if_else diff --git a/test/integration/variable_test.rb b/test/integration/variable_test.rb index bd981316..a7dc1e1e 100644 --- a/test/integration/variable_test.rb +++ b/test/integration/variable_test.rb @@ -31,6 +31,7 @@ class VariableTest < Minitest::Test def test_false_renders_as_false assert_equal 'false', Template.parse("{{ foo }}").render!('foo' => false) + assert_equal 'false', Template.parse("{{ false }}").render! end def test_preset_assigns From 5d68e8803f92dbba34c13697d9e7b616f682e928 Mon Sep 17 00:00:00 2001 From: Justin Li Date: Tue, 21 Oct 2014 14:03:10 -0400 Subject: [PATCH 2/3] Ensure nil works as a variable name --- lib/liquid/variable.rb | 1 - test/integration/tags/if_else_tag_test.rb | 4 ++++ test/integration/variable_test.rb | 4 ++++ 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/liquid/variable.rb b/lib/liquid/variable.rb index 7710cef0..5e187103 100644 --- a/lib/liquid/variable.rb +++ b/lib/liquid/variable.rb @@ -83,7 +83,6 @@ module Liquid end def render(context) - return ''.freeze if @name.nil? @filters.inject(context.evaluate(@name)) do |output, (filter_name, filter_args, filter_kwargs)| filter_args = evaluate_filter_expressions(context, filter_args, filter_kwargs) output = context.invoke(filter_name, output, *filter_args) diff --git a/test/integration/tags/if_else_tag_test.rb b/test/integration/tags/if_else_tag_test.rb index 26138359..3e1797e2 100644 --- a/test/integration/tags/if_else_tag_test.rb +++ b/test/integration/tags/if_else_tag_test.rb @@ -8,7 +8,11 @@ class IfElseTagTest < Minitest::Test assert_template_result(' this text should go into the output ', ' {% if true %} this text should go into the output {% endif %} ') assert_template_result(' you rock ?','{% if false %} you suck {% endif %} {% if true %} you rock {% endif %}?') + end + + def test_literal_comparisons assert_template_result(' NO ','{% assign v = false %}{% if v %} YES {% else %} NO {% endif %}') + assert_template_result(' YES ','{% assign v = nil %}{% if v == nil %} YES {% else %} NO {% endif %}') end def test_if_else diff --git a/test/integration/variable_test.rb b/test/integration/variable_test.rb index a7dc1e1e..4d08cf40 100644 --- a/test/integration/variable_test.rb +++ b/test/integration/variable_test.rb @@ -34,6 +34,10 @@ class VariableTest < Minitest::Test assert_equal 'false', Template.parse("{{ false }}").render! end + def test_nil_operations + assert_equal 'cat', Template.parse("{{ nil | append: 'cat' }}").render! + end + def test_preset_assigns template = Template.parse(%|{{ test }}|) template.assigns['test'] = 'worked' From 887b05e6ed3f4c079b398490c7c75c70c5ddd023 Mon Sep 17 00:00:00 2001 From: Justin Li Date: Tue, 21 Oct 2014 14:06:30 -0400 Subject: [PATCH 3/3] Clarify test name --- test/integration/variable_test.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/integration/variable_test.rb b/test/integration/variable_test.rb index 4d08cf40..b55d88a9 100644 --- a/test/integration/variable_test.rb +++ b/test/integration/variable_test.rb @@ -34,7 +34,8 @@ class VariableTest < Minitest::Test assert_equal 'false', Template.parse("{{ false }}").render! end - def test_nil_operations + def test_nil_renders_as_empty_string + assert_equal '', Template.parse("{{ nil }}").render! assert_equal 'cat', Template.parse("{{ nil | append: 'cat' }}").render! end