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,563 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'test_helper'
4
-
5
- class TrimModeTest < Minitest::Test
6
- include Liquid
7
-
8
- # Make sure the trim isn't applied to standard output
9
- def test_standard_output
10
- text = <<-END_TEMPLATE
11
- <div>
12
- <p>
13
- {{ 'John' }}
14
- </p>
15
- </div>
16
- END_TEMPLATE
17
- expected = <<-END_EXPECTED
18
- <div>
19
- <p>
20
- John
21
- </p>
22
- </div>
23
- END_EXPECTED
24
- assert_template_result(expected, text)
25
- end
26
-
27
- def test_variable_output_with_multiple_blank_lines
28
- text = <<-END_TEMPLATE
29
- <div>
30
- <p>
31
-
32
-
33
- {{- 'John' -}}
34
-
35
-
36
- </p>
37
- </div>
38
- END_TEMPLATE
39
- expected = <<-END_EXPECTED
40
- <div>
41
- <p>John</p>
42
- </div>
43
- END_EXPECTED
44
- assert_template_result(expected, text)
45
- end
46
-
47
- def test_tag_output_with_multiple_blank_lines
48
- text = <<-END_TEMPLATE
49
- <div>
50
- <p>
51
-
52
-
53
- {%- if true -%}
54
- yes
55
- {%- endif -%}
56
-
57
-
58
- </p>
59
- </div>
60
- END_TEMPLATE
61
- expected = <<-END_EXPECTED
62
- <div>
63
- <p>yes</p>
64
- </div>
65
- END_EXPECTED
66
- assert_template_result(expected, text)
67
- end
68
-
69
- # Make sure the trim isn't applied to standard tags
70
- def test_standard_tags
71
- whitespace = ' '
72
- text = <<-END_TEMPLATE
73
- <div>
74
- <p>
75
- {% if true %}
76
- yes
77
- {% endif %}
78
- </p>
79
- </div>
80
- END_TEMPLATE
81
- expected = <<~END_EXPECTED
82
- <div>
83
- <p>
84
- #{whitespace}
85
- yes
86
- #{whitespace}
87
- </p>
88
- </div>
89
- END_EXPECTED
90
- assert_template_result(expected, text)
91
-
92
- text = <<-END_TEMPLATE
93
- <div>
94
- <p>
95
- {% if false %}
96
- no
97
- {% endif %}
98
- </p>
99
- </div>
100
- END_TEMPLATE
101
- expected = <<~END_EXPECTED
102
- <div>
103
- <p>
104
- #{whitespace}
105
- </p>
106
- </div>
107
- END_EXPECTED
108
- assert_template_result(expected, text)
109
- end
110
-
111
- # Make sure the trim isn't too agressive
112
- def test_no_trim_output
113
- text = '<p>{{- \'John\' -}}</p>'
114
- expected = '<p>John</p>'
115
- assert_template_result(expected, text)
116
- end
117
-
118
- # Make sure the trim isn't too agressive
119
- def test_no_trim_tags
120
- text = '<p>{%- if true -%}yes{%- endif -%}</p>'
121
- expected = '<p>yes</p>'
122
- assert_template_result(expected, text)
123
-
124
- text = '<p>{%- if false -%}no{%- endif -%}</p>'
125
- expected = '<p></p>'
126
- assert_template_result(expected, text)
127
- end
128
-
129
- def test_single_line_outer_tag
130
- text = '<p> {%- if true %} yes {% endif -%} </p>'
131
- expected = '<p> yes </p>'
132
- assert_template_result(expected, text)
133
-
134
- text = '<p> {%- if false %} no {% endif -%} </p>'
135
- expected = '<p></p>'
136
- assert_template_result(expected, text)
137
- end
138
-
139
- def test_single_line_inner_tag
140
- text = '<p> {% if true -%} yes {%- endif %} </p>'
141
- expected = '<p> yes </p>'
142
- assert_template_result(expected, text)
143
-
144
- text = '<p> {% if false -%} no {%- endif %} </p>'
145
- expected = '<p> </p>'
146
- assert_template_result(expected, text)
147
- end
148
-
149
- def test_single_line_post_tag
150
- text = '<p> {% if true -%} yes {% endif -%} </p>'
151
- expected = '<p> yes </p>'
152
- assert_template_result(expected, text)
153
-
154
- text = '<p> {% if false -%} no {% endif -%} </p>'
155
- expected = '<p> </p>'
156
- assert_template_result(expected, text)
157
- end
158
-
159
- def test_single_line_pre_tag
160
- text = '<p> {%- if true %} yes {%- endif %} </p>'
161
- expected = '<p> yes </p>'
162
- assert_template_result(expected, text)
163
-
164
- text = '<p> {%- if false %} no {%- endif %} </p>'
165
- expected = '<p> </p>'
166
- assert_template_result(expected, text)
167
- end
168
-
169
- def test_pre_trim_output
170
- text = <<-END_TEMPLATE
171
- <div>
172
- <p>
173
- {{- 'John' }}
174
- </p>
175
- </div>
176
- END_TEMPLATE
177
- expected = <<-END_EXPECTED
178
- <div>
179
- <p>John
180
- </p>
181
- </div>
182
- END_EXPECTED
183
- assert_template_result(expected, text)
184
- end
185
-
186
- def test_pre_trim_tags
187
- text = <<-END_TEMPLATE
188
- <div>
189
- <p>
190
- {%- if true %}
191
- yes
192
- {%- endif %}
193
- </p>
194
- </div>
195
- END_TEMPLATE
196
- expected = <<-END_EXPECTED
197
- <div>
198
- <p>
199
- yes
200
- </p>
201
- </div>
202
- END_EXPECTED
203
- assert_template_result(expected, text)
204
-
205
- text = <<-END_TEMPLATE
206
- <div>
207
- <p>
208
- {%- if false %}
209
- no
210
- {%- endif %}
211
- </p>
212
- </div>
213
- END_TEMPLATE
214
- expected = <<-END_EXPECTED
215
- <div>
216
- <p>
217
- </p>
218
- </div>
219
- END_EXPECTED
220
- assert_template_result(expected, text)
221
- end
222
-
223
- def test_post_trim_output
224
- text = <<-END_TEMPLATE
225
- <div>
226
- <p>
227
- {{ 'John' -}}
228
- </p>
229
- </div>
230
- END_TEMPLATE
231
- expected = <<-END_EXPECTED
232
- <div>
233
- <p>
234
- John</p>
235
- </div>
236
- END_EXPECTED
237
- assert_template_result(expected, text)
238
- end
239
-
240
- def test_post_trim_tags
241
- text = <<-END_TEMPLATE
242
- <div>
243
- <p>
244
- {% if true -%}
245
- yes
246
- {% endif -%}
247
- </p>
248
- </div>
249
- END_TEMPLATE
250
- expected = <<-END_EXPECTED
251
- <div>
252
- <p>
253
- yes
254
- </p>
255
- </div>
256
- END_EXPECTED
257
- assert_template_result(expected, text)
258
-
259
- text = <<-END_TEMPLATE
260
- <div>
261
- <p>
262
- {% if false -%}
263
- no
264
- {% endif -%}
265
- </p>
266
- </div>
267
- END_TEMPLATE
268
- expected = <<-END_EXPECTED
269
- <div>
270
- <p>
271
- </p>
272
- </div>
273
- END_EXPECTED
274
- assert_template_result(expected, text)
275
- end
276
-
277
- def test_pre_and_post_trim_tags
278
- text = <<-END_TEMPLATE
279
- <div>
280
- <p>
281
- {%- if true %}
282
- yes
283
- {% endif -%}
284
- </p>
285
- </div>
286
- END_TEMPLATE
287
- expected = <<-END_EXPECTED
288
- <div>
289
- <p>
290
- yes
291
- </p>
292
- </div>
293
- END_EXPECTED
294
- assert_template_result(expected, text)
295
-
296
- text = <<-END_TEMPLATE
297
- <div>
298
- <p>
299
- {%- if false %}
300
- no
301
- {% endif -%}
302
- </p>
303
- </div>
304
- END_TEMPLATE
305
- expected = <<-END_EXPECTED
306
- <div>
307
- <p></p>
308
- </div>
309
- END_EXPECTED
310
- assert_template_result(expected, text)
311
- end
312
-
313
- def test_post_and_pre_trim_tags
314
- text = <<-END_TEMPLATE
315
- <div>
316
- <p>
317
- {% if true -%}
318
- yes
319
- {%- endif %}
320
- </p>
321
- </div>
322
- END_TEMPLATE
323
- expected = <<-END_EXPECTED
324
- <div>
325
- <p>
326
- yes
327
- </p>
328
- </div>
329
- END_EXPECTED
330
- assert_template_result(expected, text)
331
-
332
- whitespace = ' '
333
- text = <<-END_TEMPLATE
334
- <div>
335
- <p>
336
- {% if false -%}
337
- no
338
- {%- endif %}
339
- </p>
340
- </div>
341
- END_TEMPLATE
342
- expected = <<~END_EXPECTED
343
- <div>
344
- <p>
345
- #{whitespace}
346
- </p>
347
- </div>
348
- END_EXPECTED
349
- assert_template_result(expected, text)
350
- end
351
-
352
- def test_trim_output
353
- text = <<-END_TEMPLATE
354
- <div>
355
- <p>
356
- {{- 'John' -}}
357
- </p>
358
- </div>
359
- END_TEMPLATE
360
- expected = <<-END_EXPECTED
361
- <div>
362
- <p>John</p>
363
- </div>
364
- END_EXPECTED
365
- assert_template_result(expected, text)
366
- end
367
-
368
- def test_trim_tags
369
- text = <<-END_TEMPLATE
370
- <div>
371
- <p>
372
- {%- if true -%}
373
- yes
374
- {%- endif -%}
375
- </p>
376
- </div>
377
- END_TEMPLATE
378
- expected = <<-END_EXPECTED
379
- <div>
380
- <p>yes</p>
381
- </div>
382
- END_EXPECTED
383
- assert_template_result(expected, text)
384
-
385
- text = <<-END_TEMPLATE
386
- <div>
387
- <p>
388
- {%- if false -%}
389
- no
390
- {%- endif -%}
391
- </p>
392
- </div>
393
- END_TEMPLATE
394
- expected = <<-END_EXPECTED
395
- <div>
396
- <p></p>
397
- </div>
398
- END_EXPECTED
399
- assert_template_result(expected, text)
400
- end
401
-
402
- def test_whitespace_trim_output
403
- text = <<-END_TEMPLATE
404
- <div>
405
- <p>
406
- {{- 'John' -}},
407
- {{- '30' -}}
408
- </p>
409
- </div>
410
- END_TEMPLATE
411
- expected = <<-END_EXPECTED
412
- <div>
413
- <p>John,30</p>
414
- </div>
415
- END_EXPECTED
416
- assert_template_result(expected, text)
417
- end
418
-
419
- def test_whitespace_trim_tags
420
- text = <<-END_TEMPLATE
421
- <div>
422
- <p>
423
- {%- if true -%}
424
- yes
425
- {%- endif -%}
426
- </p>
427
- </div>
428
- END_TEMPLATE
429
- expected = <<-END_EXPECTED
430
- <div>
431
- <p>yes</p>
432
- </div>
433
- END_EXPECTED
434
- assert_template_result(expected, text)
435
-
436
- text = <<-END_TEMPLATE
437
- <div>
438
- <p>
439
- {%- if false -%}
440
- no
441
- {%- endif -%}
442
- </p>
443
- </div>
444
- END_TEMPLATE
445
- expected = <<-END_EXPECTED
446
- <div>
447
- <p></p>
448
- </div>
449
- END_EXPECTED
450
- assert_template_result(expected, text)
451
- end
452
-
453
- def test_complex_trim_output
454
- text = <<-END_TEMPLATE
455
- <div>
456
- <p>
457
- {{- 'John' -}}
458
- {{- '30' -}}
459
- </p>
460
- <b>
461
- {{ 'John' -}}
462
- {{- '30' }}
463
- </b>
464
- <i>
465
- {{- 'John' }}
466
- {{ '30' -}}
467
- </i>
468
- </div>
469
- END_TEMPLATE
470
- expected = <<-END_EXPECTED
471
- <div>
472
- <p>John30</p>
473
- <b>
474
- John30
475
- </b>
476
- <i>John
477
- 30</i>
478
- </div>
479
- END_EXPECTED
480
- assert_template_result(expected, text)
481
- end
482
-
483
- def test_complex_trim
484
- text = <<-END_TEMPLATE
485
- <div>
486
- {%- if true -%}
487
- {%- if true -%}
488
- <p>
489
- {{- 'John' -}}
490
- </p>
491
- {%- endif -%}
492
- {%- endif -%}
493
- </div>
494
- END_TEMPLATE
495
- expected = <<-END_EXPECTED
496
- <div><p>John</p></div>
497
- END_EXPECTED
498
- assert_template_result(expected, text)
499
- end
500
-
501
- def test_right_trim_followed_by_tag
502
- assert_template_result('ab c', '{{ "a" -}}{{ "b" }} c')
503
- end
504
-
505
- def test_raw_output
506
- whitespace = ' '
507
- text = <<-END_TEMPLATE
508
- <div>
509
- {% raw %}
510
- {%- if true -%}
511
- <p>
512
- {{- 'John' -}}
513
- </p>
514
- {%- endif -%}
515
- {% endraw %}
516
- </div>
517
- END_TEMPLATE
518
- expected = <<~END_EXPECTED
519
- <div>
520
- #{whitespace}
521
- {%- if true -%}
522
- <p>
523
- {{- 'John' -}}
524
- </p>
525
- {%- endif -%}
526
- #{whitespace}
527
- </div>
528
- END_EXPECTED
529
- assert_template_result(expected, text)
530
- end
531
-
532
- def test_pre_trim_blank_preceding_text
533
- template = Liquid::Template.parse("\n{%- raw %}{% endraw %}")
534
- assert_equal("", template.render)
535
-
536
- template = Liquid::Template.parse("\n{%- if true %}{% endif %}")
537
- assert_equal("", template.render)
538
-
539
- template = Liquid::Template.parse("{{ 'B' }} \n{%- if true %}C{% endif %}")
540
- assert_equal("BC", template.render)
541
- end
542
-
543
- def test_bug_compatible_pre_trim
544
- template = Liquid::Template.parse("\n {%- raw %}{% endraw %}", bug_compatible_whitespace_trimming: true)
545
- assert_equal("\n", template.render)
546
-
547
- template = Liquid::Template.parse("\n {%- if true %}{% endif %}", bug_compatible_whitespace_trimming: true)
548
- assert_equal("\n", template.render)
549
-
550
- template = Liquid::Template.parse("{{ 'B' }} \n{%- if true %}C{% endif %}", bug_compatible_whitespace_trimming: true)
551
- assert_equal("B C", template.render)
552
-
553
- template = Liquid::Template.parse("B\n {%- raw %}{% endraw %}", bug_compatible_whitespace_trimming: true)
554
- assert_equal("B", template.render)
555
-
556
- template = Liquid::Template.parse("B\n {%- if true %}{% endif %}", bug_compatible_whitespace_trimming: true)
557
- assert_equal("B", template.render)
558
- end
559
-
560
- def test_trim_blank
561
- assert_template_result('foobar', 'foo {{-}} bar')
562
- end
563
- end # TrimModeTest
@@ -1,138 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'test_helper'
4
-
5
- class VariableTest < Minitest::Test
6
- include Liquid
7
-
8
- def test_simple_variable
9
- template = Template.parse(%({{test}}))
10
- assert_equal('worked', template.render!('test' => 'worked'))
11
- assert_equal('worked wonderfully', template.render!('test' => 'worked wonderfully'))
12
- end
13
-
14
- def test_variable_render_calls_to_liquid
15
- assert_template_result('foobar', '{{ foo }}', 'foo' => ThingWithToLiquid.new)
16
- end
17
-
18
- def test_variable_lookup_calls_to_liquid_value
19
- assert_template_result('1', '{{ foo }}', 'foo' => IntegerDrop.new('1'))
20
- assert_template_result('2', '{{ list[foo] }}', 'foo' => IntegerDrop.new('1'), 'list' => [1, 2, 3])
21
- assert_template_result('one', '{{ list[foo] }}', 'foo' => IntegerDrop.new('1'), 'list' => { 1 => 'one' })
22
- assert_template_result('Yay', '{{ foo }}', 'foo' => BooleanDrop.new(true))
23
- assert_template_result('YAY', '{{ foo | upcase }}', 'foo' => BooleanDrop.new(true))
24
- end
25
-
26
- def test_if_tag_calls_to_liquid_value
27
- assert_template_result('one', '{% if foo == 1 %}one{% endif %}', 'foo' => IntegerDrop.new('1'))
28
- assert_template_result('one', '{% if 0 < foo %}one{% endif %}', 'foo' => IntegerDrop.new('1'))
29
- assert_template_result('one', '{% if foo > 0 %}one{% endif %}', 'foo' => IntegerDrop.new('1'))
30
- assert_template_result('true', '{% if foo == true %}true{% endif %}', 'foo' => BooleanDrop.new(true))
31
- assert_template_result('true', '{% if foo %}true{% endif %}', 'foo' => BooleanDrop.new(true))
32
-
33
- assert_template_result('', '{% if foo %}true{% endif %}', 'foo' => BooleanDrop.new(false))
34
- assert_template_result('', '{% if foo == true %}True{% endif %}', 'foo' => BooleanDrop.new(false))
35
- end
36
-
37
- def test_unless_tag_calls_to_liquid_value
38
- assert_template_result('', '{% unless foo %}true{% endunless %}', 'foo' => BooleanDrop.new(true))
39
- end
40
-
41
- def test_case_tag_calls_to_liquid_value
42
- assert_template_result('One', '{% case foo %}{% when 1 %}One{% endcase %}', 'foo' => IntegerDrop.new('1'))
43
- end
44
-
45
- def test_simple_with_whitespaces
46
- template = Template.parse(%( {{ test }} ))
47
- assert_equal(' worked ', template.render!('test' => 'worked'))
48
- assert_equal(' worked wonderfully ', template.render!('test' => 'worked wonderfully'))
49
- end
50
-
51
- def test_expression_with_whitespace_in_square_brackets
52
- assert_template_result('result', "{{ a[ 'b' ] }}", 'a' => { 'b' => 'result' })
53
- assert_template_result('result', "{{ a[ [ 'b' ] ] }}", 'b' => 'c', 'a' => { 'c' => 'result' })
54
- end
55
-
56
- def test_ignore_unknown
57
- template = Template.parse(%({{ test }}))
58
- assert_equal('', template.render!)
59
- end
60
-
61
- def test_using_blank_as_variable_name
62
- template = Template.parse("{% assign foo = blank %}{{ foo }}")
63
- assert_equal('', template.render!)
64
- end
65
-
66
- def test_using_empty_as_variable_name
67
- template = Template.parse("{% assign foo = empty %}{{ foo }}")
68
- assert_equal('', template.render!)
69
- end
70
-
71
- def test_hash_scoping
72
- assert_template_result('worked', "{{ test.test }}", 'test' => { 'test' => 'worked' })
73
- assert_template_result('worked', "{{ test . test }}", 'test' => { 'test' => 'worked' })
74
- end
75
-
76
- def test_false_renders_as_false
77
- assert_equal('false', Template.parse("{{ foo }}").render!('foo' => false))
78
- assert_equal('false', Template.parse("{{ false }}").render!)
79
- end
80
-
81
- def test_nil_renders_as_empty_string
82
- assert_equal('', Template.parse("{{ nil }}").render!)
83
- assert_equal('cat', Template.parse("{{ nil | append: 'cat' }}").render!)
84
- end
85
-
86
- def test_preset_assigns
87
- template = Template.parse(%({{ test }}))
88
- template.assigns['test'] = 'worked'
89
- assert_equal('worked', template.render!)
90
- end
91
-
92
- def test_reuse_parsed_template
93
- template = Template.parse(%({{ greeting }} {{ name }}))
94
- template.assigns['greeting'] = 'Goodbye'
95
- assert_equal('Hello Tobi', template.render!('greeting' => 'Hello', 'name' => 'Tobi'))
96
- assert_equal('Hello ', template.render!('greeting' => 'Hello', 'unknown' => 'Tobi'))
97
- assert_equal('Hello Brian', template.render!('greeting' => 'Hello', 'name' => 'Brian'))
98
- assert_equal('Goodbye Brian', template.render!('name' => 'Brian'))
99
- assert_equal({ 'greeting' => 'Goodbye' }, template.assigns)
100
- end
101
-
102
- def test_assigns_not_polluted_from_template
103
- template = Template.parse(%({{ test }}{% assign test = 'bar' %}{{ test }}))
104
- template.assigns['test'] = 'baz'
105
- assert_equal('bazbar', template.render!)
106
- assert_equal('bazbar', template.render!)
107
- assert_equal('foobar', template.render!('test' => 'foo'))
108
- assert_equal('bazbar', template.render!)
109
- end
110
-
111
- def test_hash_with_default_proc
112
- template = Template.parse(%(Hello {{ test }}))
113
- assigns = Hash.new { |_h, k| raise "Unknown variable '#{k}'" }
114
- assigns['test'] = 'Tobi'
115
- assert_equal('Hello Tobi', template.render!(assigns))
116
- assigns.delete('test')
117
- e = assert_raises(RuntimeError) do
118
- template.render!(assigns)
119
- end
120
- assert_equal("Unknown variable 'test'", e.message)
121
- end
122
-
123
- def test_multiline_variable
124
- assert_equal('worked', Template.parse("{{\ntest\n}}").render!('test' => 'worked'))
125
- end
126
-
127
- def test_render_symbol
128
- assert_template_result('bar', '{{ foo }}', 'foo' => :bar)
129
- end
130
-
131
- def test_dynamic_find_var
132
- assert_template_result('bar', '{{ [key] }}', 'key' => 'foo', 'foo' => 'bar')
133
- end
134
-
135
- def test_raw_value_variable
136
- assert_template_result('bar', '{{ [key] }}', 'key' => 'foo', 'foo' => 'bar')
137
- end
138
- end