liquid 2.6.3 → 5.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/History.md +272 -26
- data/README.md +67 -3
- data/lib/liquid/block.rb +62 -94
- data/lib/liquid/block_body.rb +255 -0
- data/lib/liquid/condition.rb +96 -38
- data/lib/liquid/context.rb +172 -154
- data/lib/liquid/document.rb +57 -9
- data/lib/liquid/drop.rb +33 -14
- data/lib/liquid/errors.rb +56 -10
- data/lib/liquid/expression.rb +45 -0
- data/lib/liquid/extensions.rb +21 -7
- data/lib/liquid/file_system.rb +27 -14
- data/lib/liquid/forloop_drop.rb +92 -0
- data/lib/liquid/i18n.rb +41 -0
- data/lib/liquid/interrupts.rb +3 -2
- data/lib/liquid/lexer.rb +62 -0
- data/lib/liquid/locales/en.yml +29 -0
- data/lib/liquid/parse_context.rb +54 -0
- data/lib/liquid/parse_tree_visitor.rb +42 -0
- data/lib/liquid/parser.rb +102 -0
- data/lib/liquid/parser_switching.rb +45 -0
- data/lib/liquid/partial_cache.rb +24 -0
- data/lib/liquid/profiler/hooks.rb +35 -0
- data/lib/liquid/profiler.rb +139 -0
- data/lib/liquid/range_lookup.rb +47 -0
- data/lib/liquid/registers.rb +51 -0
- data/lib/liquid/resource_limits.rb +62 -0
- data/lib/liquid/standardfilters.rb +789 -118
- data/lib/liquid/strainer_factory.rb +41 -0
- data/lib/liquid/strainer_template.rb +62 -0
- data/lib/liquid/tablerowloop_drop.rb +121 -0
- data/lib/liquid/tag/disableable.rb +22 -0
- data/lib/liquid/tag/disabler.rb +21 -0
- data/lib/liquid/tag.rb +49 -10
- data/lib/liquid/tags/assign.rb +61 -19
- data/lib/liquid/tags/break.rb +14 -4
- data/lib/liquid/tags/capture.rb +29 -21
- data/lib/liquid/tags/case.rb +80 -31
- data/lib/liquid/tags/comment.rb +24 -2
- data/lib/liquid/tags/continue.rb +14 -13
- data/lib/liquid/tags/cycle.rb +50 -32
- data/lib/liquid/tags/decrement.rb +24 -26
- data/lib/liquid/tags/echo.rb +41 -0
- data/lib/liquid/tags/for.rb +164 -100
- data/lib/liquid/tags/if.rb +105 -44
- data/lib/liquid/tags/ifchanged.rb +10 -11
- data/lib/liquid/tags/include.rb +85 -65
- data/lib/liquid/tags/increment.rb +24 -22
- data/lib/liquid/tags/inline_comment.rb +43 -0
- data/lib/liquid/tags/raw.rb +50 -11
- data/lib/liquid/tags/render.rb +109 -0
- data/lib/liquid/tags/table_row.rb +88 -0
- data/lib/liquid/tags/unless.rb +37 -21
- data/lib/liquid/template.rb +124 -46
- data/lib/liquid/template_factory.rb +9 -0
- data/lib/liquid/tokenizer.rb +39 -0
- data/lib/liquid/usage.rb +8 -0
- data/lib/liquid/utils.rb +68 -5
- data/lib/liquid/variable.rb +128 -32
- data/lib/liquid/variable_lookup.rb +96 -0
- data/lib/liquid/version.rb +3 -1
- data/lib/liquid.rb +36 -13
- metadata +69 -77
- data/lib/extras/liquid_view.rb +0 -51
- data/lib/liquid/htmltags.rb +0 -73
- data/lib/liquid/module_ex.rb +0 -62
- data/lib/liquid/strainer.rb +0 -53
- data/test/liquid/assign_test.rb +0 -21
- data/test/liquid/block_test.rb +0 -58
- data/test/liquid/capture_test.rb +0 -40
- data/test/liquid/condition_test.rb +0 -127
- data/test/liquid/context_test.rb +0 -478
- 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/regexp_test.rb +0 -44
- data/test/liquid/security_test.rb +0 -64
- data/test/liquid/standard_filter_test.rb +0 -263
- data/test/liquid/strainer_test.rb +0 -52
- data/test/liquid/tags/break_tag_test.rb +0 -16
- data/test/liquid/tags/continue_tag_test.rb +0 -16
- data/test/liquid/tags/for_tag_test.rb +0 -297
- data/test/liquid/tags/html_tag_test.rb +0 -63
- 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/raw_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
- data/test/test_helper.rb +0 -29
- /data/{MIT-LICENSE → LICENSE} +0 -0
@@ -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
|
@@ -1,134 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
class StatementsTest < Test::Unit::TestCase
|
4
|
-
include Liquid
|
5
|
-
|
6
|
-
def test_true_eql_true
|
7
|
-
text = %| {% if true == true %} true {% else %} false {% endif %} |
|
8
|
-
expected = %| true |
|
9
|
-
assert_equal expected, Template.parse(text).render
|
10
|
-
end
|
11
|
-
|
12
|
-
def test_true_not_eql_true
|
13
|
-
text = %| {% if true != true %} true {% else %} false {% endif %} |
|
14
|
-
expected = %| false |
|
15
|
-
assert_equal expected, Template.parse(text).render
|
16
|
-
end
|
17
|
-
|
18
|
-
def test_true_lq_true
|
19
|
-
text = %| {% if 0 > 0 %} true {% else %} false {% endif %} |
|
20
|
-
expected = %| false |
|
21
|
-
assert_equal expected, Template.parse(text).render
|
22
|
-
end
|
23
|
-
|
24
|
-
def test_one_lq_zero
|
25
|
-
text = %| {% if 1 > 0 %} true {% else %} false {% endif %} |
|
26
|
-
expected = %| true |
|
27
|
-
assert_equal expected, Template.parse(text).render
|
28
|
-
end
|
29
|
-
|
30
|
-
def test_zero_lq_one
|
31
|
-
text = %| {% if 0 < 1 %} true {% else %} false {% endif %} |
|
32
|
-
expected = %| true |
|
33
|
-
assert_equal expected, Template.parse(text).render
|
34
|
-
end
|
35
|
-
|
36
|
-
def test_zero_lq_or_equal_one
|
37
|
-
text = %| {% if 0 <= 0 %} true {% else %} false {% endif %} |
|
38
|
-
expected = %| true |
|
39
|
-
assert_equal expected, Template.parse(text).render
|
40
|
-
end
|
41
|
-
|
42
|
-
def test_zero_lq_or_equal_one_involving_nil
|
43
|
-
text = %| {% if null <= 0 %} true {% else %} false {% endif %} |
|
44
|
-
expected = %| false |
|
45
|
-
assert_equal expected, Template.parse(text).render
|
46
|
-
|
47
|
-
|
48
|
-
text = %| {% if 0 <= null %} true {% else %} false {% endif %} |
|
49
|
-
expected = %| false |
|
50
|
-
assert_equal expected, Template.parse(text).render
|
51
|
-
end
|
52
|
-
|
53
|
-
def test_zero_lqq_or_equal_one
|
54
|
-
text = %| {% if 0 >= 0 %} true {% else %} false {% endif %} |
|
55
|
-
expected = %| true |
|
56
|
-
assert_equal expected, Template.parse(text).render
|
57
|
-
end
|
58
|
-
|
59
|
-
def test_strings
|
60
|
-
text = %| {% if 'test' == 'test' %} true {% else %} false {% endif %} |
|
61
|
-
expected = %| true |
|
62
|
-
assert_equal expected, Template.parse(text).render
|
63
|
-
end
|
64
|
-
|
65
|
-
def test_strings_not_equal
|
66
|
-
text = %| {% if 'test' != 'test' %} true {% else %} false {% endif %} |
|
67
|
-
expected = %| false |
|
68
|
-
assert_equal expected, Template.parse(text).render
|
69
|
-
end
|
70
|
-
|
71
|
-
def test_var_strings_equal
|
72
|
-
text = %| {% if var == "hello there!" %} true {% else %} false {% endif %} |
|
73
|
-
expected = %| true |
|
74
|
-
assert_equal expected, Template.parse(text).render('var' => 'hello there!')
|
75
|
-
end
|
76
|
-
|
77
|
-
def test_var_strings_are_not_equal
|
78
|
-
text = %| {% if "hello there!" == var %} true {% else %} false {% endif %} |
|
79
|
-
expected = %| true |
|
80
|
-
assert_equal expected, Template.parse(text).render('var' => 'hello there!')
|
81
|
-
end
|
82
|
-
|
83
|
-
def test_var_and_long_string_are_equal
|
84
|
-
text = %| {% if var == 'hello there!' %} true {% else %} false {% endif %} |
|
85
|
-
expected = %| true |
|
86
|
-
assert_equal expected, Template.parse(text).render('var' => 'hello there!')
|
87
|
-
end
|
88
|
-
|
89
|
-
|
90
|
-
def test_var_and_long_string_are_equal_backwards
|
91
|
-
text = %| {% if 'hello there!' == var %} true {% else %} false {% endif %} |
|
92
|
-
expected = %| true |
|
93
|
-
assert_equal expected, Template.parse(text).render('var' => 'hello there!')
|
94
|
-
end
|
95
|
-
|
96
|
-
#def test_is_nil
|
97
|
-
# text = %| {% if var != nil %} true {% else %} false {% end %} |
|
98
|
-
# @template.assigns = { 'var' => 'hello there!'}
|
99
|
-
# expected = %| true |
|
100
|
-
# assert_equal expected, @template.parse(text)
|
101
|
-
#end
|
102
|
-
|
103
|
-
def test_is_collection_empty
|
104
|
-
text = %| {% if array == empty %} true {% else %} false {% endif %} |
|
105
|
-
expected = %| true |
|
106
|
-
assert_equal expected, Template.parse(text).render('array' => [])
|
107
|
-
end
|
108
|
-
|
109
|
-
def test_is_not_collection_empty
|
110
|
-
text = %| {% if array == empty %} true {% else %} false {% endif %} |
|
111
|
-
expected = %| false |
|
112
|
-
assert_equal expected, Template.parse(text).render('array' => [1,2,3])
|
113
|
-
end
|
114
|
-
|
115
|
-
def test_nil
|
116
|
-
text = %| {% if var == nil %} true {% else %} false {% endif %} |
|
117
|
-
expected = %| true |
|
118
|
-
assert_equal expected, Template.parse(text).render('var' => nil)
|
119
|
-
|
120
|
-
text = %| {% if var == null %} true {% else %} false {% endif %} |
|
121
|
-
expected = %| true |
|
122
|
-
assert_equal expected, Template.parse(text).render('var' => nil)
|
123
|
-
end
|
124
|
-
|
125
|
-
def test_not_nil
|
126
|
-
text = %| {% if var != nil %} true {% else %} false {% endif %} |
|
127
|
-
expected = %| true |
|
128
|
-
assert_equal expected, Template.parse(text).render('var' => 1 )
|
129
|
-
|
130
|
-
text = %| {% if var != null %} true {% else %} false {% endif %} |
|
131
|
-
expected = %| true |
|
132
|
-
assert_equal expected, Template.parse(text).render('var' => 1 )
|
133
|
-
end
|
134
|
-
end # StatementsTest
|
@@ -1,26 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
class UnlessElseTagTest < Test::Unit::TestCase
|
4
|
-
include Liquid
|
5
|
-
|
6
|
-
def test_unless
|
7
|
-
assert_template_result(' ',' {% unless true %} this text should not go into the output {% endunless %} ')
|
8
|
-
assert_template_result(' this text should go into the output ',
|
9
|
-
' {% unless false %} this text should go into the output {% endunless %} ')
|
10
|
-
assert_template_result(' you rock ?','{% unless true %} you suck {% endunless %} {% unless false %} you rock {% endunless %}?')
|
11
|
-
end
|
12
|
-
|
13
|
-
def test_unless_else
|
14
|
-
assert_template_result(' YES ','{% unless true %} NO {% else %} YES {% endunless %}')
|
15
|
-
assert_template_result(' YES ','{% unless false %} YES {% else %} NO {% endunless %}')
|
16
|
-
assert_template_result(' YES ','{% unless "foo" %} NO {% else %} YES {% endunless %}')
|
17
|
-
end
|
18
|
-
|
19
|
-
def test_unless_in_loop
|
20
|
-
assert_template_result '23', '{% for i in choices %}{% unless i %}{{ forloop.index }}{% endunless %}{% endfor %}', 'choices' => [1, nil, false]
|
21
|
-
end
|
22
|
-
|
23
|
-
def test_unless_else_in_loop
|
24
|
-
assert_template_result ' TRUE 2 3 ', '{% for i in choices %}{% unless i %} {{ forloop.index }} {% else %} TRUE {% endunless %}{% endfor %}', 'choices' => [1, nil, false]
|
25
|
-
end
|
26
|
-
end # UnlessElseTest
|
@@ -1,146 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
class TemplateContextDrop < Liquid::Drop
|
4
|
-
def before_method(method)
|
5
|
-
method
|
6
|
-
end
|
7
|
-
|
8
|
-
def foo
|
9
|
-
'fizzbuzz'
|
10
|
-
end
|
11
|
-
|
12
|
-
def baz
|
13
|
-
@context.registers['lulz']
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
class TemplateTest < Test::Unit::TestCase
|
18
|
-
include Liquid
|
19
|
-
|
20
|
-
def test_tokenize_strings
|
21
|
-
assert_equal [' '], Template.new.send(:tokenize, ' ')
|
22
|
-
assert_equal ['hello world'], Template.new.send(:tokenize, 'hello world')
|
23
|
-
end
|
24
|
-
|
25
|
-
def test_tokenize_variables
|
26
|
-
assert_equal ['{{funk}}'], Template.new.send(:tokenize, '{{funk}}')
|
27
|
-
assert_equal [' ', '{{funk}}', ' '], Template.new.send(:tokenize, ' {{funk}} ')
|
28
|
-
assert_equal [' ', '{{funk}}', ' ', '{{so}}', ' ', '{{brother}}', ' '], Template.new.send(:tokenize, ' {{funk}} {{so}} {{brother}} ')
|
29
|
-
assert_equal [' ', '{{ funk }}', ' '], Template.new.send(:tokenize, ' {{ funk }} ')
|
30
|
-
end
|
31
|
-
|
32
|
-
def test_tokenize_blocks
|
33
|
-
assert_equal ['{%comment%}'], Template.new.send(:tokenize, '{%comment%}')
|
34
|
-
assert_equal [' ', '{%comment%}', ' '], Template.new.send(:tokenize, ' {%comment%} ')
|
35
|
-
|
36
|
-
assert_equal [' ', '{%comment%}', ' ', '{%endcomment%}', ' '], Template.new.send(:tokenize, ' {%comment%} {%endcomment%} ')
|
37
|
-
assert_equal [' ', '{% comment %}', ' ', '{% endcomment %}', ' '], Template.new.send(:tokenize, " {% comment %} {% endcomment %} ")
|
38
|
-
end
|
39
|
-
|
40
|
-
def test_instance_assigns_persist_on_same_template_object_between_parses
|
41
|
-
t = Template.new
|
42
|
-
assert_equal 'from instance assigns', t.parse("{% assign foo = 'from instance assigns' %}{{ foo }}").render
|
43
|
-
assert_equal 'from instance assigns', t.parse("{{ foo }}").render
|
44
|
-
end
|
45
|
-
|
46
|
-
def test_instance_assigns_persist_on_same_template_parsing_between_renders
|
47
|
-
t = Template.new.parse("{{ foo }}{% assign foo = 'foo' %}{{ foo }}")
|
48
|
-
assert_equal 'foo', t.render
|
49
|
-
assert_equal 'foofoo', t.render
|
50
|
-
end
|
51
|
-
|
52
|
-
def test_custom_assigns_do_not_persist_on_same_template
|
53
|
-
t = Template.new
|
54
|
-
assert_equal 'from custom assigns', t.parse("{{ foo }}").render('foo' => 'from custom assigns')
|
55
|
-
assert_equal '', t.parse("{{ foo }}").render
|
56
|
-
end
|
57
|
-
|
58
|
-
def test_custom_assigns_squash_instance_assigns
|
59
|
-
t = Template.new
|
60
|
-
assert_equal 'from instance assigns', t.parse("{% assign foo = 'from instance assigns' %}{{ foo }}").render
|
61
|
-
assert_equal 'from custom assigns', t.parse("{{ foo }}").render('foo' => 'from custom assigns')
|
62
|
-
end
|
63
|
-
|
64
|
-
def test_persistent_assigns_squash_instance_assigns
|
65
|
-
t = Template.new
|
66
|
-
assert_equal 'from instance assigns', t.parse("{% assign foo = 'from instance assigns' %}{{ foo }}").render
|
67
|
-
t.assigns['foo'] = 'from persistent assigns'
|
68
|
-
assert_equal 'from persistent assigns', t.parse("{{ foo }}").render
|
69
|
-
end
|
70
|
-
|
71
|
-
def test_lambda_is_called_once_from_persistent_assigns_over_multiple_parses_and_renders
|
72
|
-
t = Template.new
|
73
|
-
t.assigns['number'] = lambda { @global ||= 0; @global += 1 }
|
74
|
-
assert_equal '1', t.parse("{{number}}").render
|
75
|
-
assert_equal '1', t.parse("{{number}}").render
|
76
|
-
assert_equal '1', t.render
|
77
|
-
@global = nil
|
78
|
-
end
|
79
|
-
|
80
|
-
def test_lambda_is_called_once_from_custom_assigns_over_multiple_parses_and_renders
|
81
|
-
t = Template.new
|
82
|
-
assigns = {'number' => lambda { @global ||= 0; @global += 1 }}
|
83
|
-
assert_equal '1', t.parse("{{number}}").render(assigns)
|
84
|
-
assert_equal '1', t.parse("{{number}}").render(assigns)
|
85
|
-
assert_equal '1', t.render(assigns)
|
86
|
-
@global = nil
|
87
|
-
end
|
88
|
-
|
89
|
-
def test_resource_limits_render_length
|
90
|
-
t = Template.parse("0123456789")
|
91
|
-
t.resource_limits = { :render_length_limit => 5 }
|
92
|
-
assert_equal "Liquid error: Memory limits exceeded", t.render()
|
93
|
-
assert t.resource_limits[:reached]
|
94
|
-
t.resource_limits = { :render_length_limit => 10 }
|
95
|
-
assert_equal "0123456789", t.render()
|
96
|
-
assert_not_nil t.resource_limits[:render_length_current]
|
97
|
-
end
|
98
|
-
|
99
|
-
def test_resource_limits_render_score
|
100
|
-
t = Template.parse("{% for a in (1..10) %} {% for a in (1..10) %} foo {% endfor %} {% endfor %}")
|
101
|
-
t.resource_limits = { :render_score_limit => 50 }
|
102
|
-
assert_equal "Liquid error: Memory limits exceeded", t.render()
|
103
|
-
assert t.resource_limits[:reached]
|
104
|
-
t = Template.parse("{% for a in (1..100) %} foo {% endfor %}")
|
105
|
-
t.resource_limits = { :render_score_limit => 50 }
|
106
|
-
assert_equal "Liquid error: Memory limits exceeded", t.render()
|
107
|
-
assert t.resource_limits[:reached]
|
108
|
-
t.resource_limits = { :render_score_limit => 200 }
|
109
|
-
assert_equal (" foo " * 100), t.render()
|
110
|
-
assert_not_nil t.resource_limits[:render_score_current]
|
111
|
-
end
|
112
|
-
|
113
|
-
def test_resource_limits_assign_score
|
114
|
-
t = Template.parse("{% assign foo = 42 %}{% assign bar = 23 %}")
|
115
|
-
t.resource_limits = { :assign_score_limit => 1 }
|
116
|
-
assert_equal "Liquid error: Memory limits exceeded", t.render()
|
117
|
-
assert t.resource_limits[:reached]
|
118
|
-
t.resource_limits = { :assign_score_limit => 2 }
|
119
|
-
assert_equal "", t.render()
|
120
|
-
assert_not_nil t.resource_limits[:assign_score_current]
|
121
|
-
end
|
122
|
-
|
123
|
-
def test_resource_limits_aborts_rendering_after_first_error
|
124
|
-
t = Template.parse("{% for a in (1..100) %} foo1 {% endfor %} bar {% for a in (1..100) %} foo2 {% endfor %}")
|
125
|
-
t.resource_limits = { :render_score_limit => 50 }
|
126
|
-
assert_equal "Liquid error: Memory limits exceeded", t.render()
|
127
|
-
assert t.resource_limits[:reached]
|
128
|
-
end
|
129
|
-
|
130
|
-
def test_resource_limits_hash_in_template_gets_updated_even_if_no_limits_are_set
|
131
|
-
t = Template.parse("{% for a in (1..100) %} {% assign foo = 1 %} {% endfor %}")
|
132
|
-
t.render()
|
133
|
-
assert t.resource_limits[:assign_score_current] > 0
|
134
|
-
assert t.resource_limits[:render_score_current] > 0
|
135
|
-
assert t.resource_limits[:render_length_current] > 0
|
136
|
-
end
|
137
|
-
|
138
|
-
def test_can_use_drop_as_context
|
139
|
-
t = Template.new
|
140
|
-
t.registers['lulz'] = 'haha'
|
141
|
-
drop = TemplateContextDrop.new
|
142
|
-
assert_equal 'fizzbuzz', t.parse('{{foo}}').render(drop)
|
143
|
-
assert_equal 'bar', t.parse('{{bar}}').render(drop)
|
144
|
-
assert_equal 'haha', t.parse("{{baz}}").render(drop)
|
145
|
-
end
|
146
|
-
end # TemplateTest
|