liquid 2.6.1 → 4.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/History.md +194 -29
- data/{MIT-LICENSE → LICENSE} +0 -0
- data/README.md +60 -2
- data/lib/liquid.rb +25 -14
- data/lib/liquid/block.rb +47 -96
- data/lib/liquid/block_body.rb +143 -0
- data/lib/liquid/condition.rb +70 -39
- data/lib/liquid/context.rb +116 -157
- data/lib/liquid/document.rb +19 -9
- data/lib/liquid/drop.rb +31 -14
- data/lib/liquid/errors.rb +54 -10
- data/lib/liquid/expression.rb +49 -0
- data/lib/liquid/extensions.rb +19 -7
- data/lib/liquid/file_system.rb +25 -14
- data/lib/liquid/forloop_drop.rb +42 -0
- data/lib/liquid/i18n.rb +39 -0
- data/lib/liquid/interrupts.rb +2 -3
- data/lib/liquid/lexer.rb +55 -0
- data/lib/liquid/locales/en.yml +26 -0
- data/lib/liquid/parse_context.rb +38 -0
- data/lib/liquid/parse_tree_visitor.rb +42 -0
- data/lib/liquid/parser.rb +90 -0
- data/lib/liquid/parser_switching.rb +31 -0
- data/lib/liquid/profiler.rb +158 -0
- data/lib/liquid/profiler/hooks.rb +23 -0
- data/lib/liquid/range_lookup.rb +37 -0
- data/lib/liquid/resource_limits.rb +23 -0
- data/lib/liquid/standardfilters.rb +311 -77
- data/lib/liquid/strainer.rb +39 -26
- data/lib/liquid/tablerowloop_drop.rb +62 -0
- data/lib/liquid/tag.rb +28 -11
- data/lib/liquid/tags/assign.rb +34 -10
- data/lib/liquid/tags/break.rb +1 -4
- data/lib/liquid/tags/capture.rb +11 -9
- data/lib/liquid/tags/case.rb +37 -22
- data/lib/liquid/tags/comment.rb +10 -3
- data/lib/liquid/tags/continue.rb +1 -4
- data/lib/liquid/tags/cycle.rb +20 -14
- data/lib/liquid/tags/decrement.rb +4 -8
- data/lib/liquid/tags/for.rb +121 -60
- data/lib/liquid/tags/if.rb +73 -30
- data/lib/liquid/tags/ifchanged.rb +3 -5
- data/lib/liquid/tags/include.rb +77 -46
- data/lib/liquid/tags/increment.rb +4 -8
- data/lib/liquid/tags/raw.rb +35 -10
- data/lib/liquid/tags/table_row.rb +62 -0
- data/lib/liquid/tags/unless.rb +6 -9
- data/lib/liquid/template.rb +130 -32
- data/lib/liquid/tokenizer.rb +31 -0
- data/lib/liquid/truffle.rb +5 -0
- data/lib/liquid/utils.rb +57 -4
- data/lib/liquid/variable.rb +121 -30
- data/lib/liquid/variable_lookup.rb +88 -0
- data/lib/liquid/version.rb +2 -1
- data/test/fixtures/en_locale.yml +9 -0
- data/test/integration/assign_test.rb +48 -0
- data/test/integration/blank_test.rb +106 -0
- data/test/integration/block_test.rb +12 -0
- data/test/{liquid → integration}/capture_test.rb +13 -3
- data/test/integration/context_test.rb +32 -0
- data/test/integration/document_test.rb +19 -0
- data/test/integration/drop_test.rb +273 -0
- data/test/integration/error_handling_test.rb +260 -0
- data/test/integration/filter_test.rb +178 -0
- data/test/integration/hash_ordering_test.rb +23 -0
- data/test/integration/output_test.rb +123 -0
- data/test/integration/parse_tree_visitor_test.rb +247 -0
- data/test/integration/parsing_quirks_test.rb +122 -0
- data/test/integration/render_profiling_test.rb +154 -0
- data/test/integration/security_test.rb +80 -0
- data/test/integration/standard_filter_test.rb +776 -0
- data/test/{liquid → integration}/tags/break_tag_test.rb +2 -3
- data/test/{liquid → integration}/tags/continue_tag_test.rb +1 -2
- data/test/integration/tags/for_tag_test.rb +410 -0
- data/test/integration/tags/if_else_tag_test.rb +188 -0
- data/test/integration/tags/include_tag_test.rb +253 -0
- data/test/integration/tags/increment_tag_test.rb +23 -0
- data/test/{liquid → integration}/tags/raw_tag_test.rb +9 -2
- data/test/integration/tags/standard_tag_test.rb +296 -0
- data/test/integration/tags/statements_test.rb +111 -0
- data/test/{liquid/tags/html_tag_test.rb → integration/tags/table_row_test.rb} +25 -24
- data/test/integration/tags/unless_else_tag_test.rb +26 -0
- data/test/integration/template_test.rb +332 -0
- data/test/integration/trim_mode_test.rb +529 -0
- data/test/integration/variable_test.rb +96 -0
- data/test/test_helper.rb +106 -19
- data/test/truffle/truffle_test.rb +9 -0
- data/test/{liquid/block_test.rb → unit/block_unit_test.rb} +9 -9
- data/test/unit/condition_unit_test.rb +166 -0
- data/test/{liquid/context_test.rb → unit/context_unit_test.rb} +85 -74
- data/test/unit/file_system_unit_test.rb +35 -0
- data/test/unit/i18n_unit_test.rb +37 -0
- data/test/unit/lexer_unit_test.rb +51 -0
- data/test/unit/parser_unit_test.rb +82 -0
- data/test/{liquid/regexp_test.rb → unit/regexp_unit_test.rb} +4 -4
- data/test/unit/strainer_unit_test.rb +164 -0
- data/test/unit/tag_unit_test.rb +21 -0
- data/test/unit/tags/case_tag_unit_test.rb +10 -0
- data/test/unit/tags/for_tag_unit_test.rb +13 -0
- data/test/unit/tags/if_tag_unit_test.rb +8 -0
- data/test/unit/template_unit_test.rb +78 -0
- data/test/unit/tokenizer_unit_test.rb +55 -0
- data/test/unit/variable_unit_test.rb +162 -0
- metadata +157 -77
- data/lib/extras/liquid_view.rb +0 -51
- data/lib/liquid/htmltags.rb +0 -74
- data/lib/liquid/module_ex.rb +0 -62
- data/test/liquid/assign_test.rb +0 -21
- data/test/liquid/condition_test.rb +0 -127
- data/test/liquid/drop_test.rb +0 -180
- data/test/liquid/error_handling_test.rb +0 -81
- data/test/liquid/file_system_test.rb +0 -29
- data/test/liquid/filter_test.rb +0 -125
- data/test/liquid/hash_ordering_test.rb +0 -25
- data/test/liquid/module_ex_test.rb +0 -87
- data/test/liquid/output_test.rb +0 -116
- data/test/liquid/parsing_quirks_test.rb +0 -52
- data/test/liquid/security_test.rb +0 -64
- data/test/liquid/standard_filter_test.rb +0 -251
- data/test/liquid/strainer_test.rb +0 -52
- data/test/liquid/tags/for_tag_test.rb +0 -297
- data/test/liquid/tags/if_else_tag_test.rb +0 -166
- data/test/liquid/tags/include_tag_test.rb +0 -166
- data/test/liquid/tags/increment_tag_test.rb +0 -24
- data/test/liquid/tags/standard_tag_test.rb +0 -295
- data/test/liquid/tags/statements_test.rb +0 -134
- data/test/liquid/tags/unless_else_tag_test.rb +0 -26
- data/test/liquid/template_test.rb +0 -146
- data/test/liquid/variable_test.rb +0 -186
@@ -1,166 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
class IfElseTagTest < Test::Unit::TestCase
|
4
|
-
include Liquid
|
5
|
-
|
6
|
-
def test_if
|
7
|
-
assert_template_result(' ',' {% if false %} this text should not go into the output {% endif %} ')
|
8
|
-
assert_template_result(' this text should go into the output ',
|
9
|
-
' {% if true %} this text should go into the output {% endif %} ')
|
10
|
-
assert_template_result(' you rock ?','{% if false %} you suck {% endif %} {% if true %} you rock {% endif %}?')
|
11
|
-
end
|
12
|
-
|
13
|
-
def test_if_else
|
14
|
-
assert_template_result(' YES ','{% if false %} NO {% else %} YES {% endif %}')
|
15
|
-
assert_template_result(' YES ','{% if true %} YES {% else %} NO {% endif %}')
|
16
|
-
assert_template_result(' YES ','{% if "foo" %} YES {% else %} NO {% endif %}')
|
17
|
-
end
|
18
|
-
|
19
|
-
def test_if_boolean
|
20
|
-
assert_template_result(' YES ','{% if var %} YES {% endif %}', 'var' => true)
|
21
|
-
end
|
22
|
-
|
23
|
-
def test_if_or
|
24
|
-
assert_template_result(' YES ','{% if a or b %} YES {% endif %}', 'a' => true, 'b' => true)
|
25
|
-
assert_template_result(' YES ','{% if a or b %} YES {% endif %}', 'a' => true, 'b' => false)
|
26
|
-
assert_template_result(' YES ','{% if a or b %} YES {% endif %}', 'a' => false, 'b' => true)
|
27
|
-
assert_template_result('', '{% if a or b %} YES {% endif %}', 'a' => false, 'b' => false)
|
28
|
-
|
29
|
-
assert_template_result(' YES ','{% if a or b or c %} YES {% endif %}', 'a' => false, 'b' => false, 'c' => true)
|
30
|
-
assert_template_result('', '{% if a or b or c %} YES {% endif %}', 'a' => false, 'b' => false, 'c' => false)
|
31
|
-
end
|
32
|
-
|
33
|
-
def test_if_or_with_operators
|
34
|
-
assert_template_result(' YES ','{% if a == true or b == true %} YES {% endif %}', 'a' => true, 'b' => true)
|
35
|
-
assert_template_result(' YES ','{% if a == true or b == false %} YES {% endif %}', 'a' => true, 'b' => true)
|
36
|
-
assert_template_result('','{% if a == false or b == false %} YES {% endif %}', 'a' => true, 'b' => true)
|
37
|
-
end
|
38
|
-
|
39
|
-
def test_comparison_of_strings_containing_and_or_or
|
40
|
-
assert_nothing_raised do
|
41
|
-
awful_markup = "a == 'and' and b == 'or' and c == 'foo and bar' and d == 'bar or baz' and e == 'foo' and foo and bar"
|
42
|
-
assigns = {'a' => 'and', 'b' => 'or', 'c' => 'foo and bar', 'd' => 'bar or baz', 'e' => 'foo', 'foo' => true, 'bar' => true}
|
43
|
-
assert_template_result(' YES ',"{% if #{awful_markup} %} YES {% endif %}", assigns)
|
44
|
-
end
|
45
|
-
end
|
46
|
-
|
47
|
-
def test_comparison_of_expressions_starting_with_and_or_or
|
48
|
-
assigns = {'order' => {'items_count' => 0}, 'android' => {'name' => 'Roy'}}
|
49
|
-
assert_nothing_raised do
|
50
|
-
assert_template_result( "YES",
|
51
|
-
"{% if android.name == 'Roy' %}YES{% endif %}",
|
52
|
-
assigns)
|
53
|
-
end
|
54
|
-
assert_nothing_raised do
|
55
|
-
assert_template_result( "YES",
|
56
|
-
"{% if order.items_count == 0 %}YES{% endif %}",
|
57
|
-
assigns)
|
58
|
-
end
|
59
|
-
end
|
60
|
-
|
61
|
-
def test_if_and
|
62
|
-
assert_template_result(' YES ','{% if true and true %} YES {% endif %}')
|
63
|
-
assert_template_result('','{% if false and true %} YES {% endif %}')
|
64
|
-
assert_template_result('','{% if false and true %} YES {% endif %}')
|
65
|
-
end
|
66
|
-
|
67
|
-
|
68
|
-
def test_hash_miss_generates_false
|
69
|
-
assert_template_result('','{% if foo.bar %} NO {% endif %}', 'foo' => {})
|
70
|
-
end
|
71
|
-
|
72
|
-
def test_if_from_variable
|
73
|
-
assert_template_result('','{% if var %} NO {% endif %}', 'var' => false)
|
74
|
-
assert_template_result('','{% if var %} NO {% endif %}', 'var' => nil)
|
75
|
-
assert_template_result('','{% if foo.bar %} NO {% endif %}', 'foo' => {'bar' => false})
|
76
|
-
assert_template_result('','{% if foo.bar %} NO {% endif %}', 'foo' => {})
|
77
|
-
assert_template_result('','{% if foo.bar %} NO {% endif %}', 'foo' => nil)
|
78
|
-
assert_template_result('','{% if foo.bar %} NO {% endif %}', 'foo' => true)
|
79
|
-
|
80
|
-
assert_template_result(' YES ','{% if var %} YES {% endif %}', 'var' => "text")
|
81
|
-
assert_template_result(' YES ','{% if var %} YES {% endif %}', 'var' => true)
|
82
|
-
assert_template_result(' YES ','{% if var %} YES {% endif %}', 'var' => 1)
|
83
|
-
assert_template_result(' YES ','{% if var %} YES {% endif %}', 'var' => {})
|
84
|
-
assert_template_result(' YES ','{% if var %} YES {% endif %}', 'var' => [])
|
85
|
-
assert_template_result(' YES ','{% if "foo" %} YES {% endif %}')
|
86
|
-
assert_template_result(' YES ','{% if foo.bar %} YES {% endif %}', 'foo' => {'bar' => true})
|
87
|
-
assert_template_result(' YES ','{% if foo.bar %} YES {% endif %}', 'foo' => {'bar' => "text"})
|
88
|
-
assert_template_result(' YES ','{% if foo.bar %} YES {% endif %}', 'foo' => {'bar' => 1 })
|
89
|
-
assert_template_result(' YES ','{% if foo.bar %} YES {% endif %}', 'foo' => {'bar' => {} })
|
90
|
-
assert_template_result(' YES ','{% if foo.bar %} YES {% endif %}', 'foo' => {'bar' => [] })
|
91
|
-
|
92
|
-
assert_template_result(' YES ','{% if var %} NO {% else %} YES {% endif %}', 'var' => false)
|
93
|
-
assert_template_result(' YES ','{% if var %} NO {% else %} YES {% endif %}', 'var' => nil)
|
94
|
-
assert_template_result(' YES ','{% if var %} YES {% else %} NO {% endif %}', 'var' => true)
|
95
|
-
assert_template_result(' YES ','{% if "foo" %} YES {% else %} NO {% endif %}', 'var' => "text")
|
96
|
-
|
97
|
-
assert_template_result(' YES ','{% if foo.bar %} NO {% else %} YES {% endif %}', 'foo' => {'bar' => false})
|
98
|
-
assert_template_result(' YES ','{% if foo.bar %} YES {% else %} NO {% endif %}', 'foo' => {'bar' => true})
|
99
|
-
assert_template_result(' YES ','{% if foo.bar %} YES {% else %} NO {% endif %}', 'foo' => {'bar' => "text"})
|
100
|
-
assert_template_result(' YES ','{% if foo.bar %} NO {% else %} YES {% endif %}', 'foo' => {'notbar' => true})
|
101
|
-
assert_template_result(' YES ','{% if foo.bar %} NO {% else %} YES {% endif %}', 'foo' => {})
|
102
|
-
assert_template_result(' YES ','{% if foo.bar %} NO {% else %} YES {% endif %}', 'notfoo' => {'bar' => true})
|
103
|
-
end
|
104
|
-
|
105
|
-
def test_nested_if
|
106
|
-
assert_template_result('', '{% if false %}{% if false %} NO {% endif %}{% endif %}')
|
107
|
-
assert_template_result('', '{% if false %}{% if true %} NO {% endif %}{% endif %}')
|
108
|
-
assert_template_result('', '{% if true %}{% if false %} NO {% endif %}{% endif %}')
|
109
|
-
assert_template_result(' YES ', '{% if true %}{% if true %} YES {% endif %}{% endif %}')
|
110
|
-
|
111
|
-
assert_template_result(' YES ', '{% if true %}{% if true %} YES {% else %} NO {% endif %}{% else %} NO {% endif %}')
|
112
|
-
assert_template_result(' YES ', '{% if true %}{% if false %} NO {% else %} YES {% endif %}{% else %} NO {% endif %}')
|
113
|
-
assert_template_result(' YES ', '{% if false %}{% if true %} NO {% else %} NONO {% endif %}{% else %} YES {% endif %}')
|
114
|
-
|
115
|
-
end
|
116
|
-
|
117
|
-
def test_comparisons_on_null
|
118
|
-
assert_template_result('','{% if null < 10 %} NO {% endif %}')
|
119
|
-
assert_template_result('','{% if null <= 10 %} NO {% endif %}')
|
120
|
-
assert_template_result('','{% if null >= 10 %} NO {% endif %}')
|
121
|
-
assert_template_result('','{% if null > 10 %} NO {% endif %}')
|
122
|
-
|
123
|
-
assert_template_result('','{% if 10 < null %} NO {% endif %}')
|
124
|
-
assert_template_result('','{% if 10 <= null %} NO {% endif %}')
|
125
|
-
assert_template_result('','{% if 10 >= null %} NO {% endif %}')
|
126
|
-
assert_template_result('','{% if 10 > null %} NO {% endif %}')
|
127
|
-
end
|
128
|
-
|
129
|
-
def test_else_if
|
130
|
-
assert_template_result('0','{% if 0 == 0 %}0{% elsif 1 == 1%}1{% else %}2{% endif %}')
|
131
|
-
assert_template_result('1','{% if 0 != 0 %}0{% elsif 1 == 1%}1{% else %}2{% endif %}')
|
132
|
-
assert_template_result('2','{% if 0 != 0 %}0{% elsif 1 != 1%}1{% else %}2{% endif %}')
|
133
|
-
|
134
|
-
assert_template_result('elsif','{% if false %}if{% elsif true %}elsif{% endif %}')
|
135
|
-
end
|
136
|
-
|
137
|
-
def test_syntax_error_no_variable
|
138
|
-
assert_raise(SyntaxError){ assert_template_result('', '{% if jerry == 1 %}')}
|
139
|
-
end
|
140
|
-
|
141
|
-
def test_syntax_error_no_expression
|
142
|
-
assert_raise(SyntaxError) { assert_template_result('', '{% if %}') }
|
143
|
-
end
|
144
|
-
|
145
|
-
def test_if_with_custom_condition
|
146
|
-
Condition.operators['contains'] = :[]
|
147
|
-
|
148
|
-
assert_template_result('yes', %({% if 'bob' contains 'o' %}yes{% endif %}))
|
149
|
-
assert_template_result('no', %({% if 'bob' contains 'f' %}yes{% else %}no{% endif %}))
|
150
|
-
ensure
|
151
|
-
Condition.operators.delete 'contains'
|
152
|
-
end
|
153
|
-
|
154
|
-
def test_operators_are_ignored_unless_isolated
|
155
|
-
Condition.operators['contains'] = :[]
|
156
|
-
|
157
|
-
assert_template_result('yes',
|
158
|
-
%({% if 'gnomeslab-and-or-liquid' contains 'gnomeslab-and-or-liquid' %}yes{% endif %}))
|
159
|
-
end
|
160
|
-
|
161
|
-
def test_operators_are_whitelisted
|
162
|
-
assert_raise(SyntaxError) do
|
163
|
-
assert_template_result('', %({% if 1 or throw or or 1 %}yes{% endif %}))
|
164
|
-
end
|
165
|
-
end
|
166
|
-
end # IfElseTest
|
@@ -1,166 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
class TestFileSystem
|
4
|
-
def read_template_file(template_path, context)
|
5
|
-
case template_path
|
6
|
-
when "product"
|
7
|
-
"Product: {{ product.title }} "
|
8
|
-
|
9
|
-
when "locale_variables"
|
10
|
-
"Locale: {{echo1}} {{echo2}}"
|
11
|
-
|
12
|
-
when "variant"
|
13
|
-
"Variant: {{ variant.title }}"
|
14
|
-
|
15
|
-
when "nested_template"
|
16
|
-
"{% include 'header' %} {% include 'body' %} {% include 'footer' %}"
|
17
|
-
|
18
|
-
when "body"
|
19
|
-
"body {% include 'body_detail' %}"
|
20
|
-
|
21
|
-
when "nested_product_template"
|
22
|
-
"Product: {{ nested_product_template.title }} {%include 'details'%} "
|
23
|
-
|
24
|
-
when "recursively_nested_template"
|
25
|
-
"-{% include 'recursively_nested_template' %}"
|
26
|
-
|
27
|
-
when "pick_a_source"
|
28
|
-
"from TestFileSystem"
|
29
|
-
|
30
|
-
else
|
31
|
-
template_path
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end
|
35
|
-
|
36
|
-
class OtherFileSystem
|
37
|
-
def read_template_file(template_path, context)
|
38
|
-
'from OtherFileSystem'
|
39
|
-
end
|
40
|
-
end
|
41
|
-
|
42
|
-
class CountingFileSystem
|
43
|
-
attr_reader :count
|
44
|
-
def read_template_file(template_path, context)
|
45
|
-
@count ||= 0
|
46
|
-
@count += 1
|
47
|
-
'from CountingFileSystem'
|
48
|
-
end
|
49
|
-
end
|
50
|
-
|
51
|
-
class IncludeTagTest < Test::Unit::TestCase
|
52
|
-
include Liquid
|
53
|
-
|
54
|
-
def setup
|
55
|
-
Liquid::Template.file_system = TestFileSystem.new
|
56
|
-
end
|
57
|
-
|
58
|
-
def test_include_tag_looks_for_file_system_in_registers_first
|
59
|
-
assert_equal 'from OtherFileSystem',
|
60
|
-
Template.parse("{% include 'pick_a_source' %}").render({}, :registers => {:file_system => OtherFileSystem.new})
|
61
|
-
end
|
62
|
-
|
63
|
-
|
64
|
-
def test_include_tag_with
|
65
|
-
assert_equal "Product: Draft 151cm ",
|
66
|
-
Template.parse("{% include 'product' with products[0] %}").render( "products" => [ {'title' => 'Draft 151cm'}, {'title' => 'Element 155cm'} ] )
|
67
|
-
end
|
68
|
-
|
69
|
-
def test_include_tag_with_default_name
|
70
|
-
assert_equal "Product: Draft 151cm ",
|
71
|
-
Template.parse("{% include 'product' %}").render( "product" => {'title' => 'Draft 151cm'} )
|
72
|
-
end
|
73
|
-
|
74
|
-
def test_include_tag_for
|
75
|
-
|
76
|
-
assert_equal "Product: Draft 151cm Product: Element 155cm ",
|
77
|
-
Template.parse("{% include 'product' for products %}").render( "products" => [ {'title' => 'Draft 151cm'}, {'title' => 'Element 155cm'} ] )
|
78
|
-
end
|
79
|
-
|
80
|
-
def test_include_tag_with_local_variables
|
81
|
-
assert_equal "Locale: test123 ",
|
82
|
-
Template.parse("{% include 'locale_variables' echo1: 'test123' %}").render
|
83
|
-
end
|
84
|
-
|
85
|
-
def test_include_tag_with_multiple_local_variables
|
86
|
-
assert_equal "Locale: test123 test321",
|
87
|
-
Template.parse("{% include 'locale_variables' echo1: 'test123', echo2: 'test321' %}").render
|
88
|
-
end
|
89
|
-
|
90
|
-
def test_include_tag_with_multiple_local_variables_from_context
|
91
|
-
assert_equal "Locale: test123 test321",
|
92
|
-
Template.parse("{% include 'locale_variables' echo1: echo1, echo2: more_echos.echo2 %}").render('echo1' => 'test123', 'more_echos' => { "echo2" => 'test321'})
|
93
|
-
end
|
94
|
-
|
95
|
-
def test_nested_include_tag
|
96
|
-
assert_equal "body body_detail",
|
97
|
-
Template.parse("{% include 'body' %}").render
|
98
|
-
|
99
|
-
assert_equal "header body body_detail footer",
|
100
|
-
Template.parse("{% include 'nested_template' %}").render
|
101
|
-
end
|
102
|
-
|
103
|
-
def test_nested_include_with_variable
|
104
|
-
|
105
|
-
assert_equal "Product: Draft 151cm details ",
|
106
|
-
Template.parse("{% include 'nested_product_template' with product %}").render("product" => {"title" => 'Draft 151cm'})
|
107
|
-
|
108
|
-
assert_equal "Product: Draft 151cm details Product: Element 155cm details ",
|
109
|
-
Template.parse("{% include 'nested_product_template' for products %}").render("products" => [{"title" => 'Draft 151cm'}, {"title" => 'Element 155cm'}])
|
110
|
-
|
111
|
-
end
|
112
|
-
|
113
|
-
def test_recursively_included_template_does_not_produce_endless_loop
|
114
|
-
|
115
|
-
infinite_file_system = Class.new do
|
116
|
-
def read_template_file(template_path, context)
|
117
|
-
"-{% include 'loop' %}"
|
118
|
-
end
|
119
|
-
end
|
120
|
-
|
121
|
-
Liquid::Template.file_system = infinite_file_system.new
|
122
|
-
|
123
|
-
assert_raise(Liquid::StackLevelError) do
|
124
|
-
Template.parse("{% include 'loop' %}").render!
|
125
|
-
end
|
126
|
-
|
127
|
-
end
|
128
|
-
|
129
|
-
def test_backwards_compatability_support_for_overridden_read_template_file
|
130
|
-
infinite_file_system = Class.new do
|
131
|
-
def read_template_file(template_path) # testing only one argument here.
|
132
|
-
"- hi mom"
|
133
|
-
end
|
134
|
-
end
|
135
|
-
|
136
|
-
Liquid::Template.file_system = infinite_file_system.new
|
137
|
-
|
138
|
-
Template.parse("{% include 'hi_mom' %}").render!
|
139
|
-
end
|
140
|
-
|
141
|
-
def test_dynamically_choosen_template
|
142
|
-
|
143
|
-
assert_equal "Test123", Template.parse("{% include template %}").render("template" => 'Test123')
|
144
|
-
assert_equal "Test321", Template.parse("{% include template %}").render("template" => 'Test321')
|
145
|
-
|
146
|
-
assert_equal "Product: Draft 151cm ", Template.parse("{% include template for product %}").render("template" => 'product', 'product' => { 'title' => 'Draft 151cm'})
|
147
|
-
end
|
148
|
-
|
149
|
-
def test_include_tag_caches_second_read_of_same_partial
|
150
|
-
file_system = CountingFileSystem.new
|
151
|
-
assert_equal 'from CountingFileSystemfrom CountingFileSystem',
|
152
|
-
Template.parse("{% include 'pick_a_source' %}{% include 'pick_a_source' %}").render({}, :registers => {:file_system => file_system})
|
153
|
-
assert_equal 1, file_system.count
|
154
|
-
end
|
155
|
-
|
156
|
-
def test_include_tag_doesnt_cache_partials_across_renders
|
157
|
-
file_system = CountingFileSystem.new
|
158
|
-
assert_equal 'from CountingFileSystem',
|
159
|
-
Template.parse("{% include 'pick_a_source' %}").render({}, :registers => {:file_system => file_system})
|
160
|
-
assert_equal 1, file_system.count
|
161
|
-
|
162
|
-
assert_equal 'from CountingFileSystem',
|
163
|
-
Template.parse("{% include 'pick_a_source' %}").render({}, :registers => {:file_system => file_system})
|
164
|
-
assert_equal 2, file_system.count
|
165
|
-
end
|
166
|
-
end # IncludeTagTest
|
@@ -1,24 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
class IncrementTagTest < Test::Unit::TestCase
|
4
|
-
include Liquid
|
5
|
-
|
6
|
-
def test_inc
|
7
|
-
assert_template_result('0','{%increment port %}', {})
|
8
|
-
assert_template_result('0 1','{%increment port %} {%increment port%}', {})
|
9
|
-
assert_template_result('0 0 1 2 1',
|
10
|
-
'{%increment port %} {%increment starboard%} ' +
|
11
|
-
'{%increment port %} {%increment port%} ' +
|
12
|
-
'{%increment starboard %}', {})
|
13
|
-
end
|
14
|
-
|
15
|
-
def test_dec
|
16
|
-
assert_template_result('9','{%decrement port %}', { 'port' => 10})
|
17
|
-
assert_template_result('-1 -2','{%decrement port %} {%decrement port%}', {})
|
18
|
-
assert_template_result('1 5 2 2 5',
|
19
|
-
'{%increment port %} {%increment starboard%} ' +
|
20
|
-
'{%increment port %} {%decrement port%} ' +
|
21
|
-
'{%decrement starboard %}', { 'port' => 1, 'starboard' => 5 })
|
22
|
-
end
|
23
|
-
|
24
|
-
end
|
@@ -1,295 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
class StandardTagTest < Test::Unit::TestCase
|
4
|
-
include Liquid
|
5
|
-
|
6
|
-
def test_tag
|
7
|
-
tag = Tag.new('tag', [], [])
|
8
|
-
assert_equal 'liquid::tag', tag.name
|
9
|
-
assert_equal '', tag.render(Context.new)
|
10
|
-
end
|
11
|
-
|
12
|
-
def test_no_transform
|
13
|
-
assert_template_result('this text should come out of the template without change...',
|
14
|
-
'this text should come out of the template without change...')
|
15
|
-
|
16
|
-
assert_template_result('blah','blah')
|
17
|
-
assert_template_result('<blah>','<blah>')
|
18
|
-
assert_template_result('|,.:','|,.:')
|
19
|
-
assert_template_result('','')
|
20
|
-
|
21
|
-
text = %|this shouldnt see any transformation either but has multiple lines
|
22
|
-
as you can clearly see here ...|
|
23
|
-
assert_template_result(text,text)
|
24
|
-
end
|
25
|
-
|
26
|
-
def test_has_a_block_which_does_nothing
|
27
|
-
assert_template_result(%|the comment block should be removed .. right?|,
|
28
|
-
%|the comment block should be removed {%comment%} be gone.. {%endcomment%} .. right?|)
|
29
|
-
|
30
|
-
assert_template_result('','{%comment%}{%endcomment%}')
|
31
|
-
assert_template_result('','{%comment%}{% endcomment %}')
|
32
|
-
assert_template_result('','{% comment %}{%endcomment%}')
|
33
|
-
assert_template_result('','{% comment %}{% endcomment %}')
|
34
|
-
assert_template_result('','{%comment%}comment{%endcomment%}')
|
35
|
-
assert_template_result('','{% comment %}comment{% endcomment %}')
|
36
|
-
|
37
|
-
assert_template_result('foobar','foo{%comment%}comment{%endcomment%}bar')
|
38
|
-
assert_template_result('foobar','foo{% comment %}comment{% endcomment %}bar')
|
39
|
-
assert_template_result('foobar','foo{%comment%} comment {%endcomment%}bar')
|
40
|
-
assert_template_result('foobar','foo{% comment %} comment {% endcomment %}bar')
|
41
|
-
|
42
|
-
assert_template_result('foo bar','foo {%comment%} {%endcomment%} bar')
|
43
|
-
assert_template_result('foo bar','foo {%comment%}comment{%endcomment%} bar')
|
44
|
-
assert_template_result('foo bar','foo {%comment%} comment {%endcomment%} bar')
|
45
|
-
|
46
|
-
assert_template_result('foobar','foo{%comment%}
|
47
|
-
{%endcomment%}bar')
|
48
|
-
end
|
49
|
-
|
50
|
-
def test_assign
|
51
|
-
assigns = {'var' => 'content' }
|
52
|
-
assert_template_result('var2: var2:content', 'var2:{{var2}} {%assign var2 = var%} var2:{{var2}}', assigns)
|
53
|
-
|
54
|
-
end
|
55
|
-
|
56
|
-
def test_hyphenated_assign
|
57
|
-
assigns = {'a-b' => '1' }
|
58
|
-
assert_template_result('a-b:1 a-b:2', 'a-b:{{a-b}} {%assign a-b = 2 %}a-b:{{a-b}}', assigns)
|
59
|
-
|
60
|
-
end
|
61
|
-
|
62
|
-
def test_assign_with_colon_and_spaces
|
63
|
-
assigns = {'var' => {'a:b c' => {'paged' => '1' }}}
|
64
|
-
assert_template_result('var2: 1', '{%assign var2 = var["a:b c"].paged %}var2: {{var2}}', assigns)
|
65
|
-
end
|
66
|
-
|
67
|
-
def test_capture
|
68
|
-
assigns = {'var' => 'content' }
|
69
|
-
assert_template_result('content foo content foo ',
|
70
|
-
'{{ var2 }}{% capture var2 %}{{ var }} foo {% endcapture %}{{ var2 }}{{ var2 }}',
|
71
|
-
assigns)
|
72
|
-
end
|
73
|
-
|
74
|
-
def test_capture_detects_bad_syntax
|
75
|
-
assert_raise(SyntaxError) do
|
76
|
-
assert_template_result('content foo content foo ',
|
77
|
-
'{{ var2 }}{% capture %}{{ var }} foo {% endcapture %}{{ var2 }}{{ var2 }}',
|
78
|
-
{'var' => 'content' })
|
79
|
-
end
|
80
|
-
end
|
81
|
-
|
82
|
-
def test_case
|
83
|
-
assigns = {'condition' => 2 }
|
84
|
-
assert_template_result(' its 2 ',
|
85
|
-
'{% case condition %}{% when 1 %} its 1 {% when 2 %} its 2 {% endcase %}',
|
86
|
-
assigns)
|
87
|
-
|
88
|
-
assigns = {'condition' => 1 }
|
89
|
-
assert_template_result(' its 1 ',
|
90
|
-
'{% case condition %}{% when 1 %} its 1 {% when 2 %} its 2 {% endcase %}',
|
91
|
-
assigns)
|
92
|
-
|
93
|
-
assigns = {'condition' => 3 }
|
94
|
-
assert_template_result('',
|
95
|
-
'{% case condition %}{% when 1 %} its 1 {% when 2 %} its 2 {% endcase %}',
|
96
|
-
assigns)
|
97
|
-
|
98
|
-
assigns = {'condition' => "string here" }
|
99
|
-
assert_template_result(' hit ',
|
100
|
-
'{% case condition %}{% when "string here" %} hit {% endcase %}',
|
101
|
-
assigns)
|
102
|
-
|
103
|
-
assigns = {'condition' => "bad string here" }
|
104
|
-
assert_template_result('',
|
105
|
-
'{% case condition %}{% when "string here" %} hit {% endcase %}',\
|
106
|
-
assigns)
|
107
|
-
end
|
108
|
-
|
109
|
-
def test_case_with_else
|
110
|
-
assigns = {'condition' => 5 }
|
111
|
-
assert_template_result(' hit ',
|
112
|
-
'{% case condition %}{% when 5 %} hit {% else %} else {% endcase %}',
|
113
|
-
assigns)
|
114
|
-
|
115
|
-
assigns = {'condition' => 6 }
|
116
|
-
assert_template_result(' else ',
|
117
|
-
'{% case condition %}{% when 5 %} hit {% else %} else {% endcase %}',
|
118
|
-
assigns)
|
119
|
-
|
120
|
-
assigns = {'condition' => 6 }
|
121
|
-
assert_template_result(' else ',
|
122
|
-
'{% case condition %} {% when 5 %} hit {% else %} else {% endcase %}',
|
123
|
-
assigns)
|
124
|
-
end
|
125
|
-
|
126
|
-
def test_case_on_size
|
127
|
-
assert_template_result('', '{% case a.size %}{% when 1 %}1{% when 2 %}2{% endcase %}', 'a' => [])
|
128
|
-
assert_template_result('1', '{% case a.size %}{% when 1 %}1{% when 2 %}2{% endcase %}', 'a' => [1])
|
129
|
-
assert_template_result('2', '{% case a.size %}{% when 1 %}1{% when 2 %}2{% endcase %}', 'a' => [1, 1])
|
130
|
-
assert_template_result('', '{% case a.size %}{% when 1 %}1{% when 2 %}2{% endcase %}', 'a' => [1, 1, 1])
|
131
|
-
assert_template_result('', '{% case a.size %}{% when 1 %}1{% when 2 %}2{% endcase %}', 'a' => [1, 1, 1, 1])
|
132
|
-
assert_template_result('', '{% case a.size %}{% when 1 %}1{% when 2 %}2{% endcase %}', 'a' => [1, 1, 1, 1, 1])
|
133
|
-
end
|
134
|
-
|
135
|
-
def test_case_on_size_with_else
|
136
|
-
assert_template_result('else',
|
137
|
-
'{% case a.size %}{% when 1 %}1{% when 2 %}2{% else %}else{% endcase %}',
|
138
|
-
'a' => [])
|
139
|
-
|
140
|
-
assert_template_result('1',
|
141
|
-
'{% case a.size %}{% when 1 %}1{% when 2 %}2{% else %}else{% endcase %}',
|
142
|
-
'a' => [1])
|
143
|
-
|
144
|
-
assert_template_result('2',
|
145
|
-
'{% case a.size %}{% when 1 %}1{% when 2 %}2{% else %}else{% endcase %}',
|
146
|
-
'a' => [1, 1])
|
147
|
-
|
148
|
-
assert_template_result('else',
|
149
|
-
'{% case a.size %}{% when 1 %}1{% when 2 %}2{% else %}else{% endcase %}',
|
150
|
-
'a' => [1, 1, 1])
|
151
|
-
|
152
|
-
assert_template_result('else',
|
153
|
-
'{% case a.size %}{% when 1 %}1{% when 2 %}2{% else %}else{% endcase %}',
|
154
|
-
'a' => [1, 1, 1, 1])
|
155
|
-
|
156
|
-
assert_template_result('else',
|
157
|
-
'{% case a.size %}{% when 1 %}1{% when 2 %}2{% else %}else{% endcase %}',
|
158
|
-
'a' => [1, 1, 1, 1, 1])
|
159
|
-
end
|
160
|
-
|
161
|
-
def test_case_on_length_with_else
|
162
|
-
assert_template_result('else',
|
163
|
-
'{% case a.empty? %}{% when true %}true{% when false %}false{% else %}else{% endcase %}',
|
164
|
-
{})
|
165
|
-
|
166
|
-
assert_template_result('false',
|
167
|
-
'{% case false %}{% when true %}true{% when false %}false{% else %}else{% endcase %}',
|
168
|
-
{})
|
169
|
-
|
170
|
-
assert_template_result('true',
|
171
|
-
'{% case true %}{% when true %}true{% when false %}false{% else %}else{% endcase %}',
|
172
|
-
{})
|
173
|
-
|
174
|
-
assert_template_result('else',
|
175
|
-
'{% case NULL %}{% when true %}true{% when false %}false{% else %}else{% endcase %}',
|
176
|
-
{})
|
177
|
-
end
|
178
|
-
|
179
|
-
def test_assign_from_case
|
180
|
-
# Example from the shopify forums
|
181
|
-
code = %q({% case collection.handle %}{% when 'menswear-jackets' %}{% assign ptitle = 'menswear' %}{% when 'menswear-t-shirts' %}{% assign ptitle = 'menswear' %}{% else %}{% assign ptitle = 'womenswear' %}{% endcase %}{{ ptitle }})
|
182
|
-
template = Liquid::Template.parse(code)
|
183
|
-
assert_equal "menswear", template.render("collection" => {'handle' => 'menswear-jackets'})
|
184
|
-
assert_equal "menswear", template.render("collection" => {'handle' => 'menswear-t-shirts'})
|
185
|
-
assert_equal "womenswear", template.render("collection" => {'handle' => 'x'})
|
186
|
-
assert_equal "womenswear", template.render("collection" => {'handle' => 'y'})
|
187
|
-
assert_equal "womenswear", template.render("collection" => {'handle' => 'z'})
|
188
|
-
end
|
189
|
-
|
190
|
-
def test_case_when_or
|
191
|
-
code = '{% case condition %}{% when 1 or 2 or 3 %} its 1 or 2 or 3 {% when 4 %} its 4 {% endcase %}'
|
192
|
-
assert_template_result(' its 1 or 2 or 3 ', code, {'condition' => 1 })
|
193
|
-
assert_template_result(' its 1 or 2 or 3 ', code, {'condition' => 2 })
|
194
|
-
assert_template_result(' its 1 or 2 or 3 ', code, {'condition' => 3 })
|
195
|
-
assert_template_result(' its 4 ', code, {'condition' => 4 })
|
196
|
-
assert_template_result('', code, {'condition' => 5 })
|
197
|
-
|
198
|
-
code = '{% case condition %}{% when 1 or "string" or null %} its 1 or 2 or 3 {% when 4 %} its 4 {% endcase %}'
|
199
|
-
assert_template_result(' its 1 or 2 or 3 ', code, {'condition' => 1 })
|
200
|
-
assert_template_result(' its 1 or 2 or 3 ', code, {'condition' => 'string' })
|
201
|
-
assert_template_result(' its 1 or 2 or 3 ', code, {'condition' => nil })
|
202
|
-
assert_template_result('', code, {'condition' => 'something else' })
|
203
|
-
end
|
204
|
-
|
205
|
-
def test_case_when_comma
|
206
|
-
code = '{% case condition %}{% when 1, 2, 3 %} its 1 or 2 or 3 {% when 4 %} its 4 {% endcase %}'
|
207
|
-
assert_template_result(' its 1 or 2 or 3 ', code, {'condition' => 1 })
|
208
|
-
assert_template_result(' its 1 or 2 or 3 ', code, {'condition' => 2 })
|
209
|
-
assert_template_result(' its 1 or 2 or 3 ', code, {'condition' => 3 })
|
210
|
-
assert_template_result(' its 4 ', code, {'condition' => 4 })
|
211
|
-
assert_template_result('', code, {'condition' => 5 })
|
212
|
-
|
213
|
-
code = '{% case condition %}{% when 1, "string", null %} its 1 or 2 or 3 {% when 4 %} its 4 {% endcase %}'
|
214
|
-
assert_template_result(' its 1 or 2 or 3 ', code, {'condition' => 1 })
|
215
|
-
assert_template_result(' its 1 or 2 or 3 ', code, {'condition' => 'string' })
|
216
|
-
assert_template_result(' its 1 or 2 or 3 ', code, {'condition' => nil })
|
217
|
-
assert_template_result('', code, {'condition' => 'something else' })
|
218
|
-
end
|
219
|
-
|
220
|
-
def test_assign
|
221
|
-
assert_equal 'variable', Liquid::Template.parse( '{% assign a = "variable"%}{{a}}' ).render
|
222
|
-
end
|
223
|
-
|
224
|
-
def test_assign_an_empty_string
|
225
|
-
assert_equal '', Liquid::Template.parse( '{% assign a = ""%}{{a}}' ).render
|
226
|
-
end
|
227
|
-
|
228
|
-
def test_assign_is_global
|
229
|
-
assert_equal 'variable',
|
230
|
-
Liquid::Template.parse( '{%for i in (1..2) %}{% assign a = "variable"%}{% endfor %}{{a}}' ).render
|
231
|
-
end
|
232
|
-
|
233
|
-
def test_case_detects_bad_syntax
|
234
|
-
assert_raise(SyntaxError) do
|
235
|
-
assert_template_result('', '{% case false %}{% when %}true{% endcase %}', {})
|
236
|
-
end
|
237
|
-
|
238
|
-
assert_raise(SyntaxError) do
|
239
|
-
assert_template_result('', '{% case false %}{% huh %}true{% endcase %}', {})
|
240
|
-
end
|
241
|
-
|
242
|
-
end
|
243
|
-
|
244
|
-
def test_cycle
|
245
|
-
assert_template_result('one','{%cycle "one", "two"%}')
|
246
|
-
assert_template_result('one two','{%cycle "one", "two"%} {%cycle "one", "two"%}')
|
247
|
-
assert_template_result(' two','{%cycle "", "two"%} {%cycle "", "two"%}')
|
248
|
-
|
249
|
-
assert_template_result('one two one','{%cycle "one", "two"%} {%cycle "one", "two"%} {%cycle "one", "two"%}')
|
250
|
-
|
251
|
-
assert_template_result('text-align: left text-align: right',
|
252
|
-
'{%cycle "text-align: left", "text-align: right" %} {%cycle "text-align: left", "text-align: right"%}')
|
253
|
-
end
|
254
|
-
|
255
|
-
def test_multiple_cycles
|
256
|
-
assert_template_result('1 2 1 1 2 3 1',
|
257
|
-
'{%cycle 1,2%} {%cycle 1,2%} {%cycle 1,2%} {%cycle 1,2,3%} {%cycle 1,2,3%} {%cycle 1,2,3%} {%cycle 1,2,3%}')
|
258
|
-
end
|
259
|
-
|
260
|
-
def test_multiple_named_cycles
|
261
|
-
assert_template_result('one one two two one one',
|
262
|
-
'{%cycle 1: "one", "two" %} {%cycle 2: "one", "two" %} {%cycle 1: "one", "two" %} {%cycle 2: "one", "two" %} {%cycle 1: "one", "two" %} {%cycle 2: "one", "two" %}')
|
263
|
-
end
|
264
|
-
|
265
|
-
def test_multiple_named_cycles_with_names_from_context
|
266
|
-
assigns = {"var1" => 1, "var2" => 2 }
|
267
|
-
assert_template_result('one one two two one one',
|
268
|
-
'{%cycle var1: "one", "two" %} {%cycle var2: "one", "two" %} {%cycle var1: "one", "two" %} {%cycle var2: "one", "two" %} {%cycle var1: "one", "two" %} {%cycle var2: "one", "two" %}', assigns)
|
269
|
-
end
|
270
|
-
|
271
|
-
def test_size_of_array
|
272
|
-
assigns = {"array" => [1,2,3,4]}
|
273
|
-
assert_template_result('array has 4 elements', "array has {{ array.size }} elements", assigns)
|
274
|
-
end
|
275
|
-
|
276
|
-
def test_size_of_hash
|
277
|
-
assigns = {"hash" => {:a => 1, :b => 2, :c=> 3, :d => 4}}
|
278
|
-
assert_template_result('hash has 4 elements', "hash has {{ hash.size }} elements", assigns)
|
279
|
-
end
|
280
|
-
|
281
|
-
def test_illegal_symbols
|
282
|
-
assert_template_result('', '{% if true == empty %}?{% endif %}', {})
|
283
|
-
assert_template_result('', '{% if true == null %}?{% endif %}', {})
|
284
|
-
assert_template_result('', '{% if empty == true %}?{% endif %}', {})
|
285
|
-
assert_template_result('', '{% if null == true %}?{% endif %}', {})
|
286
|
-
end
|
287
|
-
|
288
|
-
def test_ifchanged
|
289
|
-
assigns = {'array' => [ 1, 1, 2, 2, 3, 3] }
|
290
|
-
assert_template_result('123','{%for item in array%}{%ifchanged%}{{item}}{% endifchanged %}{%endfor%}',assigns)
|
291
|
-
|
292
|
-
assigns = {'array' => [ 1, 1, 1, 1] }
|
293
|
-
assert_template_result('1','{%for item in array%}{%ifchanged%}{{item}}{% endifchanged %}{%endfor%}',assigns)
|
294
|
-
end
|
295
|
-
end # StandardTagTest
|