liquid 2.6.3 → 5.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- 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
data/test/liquid/context_test.rb
DELETED
@@ -1,478 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
class HundredCentes
|
4
|
-
def to_liquid
|
5
|
-
100
|
6
|
-
end
|
7
|
-
end
|
8
|
-
|
9
|
-
class CentsDrop < Liquid::Drop
|
10
|
-
def amount
|
11
|
-
HundredCentes.new
|
12
|
-
end
|
13
|
-
|
14
|
-
def non_zero?
|
15
|
-
true
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
class ContextSensitiveDrop < Liquid::Drop
|
20
|
-
def test
|
21
|
-
@context['test']
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
class Category < Liquid::Drop
|
26
|
-
attr_accessor :name
|
27
|
-
|
28
|
-
def initialize(name)
|
29
|
-
@name = name
|
30
|
-
end
|
31
|
-
|
32
|
-
def to_liquid
|
33
|
-
CategoryDrop.new(self)
|
34
|
-
end
|
35
|
-
end
|
36
|
-
|
37
|
-
class CategoryDrop
|
38
|
-
attr_accessor :category, :context
|
39
|
-
def initialize(category)
|
40
|
-
@category = category
|
41
|
-
end
|
42
|
-
end
|
43
|
-
|
44
|
-
class CounterDrop < Liquid::Drop
|
45
|
-
def count
|
46
|
-
@count ||= 0
|
47
|
-
@count += 1
|
48
|
-
end
|
49
|
-
end
|
50
|
-
|
51
|
-
class ArrayLike
|
52
|
-
def fetch(index)
|
53
|
-
end
|
54
|
-
|
55
|
-
def [](index)
|
56
|
-
@counts ||= []
|
57
|
-
@counts[index] ||= 0
|
58
|
-
@counts[index] += 1
|
59
|
-
end
|
60
|
-
|
61
|
-
def to_liquid
|
62
|
-
self
|
63
|
-
end
|
64
|
-
end
|
65
|
-
|
66
|
-
class ContextTest < Test::Unit::TestCase
|
67
|
-
include Liquid
|
68
|
-
|
69
|
-
def setup
|
70
|
-
@context = Liquid::Context.new
|
71
|
-
end
|
72
|
-
|
73
|
-
def test_variables
|
74
|
-
@context['string'] = 'string'
|
75
|
-
assert_equal 'string', @context['string']
|
76
|
-
|
77
|
-
@context['num'] = 5
|
78
|
-
assert_equal 5, @context['num']
|
79
|
-
|
80
|
-
@context['time'] = Time.parse('2006-06-06 12:00:00')
|
81
|
-
assert_equal Time.parse('2006-06-06 12:00:00'), @context['time']
|
82
|
-
|
83
|
-
@context['date'] = Date.today
|
84
|
-
assert_equal Date.today, @context['date']
|
85
|
-
|
86
|
-
now = DateTime.now
|
87
|
-
@context['datetime'] = now
|
88
|
-
assert_equal now, @context['datetime']
|
89
|
-
|
90
|
-
@context['bool'] = true
|
91
|
-
assert_equal true, @context['bool']
|
92
|
-
|
93
|
-
@context['bool'] = false
|
94
|
-
assert_equal false, @context['bool']
|
95
|
-
|
96
|
-
@context['nil'] = nil
|
97
|
-
assert_equal nil, @context['nil']
|
98
|
-
assert_equal nil, @context['nil']
|
99
|
-
end
|
100
|
-
|
101
|
-
def test_variables_not_existing
|
102
|
-
assert_equal nil, @context['does_not_exist']
|
103
|
-
end
|
104
|
-
|
105
|
-
def test_scoping
|
106
|
-
assert_nothing_raised do
|
107
|
-
@context.push
|
108
|
-
@context.pop
|
109
|
-
end
|
110
|
-
|
111
|
-
assert_raise(Liquid::ContextError) do
|
112
|
-
@context.pop
|
113
|
-
end
|
114
|
-
|
115
|
-
assert_raise(Liquid::ContextError) do
|
116
|
-
@context.push
|
117
|
-
@context.pop
|
118
|
-
@context.pop
|
119
|
-
end
|
120
|
-
end
|
121
|
-
|
122
|
-
def test_length_query
|
123
|
-
|
124
|
-
@context['numbers'] = [1,2,3,4]
|
125
|
-
|
126
|
-
assert_equal 4, @context['numbers.size']
|
127
|
-
|
128
|
-
@context['numbers'] = {1 => 1,2 => 2,3 => 3,4 => 4}
|
129
|
-
|
130
|
-
assert_equal 4, @context['numbers.size']
|
131
|
-
|
132
|
-
@context['numbers'] = {1 => 1,2 => 2,3 => 3,4 => 4, 'size' => 1000}
|
133
|
-
|
134
|
-
assert_equal 1000, @context['numbers.size']
|
135
|
-
|
136
|
-
end
|
137
|
-
|
138
|
-
def test_hyphenated_variable
|
139
|
-
|
140
|
-
@context['oh-my'] = 'godz'
|
141
|
-
assert_equal 'godz', @context['oh-my']
|
142
|
-
|
143
|
-
end
|
144
|
-
|
145
|
-
def test_add_filter
|
146
|
-
|
147
|
-
filter = Module.new do
|
148
|
-
def hi(output)
|
149
|
-
output + ' hi!'
|
150
|
-
end
|
151
|
-
end
|
152
|
-
|
153
|
-
context = Context.new
|
154
|
-
context.add_filters(filter)
|
155
|
-
assert_equal 'hi? hi!', context.invoke(:hi, 'hi?')
|
156
|
-
|
157
|
-
context = Context.new
|
158
|
-
assert_equal 'hi?', context.invoke(:hi, 'hi?')
|
159
|
-
|
160
|
-
context.add_filters(filter)
|
161
|
-
assert_equal 'hi? hi!', context.invoke(:hi, 'hi?')
|
162
|
-
|
163
|
-
end
|
164
|
-
|
165
|
-
def test_override_global_filter
|
166
|
-
global = Module.new do
|
167
|
-
def notice(output)
|
168
|
-
"Global #{output}"
|
169
|
-
end
|
170
|
-
end
|
171
|
-
|
172
|
-
local = Module.new do
|
173
|
-
def notice(output)
|
174
|
-
"Local #{output}"
|
175
|
-
end
|
176
|
-
end
|
177
|
-
|
178
|
-
Template.register_filter(global)
|
179
|
-
assert_equal 'Global test', Template.parse("{{'test' | notice }}").render
|
180
|
-
assert_equal 'Local test', Template.parse("{{'test' | notice }}").render({}, :filters => [local])
|
181
|
-
end
|
182
|
-
|
183
|
-
def test_only_intended_filters_make_it_there
|
184
|
-
|
185
|
-
filter = Module.new do
|
186
|
-
def hi(output)
|
187
|
-
output + ' hi!'
|
188
|
-
end
|
189
|
-
end
|
190
|
-
|
191
|
-
context = Context.new
|
192
|
-
assert_equal "Wookie", context.invoke("hi", "Wookie")
|
193
|
-
|
194
|
-
context.add_filters(filter)
|
195
|
-
assert_equal "Wookie hi!", context.invoke("hi", "Wookie")
|
196
|
-
end
|
197
|
-
|
198
|
-
def test_add_item_in_outer_scope
|
199
|
-
@context['test'] = 'test'
|
200
|
-
@context.push
|
201
|
-
assert_equal 'test', @context['test']
|
202
|
-
@context.pop
|
203
|
-
assert_equal 'test', @context['test']
|
204
|
-
end
|
205
|
-
|
206
|
-
def test_add_item_in_inner_scope
|
207
|
-
@context.push
|
208
|
-
@context['test'] = 'test'
|
209
|
-
assert_equal 'test', @context['test']
|
210
|
-
@context.pop
|
211
|
-
assert_equal nil, @context['test']
|
212
|
-
end
|
213
|
-
|
214
|
-
def test_hierachical_data
|
215
|
-
@context['hash'] = {"name" => 'tobi'}
|
216
|
-
assert_equal 'tobi', @context['hash.name']
|
217
|
-
assert_equal 'tobi', @context['hash["name"]']
|
218
|
-
end
|
219
|
-
|
220
|
-
def test_keywords
|
221
|
-
assert_equal true, @context['true']
|
222
|
-
assert_equal false, @context['false']
|
223
|
-
end
|
224
|
-
|
225
|
-
def test_digits
|
226
|
-
assert_equal 100, @context['100']
|
227
|
-
assert_equal 100.00, @context['100.00']
|
228
|
-
end
|
229
|
-
|
230
|
-
def test_strings
|
231
|
-
assert_equal "hello!", @context['"hello!"']
|
232
|
-
assert_equal "hello!", @context["'hello!'"]
|
233
|
-
end
|
234
|
-
|
235
|
-
def test_merge
|
236
|
-
@context.merge({ "test" => "test" })
|
237
|
-
assert_equal 'test', @context['test']
|
238
|
-
@context.merge({ "test" => "newvalue", "foo" => "bar" })
|
239
|
-
assert_equal 'newvalue', @context['test']
|
240
|
-
assert_equal 'bar', @context['foo']
|
241
|
-
end
|
242
|
-
|
243
|
-
def test_array_notation
|
244
|
-
@context['test'] = [1,2,3,4,5]
|
245
|
-
|
246
|
-
assert_equal 1, @context['test[0]']
|
247
|
-
assert_equal 2, @context['test[1]']
|
248
|
-
assert_equal 3, @context['test[2]']
|
249
|
-
assert_equal 4, @context['test[3]']
|
250
|
-
assert_equal 5, @context['test[4]']
|
251
|
-
end
|
252
|
-
|
253
|
-
def test_recoursive_array_notation
|
254
|
-
@context['test'] = {'test' => [1,2,3,4,5]}
|
255
|
-
|
256
|
-
assert_equal 1, @context['test.test[0]']
|
257
|
-
|
258
|
-
@context['test'] = [{'test' => 'worked'}]
|
259
|
-
|
260
|
-
assert_equal 'worked', @context['test[0].test']
|
261
|
-
end
|
262
|
-
|
263
|
-
def test_hash_to_array_transition
|
264
|
-
@context['colors'] = {
|
265
|
-
'Blue' => ['003366','336699', '6699CC', '99CCFF'],
|
266
|
-
'Green' => ['003300','336633', '669966', '99CC99'],
|
267
|
-
'Yellow' => ['CC9900','FFCC00', 'FFFF99', 'FFFFCC'],
|
268
|
-
'Red' => ['660000','993333', 'CC6666', 'FF9999']
|
269
|
-
}
|
270
|
-
|
271
|
-
assert_equal '003366', @context['colors.Blue[0]']
|
272
|
-
assert_equal 'FF9999', @context['colors.Red[3]']
|
273
|
-
end
|
274
|
-
|
275
|
-
def test_try_first
|
276
|
-
@context['test'] = [1,2,3,4,5]
|
277
|
-
|
278
|
-
assert_equal 1, @context['test.first']
|
279
|
-
assert_equal 5, @context['test.last']
|
280
|
-
|
281
|
-
@context['test'] = {'test' => [1,2,3,4,5]}
|
282
|
-
|
283
|
-
assert_equal 1, @context['test.test.first']
|
284
|
-
assert_equal 5, @context['test.test.last']
|
285
|
-
|
286
|
-
@context['test'] = [1]
|
287
|
-
assert_equal 1, @context['test.first']
|
288
|
-
assert_equal 1, @context['test.last']
|
289
|
-
end
|
290
|
-
|
291
|
-
def test_access_hashes_with_hash_notation
|
292
|
-
@context['products'] = {'count' => 5, 'tags' => ['deepsnow', 'freestyle'] }
|
293
|
-
@context['product'] = {'variants' => [ {'title' => 'draft151cm'}, {'title' => 'element151cm'} ]}
|
294
|
-
|
295
|
-
assert_equal 5, @context['products["count"]']
|
296
|
-
assert_equal 'deepsnow', @context['products["tags"][0]']
|
297
|
-
assert_equal 'deepsnow', @context['products["tags"].first']
|
298
|
-
assert_equal 'draft151cm', @context['product["variants"][0]["title"]']
|
299
|
-
assert_equal 'element151cm', @context['product["variants"][1]["title"]']
|
300
|
-
assert_equal 'draft151cm', @context['product["variants"][0]["title"]']
|
301
|
-
assert_equal 'element151cm', @context['product["variants"].last["title"]']
|
302
|
-
end
|
303
|
-
|
304
|
-
def test_access_variable_with_hash_notation
|
305
|
-
@context['foo'] = 'baz'
|
306
|
-
@context['bar'] = 'foo'
|
307
|
-
|
308
|
-
assert_equal 'baz', @context['["foo"]']
|
309
|
-
assert_equal 'baz', @context['[bar]']
|
310
|
-
end
|
311
|
-
|
312
|
-
def test_access_hashes_with_hash_access_variables
|
313
|
-
|
314
|
-
@context['var'] = 'tags'
|
315
|
-
@context['nested'] = {'var' => 'tags'}
|
316
|
-
@context['products'] = {'count' => 5, 'tags' => ['deepsnow', 'freestyle'] }
|
317
|
-
|
318
|
-
assert_equal 'deepsnow', @context['products[var].first']
|
319
|
-
assert_equal 'freestyle', @context['products[nested.var].last']
|
320
|
-
end
|
321
|
-
|
322
|
-
def test_hash_notation_only_for_hash_access
|
323
|
-
@context['array'] = [1,2,3,4,5]
|
324
|
-
@context['hash'] = {'first' => 'Hello'}
|
325
|
-
|
326
|
-
assert_equal 1, @context['array.first']
|
327
|
-
assert_equal nil, @context['array["first"]']
|
328
|
-
assert_equal 'Hello', @context['hash["first"]']
|
329
|
-
end
|
330
|
-
|
331
|
-
def test_first_can_appear_in_middle_of_callchain
|
332
|
-
|
333
|
-
@context['product'] = {'variants' => [ {'title' => 'draft151cm'}, {'title' => 'element151cm'} ]}
|
334
|
-
|
335
|
-
assert_equal 'draft151cm', @context['product.variants[0].title']
|
336
|
-
assert_equal 'element151cm', @context['product.variants[1].title']
|
337
|
-
assert_equal 'draft151cm', @context['product.variants.first.title']
|
338
|
-
assert_equal 'element151cm', @context['product.variants.last.title']
|
339
|
-
|
340
|
-
end
|
341
|
-
|
342
|
-
def test_cents
|
343
|
-
@context.merge( "cents" => HundredCentes.new )
|
344
|
-
assert_equal 100, @context['cents']
|
345
|
-
end
|
346
|
-
|
347
|
-
def test_nested_cents
|
348
|
-
@context.merge( "cents" => { 'amount' => HundredCentes.new} )
|
349
|
-
assert_equal 100, @context['cents.amount']
|
350
|
-
|
351
|
-
@context.merge( "cents" => { 'cents' => { 'amount' => HundredCentes.new} } )
|
352
|
-
assert_equal 100, @context['cents.cents.amount']
|
353
|
-
end
|
354
|
-
|
355
|
-
def test_cents_through_drop
|
356
|
-
@context.merge( "cents" => CentsDrop.new )
|
357
|
-
assert_equal 100, @context['cents.amount']
|
358
|
-
end
|
359
|
-
|
360
|
-
def test_nested_cents_through_drop
|
361
|
-
@context.merge( "vars" => {"cents" => CentsDrop.new} )
|
362
|
-
assert_equal 100, @context['vars.cents.amount']
|
363
|
-
end
|
364
|
-
|
365
|
-
def test_drop_methods_with_question_marks
|
366
|
-
@context.merge( "cents" => CentsDrop.new )
|
367
|
-
assert @context['cents.non_zero?']
|
368
|
-
end
|
369
|
-
|
370
|
-
def test_context_from_within_drop
|
371
|
-
@context.merge( "test" => '123', "vars" => ContextSensitiveDrop.new )
|
372
|
-
assert_equal '123', @context['vars.test']
|
373
|
-
end
|
374
|
-
|
375
|
-
def test_nested_context_from_within_drop
|
376
|
-
@context.merge( "test" => '123', "vars" => {"local" => ContextSensitiveDrop.new } )
|
377
|
-
assert_equal '123', @context['vars.local.test']
|
378
|
-
end
|
379
|
-
|
380
|
-
def test_ranges
|
381
|
-
@context.merge( "test" => '5' )
|
382
|
-
assert_equal (1..5), @context['(1..5)']
|
383
|
-
assert_equal (1..5), @context['(1..test)']
|
384
|
-
assert_equal (5..5), @context['(test..test)']
|
385
|
-
end
|
386
|
-
|
387
|
-
def test_cents_through_drop_nestedly
|
388
|
-
@context.merge( "cents" => {"cents" => CentsDrop.new} )
|
389
|
-
assert_equal 100, @context['cents.cents.amount']
|
390
|
-
|
391
|
-
@context.merge( "cents" => { "cents" => {"cents" => CentsDrop.new}} )
|
392
|
-
assert_equal 100, @context['cents.cents.cents.amount']
|
393
|
-
end
|
394
|
-
|
395
|
-
def test_drop_with_variable_called_only_once
|
396
|
-
@context['counter'] = CounterDrop.new
|
397
|
-
|
398
|
-
assert_equal 1, @context['counter.count']
|
399
|
-
assert_equal 2, @context['counter.count']
|
400
|
-
assert_equal 3, @context['counter.count']
|
401
|
-
end
|
402
|
-
|
403
|
-
def test_drop_with_key_called_only_once
|
404
|
-
@context['counter'] = CounterDrop.new
|
405
|
-
|
406
|
-
assert_equal 1, @context['counter["count"]']
|
407
|
-
assert_equal 2, @context['counter["count"]']
|
408
|
-
assert_equal 3, @context['counter["count"]']
|
409
|
-
end
|
410
|
-
|
411
|
-
def test_proc_as_variable
|
412
|
-
@context['dynamic'] = Proc.new { 'Hello' }
|
413
|
-
|
414
|
-
assert_equal 'Hello', @context['dynamic']
|
415
|
-
end
|
416
|
-
|
417
|
-
def test_lambda_as_variable
|
418
|
-
@context['dynamic'] = proc { 'Hello' }
|
419
|
-
|
420
|
-
assert_equal 'Hello', @context['dynamic']
|
421
|
-
end
|
422
|
-
|
423
|
-
def test_nested_lambda_as_variable
|
424
|
-
@context['dynamic'] = { "lambda" => proc { 'Hello' } }
|
425
|
-
|
426
|
-
assert_equal 'Hello', @context['dynamic.lambda']
|
427
|
-
end
|
428
|
-
|
429
|
-
def test_array_containing_lambda_as_variable
|
430
|
-
@context['dynamic'] = [1,2, proc { 'Hello' } ,4,5]
|
431
|
-
|
432
|
-
assert_equal 'Hello', @context['dynamic[2]']
|
433
|
-
end
|
434
|
-
|
435
|
-
def test_lambda_is_called_once
|
436
|
-
@context['callcount'] = proc { @global ||= 0; @global += 1; @global.to_s }
|
437
|
-
|
438
|
-
assert_equal '1', @context['callcount']
|
439
|
-
assert_equal '1', @context['callcount']
|
440
|
-
assert_equal '1', @context['callcount']
|
441
|
-
|
442
|
-
@global = nil
|
443
|
-
end
|
444
|
-
|
445
|
-
def test_nested_lambda_is_called_once
|
446
|
-
@context['callcount'] = { "lambda" => proc { @global ||= 0; @global += 1; @global.to_s } }
|
447
|
-
|
448
|
-
assert_equal '1', @context['callcount.lambda']
|
449
|
-
assert_equal '1', @context['callcount.lambda']
|
450
|
-
assert_equal '1', @context['callcount.lambda']
|
451
|
-
|
452
|
-
@global = nil
|
453
|
-
end
|
454
|
-
|
455
|
-
def test_lambda_in_array_is_called_once
|
456
|
-
@context['callcount'] = [1,2, proc { @global ||= 0; @global += 1; @global.to_s } ,4,5]
|
457
|
-
|
458
|
-
assert_equal '1', @context['callcount[2]']
|
459
|
-
assert_equal '1', @context['callcount[2]']
|
460
|
-
assert_equal '1', @context['callcount[2]']
|
461
|
-
|
462
|
-
@global = nil
|
463
|
-
end
|
464
|
-
|
465
|
-
def test_access_to_context_from_proc
|
466
|
-
@context.registers[:magic] = 345392
|
467
|
-
|
468
|
-
@context['magic'] = proc { @context.registers[:magic] }
|
469
|
-
|
470
|
-
assert_equal 345392, @context['magic']
|
471
|
-
end
|
472
|
-
|
473
|
-
def test_to_liquid_and_context_at_first_level
|
474
|
-
@context['category'] = Category.new("foobar")
|
475
|
-
assert_kind_of CategoryDrop, @context['category']
|
476
|
-
assert_equal @context, @context['category'].context
|
477
|
-
end
|
478
|
-
end # ContextTest
|
data/test/liquid/drop_test.rb
DELETED
@@ -1,180 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
class ContextDrop < Liquid::Drop
|
4
|
-
def scopes
|
5
|
-
@context.scopes.size
|
6
|
-
end
|
7
|
-
|
8
|
-
def scopes_as_array
|
9
|
-
(1..@context.scopes.size).to_a
|
10
|
-
end
|
11
|
-
|
12
|
-
def loop_pos
|
13
|
-
@context['forloop.index']
|
14
|
-
end
|
15
|
-
|
16
|
-
def before_method(method)
|
17
|
-
return @context[method]
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
class ProductDrop < Liquid::Drop
|
22
|
-
|
23
|
-
class TextDrop < Liquid::Drop
|
24
|
-
def array
|
25
|
-
['text1', 'text2']
|
26
|
-
end
|
27
|
-
|
28
|
-
def text
|
29
|
-
'text1'
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
class CatchallDrop < Liquid::Drop
|
34
|
-
def before_method(method)
|
35
|
-
return 'method: ' << method.to_s
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
|
-
def texts
|
40
|
-
TextDrop.new
|
41
|
-
end
|
42
|
-
|
43
|
-
def catchall
|
44
|
-
CatchallDrop.new
|
45
|
-
end
|
46
|
-
|
47
|
-
def context
|
48
|
-
ContextDrop.new
|
49
|
-
end
|
50
|
-
|
51
|
-
protected
|
52
|
-
def callmenot
|
53
|
-
"protected"
|
54
|
-
end
|
55
|
-
end
|
56
|
-
|
57
|
-
class EnumerableDrop < Liquid::Drop
|
58
|
-
|
59
|
-
def size
|
60
|
-
3
|
61
|
-
end
|
62
|
-
|
63
|
-
def each
|
64
|
-
yield 1
|
65
|
-
yield 2
|
66
|
-
yield 3
|
67
|
-
end
|
68
|
-
end
|
69
|
-
|
70
|
-
class DropsTest < Test::Unit::TestCase
|
71
|
-
include Liquid
|
72
|
-
|
73
|
-
def test_product_drop
|
74
|
-
assert_nothing_raised do
|
75
|
-
tpl = Liquid::Template.parse( ' ' )
|
76
|
-
tpl.render('product' => ProductDrop.new)
|
77
|
-
end
|
78
|
-
end
|
79
|
-
|
80
|
-
def test_drop_does_only_respond_to_whitelisted_methods
|
81
|
-
assert_equal "", Liquid::Template.parse("{{ product.inspect }}").render('product' => ProductDrop.new)
|
82
|
-
assert_equal "", Liquid::Template.parse("{{ product.pretty_inspect }}").render('product' => ProductDrop.new)
|
83
|
-
assert_equal "", Liquid::Template.parse("{{ product.whatever }}").render('product' => ProductDrop.new)
|
84
|
-
assert_equal "", Liquid::Template.parse('{{ product | map: "inspect" }}').render('product' => ProductDrop.new)
|
85
|
-
assert_equal "", Liquid::Template.parse('{{ product | map: "pretty_inspect" }}').render('product' => ProductDrop.new)
|
86
|
-
assert_equal "", Liquid::Template.parse('{{ product | map: "whatever" }}').render('product' => ProductDrop.new)
|
87
|
-
end
|
88
|
-
|
89
|
-
def test_drops_respond_to_to_liquid
|
90
|
-
assert_equal "text1", Liquid::Template.parse("{{ product.to_liquid.texts.text }}").render('product' => ProductDrop.new)
|
91
|
-
assert_equal "text1", Liquid::Template.parse('{{ product | map: "to_liquid" | map: "texts" | map: "text" }}').render('product' => ProductDrop.new)
|
92
|
-
end
|
93
|
-
|
94
|
-
def test_text_drop
|
95
|
-
output = Liquid::Template.parse( ' {{ product.texts.text }} ' ).render('product' => ProductDrop.new)
|
96
|
-
assert_equal ' text1 ', output
|
97
|
-
end
|
98
|
-
|
99
|
-
def test_unknown_method
|
100
|
-
output = Liquid::Template.parse( ' {{ product.catchall.unknown }} ' ).render('product' => ProductDrop.new)
|
101
|
-
assert_equal ' method: unknown ', output
|
102
|
-
end
|
103
|
-
|
104
|
-
def test_integer_argument_drop
|
105
|
-
output = Liquid::Template.parse( ' {{ product.catchall[8] }} ' ).render('product' => ProductDrop.new)
|
106
|
-
assert_equal ' method: 8 ', output
|
107
|
-
end
|
108
|
-
|
109
|
-
def test_text_array_drop
|
110
|
-
output = Liquid::Template.parse( '{% for text in product.texts.array %} {{text}} {% endfor %}' ).render('product' => ProductDrop.new)
|
111
|
-
assert_equal ' text1 text2 ', output
|
112
|
-
end
|
113
|
-
|
114
|
-
def test_context_drop
|
115
|
-
output = Liquid::Template.parse( ' {{ context.bar }} ' ).render('context' => ContextDrop.new, 'bar' => "carrot")
|
116
|
-
assert_equal ' carrot ', output
|
117
|
-
end
|
118
|
-
|
119
|
-
def test_nested_context_drop
|
120
|
-
output = Liquid::Template.parse( ' {{ product.context.foo }} ' ).render('product' => ProductDrop.new, 'foo' => "monkey")
|
121
|
-
assert_equal ' monkey ', output
|
122
|
-
end
|
123
|
-
|
124
|
-
def test_protected
|
125
|
-
output = Liquid::Template.parse( ' {{ product.callmenot }} ' ).render('product' => ProductDrop.new)
|
126
|
-
assert_equal ' ', output
|
127
|
-
end
|
128
|
-
|
129
|
-
def test_object_methods_not_allowed
|
130
|
-
[:dup, :clone, :singleton_class, :eval, :class_eval, :inspect].each do |method|
|
131
|
-
output = Liquid::Template.parse(" {{ product.#{method} }} ").render('product' => ProductDrop.new)
|
132
|
-
assert_equal ' ', output
|
133
|
-
end
|
134
|
-
end
|
135
|
-
|
136
|
-
def test_scope
|
137
|
-
assert_equal '1', Liquid::Template.parse( '{{ context.scopes }}' ).render('context' => ContextDrop.new)
|
138
|
-
assert_equal '2', Liquid::Template.parse( '{%for i in dummy%}{{ context.scopes }}{%endfor%}' ).render('context' => ContextDrop.new, 'dummy' => [1])
|
139
|
-
assert_equal '3', Liquid::Template.parse( '{%for i in dummy%}{%for i in dummy%}{{ context.scopes }}{%endfor%}{%endfor%}' ).render('context' => ContextDrop.new, 'dummy' => [1])
|
140
|
-
end
|
141
|
-
|
142
|
-
def test_scope_though_proc
|
143
|
-
assert_equal '1', Liquid::Template.parse( '{{ s }}' ).render('context' => ContextDrop.new, 's' => Proc.new{|c| c['context.scopes'] })
|
144
|
-
assert_equal '2', Liquid::Template.parse( '{%for i in dummy%}{{ s }}{%endfor%}' ).render('context' => ContextDrop.new, 's' => Proc.new{|c| c['context.scopes'] }, 'dummy' => [1])
|
145
|
-
assert_equal '3', Liquid::Template.parse( '{%for i in dummy%}{%for i in dummy%}{{ s }}{%endfor%}{%endfor%}' ).render('context' => ContextDrop.new, 's' => Proc.new{|c| c['context.scopes'] }, 'dummy' => [1])
|
146
|
-
end
|
147
|
-
|
148
|
-
def test_scope_with_assigns
|
149
|
-
assert_equal 'variable', Liquid::Template.parse( '{% assign a = "variable"%}{{a}}' ).render('context' => ContextDrop.new)
|
150
|
-
assert_equal 'variable', Liquid::Template.parse( '{% assign a = "variable"%}{%for i in dummy%}{{a}}{%endfor%}' ).render('context' => ContextDrop.new, 'dummy' => [1])
|
151
|
-
assert_equal 'test', Liquid::Template.parse( '{% assign header_gif = "test"%}{{header_gif}}' ).render('context' => ContextDrop.new)
|
152
|
-
assert_equal 'test', Liquid::Template.parse( "{% assign header_gif = 'test'%}{{header_gif}}" ).render('context' => ContextDrop.new)
|
153
|
-
end
|
154
|
-
|
155
|
-
def test_scope_from_tags
|
156
|
-
assert_equal '1', Liquid::Template.parse( '{% for i in context.scopes_as_array %}{{i}}{% endfor %}' ).render('context' => ContextDrop.new, 'dummy' => [1])
|
157
|
-
assert_equal '12', Liquid::Template.parse( '{%for a in dummy%}{% for i in context.scopes_as_array %}{{i}}{% endfor %}{% endfor %}' ).render('context' => ContextDrop.new, 'dummy' => [1])
|
158
|
-
assert_equal '123', Liquid::Template.parse( '{%for a in dummy%}{%for a in dummy%}{% for i in context.scopes_as_array %}{{i}}{% endfor %}{% endfor %}{% endfor %}' ).render('context' => ContextDrop.new, 'dummy' => [1])
|
159
|
-
end
|
160
|
-
|
161
|
-
def test_access_context_from_drop
|
162
|
-
assert_equal '123', Liquid::Template.parse( '{%for a in dummy%}{{ context.loop_pos }}{% endfor %}' ).render('context' => ContextDrop.new, 'dummy' => [1,2,3])
|
163
|
-
end
|
164
|
-
|
165
|
-
def test_enumerable_drop
|
166
|
-
assert_equal '123', Liquid::Template.parse( '{% for c in collection %}{{c}}{% endfor %}').render('collection' => EnumerableDrop.new)
|
167
|
-
end
|
168
|
-
|
169
|
-
def test_enumerable_drop_size
|
170
|
-
assert_equal '3', Liquid::Template.parse( '{{collection.size}}').render('collection' => EnumerableDrop.new)
|
171
|
-
end
|
172
|
-
|
173
|
-
def test_empty_string_value_access
|
174
|
-
assert_equal '', Liquid::Template.parse('{{ product[value] }}').render('product' => ProductDrop.new, 'value' => '')
|
175
|
-
end
|
176
|
-
|
177
|
-
def test_nil_value_access
|
178
|
-
assert_equal '', Liquid::Template.parse('{{ product[value] }}').render('product' => ProductDrop.new, 'value' => nil)
|
179
|
-
end
|
180
|
-
end # DropsTest
|