liquid 3.0.6 → 5.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (113) hide show
  1. checksums.yaml +5 -5
  2. data/History.md +243 -58
  3. data/README.md +43 -4
  4. data/lib/liquid/block.rb +57 -123
  5. data/lib/liquid/block_body.rb +217 -85
  6. data/lib/liquid/condition.rb +92 -45
  7. data/lib/liquid/context.rb +154 -89
  8. data/lib/liquid/document.rb +57 -9
  9. data/lib/liquid/drop.rb +20 -17
  10. data/lib/liquid/errors.rb +27 -29
  11. data/lib/liquid/expression.rb +32 -20
  12. data/lib/liquid/extensions.rb +21 -7
  13. data/lib/liquid/file_system.rb +17 -15
  14. data/lib/liquid/forloop_drop.rb +92 -0
  15. data/lib/liquid/i18n.rb +10 -8
  16. data/lib/liquid/interrupts.rb +4 -3
  17. data/lib/liquid/lexer.rb +37 -26
  18. data/lib/liquid/locales/en.yml +13 -6
  19. data/lib/liquid/parse_context.rb +54 -0
  20. data/lib/liquid/parse_tree_visitor.rb +42 -0
  21. data/lib/liquid/parser.rb +30 -18
  22. data/lib/liquid/parser_switching.rb +20 -6
  23. data/lib/liquid/partial_cache.rb +24 -0
  24. data/lib/liquid/profiler/hooks.rb +26 -14
  25. data/lib/liquid/profiler.rb +72 -92
  26. data/lib/liquid/range_lookup.rb +28 -3
  27. data/lib/liquid/registers.rb +51 -0
  28. data/lib/liquid/resource_limits.rb +62 -0
  29. data/lib/liquid/standardfilters.rb +715 -132
  30. data/lib/liquid/strainer_factory.rb +41 -0
  31. data/lib/liquid/strainer_template.rb +62 -0
  32. data/lib/liquid/tablerowloop_drop.rb +121 -0
  33. data/lib/liquid/tag/disableable.rb +22 -0
  34. data/lib/liquid/tag/disabler.rb +21 -0
  35. data/lib/liquid/tag.rb +35 -12
  36. data/lib/liquid/tags/assign.rb +57 -18
  37. data/lib/liquid/tags/break.rb +15 -5
  38. data/lib/liquid/tags/capture.rb +24 -18
  39. data/lib/liquid/tags/case.rb +79 -30
  40. data/lib/liquid/tags/comment.rb +19 -4
  41. data/lib/liquid/tags/continue.rb +16 -12
  42. data/lib/liquid/tags/cycle.rb +47 -27
  43. data/lib/liquid/tags/decrement.rb +23 -24
  44. data/lib/liquid/tags/echo.rb +41 -0
  45. data/lib/liquid/tags/for.rb +155 -124
  46. data/lib/liquid/tags/if.rb +97 -63
  47. data/lib/liquid/tags/ifchanged.rb +11 -12
  48. data/lib/liquid/tags/include.rb +82 -73
  49. data/lib/liquid/tags/increment.rb +23 -17
  50. data/lib/liquid/tags/inline_comment.rb +43 -0
  51. data/lib/liquid/tags/raw.rb +50 -8
  52. data/lib/liquid/tags/render.rb +109 -0
  53. data/lib/liquid/tags/table_row.rb +57 -41
  54. data/lib/liquid/tags/unless.rb +38 -20
  55. data/lib/liquid/template.rb +71 -103
  56. data/lib/liquid/template_factory.rb +9 -0
  57. data/lib/liquid/tokenizer.rb +39 -0
  58. data/lib/liquid/usage.rb +8 -0
  59. data/lib/liquid/utils.rb +63 -9
  60. data/lib/liquid/variable.rb +74 -56
  61. data/lib/liquid/variable_lookup.rb +31 -15
  62. data/lib/liquid/version.rb +3 -1
  63. data/lib/liquid.rb +27 -12
  64. metadata +30 -106
  65. data/lib/liquid/module_ex.rb +0 -62
  66. data/lib/liquid/strainer.rb +0 -59
  67. data/lib/liquid/token.rb +0 -18
  68. data/test/fixtures/en_locale.yml +0 -9
  69. data/test/integration/assign_test.rb +0 -48
  70. data/test/integration/blank_test.rb +0 -106
  71. data/test/integration/capture_test.rb +0 -50
  72. data/test/integration/context_test.rb +0 -32
  73. data/test/integration/drop_test.rb +0 -271
  74. data/test/integration/error_handling_test.rb +0 -207
  75. data/test/integration/filter_test.rb +0 -138
  76. data/test/integration/hash_ordering_test.rb +0 -23
  77. data/test/integration/output_test.rb +0 -124
  78. data/test/integration/parsing_quirks_test.rb +0 -116
  79. data/test/integration/render_profiling_test.rb +0 -154
  80. data/test/integration/security_test.rb +0 -64
  81. data/test/integration/standard_filter_test.rb +0 -396
  82. data/test/integration/tags/break_tag_test.rb +0 -16
  83. data/test/integration/tags/continue_tag_test.rb +0 -16
  84. data/test/integration/tags/for_tag_test.rb +0 -375
  85. data/test/integration/tags/if_else_tag_test.rb +0 -190
  86. data/test/integration/tags/include_tag_test.rb +0 -234
  87. data/test/integration/tags/increment_tag_test.rb +0 -24
  88. data/test/integration/tags/raw_tag_test.rb +0 -25
  89. data/test/integration/tags/standard_tag_test.rb +0 -297
  90. data/test/integration/tags/statements_test.rb +0 -113
  91. data/test/integration/tags/table_row_test.rb +0 -63
  92. data/test/integration/tags/unless_else_tag_test.rb +0 -26
  93. data/test/integration/template_test.rb +0 -182
  94. data/test/integration/variable_test.rb +0 -82
  95. data/test/test_helper.rb +0 -89
  96. data/test/unit/block_unit_test.rb +0 -55
  97. data/test/unit/condition_unit_test.rb +0 -149
  98. data/test/unit/context_unit_test.rb +0 -492
  99. data/test/unit/file_system_unit_test.rb +0 -35
  100. data/test/unit/i18n_unit_test.rb +0 -37
  101. data/test/unit/lexer_unit_test.rb +0 -48
  102. data/test/unit/module_ex_unit_test.rb +0 -87
  103. data/test/unit/parser_unit_test.rb +0 -82
  104. data/test/unit/regexp_unit_test.rb +0 -44
  105. data/test/unit/strainer_unit_test.rb +0 -69
  106. data/test/unit/tag_unit_test.rb +0 -16
  107. data/test/unit/tags/case_tag_unit_test.rb +0 -10
  108. data/test/unit/tags/for_tag_unit_test.rb +0 -13
  109. data/test/unit/tags/if_tag_unit_test.rb +0 -8
  110. data/test/unit/template_unit_test.rb +0 -69
  111. data/test/unit/tokenizer_unit_test.rb +0 -38
  112. data/test/unit/variable_unit_test.rb +0 -145
  113. /data/{MIT-LICENSE → LICENSE} +0 -0
@@ -1,113 +0,0 @@
1
- require 'test_helper'
2
-
3
- class StatementsTest < Minitest::Test
4
- include Liquid
5
-
6
- def test_true_eql_true
7
- text = ' {% if true == true %} true {% else %} false {% endif %} '
8
- assert_template_result ' true ', text
9
- end
10
-
11
- def test_true_not_eql_true
12
- text = ' {% if true != true %} true {% else %} false {% endif %} '
13
- assert_template_result ' false ', text
14
- end
15
-
16
- def test_true_lq_true
17
- text = ' {% if 0 > 0 %} true {% else %} false {% endif %} '
18
- assert_template_result ' false ', text
19
- end
20
-
21
- def test_one_lq_zero
22
- text = ' {% if 1 > 0 %} true {% else %} false {% endif %} '
23
- assert_template_result ' true ', text
24
- end
25
-
26
- def test_zero_lq_one
27
- text = ' {% if 0 < 1 %} true {% else %} false {% endif %} '
28
- assert_template_result ' true ', text
29
- end
30
-
31
- def test_zero_lq_or_equal_one
32
- text = ' {% if 0 <= 0 %} true {% else %} false {% endif %} '
33
- assert_template_result ' true ', text
34
- end
35
-
36
- def test_zero_lq_or_equal_one_involving_nil
37
- text = ' {% if null <= 0 %} true {% else %} false {% endif %} '
38
- assert_template_result ' false ', text
39
-
40
-
41
- text = ' {% if 0 <= null %} true {% else %} false {% endif %} '
42
- assert_template_result ' false ', text
43
- end
44
-
45
- def test_zero_lqq_or_equal_one
46
- text = ' {% if 0 >= 0 %} true {% else %} false {% endif %} '
47
- assert_template_result ' true ', text
48
- end
49
-
50
- def test_strings
51
- text = " {% if 'test' == 'test' %} true {% else %} false {% endif %} "
52
- assert_template_result ' true ', text
53
- end
54
-
55
- def test_strings_not_equal
56
- text = " {% if 'test' != 'test' %} true {% else %} false {% endif %} "
57
- assert_template_result ' false ', text
58
- end
59
-
60
- def test_var_strings_equal
61
- text = ' {% if var == "hello there!" %} true {% else %} false {% endif %} '
62
- assert_template_result ' true ', text, 'var' => 'hello there!'
63
- end
64
-
65
- def test_var_strings_are_not_equal
66
- text = ' {% if "hello there!" == var %} true {% else %} false {% endif %} '
67
- assert_template_result ' true ', text, 'var' => 'hello there!'
68
- end
69
-
70
- def test_var_and_long_string_are_equal
71
- text = " {% if var == 'hello there!' %} true {% else %} false {% endif %} "
72
- assert_template_result ' true ', text, 'var' => 'hello there!'
73
- end
74
-
75
-
76
- def test_var_and_long_string_are_equal_backwards
77
- text = " {% if 'hello there!' == var %} true {% else %} false {% endif %} "
78
- assert_template_result ' true ', text, 'var' => 'hello there!'
79
- end
80
-
81
- #def test_is_nil
82
- # text = %| {% if var != nil %} true {% else %} false {% end %} |
83
- # @template.assigns = { 'var' => 'hello there!'}
84
- # expected = %| true |
85
- # assert_equal expected, @template.parse(text)
86
- #end
87
-
88
- def test_is_collection_empty
89
- text = ' {% if array == empty %} true {% else %} false {% endif %} '
90
- assert_template_result ' true ', text, 'array' => []
91
- end
92
-
93
- def test_is_not_collection_empty
94
- text = ' {% if array == empty %} true {% else %} false {% endif %} '
95
- assert_template_result ' false ', text, 'array' => [1,2,3]
96
- end
97
-
98
- def test_nil
99
- text = ' {% if var == nil %} true {% else %} false {% endif %} '
100
- assert_template_result ' true ', text, 'var' => nil
101
-
102
- text = ' {% if var == null %} true {% else %} false {% endif %} '
103
- assert_template_result ' true ', text, 'var' => nil
104
- end
105
-
106
- def test_not_nil
107
- text = ' {% if var != nil %} true {% else %} false {% endif %} '
108
- assert_template_result ' true ', text, 'var' => 1
109
-
110
- text = ' {% if var != null %} true {% else %} false {% endif %} '
111
- assert_template_result ' true ', text, 'var' => 1
112
- end
113
- end # StatementsTest
@@ -1,63 +0,0 @@
1
- require 'test_helper'
2
-
3
- class TableRowTest < Minitest::Test
4
- include Liquid
5
-
6
- class ArrayDrop < Liquid::Drop
7
- include Enumerable
8
-
9
- def initialize(array)
10
- @array = array
11
- end
12
-
13
- def each(&block)
14
- @array.each(&block)
15
- end
16
- end
17
-
18
- def test_table_row
19
-
20
- assert_template_result("<tr class=\"row1\">\n<td class=\"col1\"> 1 </td><td class=\"col2\"> 2 </td><td class=\"col3\"> 3 </td></tr>\n<tr class=\"row2\"><td class=\"col1\"> 4 </td><td class=\"col2\"> 5 </td><td class=\"col3\"> 6 </td></tr>\n",
21
- '{% tablerow n in numbers cols:3%} {{n}} {% endtablerow %}',
22
- 'numbers' => [1,2,3,4,5,6])
23
-
24
- assert_template_result("<tr class=\"row1\">\n</tr>\n",
25
- '{% tablerow n in numbers cols:3%} {{n}} {% endtablerow %}',
26
- 'numbers' => [])
27
- end
28
-
29
- def test_table_row_with_different_cols
30
- assert_template_result("<tr class=\"row1\">\n<td class=\"col1\"> 1 </td><td class=\"col2\"> 2 </td><td class=\"col3\"> 3 </td><td class=\"col4\"> 4 </td><td class=\"col5\"> 5 </td></tr>\n<tr class=\"row2\"><td class=\"col1\"> 6 </td></tr>\n",
31
- '{% tablerow n in numbers cols:5%} {{n}} {% endtablerow %}',
32
- 'numbers' => [1,2,3,4,5,6])
33
-
34
- end
35
-
36
- def test_table_col_counter
37
- assert_template_result("<tr class=\"row1\">\n<td class=\"col1\">1</td><td class=\"col2\">2</td></tr>\n<tr class=\"row2\"><td class=\"col1\">1</td><td class=\"col2\">2</td></tr>\n<tr class=\"row3\"><td class=\"col1\">1</td><td class=\"col2\">2</td></tr>\n",
38
- '{% tablerow n in numbers cols:2%}{{tablerowloop.col}}{% endtablerow %}',
39
- 'numbers' => [1,2,3,4,5,6])
40
- end
41
-
42
- def test_quoted_fragment
43
- assert_template_result("<tr class=\"row1\">\n<td class=\"col1\"> 1 </td><td class=\"col2\"> 2 </td><td class=\"col3\"> 3 </td></tr>\n<tr class=\"row2\"><td class=\"col1\"> 4 </td><td class=\"col2\"> 5 </td><td class=\"col3\"> 6 </td></tr>\n",
44
- "{% tablerow n in collections.frontpage cols:3%} {{n}} {% endtablerow %}",
45
- 'collections' => {'frontpage' => [1,2,3,4,5,6]})
46
- assert_template_result("<tr class=\"row1\">\n<td class=\"col1\"> 1 </td><td class=\"col2\"> 2 </td><td class=\"col3\"> 3 </td></tr>\n<tr class=\"row2\"><td class=\"col1\"> 4 </td><td class=\"col2\"> 5 </td><td class=\"col3\"> 6 </td></tr>\n",
47
- "{% tablerow n in collections['frontpage'] cols:3%} {{n}} {% endtablerow %}",
48
- 'collections' => {'frontpage' => [1,2,3,4,5,6]})
49
-
50
- end
51
-
52
- def test_enumerable_drop
53
- assert_template_result("<tr class=\"row1\">\n<td class=\"col1\"> 1 </td><td class=\"col2\"> 2 </td><td class=\"col3\"> 3 </td></tr>\n<tr class=\"row2\"><td class=\"col1\"> 4 </td><td class=\"col2\"> 5 </td><td class=\"col3\"> 6 </td></tr>\n",
54
- '{% tablerow n in numbers cols:3%} {{n}} {% endtablerow %}',
55
- 'numbers' => ArrayDrop.new([1,2,3,4,5,6]))
56
- end
57
-
58
- def test_offset_and_limit
59
- assert_template_result("<tr class=\"row1\">\n<td class=\"col1\"> 1 </td><td class=\"col2\"> 2 </td><td class=\"col3\"> 3 </td></tr>\n<tr class=\"row2\"><td class=\"col1\"> 4 </td><td class=\"col2\"> 5 </td><td class=\"col3\"> 6 </td></tr>\n",
60
- '{% tablerow n in numbers cols:3 offset:1 limit:6%} {{n}} {% endtablerow %}',
61
- 'numbers' => [0,1,2,3,4,5,6,7])
62
- end
63
- end
@@ -1,26 +0,0 @@
1
- require 'test_helper'
2
-
3
- class UnlessElseTagTest < Minitest::Test
4
- include Liquid
5
-
6
- def test_unless
7
- assert_template_result(' ',' {% unless true %} this text should not go into the output {% endunless %} ')
8
- assert_template_result(' this text should go into the output ',
9
- ' {% unless false %} this text should go into the output {% endunless %} ')
10
- assert_template_result(' you rock ?','{% unless true %} you suck {% endunless %} {% unless false %} you rock {% endunless %}?')
11
- end
12
-
13
- def test_unless_else
14
- assert_template_result(' YES ','{% unless true %} NO {% else %} YES {% endunless %}')
15
- assert_template_result(' YES ','{% unless false %} YES {% else %} NO {% endunless %}')
16
- assert_template_result(' YES ','{% unless "foo" %} NO {% else %} YES {% endunless %}')
17
- end
18
-
19
- def test_unless_in_loop
20
- assert_template_result '23', '{% for i in choices %}{% unless i %}{{ forloop.index }}{% endunless %}{% endfor %}', 'choices' => [1, nil, false]
21
- end
22
-
23
- def test_unless_else_in_loop
24
- assert_template_result ' TRUE 2 3 ', '{% for i in choices %}{% unless i %} {{ forloop.index }} {% else %} TRUE {% endunless %}{% endfor %}', 'choices' => [1, nil, false]
25
- end
26
- end # UnlessElseTest
@@ -1,182 +0,0 @@
1
- require 'test_helper'
2
-
3
- class TemplateContextDrop < Liquid::Drop
4
- def before_method(method)
5
- method
6
- end
7
-
8
- def foo
9
- 'fizzbuzz'
10
- end
11
-
12
- def baz
13
- @context.registers['lulz']
14
- end
15
- end
16
-
17
- class SomethingWithLength
18
- def length
19
- nil
20
- end
21
-
22
- liquid_methods :length
23
- end
24
-
25
- class ErroneousDrop < Liquid::Drop
26
- def bad_method
27
- raise 'ruby error in drop'
28
- end
29
- end
30
-
31
- class TemplateTest < Minitest::Test
32
- include Liquid
33
-
34
- def test_instance_assigns_persist_on_same_template_object_between_parses
35
- t = Template.new
36
- assert_equal 'from instance assigns', t.parse("{% assign foo = 'from instance assigns' %}{{ foo }}").render!
37
- assert_equal 'from instance assigns', t.parse("{{ foo }}").render!
38
- end
39
-
40
- def test_instance_assigns_persist_on_same_template_parsing_between_renders
41
- t = Template.new.parse("{{ foo }}{% assign foo = 'foo' %}{{ foo }}")
42
- assert_equal 'foo', t.render!
43
- assert_equal 'foofoo', t.render!
44
- end
45
-
46
- def test_custom_assigns_do_not_persist_on_same_template
47
- t = Template.new
48
- assert_equal 'from custom assigns', t.parse("{{ foo }}").render!('foo' => 'from custom assigns')
49
- assert_equal '', t.parse("{{ foo }}").render!
50
- end
51
-
52
- def test_custom_assigns_squash_instance_assigns
53
- t = Template.new
54
- assert_equal 'from instance assigns', t.parse("{% assign foo = 'from instance assigns' %}{{ foo }}").render!
55
- assert_equal 'from custom assigns', t.parse("{{ foo }}").render!('foo' => 'from custom assigns')
56
- end
57
-
58
- def test_persistent_assigns_squash_instance_assigns
59
- t = Template.new
60
- assert_equal 'from instance assigns', t.parse("{% assign foo = 'from instance assigns' %}{{ foo }}").render!
61
- t.assigns['foo'] = 'from persistent assigns'
62
- assert_equal 'from persistent assigns', t.parse("{{ foo }}").render!
63
- end
64
-
65
- def test_lambda_is_called_once_from_persistent_assigns_over_multiple_parses_and_renders
66
- t = Template.new
67
- t.assigns['number'] = lambda { @global ||= 0; @global += 1 }
68
- assert_equal '1', t.parse("{{number}}").render!
69
- assert_equal '1', t.parse("{{number}}").render!
70
- assert_equal '1', t.render!
71
- @global = nil
72
- end
73
-
74
- def test_lambda_is_called_once_from_custom_assigns_over_multiple_parses_and_renders
75
- t = Template.new
76
- assigns = {'number' => lambda { @global ||= 0; @global += 1 }}
77
- assert_equal '1', t.parse("{{number}}").render!(assigns)
78
- assert_equal '1', t.parse("{{number}}").render!(assigns)
79
- assert_equal '1', t.render!(assigns)
80
- @global = nil
81
- end
82
-
83
- def test_resource_limits_works_with_custom_length_method
84
- t = Template.parse("{% assign foo = bar %}")
85
- t.resource_limits = { :render_length_limit => 42 }
86
- assert_equal "", t.render!("bar" => SomethingWithLength.new)
87
- end
88
-
89
- def test_resource_limits_render_length
90
- t = Template.parse("0123456789")
91
- t.resource_limits = { :render_length_limit => 5 }
92
- assert_equal "Liquid error: Memory limits exceeded", t.render()
93
- assert t.resource_limits[:reached]
94
- t.resource_limits = { :render_length_limit => 10 }
95
- assert_equal "0123456789", t.render!()
96
- refute_nil t.resource_limits[:render_length_current]
97
- end
98
-
99
- def test_resource_limits_render_score
100
- t = Template.parse("{% for a in (1..10) %} {% for a in (1..10) %} foo {% endfor %} {% endfor %}")
101
- t.resource_limits = { :render_score_limit => 50 }
102
- assert_equal "Liquid error: Memory limits exceeded", t.render()
103
- assert t.resource_limits[:reached]
104
- t = Template.parse("{% for a in (1..100) %} foo {% endfor %}")
105
- t.resource_limits = { :render_score_limit => 50 }
106
- assert_equal "Liquid error: Memory limits exceeded", t.render()
107
- assert t.resource_limits[:reached]
108
- t.resource_limits = { :render_score_limit => 200 }
109
- assert_equal (" foo " * 100), t.render!()
110
- refute_nil t.resource_limits[:render_score_current]
111
- end
112
-
113
- def test_resource_limits_assign_score
114
- t = Template.parse("{% assign foo = 42 %}{% assign bar = 23 %}")
115
- t.resource_limits = { :assign_score_limit => 1 }
116
- assert_equal "Liquid error: Memory limits exceeded", t.render()
117
- assert t.resource_limits[:reached]
118
- t.resource_limits = { :assign_score_limit => 2 }
119
- assert_equal "", t.render!()
120
- refute_nil t.resource_limits[:assign_score_current]
121
- end
122
-
123
- def test_resource_limits_aborts_rendering_after_first_error
124
- t = Template.parse("{% for a in (1..100) %} foo1 {% endfor %} bar {% for a in (1..100) %} foo2 {% endfor %}")
125
- t.resource_limits = { :render_score_limit => 50 }
126
- assert_equal "Liquid error: Memory limits exceeded", t.render()
127
- assert t.resource_limits[:reached]
128
- end
129
-
130
- def test_resource_limits_hash_in_template_gets_updated_even_if_no_limits_are_set
131
- t = Template.parse("{% for a in (1..100) %} {% assign foo = 1 %} {% endfor %}")
132
- t.render!()
133
- assert t.resource_limits[:assign_score_current] > 0
134
- assert t.resource_limits[:render_score_current] > 0
135
- assert t.resource_limits[:render_length_current] > 0
136
- end
137
-
138
- def test_default_resource_limits_unaffected_by_render_with_context
139
- context = Context.new
140
- t = Template.parse("{% for a in (1..100) %} {% assign foo = 1 %} {% endfor %}")
141
- t.render!(context)
142
- assert context.resource_limits[:assign_score_current] > 0
143
- assert context.resource_limits[:render_score_current] > 0
144
- assert context.resource_limits[:render_length_current] > 0
145
- refute Template.default_resource_limits.key?(:assign_score_current)
146
- refute Template.default_resource_limits.key?(:render_score_current)
147
- refute Template.default_resource_limits.key?(:render_length_current)
148
- end
149
-
150
- def test_can_use_drop_as_context
151
- t = Template.new
152
- t.registers['lulz'] = 'haha'
153
- drop = TemplateContextDrop.new
154
- assert_equal 'fizzbuzz', t.parse('{{foo}}').render!(drop)
155
- assert_equal 'bar', t.parse('{{bar}}').render!(drop)
156
- assert_equal 'haha', t.parse("{{baz}}").render!(drop)
157
- end
158
-
159
- def test_render_bang_force_rethrow_errors_on_passed_context
160
- context = Context.new({'drop' => ErroneousDrop.new})
161
- t = Template.new.parse('{{ drop.bad_method }}')
162
-
163
- e = assert_raises RuntimeError do
164
- t.render!(context)
165
- end
166
- assert_equal 'ruby error in drop', e.message
167
- end
168
-
169
- def test_exception_handler_doesnt_reraise_if_it_returns_false
170
- exception = nil
171
- Template.parse("{{ 1 | divided_by: 0 }}").render({}, exception_handler: ->(e) { exception = e; false })
172
- assert exception.is_a?(ZeroDivisionError)
173
- end
174
-
175
- def test_exception_handler_does_reraise_if_it_returns_true
176
- exception = nil
177
- assert_raises(ZeroDivisionError) do
178
- Template.parse("{{ 1 | divided_by: 0 }}").render({}, exception_handler: ->(e) { exception = e; true })
179
- end
180
- assert exception.is_a?(ZeroDivisionError)
181
- end
182
- end
@@ -1,82 +0,0 @@
1
- require 'test_helper'
2
-
3
- class VariableTest < Minitest::Test
4
- include Liquid
5
-
6
- def test_simple_variable
7
- template = Template.parse(%|{{test}}|)
8
- assert_equal 'worked', template.render!('test' => 'worked')
9
- assert_equal 'worked wonderfully', template.render!('test' => 'worked wonderfully')
10
- end
11
-
12
- def test_variable_render_calls_to_liquid
13
- assert_template_result 'foobar', '{{ foo }}', 'foo' => ThingWithToLiquid.new
14
- end
15
-
16
- def test_simple_with_whitespaces
17
- template = Template.parse(%| {{ test }} |)
18
- assert_equal ' worked ', template.render!('test' => 'worked')
19
- assert_equal ' worked wonderfully ', template.render!('test' => 'worked wonderfully')
20
- end
21
-
22
- def test_ignore_unknown
23
- template = Template.parse(%|{{ test }}|)
24
- assert_equal '', template.render!
25
- end
26
-
27
- def test_hash_scoping
28
- template = Template.parse(%|{{ test.test }}|)
29
- assert_equal 'worked', template.render!('test' => {'test' => 'worked'})
30
- end
31
-
32
- def test_false_renders_as_false
33
- assert_equal 'false', Template.parse("{{ foo }}").render!('foo' => false)
34
- assert_equal 'false', Template.parse("{{ false }}").render!
35
- end
36
-
37
- def test_nil_renders_as_empty_string
38
- assert_equal '', Template.parse("{{ nil }}").render!
39
- assert_equal 'cat', Template.parse("{{ nil | append: 'cat' }}").render!
40
- end
41
-
42
- def test_preset_assigns
43
- template = Template.parse(%|{{ test }}|)
44
- template.assigns['test'] = 'worked'
45
- assert_equal 'worked', template.render!
46
- end
47
-
48
- def test_reuse_parsed_template
49
- template = Template.parse(%|{{ greeting }} {{ name }}|)
50
- template.assigns['greeting'] = 'Goodbye'
51
- assert_equal 'Hello Tobi', template.render!('greeting' => 'Hello', 'name' => 'Tobi')
52
- assert_equal 'Hello ', template.render!('greeting' => 'Hello', 'unknown' => 'Tobi')
53
- assert_equal 'Hello Brian', template.render!('greeting' => 'Hello', 'name' => 'Brian')
54
- assert_equal 'Goodbye Brian', template.render!('name' => 'Brian')
55
- assert_equal({'greeting'=>'Goodbye'}, template.assigns)
56
- end
57
-
58
- def test_assigns_not_polluted_from_template
59
- template = Template.parse(%|{{ test }}{% assign test = 'bar' %}{{ test }}|)
60
- template.assigns['test'] = 'baz'
61
- assert_equal 'bazbar', template.render!
62
- assert_equal 'bazbar', template.render!
63
- assert_equal 'foobar', template.render!('test' => 'foo')
64
- assert_equal 'bazbar', template.render!
65
- end
66
-
67
- def test_hash_with_default_proc
68
- template = Template.parse(%|Hello {{ test }}|)
69
- assigns = Hash.new { |h,k| raise "Unknown variable '#{k}'" }
70
- assigns['test'] = 'Tobi'
71
- assert_equal 'Hello Tobi', template.render!(assigns)
72
- assigns.delete('test')
73
- e = assert_raises(RuntimeError) {
74
- template.render!(assigns)
75
- }
76
- assert_equal "Unknown variable 'test'", e.message
77
- end
78
-
79
- def test_multiline_variable
80
- assert_equal 'worked', Template.parse("{{\ntest\n}}").render!('test' => 'worked')
81
- end
82
- end
data/test/test_helper.rb DELETED
@@ -1,89 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- ENV["MT_NO_EXPECTATIONS"] = "1"
4
- require 'minitest/autorun'
5
- require 'spy/integration'
6
-
7
- $:.unshift(File.join(File.expand_path(File.dirname(__FILE__)), '..', 'lib'))
8
- require 'liquid.rb'
9
-
10
- mode = :strict
11
- if env_mode = ENV['LIQUID_PARSER_MODE']
12
- puts "-- #{env_mode.upcase} ERROR MODE"
13
- mode = env_mode.to_sym
14
- end
15
- Liquid::Template.error_mode = mode
16
-
17
- if Minitest.const_defined?('Test')
18
- # We're on Minitest 5+. Nothing to do here.
19
- else
20
- # Minitest 4 doesn't have Minitest::Test yet.
21
- Minitest::Test = MiniTest::Unit::TestCase
22
- end
23
-
24
- module Minitest
25
- class Test
26
- def fixture(name)
27
- File.join(File.expand_path(File.dirname(__FILE__)), "fixtures", name)
28
- end
29
- end
30
-
31
- module Assertions
32
- include Liquid
33
-
34
- def assert_template_result(expected, template, assigns = {}, message = nil)
35
- assert_equal expected, Template.parse(template).render!(assigns), message
36
- end
37
-
38
- def assert_template_result_matches(expected, template, assigns = {}, message = nil)
39
- return assert_template_result(expected, template, assigns, message) unless expected.is_a? Regexp
40
-
41
- assert_match expected, Template.parse(template).render!(assigns), message
42
- end
43
-
44
- def assert_match_syntax_error(match, template, registers = {})
45
- exception = assert_raises(Liquid::SyntaxError) {
46
- Template.parse(template).render(assigns)
47
- }
48
- assert_match match, exception.message
49
- end
50
-
51
- def with_global_filter(*globals)
52
- original_global_strainer = Liquid::Strainer.class_variable_get(:@@global_strainer)
53
- Liquid::Strainer.class_variable_set(:@@global_strainer, Class.new(Liquid::Strainer) do
54
- @filter_methods = Set.new
55
- end)
56
- Liquid::Strainer.class_variable_get(:@@strainer_class_cache).clear
57
-
58
- globals.each do |global|
59
- Liquid::Template.register_filter(global)
60
- end
61
- yield
62
- ensure
63
- Liquid::Strainer.class_variable_get(:@@strainer_class_cache).clear
64
- Liquid::Strainer.class_variable_set(:@@global_strainer, original_global_strainer)
65
- end
66
-
67
- def with_taint_mode(mode)
68
- old_mode = Liquid::Template.taint_mode
69
- Liquid::Template.taint_mode = mode
70
- yield
71
- ensure
72
- Liquid::Template.taint_mode = old_mode
73
- end
74
-
75
- def with_error_mode(mode)
76
- old_mode = Liquid::Template.error_mode
77
- Liquid::Template.error_mode = mode
78
- yield
79
- ensure
80
- Liquid::Template.error_mode = old_mode
81
- end
82
- end
83
- end
84
-
85
- class ThingWithToLiquid
86
- def to_liquid
87
- 'foobar'
88
- end
89
- end
@@ -1,55 +0,0 @@
1
- require 'test_helper'
2
-
3
- class BlockUnitTest < Minitest::Test
4
- include Liquid
5
-
6
- def test_blankspace
7
- template = Liquid::Template.parse(" ")
8
- assert_equal [" "], template.root.nodelist
9
- end
10
-
11
- def test_variable_beginning
12
- template = Liquid::Template.parse("{{funk}} ")
13
- assert_equal 2, template.root.nodelist.size
14
- assert_equal Variable, template.root.nodelist[0].class
15
- assert_equal String, template.root.nodelist[1].class
16
- end
17
-
18
- def test_variable_end
19
- template = Liquid::Template.parse(" {{funk}}")
20
- assert_equal 2, template.root.nodelist.size
21
- assert_equal String, template.root.nodelist[0].class
22
- assert_equal Variable, template.root.nodelist[1].class
23
- end
24
-
25
- def test_variable_middle
26
- template = Liquid::Template.parse(" {{funk}} ")
27
- assert_equal 3, template.root.nodelist.size
28
- assert_equal String, template.root.nodelist[0].class
29
- assert_equal Variable, template.root.nodelist[1].class
30
- assert_equal String, template.root.nodelist[2].class
31
- end
32
-
33
- def test_variable_many_embedded_fragments
34
- template = Liquid::Template.parse(" {{funk}} {{so}} {{brother}} ")
35
- assert_equal 7, template.root.nodelist.size
36
- assert_equal [String, Variable, String, Variable, String, Variable, String],
37
- block_types(template.root.nodelist)
38
- end
39
-
40
- def test_with_block
41
- template = Liquid::Template.parse(" {% comment %} {% endcomment %} ")
42
- assert_equal [String, Comment, String], block_types(template.root.nodelist)
43
- assert_equal 3, template.root.nodelist.size
44
- end
45
-
46
- def test_with_custom_tag
47
- Liquid::Template.register_tag("testtag", Block)
48
- assert Liquid::Template.parse( "{% testtag %} {% endtesttag %}")
49
- end
50
-
51
- private
52
- def block_types(nodelist)
53
- nodelist.collect { |node| node.class }
54
- end
55
- end # VariableTest