liquid 5.1.0 → 5.4.0

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