liquid 5.3.0 → 5.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (96) hide show
  1. checksums.yaml +4 -4
  2. data/History.md +16 -1
  3. data/README.md +2 -2
  4. data/lib/liquid/block_body.rb +4 -4
  5. data/lib/liquid/context.rb +6 -2
  6. data/lib/liquid/forloop_drop.rb +44 -1
  7. data/lib/liquid/locales/en.yml +6 -5
  8. data/lib/liquid/partial_cache.rb +3 -3
  9. data/lib/liquid/{static_registers.rb → registers.rb} +13 -10
  10. data/lib/liquid/standardfilters.rb +406 -57
  11. data/lib/liquid/strainer_factory.rb +4 -0
  12. data/lib/liquid/strainer_template.rb +4 -0
  13. data/lib/liquid/tablerowloop_drop.rb +58 -1
  14. data/lib/liquid/tags/assign.rb +12 -8
  15. data/lib/liquid/tags/break.rb +8 -0
  16. data/lib/liquid/tags/capture.rb +13 -10
  17. data/lib/liquid/tags/case.rb +21 -0
  18. data/lib/liquid/tags/comment.rb +13 -0
  19. data/lib/liquid/tags/continue.rb +8 -9
  20. data/lib/liquid/tags/cycle.rb +12 -11
  21. data/lib/liquid/tags/decrement.rb +16 -17
  22. data/lib/liquid/tags/echo.rb +16 -9
  23. data/lib/liquid/tags/for.rb +22 -43
  24. data/lib/liquid/tags/if.rb +11 -9
  25. data/lib/liquid/tags/include.rb +15 -13
  26. data/lib/liquid/tags/increment.rb +16 -14
  27. data/lib/liquid/tags/inline_comment.rb +43 -0
  28. data/lib/liquid/tags/raw.rb +11 -0
  29. data/lib/liquid/tags/render.rb +29 -4
  30. data/lib/liquid/tags/table_row.rb +22 -0
  31. data/lib/liquid/tags/unless.rb +15 -4
  32. data/lib/liquid/template.rb +2 -3
  33. data/lib/liquid/variable.rb +4 -4
  34. data/lib/liquid/variable_lookup.rb +10 -7
  35. data/lib/liquid/version.rb +1 -1
  36. data/lib/liquid.rb +2 -2
  37. metadata +7 -123
  38. data/lib/liquid/register.rb +0 -6
  39. data/test/fixtures/en_locale.yml +0 -9
  40. data/test/integration/assign_test.rb +0 -117
  41. data/test/integration/blank_test.rb +0 -109
  42. data/test/integration/block_test.rb +0 -58
  43. data/test/integration/capture_test.rb +0 -58
  44. data/test/integration/context_test.rb +0 -634
  45. data/test/integration/document_test.rb +0 -21
  46. data/test/integration/drop_test.rb +0 -257
  47. data/test/integration/error_handling_test.rb +0 -272
  48. data/test/integration/expression_test.rb +0 -46
  49. data/test/integration/filter_kwarg_test.rb +0 -24
  50. data/test/integration/filter_test.rb +0 -189
  51. data/test/integration/hash_ordering_test.rb +0 -25
  52. data/test/integration/output_test.rb +0 -125
  53. data/test/integration/parsing_quirks_test.rb +0 -134
  54. data/test/integration/profiler_test.rb +0 -240
  55. data/test/integration/security_test.rb +0 -89
  56. data/test/integration/standard_filter_test.rb +0 -925
  57. data/test/integration/tag/disableable_test.rb +0 -59
  58. data/test/integration/tag_test.rb +0 -45
  59. data/test/integration/tags/break_tag_test.rb +0 -17
  60. data/test/integration/tags/continue_tag_test.rb +0 -17
  61. data/test/integration/tags/echo_test.rb +0 -13
  62. data/test/integration/tags/for_tag_test.rb +0 -466
  63. data/test/integration/tags/if_else_tag_test.rb +0 -190
  64. data/test/integration/tags/include_tag_test.rb +0 -269
  65. data/test/integration/tags/increment_tag_test.rb +0 -25
  66. data/test/integration/tags/liquid_tag_test.rb +0 -116
  67. data/test/integration/tags/raw_tag_test.rb +0 -34
  68. data/test/integration/tags/render_tag_test.rb +0 -213
  69. data/test/integration/tags/standard_tag_test.rb +0 -303
  70. data/test/integration/tags/statements_test.rb +0 -113
  71. data/test/integration/tags/table_row_test.rb +0 -66
  72. data/test/integration/tags/unless_else_tag_test.rb +0 -28
  73. data/test/integration/template_test.rb +0 -340
  74. data/test/integration/trim_mode_test.rb +0 -563
  75. data/test/integration/variable_test.rb +0 -138
  76. data/test/test_helper.rb +0 -207
  77. data/test/unit/block_unit_test.rb +0 -53
  78. data/test/unit/condition_unit_test.rb +0 -181
  79. data/test/unit/file_system_unit_test.rb +0 -37
  80. data/test/unit/i18n_unit_test.rb +0 -39
  81. data/test/unit/lexer_unit_test.rb +0 -53
  82. data/test/unit/parse_tree_visitor_test.rb +0 -261
  83. data/test/unit/parser_unit_test.rb +0 -84
  84. data/test/unit/partial_cache_unit_test.rb +0 -128
  85. data/test/unit/regexp_unit_test.rb +0 -46
  86. data/test/unit/static_registers_unit_test.rb +0 -156
  87. data/test/unit/strainer_factory_unit_test.rb +0 -101
  88. data/test/unit/strainer_template_unit_test.rb +0 -82
  89. data/test/unit/tag_unit_test.rb +0 -23
  90. data/test/unit/tags/case_tag_unit_test.rb +0 -12
  91. data/test/unit/tags/for_tag_unit_test.rb +0 -15
  92. data/test/unit/tags/if_tag_unit_test.rb +0 -10
  93. data/test/unit/template_factory_unit_test.rb +0 -12
  94. data/test/unit/template_unit_test.rb +0 -87
  95. data/test/unit/tokenizer_unit_test.rb +0 -62
  96. data/test/unit/variable_unit_test.rb +0 -164
@@ -1,634 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'test_helper'
4
-
5
- class HundredCentes
6
- def to_liquid
7
- 100
8
- end
9
- end
10
-
11
- class CentsDrop < Liquid::Drop
12
- def amount
13
- HundredCentes.new
14
- end
15
-
16
- def non_zero?
17
- true
18
- end
19
- end
20
-
21
- class ContextSensitiveDrop < Liquid::Drop
22
- def test
23
- @context['test']
24
- end
25
- end
26
-
27
- class Category
28
- attr_accessor :name
29
-
30
- def initialize(name)
31
- @name = name
32
- end
33
-
34
- def to_liquid
35
- CategoryDrop.new(self)
36
- end
37
- end
38
-
39
- class CategoryDrop < Liquid::Drop
40
- attr_accessor :category, :context
41
-
42
- def initialize(category)
43
- @category = category
44
- end
45
- end
46
-
47
- class CounterDrop < Liquid::Drop
48
- def count
49
- @count ||= 0
50
- @count += 1
51
- end
52
- end
53
-
54
- class ArrayLike
55
- def fetch(index)
56
- end
57
-
58
- def [](index)
59
- @counts ||= []
60
- @counts[index] ||= 0
61
- @counts[index] += 1
62
- end
63
-
64
- def to_liquid
65
- self
66
- end
67
- end
68
-
69
- class ContextTest < Minitest::Test
70
- include Liquid
71
-
72
- def setup
73
- @context = Liquid::Context.new
74
- end
75
-
76
- def test_variables
77
- @context['string'] = 'string'
78
- assert_equal('string', @context['string'])
79
-
80
- @context['num'] = 5
81
- assert_equal(5, @context['num'])
82
-
83
- @context['time'] = Time.parse('2006-06-06 12:00:00')
84
- assert_equal(Time.parse('2006-06-06 12:00:00'), @context['time'])
85
-
86
- @context['date'] = Date.today
87
- assert_equal(Date.today, @context['date'])
88
-
89
- now = Time.now
90
- @context['datetime'] = now
91
- assert_equal(now, @context['datetime'])
92
-
93
- @context['bool'] = true
94
- assert_equal(true, @context['bool'])
95
-
96
- @context['bool'] = false
97
- assert_equal(false, @context['bool'])
98
-
99
- @context['nil'] = nil
100
- assert_nil(@context['nil'])
101
- assert_nil(@context['nil'])
102
- end
103
-
104
- def test_variables_not_existing
105
- assert_nil(@context['does_not_exist'])
106
- end
107
-
108
- def test_scoping
109
- @context.push
110
- @context.pop
111
-
112
- assert_raises(Liquid::ContextError) do
113
- @context.pop
114
- end
115
-
116
- assert_raises(Liquid::ContextError) do
117
- @context.push
118
- @context.pop
119
- @context.pop
120
- end
121
- end
122
-
123
- def test_length_query
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
- end
136
-
137
- def test_hyphenated_variable
138
- @context['oh-my'] = 'godz'
139
- assert_equal('godz', @context['oh-my'])
140
- end
141
-
142
- def test_add_filter
143
- filter = Module.new do
144
- def hi(output)
145
- output + ' hi!'
146
- end
147
- end
148
-
149
- context = Context.new
150
- context.add_filters(filter)
151
- assert_equal('hi? hi!', context.invoke(:hi, 'hi?'))
152
-
153
- context = Context.new
154
- assert_equal('hi?', context.invoke(:hi, 'hi?'))
155
-
156
- context.add_filters(filter)
157
- assert_equal('hi? hi!', context.invoke(:hi, 'hi?'))
158
- end
159
-
160
- def test_only_intended_filters_make_it_there
161
- filter = Module.new do
162
- def hi(output)
163
- output + ' hi!'
164
- end
165
- end
166
-
167
- context = Context.new
168
- assert_equal("Wookie", context.invoke("hi", "Wookie"))
169
-
170
- context.add_filters(filter)
171
- assert_equal("Wookie hi!", context.invoke("hi", "Wookie"))
172
- end
173
-
174
- def test_add_item_in_outer_scope
175
- @context['test'] = 'test'
176
- @context.push
177
- assert_equal('test', @context['test'])
178
- @context.pop
179
- assert_equal('test', @context['test'])
180
- end
181
-
182
- def test_add_item_in_inner_scope
183
- @context.push
184
- @context['test'] = 'test'
185
- assert_equal('test', @context['test'])
186
- @context.pop
187
- assert_nil(@context['test'])
188
- end
189
-
190
- def test_hierachical_data
191
- @context['hash'] = { "name" => 'tobi' }
192
- assert_equal('tobi', @context['hash.name'])
193
- assert_equal('tobi', @context['hash["name"]'])
194
- end
195
-
196
- def test_keywords
197
- assert_equal(true, @context['true'])
198
- assert_equal(false, @context['false'])
199
- end
200
-
201
- def test_digits
202
- assert_equal(100, @context['100'])
203
- assert_equal(100.00, @context['100.00'])
204
- end
205
-
206
- def test_strings
207
- assert_equal("hello!", @context['"hello!"'])
208
- assert_equal("hello!", @context["'hello!'"])
209
- end
210
-
211
- def test_merge
212
- @context.merge("test" => "test")
213
- assert_equal('test', @context['test'])
214
- @context.merge("test" => "newvalue", "foo" => "bar")
215
- assert_equal('newvalue', @context['test'])
216
- assert_equal('bar', @context['foo'])
217
- end
218
-
219
- def test_array_notation
220
- @context['test'] = [1, 2, 3, 4, 5]
221
-
222
- assert_equal(1, @context['test[0]'])
223
- assert_equal(2, @context['test[1]'])
224
- assert_equal(3, @context['test[2]'])
225
- assert_equal(4, @context['test[3]'])
226
- assert_equal(5, @context['test[4]'])
227
- end
228
-
229
- def test_recoursive_array_notation
230
- @context['test'] = { 'test' => [1, 2, 3, 4, 5] }
231
-
232
- assert_equal(1, @context['test.test[0]'])
233
-
234
- @context['test'] = [{ 'test' => 'worked' }]
235
-
236
- assert_equal('worked', @context['test[0].test'])
237
- end
238
-
239
- def test_hash_to_array_transition
240
- @context['colors'] = {
241
- 'Blue' => ['003366', '336699', '6699CC', '99CCFF'],
242
- 'Green' => ['003300', '336633', '669966', '99CC99'],
243
- 'Yellow' => ['CC9900', 'FFCC00', 'FFFF99', 'FFFFCC'],
244
- 'Red' => ['660000', '993333', 'CC6666', 'FF9999'],
245
- }
246
-
247
- assert_equal('003366', @context['colors.Blue[0]'])
248
- assert_equal('FF9999', @context['colors.Red[3]'])
249
- end
250
-
251
- def test_try_first
252
- @context['test'] = [1, 2, 3, 4, 5]
253
-
254
- assert_equal(1, @context['test.first'])
255
- assert_equal(5, @context['test.last'])
256
-
257
- @context['test'] = { 'test' => [1, 2, 3, 4, 5] }
258
-
259
- assert_equal(1, @context['test.test.first'])
260
- assert_equal(5, @context['test.test.last'])
261
-
262
- @context['test'] = [1]
263
- assert_equal(1, @context['test.first'])
264
- assert_equal(1, @context['test.last'])
265
- end
266
-
267
- def test_access_hashes_with_hash_notation
268
- @context['products'] = { 'count' => 5, 'tags' => ['deepsnow', 'freestyle'] }
269
- @context['product'] = { 'variants' => [{ 'title' => 'draft151cm' }, { 'title' => 'element151cm' }] }
270
-
271
- assert_equal(5, @context['products["count"]'])
272
- assert_equal('deepsnow', @context['products["tags"][0]'])
273
- assert_equal('deepsnow', @context['products["tags"].first'])
274
- assert_equal('draft151cm', @context['product["variants"][0]["title"]'])
275
- assert_equal('element151cm', @context['product["variants"][1]["title"]'])
276
- assert_equal('draft151cm', @context['product["variants"][0]["title"]'])
277
- assert_equal('element151cm', @context['product["variants"].last["title"]'])
278
- end
279
-
280
- def test_access_variable_with_hash_notation
281
- @context['foo'] = 'baz'
282
- @context['bar'] = 'foo'
283
-
284
- assert_equal('baz', @context['["foo"]'])
285
- assert_equal('baz', @context['[bar]'])
286
- end
287
-
288
- def test_access_hashes_with_hash_access_variables
289
- @context['var'] = 'tags'
290
- @context['nested'] = { 'var' => 'tags' }
291
- @context['products'] = { 'count' => 5, 'tags' => ['deepsnow', 'freestyle'] }
292
-
293
- assert_equal('deepsnow', @context['products[var].first'])
294
- assert_equal('freestyle', @context['products[nested.var].last'])
295
- end
296
-
297
- def test_hash_notation_only_for_hash_access
298
- @context['array'] = [1, 2, 3, 4, 5]
299
- @context['hash'] = { 'first' => 'Hello' }
300
-
301
- assert_equal(1, @context['array.first'])
302
- assert_nil(@context['array["first"]'])
303
- assert_equal('Hello', @context['hash["first"]'])
304
- end
305
-
306
- def test_first_can_appear_in_middle_of_callchain
307
- @context['product'] = { 'variants' => [{ 'title' => 'draft151cm' }, { 'title' => 'element151cm' }] }
308
-
309
- assert_equal('draft151cm', @context['product.variants[0].title'])
310
- assert_equal('element151cm', @context['product.variants[1].title'])
311
- assert_equal('draft151cm', @context['product.variants.first.title'])
312
- assert_equal('element151cm', @context['product.variants.last.title'])
313
- end
314
-
315
- def test_cents
316
- @context.merge("cents" => HundredCentes.new)
317
- assert_equal(100, @context['cents'])
318
- end
319
-
320
- def test_nested_cents
321
- @context.merge("cents" => { 'amount' => HundredCentes.new })
322
- assert_equal(100, @context['cents.amount'])
323
-
324
- @context.merge("cents" => { 'cents' => { 'amount' => HundredCentes.new } })
325
- assert_equal(100, @context['cents.cents.amount'])
326
- end
327
-
328
- def test_cents_through_drop
329
- @context.merge("cents" => CentsDrop.new)
330
- assert_equal(100, @context['cents.amount'])
331
- end
332
-
333
- def test_nested_cents_through_drop
334
- @context.merge("vars" => { "cents" => CentsDrop.new })
335
- assert_equal(100, @context['vars.cents.amount'])
336
- end
337
-
338
- def test_drop_methods_with_question_marks
339
- @context.merge("cents" => CentsDrop.new)
340
- assert(@context['cents.non_zero?'])
341
- end
342
-
343
- def test_context_from_within_drop
344
- @context.merge("test" => '123', "vars" => ContextSensitiveDrop.new)
345
- assert_equal('123', @context['vars.test'])
346
- end
347
-
348
- def test_nested_context_from_within_drop
349
- @context.merge("test" => '123', "vars" => { "local" => ContextSensitiveDrop.new })
350
- assert_equal('123', @context['vars.local.test'])
351
- end
352
-
353
- def test_ranges
354
- @context.merge("test" => '5')
355
- assert_equal((1..5), @context['(1..5)'])
356
- assert_equal((1..5), @context['(1..test)'])
357
- assert_equal((5..5), @context['(test..test)'])
358
- end
359
-
360
- def test_cents_through_drop_nestedly
361
- @context.merge("cents" => { "cents" => CentsDrop.new })
362
- assert_equal(100, @context['cents.cents.amount'])
363
-
364
- @context.merge("cents" => { "cents" => { "cents" => CentsDrop.new } })
365
- assert_equal(100, @context['cents.cents.cents.amount'])
366
- end
367
-
368
- def test_drop_with_variable_called_only_once
369
- @context['counter'] = CounterDrop.new
370
-
371
- assert_equal(1, @context['counter.count'])
372
- assert_equal(2, @context['counter.count'])
373
- assert_equal(3, @context['counter.count'])
374
- end
375
-
376
- def test_drop_with_key_called_only_once
377
- @context['counter'] = CounterDrop.new
378
-
379
- assert_equal(1, @context['counter["count"]'])
380
- assert_equal(2, @context['counter["count"]'])
381
- assert_equal(3, @context['counter["count"]'])
382
- end
383
-
384
- def test_proc_as_variable
385
- @context['dynamic'] = proc { 'Hello' }
386
-
387
- assert_equal('Hello', @context['dynamic'])
388
- end
389
-
390
- def test_lambda_as_variable
391
- @context['dynamic'] = proc { 'Hello' }
392
-
393
- assert_equal('Hello', @context['dynamic'])
394
- end
395
-
396
- def test_nested_lambda_as_variable
397
- @context['dynamic'] = { "lambda" => proc { 'Hello' } }
398
-
399
- assert_equal('Hello', @context['dynamic.lambda'])
400
- end
401
-
402
- def test_array_containing_lambda_as_variable
403
- @context['dynamic'] = [1, 2, proc { 'Hello' }, 4, 5]
404
-
405
- assert_equal('Hello', @context['dynamic[2]'])
406
- end
407
-
408
- def test_lambda_is_called_once
409
- @global = 0
410
-
411
- @context['callcount'] = proc {
412
- @global += 1
413
- @global.to_s
414
- }
415
-
416
- assert_equal('1', @context['callcount'])
417
- assert_equal('1', @context['callcount'])
418
- assert_equal('1', @context['callcount'])
419
- end
420
-
421
- def test_nested_lambda_is_called_once
422
- @global = 0
423
-
424
- @context['callcount'] = { "lambda" => proc {
425
- @global += 1
426
- @global.to_s
427
- } }
428
-
429
- assert_equal('1', @context['callcount.lambda'])
430
- assert_equal('1', @context['callcount.lambda'])
431
- assert_equal('1', @context['callcount.lambda'])
432
- end
433
-
434
- def test_lambda_in_array_is_called_once
435
- @global = 0
436
-
437
- @context['callcount'] = [1, 2, proc {
438
- @global += 1
439
- @global.to_s
440
- }, 4, 5]
441
-
442
- assert_equal('1', @context['callcount[2]'])
443
- assert_equal('1', @context['callcount[2]'])
444
- assert_equal('1', @context['callcount[2]'])
445
- end
446
-
447
- def test_access_to_context_from_proc
448
- @context.registers[:magic] = 345392
449
-
450
- @context['magic'] = proc { @context.registers[:magic] }
451
-
452
- assert_equal(345392, @context['magic'])
453
- end
454
-
455
- def test_to_liquid_and_context_at_first_level
456
- @context['category'] = Category.new("foobar")
457
- assert_kind_of(CategoryDrop, @context['category'])
458
- assert_equal(@context, @context['category'].context)
459
- end
460
-
461
- def test_interrupt_avoids_object_allocations
462
- @context.interrupt? # ruby 3.0.0 allocates on the first call
463
- assert_no_object_allocations do
464
- @context.interrupt?
465
- end
466
- end
467
-
468
- def test_context_initialization_with_a_proc_in_environment
469
- contx = Context.new([test: ->(c) { c['poutine'] }], test: :foo)
470
-
471
- assert(contx)
472
- assert_nil(contx['poutine'])
473
- end
474
-
475
- def test_apply_global_filter
476
- global_filter_proc = ->(output) { "#{output} filtered" }
477
-
478
- context = Context.new
479
- context.global_filter = global_filter_proc
480
-
481
- assert_equal('hi filtered', context.apply_global_filter('hi'))
482
- end
483
-
484
- def test_static_environments_are_read_with_lower_priority_than_environments
485
- context = Context.build(
486
- static_environments: { 'shadowed' => 'static', 'unshadowed' => 'static' },
487
- environments: { 'shadowed' => 'dynamic' }
488
- )
489
-
490
- assert_equal('dynamic', context['shadowed'])
491
- assert_equal('static', context['unshadowed'])
492
- end
493
-
494
- def test_apply_global_filter_when_no_global_filter_exist
495
- context = Context.new
496
- assert_equal('hi', context.apply_global_filter('hi'))
497
- end
498
-
499
- def test_new_isolated_subcontext_does_not_inherit_variables
500
- super_context = Context.new
501
- super_context['my_variable'] = 'some value'
502
- subcontext = super_context.new_isolated_subcontext
503
-
504
- assert_nil(subcontext['my_variable'])
505
- end
506
-
507
- def test_new_isolated_subcontext_inherits_static_environment
508
- super_context = Context.build(static_environments: { 'my_environment_value' => 'my value' })
509
- subcontext = super_context.new_isolated_subcontext
510
-
511
- assert_equal('my value', subcontext['my_environment_value'])
512
- end
513
-
514
- def test_new_isolated_subcontext_inherits_resource_limits
515
- resource_limits = ResourceLimits.new({})
516
- super_context = Context.new({}, {}, {}, false, resource_limits)
517
- subcontext = super_context.new_isolated_subcontext
518
- assert_equal(resource_limits, subcontext.resource_limits)
519
- end
520
-
521
- def test_new_isolated_subcontext_inherits_exception_renderer
522
- super_context = Context.new
523
- super_context.exception_renderer = ->(_e) { 'my exception message' }
524
- subcontext = super_context.new_isolated_subcontext
525
- assert_equal('my exception message', subcontext.handle_error(Liquid::Error.new))
526
- end
527
-
528
- def test_new_isolated_subcontext_does_not_inherit_non_static_registers
529
- registers = {
530
- my_register: :my_value,
531
- }
532
- super_context = Context.new({}, {}, StaticRegisters.new(registers))
533
- super_context.registers[:my_register] = :my_alt_value
534
- subcontext = super_context.new_isolated_subcontext
535
- assert_equal(:my_value, subcontext.registers[:my_register])
536
- end
537
-
538
- def test_new_isolated_subcontext_inherits_static_registers
539
- super_context = Context.build(registers: { my_register: :my_value })
540
- subcontext = super_context.new_isolated_subcontext
541
- assert_equal(:my_value, subcontext.registers[:my_register])
542
- end
543
-
544
- def test_new_isolated_subcontext_registers_do_not_pollute_context
545
- super_context = Context.build(registers: { my_register: :my_value })
546
- subcontext = super_context.new_isolated_subcontext
547
- subcontext.registers[:my_register] = :my_alt_value
548
- assert_equal(:my_value, super_context.registers[:my_register])
549
- end
550
-
551
- def test_new_isolated_subcontext_inherits_filters
552
- my_filter = Module.new do
553
- def my_filter(*)
554
- 'my filter result'
555
- end
556
- end
557
-
558
- super_context = Context.new
559
- super_context.add_filters([my_filter])
560
- subcontext = super_context.new_isolated_subcontext
561
- template = Template.parse('{{ 123 | my_filter }}')
562
- assert_equal('my filter result', template.render(subcontext))
563
- end
564
-
565
- def test_disables_tag_specified
566
- context = Context.new
567
- context.with_disabled_tags(%w(foo bar)) do
568
- assert_equal(true, context.tag_disabled?("foo"))
569
- assert_equal(true, context.tag_disabled?("bar"))
570
- assert_equal(false, context.tag_disabled?("unknown"))
571
- end
572
- end
573
-
574
- def test_disables_nested_tags
575
- context = Context.new
576
- context.with_disabled_tags(["foo"]) do
577
- context.with_disabled_tags(["foo"]) do
578
- assert_equal(true, context.tag_disabled?("foo"))
579
- assert_equal(false, context.tag_disabled?("bar"))
580
- end
581
- context.with_disabled_tags(["bar"]) do
582
- assert_equal(true, context.tag_disabled?("foo"))
583
- assert_equal(true, context.tag_disabled?("bar"))
584
- context.with_disabled_tags(["foo"]) do
585
- assert_equal(true, context.tag_disabled?("foo"))
586
- assert_equal(true, context.tag_disabled?("bar"))
587
- end
588
- end
589
- assert_equal(true, context.tag_disabled?("foo"))
590
- assert_equal(false, context.tag_disabled?("bar"))
591
- end
592
- end
593
-
594
- def test_override_global_filter
595
- global = Module.new do
596
- def notice(output)
597
- "Global #{output}"
598
- end
599
- end
600
-
601
- local = Module.new do
602
- def notice(output)
603
- "Local #{output}"
604
- end
605
- end
606
-
607
- with_global_filter(global) do
608
- assert_equal('Global test', Template.parse("{{'test' | notice }}").render!)
609
- assert_equal('Local test', Template.parse("{{'test' | notice }}").render!({}, filters: [local]))
610
- end
611
- end
612
-
613
- def test_has_key_will_not_add_an_error_for_missing_keys
614
- with_error_mode(:strict) do
615
- context = Context.new
616
- context.key?('unknown')
617
- assert_empty(context.errors)
618
- end
619
- end
620
-
621
- private
622
-
623
- def assert_no_object_allocations
624
- unless RUBY_ENGINE == 'ruby'
625
- skip("stackprof needed to count object allocations")
626
- end
627
- require 'stackprof'
628
-
629
- profile = StackProf.run(mode: :object) do
630
- yield
631
- end
632
- assert_equal(0, profile[:samples])
633
- end
634
- end # ContextTest
@@ -1,21 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'test_helper'
4
-
5
- class DocumentTest < Minitest::Test
6
- include Liquid
7
-
8
- def test_unexpected_outer_tag
9
- exc = assert_raises(SyntaxError) do
10
- Template.parse("{% else %}")
11
- end
12
- assert_equal(exc.message, "Liquid syntax error: Unexpected outer 'else' tag")
13
- end
14
-
15
- def test_unknown_tag
16
- exc = assert_raises(SyntaxError) do
17
- Template.parse("{% foo %}")
18
- end
19
- assert_equal(exc.message, "Liquid syntax error: Unknown tag 'foo'")
20
- end
21
- end