Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/liquid/expression.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class Expression
'true' => true,
'false' => false,
'blank' => '',
'empty' => ''
'empty' => [].freeze
}.freeze

INTEGERS_REGEX = /\A(-?\d+)\z/
Expand Down
34 changes: 34 additions & 0 deletions test/integration/tags/statements_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
25 changes: 25 additions & 0 deletions test/integration/variable_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Comment on lines +87 to 94
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ashmaroli would using the concat filter here be enough to check if arrays from the empty literal can be filled?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that is sufficient.


def test_hash_scoping
Expand Down