diff --git a/lib/liquid/expression.rb b/lib/liquid/expression.rb index fcdd4d8ae..963c49c56 100644 --- a/lib/liquid/expression.rb +++ b/lib/liquid/expression.rb @@ -7,7 +7,7 @@ class Expression 'true' => true, 'false' => false, 'blank' => '', - 'empty' => '' + 'empty' => [].freeze }.freeze INTEGERS_REGEX = /\A(-?\d+)\z/ diff --git a/test/integration/tags/statements_test.rb b/test/integration/tags/statements_test.rb index d1c55c991..4a00837ef 100644 --- a/test/integration/tags/statements_test.rb +++ b/test/integration/tags/statements_test.rb @@ -2,17 +2,27 @@ require 'test_helper' +class EmptyDrop < Liquid::Drop + def empty? + true + end +end + class StatementsTest < Minitest::Test include Liquid def test_true_eql_true text = ' {% if true == true %} true {% else %} false {% endif %} ' assert_template_result(' true ', text) + text = ' {% if var == true %} true {% else %} false {% endif %} ' + assert_template_result(' true ', text, 'var' => true) end def test_true_not_eql_true text = ' {% if true != true %} true {% else %} false {% endif %} ' assert_template_result(' false ', text) + text = ' {% if var != true %} true {% else %} false {% endif %} ' + assert_template_result(' false ', text, 'var' => true) end def test_true_lq_true @@ -93,6 +103,30 @@ def test_is_collection_empty def test_is_not_collection_empty text = ' {% if array == empty %} true {% else %} false {% endif %} ' assert_template_result(' false ', text, 'array' => [1, 2, 3]) + + text = ' {% if array != empty %} true {% else %} false {% endif %} ' + assert_template_result(' true ', text, 'array' => [1, 2, 3]) + end + + def test_is_collection_empty_backward + text = ' {% if empty == array %} true {% else %} false {% endif %} ' + assert_template_result(' true ', text, 'array' => []) + end + + def test_is_string_empty + text = ' {% if string == empty %} true {% else %} false {% endif %} ' + assert_template_result(' true ', text, 'string' => '') + + text = ' {% if empty == string %} true {% else %} false {% endif %} ' + assert_template_result(' true ', text, 'string' => '') + end + + def test_is_non_array_empty + text = ' {% if obj == empty %} true {% else %} false {% endif %} ' + assert_template_result(' true ', text, 'obj' => EmptyDrop.new) + + text = ' {% if empty == obj %} true {% else %} false {% endif %} ' + assert_template_result(' true ', text, 'obj' => EmptyDrop.new) end def test_nil diff --git a/test/integration/variable_test.rb b/test/integration/variable_test.rb index 5c8755019..b2dd44718 100644 --- a/test/integration/variable_test.rb +++ b/test/integration/variable_test.rb @@ -66,6 +66,31 @@ def test_using_blank_as_variable_name def test_using_empty_as_variable_name template = Template.parse("{% assign foo = empty %}{{ foo }}") assert_equal('', template.render!) + template = Template.parse("{% assign foo = empty %}{% if foo == ary %}Y{% else %}N{% endif %}") + assert_equal('Y', template.render!('ary' => [])) + template = Template.parse("{% assign foo = empty %}{% if foo == foo %}Y{% else %}N{% endif %}") + assert_equal('Y', template.render!) + end + + def test_using_empty_as_variable_name_with_filters + template = Template.parse("{% assign foo = empty %}{{ foo | sort | size }}") + assert_equal('0', template.render!) + template = Template.parse("{% assign foo = empty | sort %}{{ foo | size }}") + assert_equal('0', template.render!) + end + + def test_using_empty_as_empty_array + template = Template.parse("{% for x in empty %}x{% endfor %}") + assert_equal('', template.render!) + end + + def test_empty_can_be_filled + template = Template.parse("{% assign foo = empty %}{{ foo | concat: bar }}") + assert_equal('Y', template.render!('bar' => ['Y'])) + template = Template.parse("{% assign foo = empty %}{{ foo | concat: bar | join: '--' }}") + assert_equal('A--B', template.render!('bar' => ['A', 'B'])) + template = Template.parse("{% assign foo = empty %}{% assign tar = foo | concat: bar %}{{ tar | join: '--' }}{{ tar | size }}") + assert_equal('A--B2', template.render!('bar' => ['A', 'B'])) end def test_hash_scoping