mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-18 10:20:43 -07:00
Add test for the full array structure
This commit is contained in:
@@ -8,217 +8,228 @@ class ParseTreeVisitorTest < Minitest::Test
|
|||||||
def test_variable
|
def test_variable
|
||||||
assert_equal(
|
assert_equal(
|
||||||
["test"],
|
["test"],
|
||||||
traversal(%({{ test }}))
|
visit(%({{ test }}))
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_varible_with_filter
|
def test_varible_with_filter
|
||||||
assert_equal(
|
assert_equal(
|
||||||
["test", "infilter"],
|
["test", "infilter"],
|
||||||
traversal(%({{ test | split: infilter }}))
|
visit(%({{ test | split: infilter }}))
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_dynamic_variable
|
def test_dynamic_variable
|
||||||
assert_equal(
|
assert_equal(
|
||||||
["test", "inlookup"],
|
["test", "inlookup"],
|
||||||
traversal(%({{ test[inlookup] }}))
|
visit(%({{ test[inlookup] }}))
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_if_condition
|
def test_if_condition
|
||||||
assert_equal(
|
assert_equal(
|
||||||
["test"],
|
["test"],
|
||||||
traversal(%({% if test %}{% endif %}))
|
visit(%({% if test %}{% endif %}))
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_complex_if_condition
|
def test_complex_if_condition
|
||||||
assert_equal(
|
assert_equal(
|
||||||
["test"],
|
["test"],
|
||||||
traversal(%({% if 1 == 1 and 2 == test %}{% endif %}))
|
visit(%({% if 1 == 1 and 2 == test %}{% endif %}))
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_if_body
|
def test_if_body
|
||||||
assert_equal(
|
assert_equal(
|
||||||
["test"],
|
["test"],
|
||||||
traversal(%({% if 1 == 1 %}{{ test }}{% endif %}))
|
visit(%({% if 1 == 1 %}{{ test }}{% endif %}))
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_unless_condition
|
def test_unless_condition
|
||||||
assert_equal(
|
assert_equal(
|
||||||
["test"],
|
["test"],
|
||||||
traversal(%({% unless test %}{% endunless %}))
|
visit(%({% unless test %}{% endunless %}))
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_complex_unless_condition
|
def test_complex_unless_condition
|
||||||
assert_equal(
|
assert_equal(
|
||||||
["test"],
|
["test"],
|
||||||
traversal(%({% unless 1 == 1 and 2 == test %}{% endunless %}))
|
visit(%({% unless 1 == 1 and 2 == test %}{% endunless %}))
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_unless_body
|
def test_unless_body
|
||||||
assert_equal(
|
assert_equal(
|
||||||
["test"],
|
["test"],
|
||||||
traversal(%({% unless 1 == 1 %}{{ test }}{% endunless %}))
|
visit(%({% unless 1 == 1 %}{{ test }}{% endunless %}))
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_elsif_condition
|
def test_elsif_condition
|
||||||
assert_equal(
|
assert_equal(
|
||||||
["test"],
|
["test"],
|
||||||
traversal(%({% if 1 == 1 %}{% elsif test %}{% endif %}))
|
visit(%({% if 1 == 1 %}{% elsif test %}{% endif %}))
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_complex_elsif_condition
|
def test_complex_elsif_condition
|
||||||
assert_equal(
|
assert_equal(
|
||||||
["test"],
|
["test"],
|
||||||
traversal(%({% if 1 == 1 %}{% elsif 1 == 1 and 2 == test %}{% endif %}))
|
visit(%({% if 1 == 1 %}{% elsif 1 == 1 and 2 == test %}{% endif %}))
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_elsif_body
|
def test_elsif_body
|
||||||
assert_equal(
|
assert_equal(
|
||||||
["test"],
|
["test"],
|
||||||
traversal(%({% if 1 == 1 %}{% elsif 2 == 2 %}{{ test }}{% endif %}))
|
visit(%({% if 1 == 1 %}{% elsif 2 == 2 %}{{ test }}{% endif %}))
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_else_body
|
def test_else_body
|
||||||
assert_equal(
|
assert_equal(
|
||||||
["test"],
|
["test"],
|
||||||
traversal(%({% if 1 == 1 %}{% else %}{{ test }}{% endif %}))
|
visit(%({% if 1 == 1 %}{% else %}{{ test }}{% endif %}))
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_case_left
|
def test_case_left
|
||||||
assert_equal(
|
assert_equal(
|
||||||
["test"],
|
["test"],
|
||||||
traversal(%({% case test %}{% endcase %}))
|
visit(%({% case test %}{% endcase %}))
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_case_condition
|
def test_case_condition
|
||||||
assert_equal(
|
assert_equal(
|
||||||
["test"],
|
["test"],
|
||||||
traversal(%({% case 1 %}{% when test %}{% endcase %}))
|
visit(%({% case 1 %}{% when test %}{% endcase %}))
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_case_when_body
|
def test_case_when_body
|
||||||
assert_equal(
|
assert_equal(
|
||||||
["test"],
|
["test"],
|
||||||
traversal(%({% case 1 %}{% when 2 %}{{ test }}{% endcase %}))
|
visit(%({% case 1 %}{% when 2 %}{{ test }}{% endcase %}))
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_case_else_body
|
def test_case_else_body
|
||||||
assert_equal(
|
assert_equal(
|
||||||
["test"],
|
["test"],
|
||||||
traversal(%({% case 1 %}{% else %}{{ test }}{% endcase %}))
|
visit(%({% case 1 %}{% else %}{{ test }}{% endcase %}))
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_for_in
|
def test_for_in
|
||||||
assert_equal(
|
assert_equal(
|
||||||
["test"],
|
["test"],
|
||||||
traversal(%({% for x in test %}{% endfor %}))
|
visit(%({% for x in test %}{% endfor %}))
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_for_limit
|
def test_for_limit
|
||||||
assert_equal(
|
assert_equal(
|
||||||
["test"],
|
["test"],
|
||||||
traversal(%({% for x in (1..5) limit: test %}{% endfor %}))
|
visit(%({% for x in (1..5) limit: test %}{% endfor %}))
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_for_offset
|
def test_for_offset
|
||||||
assert_equal(
|
assert_equal(
|
||||||
["test"],
|
["test"],
|
||||||
traversal(%({% for x in (1..5) offset: test %}{% endfor %}))
|
visit(%({% for x in (1..5) offset: test %}{% endfor %}))
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_for_body
|
def test_for_body
|
||||||
assert_equal(
|
assert_equal(
|
||||||
["test"],
|
["test"],
|
||||||
traversal(%({% for x in (1..5) %}{{ test }}{% endfor %}))
|
visit(%({% for x in (1..5) %}{{ test }}{% endfor %}))
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_tablerow_in
|
def test_tablerow_in
|
||||||
assert_equal(
|
assert_equal(
|
||||||
["test"],
|
["test"],
|
||||||
traversal(%({% tablerow x in test %}{% endtablerow %}))
|
visit(%({% tablerow x in test %}{% endtablerow %}))
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_tablerow_limit
|
def test_tablerow_limit
|
||||||
assert_equal(
|
assert_equal(
|
||||||
["test"],
|
["test"],
|
||||||
traversal(%({% tablerow x in (1..5) limit: test %}{% endtablerow %}))
|
visit(%({% tablerow x in (1..5) limit: test %}{% endtablerow %}))
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_tablerow_offset
|
def test_tablerow_offset
|
||||||
assert_equal(
|
assert_equal(
|
||||||
["test"],
|
["test"],
|
||||||
traversal(%({% tablerow x in (1..5) offset: test %}{% endtablerow %}))
|
visit(%({% tablerow x in (1..5) offset: test %}{% endtablerow %}))
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_tablerow_body
|
def test_tablerow_body
|
||||||
assert_equal(
|
assert_equal(
|
||||||
["test"],
|
["test"],
|
||||||
traversal(%({% tablerow x in (1..5) %}{{ test }}{% endtablerow %}))
|
visit(%({% tablerow x in (1..5) %}{{ test }}{% endtablerow %}))
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_cycle
|
def test_cycle
|
||||||
assert_equal(
|
assert_equal(
|
||||||
["test"],
|
["test"],
|
||||||
traversal(%({% cycle test %}))
|
visit(%({% cycle test %}))
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_assign
|
def test_assign
|
||||||
assert_equal(
|
assert_equal(
|
||||||
["test"],
|
["test"],
|
||||||
traversal(%({% assign x = test %}))
|
visit(%({% assign x = test %}))
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_capture
|
def test_capture
|
||||||
assert_equal(
|
assert_equal(
|
||||||
["test"],
|
["test"],
|
||||||
traversal(%({% capture x %}{{ test }}{% endcapture %}))
|
visit(%({% capture x %}{{ test }}{% endcapture %}))
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_include
|
def test_include
|
||||||
assert_equal(
|
assert_equal(
|
||||||
["test"],
|
["test"],
|
||||||
traversal(%({% include test %}))
|
visit(%({% include test %}))
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_include_with
|
def test_include_with
|
||||||
assert_equal(
|
assert_equal(
|
||||||
["test"],
|
["test"],
|
||||||
traversal(%({% include "hai" with test %}))
|
visit(%({% include "hai" with test %}))
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_include_for
|
def test_include_for
|
||||||
assert_equal(
|
assert_equal(
|
||||||
["test"],
|
["test"],
|
||||||
traversal(%({% include "hai" for test %}))
|
visit(%({% include "hai" for test %}))
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_preserve_tree_structure
|
||||||
|
assert_equal(
|
||||||
|
[[nil, [
|
||||||
|
[nil, [[nil, [["other", []]]]]],
|
||||||
|
["test", []],
|
||||||
|
["xs", []]
|
||||||
|
]]],
|
||||||
|
traversal(%({% for x in xs offset: test %}{{ other }}{% endfor %})).visit
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -228,6 +239,9 @@ class ParseTreeVisitorTest < Minitest::Test
|
|||||||
ParseTreeVisitor
|
ParseTreeVisitor
|
||||||
.for(Template.parse(template).root)
|
.for(Template.parse(template).root)
|
||||||
.add_callback_for(VariableLookup, &:name)
|
.add_callback_for(VariableLookup, &:name)
|
||||||
.visit.flatten.compact
|
end
|
||||||
|
|
||||||
|
def visit(template)
|
||||||
|
traversal(template).visit.flatten.compact
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user