mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-19 02:40:41 -07:00
optionally remove empty nodes from AST
This commit is contained in:
@@ -49,12 +49,107 @@ class BlockUnitTest < Minitest::Test
|
||||
|
||||
def test_with_block
|
||||
template = Liquid::Template.parse(" {% comment %} {% endcomment %} ")
|
||||
assert_equal([String, String], block_types(template.root.nodelist))
|
||||
assert_equal(2, template.root.nodelist.size)
|
||||
assert_equal([String, Comment, String], block_types(template.root.nodelist))
|
||||
assert_equal(3, template.root.nodelist.size)
|
||||
end
|
||||
|
||||
def test_remove_empty_for_blocks_with_optimization_option
|
||||
source = <<~LIQUID.chomp
|
||||
{% for i in (1..1000000) %}
|
||||
{% endfor %}
|
||||
LIQUID
|
||||
|
||||
assert_root_nodelist_size(source, 0, omit_blank_nodes: true)
|
||||
|
||||
source = <<~LIQUID.chomp
|
||||
{% for i in (1..1000000) %}
|
||||
{% else %}
|
||||
{% endfor %}
|
||||
LIQUID
|
||||
|
||||
assert_root_nodelist_size(source, 0, omit_blank_nodes: true)
|
||||
|
||||
source = <<~LIQUID.chomp
|
||||
{% for i in list %}
|
||||
i
|
||||
{% endfor %}
|
||||
LIQUID
|
||||
|
||||
assert_root_nodelist_size(source, 1, omit_blank_nodes: true)
|
||||
|
||||
source = <<~LIQUID.chomp
|
||||
{% for i in list %}
|
||||
{% else %}
|
||||
1
|
||||
{% endfor %}
|
||||
LIQUID
|
||||
|
||||
assert_root_nodelist_size(source, 1, omit_blank_nodes: true)
|
||||
end
|
||||
|
||||
def test_remove_comment_nodes_with_optimization_option
|
||||
source = <<~LIQUID.chomp
|
||||
{% comment %}
|
||||
{% if true %}
|
||||
{% endif %}
|
||||
{% endcomment %}
|
||||
LIQUID
|
||||
|
||||
assert_root_nodelist_size(source, 0, omit_blank_nodes: true)
|
||||
|
||||
source = <<~LIQUID.chomp
|
||||
{% liquid
|
||||
comment
|
||||
if true
|
||||
endif
|
||||
endcomment
|
||||
%}
|
||||
LIQUID
|
||||
|
||||
assert_root_nodelist_size(source, 0, omit_blank_nodes: true)
|
||||
end
|
||||
|
||||
def test_remove_if_nodes_with_optimization_option
|
||||
source = <<~LIQUID.chomp
|
||||
{% if true %}
|
||||
{% endif %}
|
||||
LIQUID
|
||||
|
||||
assert_root_nodelist_size(source, 0, omit_blank_nodes: true)
|
||||
|
||||
source = <<~LIQUID.chomp
|
||||
{% unless true %}
|
||||
{% endunless %}
|
||||
LIQUID
|
||||
|
||||
assert_root_nodelist_size(source, 0, omit_blank_nodes: true)
|
||||
|
||||
source = <<~LIQUID.chomp
|
||||
{% if false %}
|
||||
{% else %}
|
||||
{% endif %}
|
||||
LIQUID
|
||||
|
||||
assert_root_nodelist_size(source, 0, omit_blank_nodes: true)
|
||||
|
||||
source = <<~LIQUID.chomp
|
||||
{% if false %}
|
||||
{% else %}
|
||||
Hello!
|
||||
{% endif %}
|
||||
LIQUID
|
||||
|
||||
assert_root_nodelist_size(source, 1, omit_blank_nodes: true)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def assert_root_nodelist_size(source, expected_size, parse_options = {})
|
||||
template = Liquid::Template.parse(source, parse_options)
|
||||
|
||||
assert_equal(expected_size, template.root.nodelist.size)
|
||||
end
|
||||
|
||||
def block_types(nodelist)
|
||||
nodelist.collect(&:class)
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user