locomotivecms-liquid 2.6.0 → 4.0.0.alpha

Sign up to get free protection for your applications and to get access to all the features.
Files changed (103) hide show
  1. checksums.yaml +4 -4
  2. data/History.md +62 -5
  3. data/README.md +4 -4
  4. data/lib/liquid.rb +16 -12
  5. data/lib/liquid/block.rb +37 -118
  6. data/lib/liquid/block_body.rb +131 -0
  7. data/lib/liquid/condition.rb +28 -17
  8. data/lib/liquid/context.rb +94 -146
  9. data/lib/liquid/document.rb +16 -10
  10. data/lib/liquid/drop.rb +8 -5
  11. data/lib/liquid/drops/inherited_block_drop.rb +24 -0
  12. data/lib/liquid/errors.rb +44 -5
  13. data/lib/liquid/expression.rb +33 -0
  14. data/lib/liquid/file_system.rb +17 -6
  15. data/lib/liquid/i18n.rb +2 -2
  16. data/lib/liquid/interrupts.rb +1 -1
  17. data/lib/liquid/lexer.rb +11 -9
  18. data/lib/liquid/locales/en.yml +2 -4
  19. data/lib/liquid/parser.rb +2 -1
  20. data/lib/liquid/parser_switching.rb +31 -0
  21. data/lib/liquid/profiler.rb +162 -0
  22. data/lib/liquid/profiler/hooks.rb +23 -0
  23. data/lib/liquid/range_lookup.rb +22 -0
  24. data/lib/liquid/resource_limits.rb +23 -0
  25. data/lib/liquid/standardfilters.rb +142 -67
  26. data/lib/liquid/strainer.rb +14 -4
  27. data/lib/liquid/tag.rb +22 -41
  28. data/lib/liquid/tags/assign.rb +15 -10
  29. data/lib/liquid/tags/break.rb +1 -1
  30. data/lib/liquid/tags/capture.rb +7 -9
  31. data/lib/liquid/tags/case.rb +28 -19
  32. data/lib/liquid/tags/comment.rb +2 -2
  33. data/lib/liquid/tags/continue.rb +1 -4
  34. data/lib/liquid/tags/cycle.rb +10 -14
  35. data/lib/liquid/tags/decrement.rb +3 -4
  36. data/lib/liquid/tags/extends.rb +28 -44
  37. data/lib/liquid/tags/for.rb +64 -42
  38. data/lib/liquid/tags/if.rb +30 -19
  39. data/lib/liquid/tags/ifchanged.rb +4 -4
  40. data/lib/liquid/tags/include.rb +30 -20
  41. data/lib/liquid/tags/increment.rb +3 -8
  42. data/lib/liquid/tags/inherited_block.rb +54 -56
  43. data/lib/liquid/tags/raw.rb +18 -10
  44. data/lib/liquid/tags/table_row.rb +72 -0
  45. data/lib/liquid/tags/unless.rb +5 -7
  46. data/lib/liquid/template.rb +113 -53
  47. data/lib/liquid/token.rb +18 -0
  48. data/lib/liquid/utils.rb +13 -4
  49. data/lib/liquid/variable.rb +68 -50
  50. data/lib/liquid/variable_lookup.rb +78 -0
  51. data/lib/liquid/version.rb +1 -1
  52. data/test/fixtures/en_locale.yml +9 -0
  53. data/test/integration/assign_test.rb +48 -0
  54. data/test/integration/blank_test.rb +106 -0
  55. data/test/integration/capture_test.rb +50 -0
  56. data/test/integration/context_test.rb +32 -0
  57. data/test/integration/document_test.rb +19 -0
  58. data/test/integration/drop_test.rb +271 -0
  59. data/test/integration/error_handling_test.rb +207 -0
  60. data/test/integration/filter_test.rb +125 -0
  61. data/test/integration/hash_ordering_test.rb +23 -0
  62. data/test/integration/output_test.rb +116 -0
  63. data/test/integration/parsing_quirks_test.rb +119 -0
  64. data/test/integration/render_profiling_test.rb +154 -0
  65. data/test/integration/security_test.rb +64 -0
  66. data/test/integration/standard_filter_test.rb +379 -0
  67. data/test/integration/tags/break_tag_test.rb +16 -0
  68. data/test/integration/tags/continue_tag_test.rb +16 -0
  69. data/test/integration/tags/extends_tag_test.rb +104 -0
  70. data/test/integration/tags/for_tag_test.rb +375 -0
  71. data/test/integration/tags/if_else_tag_test.rb +169 -0
  72. data/test/integration/tags/include_tag_test.rb +234 -0
  73. data/test/integration/tags/increment_tag_test.rb +24 -0
  74. data/test/integration/tags/raw_tag_test.rb +25 -0
  75. data/test/integration/tags/standard_tag_test.rb +297 -0
  76. data/test/integration/tags/statements_test.rb +113 -0
  77. data/test/integration/tags/table_row_test.rb +63 -0
  78. data/test/integration/tags/unless_else_tag_test.rb +26 -0
  79. data/test/integration/template_test.rb +216 -0
  80. data/test/integration/variable_test.rb +82 -0
  81. data/test/test_helper.rb +83 -0
  82. data/test/unit/block_unit_test.rb +55 -0
  83. data/test/unit/condition_unit_test.rb +149 -0
  84. data/test/unit/context_unit_test.rb +482 -0
  85. data/test/unit/file_system_unit_test.rb +35 -0
  86. data/test/unit/i18n_unit_test.rb +37 -0
  87. data/test/unit/lexer_unit_test.rb +51 -0
  88. data/test/unit/module_ex_unit_test.rb +87 -0
  89. data/test/unit/parser_unit_test.rb +82 -0
  90. data/test/unit/regexp_unit_test.rb +44 -0
  91. data/test/unit/strainer_unit_test.rb +71 -0
  92. data/test/unit/tag_unit_test.rb +16 -0
  93. data/test/unit/tags/case_tag_unit_test.rb +10 -0
  94. data/test/unit/tags/for_tag_unit_test.rb +13 -0
  95. data/test/unit/tags/if_tag_unit_test.rb +8 -0
  96. data/test/unit/template_unit_test.rb +70 -0
  97. data/test/unit/tokenizer_unit_test.rb +38 -0
  98. data/test/unit/variable_unit_test.rb +150 -0
  99. metadata +144 -15
  100. data/lib/extras/liquid_view.rb +0 -51
  101. data/lib/liquid/htmltags.rb +0 -74
  102. data/lib/liquid/tags/default_content.rb +0 -21
  103. data/lib/locomotivecms-liquid.rb +0 -1
@@ -0,0 +1,32 @@
1
+ require 'test_helper'
2
+
3
+ class ContextTest < Minitest::Test
4
+ include Liquid
5
+
6
+ def test_override_global_filter
7
+ global = Module.new do
8
+ def notice(output)
9
+ "Global #{output}"
10
+ end
11
+ end
12
+
13
+ local = Module.new do
14
+ def notice(output)
15
+ "Local #{output}"
16
+ end
17
+ end
18
+
19
+ with_global_filter(global) do
20
+ assert_equal 'Global test', Template.parse("{{'test' | notice }}").render!
21
+ assert_equal 'Local test', Template.parse("{{'test' | notice }}").render!({}, :filters => [local])
22
+ end
23
+ end
24
+
25
+ def test_has_key_will_not_add_an_error_for_missing_keys
26
+ with_error_mode :strict do
27
+ context = Context.new
28
+ context.has_key?('unknown')
29
+ assert_empty context.errors
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,19 @@
1
+ require 'test_helper'
2
+
3
+ class DocumentTest < Minitest::Test
4
+ include Liquid
5
+
6
+ def test_unexpected_outer_tag
7
+ exc = assert_raises(SyntaxError) do
8
+ Template.parse("{% else %}")
9
+ end
10
+ assert_equal exc.message, "Liquid syntax error: Unexpected outer 'else' tag"
11
+ end
12
+
13
+ def test_unknown_tag
14
+ exc = assert_raises(SyntaxError) do
15
+ Template.parse("{% foo %}")
16
+ end
17
+ assert_equal exc.message, "Liquid syntax error: Unknown tag 'foo'"
18
+ end
19
+ end
@@ -0,0 +1,271 @@
1
+ require 'test_helper'
2
+
3
+ class ContextDrop < Liquid::Drop
4
+ def scopes
5
+ @context.scopes.size
6
+ end
7
+
8
+ def scopes_as_array
9
+ (1..@context.scopes.size).to_a
10
+ end
11
+
12
+ def loop_pos
13
+ @context['forloop.index']
14
+ end
15
+
16
+ def before_method(method)
17
+ return @context[method]
18
+ end
19
+ end
20
+
21
+ class ProductDrop < Liquid::Drop
22
+
23
+ class TextDrop < Liquid::Drop
24
+ def array
25
+ ['text1', 'text2']
26
+ end
27
+
28
+ def text
29
+ 'text1'
30
+ end
31
+ end
32
+
33
+ class CatchallDrop < Liquid::Drop
34
+ def before_method(method)
35
+ return 'method: ' << method.to_s
36
+ end
37
+ end
38
+
39
+ def texts
40
+ TextDrop.new
41
+ end
42
+
43
+ def catchall
44
+ CatchallDrop.new
45
+ end
46
+
47
+ def context
48
+ ContextDrop.new
49
+ end
50
+
51
+ def user_input
52
+ "foo".taint
53
+ end
54
+
55
+ protected
56
+ def callmenot
57
+ "protected"
58
+ end
59
+ end
60
+
61
+ class EnumerableDrop < Liquid::Drop
62
+ def before_method(method)
63
+ method
64
+ end
65
+
66
+ def size
67
+ 3
68
+ end
69
+
70
+ def first
71
+ 1
72
+ end
73
+
74
+ def count
75
+ 3
76
+ end
77
+
78
+ def min
79
+ 1
80
+ end
81
+
82
+ def max
83
+ 3
84
+ end
85
+
86
+ def each
87
+ yield 1
88
+ yield 2
89
+ yield 3
90
+ end
91
+ end
92
+
93
+ class RealEnumerableDrop < Liquid::Drop
94
+ include Enumerable
95
+
96
+ def before_method(method)
97
+ method
98
+ end
99
+
100
+ def each
101
+ yield 1
102
+ yield 2
103
+ yield 3
104
+ end
105
+ end
106
+
107
+ class DropsTest < Minitest::Test
108
+ include Liquid
109
+
110
+ def test_product_drop
111
+ tpl = Liquid::Template.parse(' ')
112
+ assert_equal ' ', tpl.render!('product' => ProductDrop.new)
113
+ end
114
+
115
+ def test_rendering_raises_on_tainted_attr
116
+ with_taint_mode(:error) do
117
+ tpl = Liquid::Template.parse('{{ product.user_input }}')
118
+ assert_raises TaintedError do
119
+ tpl.render!('product' => ProductDrop.new)
120
+ end
121
+ end
122
+ end
123
+
124
+ def test_rendering_warns_on_tainted_attr
125
+ with_taint_mode(:warn) do
126
+ tpl = Liquid::Template.parse('{{ product.user_input }}')
127
+ tpl.render!('product' => ProductDrop.new)
128
+ assert_match /tainted/, tpl.warnings.first
129
+ end
130
+ end
131
+
132
+ def test_rendering_doesnt_raise_on_escaped_tainted_attr
133
+ with_taint_mode(:error) do
134
+ tpl = Liquid::Template.parse('{{ product.user_input | escape }}')
135
+ tpl.render!('product' => ProductDrop.new)
136
+ end
137
+ end
138
+
139
+ def test_drop_does_only_respond_to_whitelisted_methods
140
+ assert_equal "", Liquid::Template.parse("{{ product.inspect }}").render!('product' => ProductDrop.new)
141
+ assert_equal "", Liquid::Template.parse("{{ product.pretty_inspect }}").render!('product' => ProductDrop.new)
142
+ assert_equal "", Liquid::Template.parse("{{ product.whatever }}").render!('product' => ProductDrop.new)
143
+ assert_equal "", Liquid::Template.parse('{{ product | map: "inspect" }}').render!('product' => ProductDrop.new)
144
+ assert_equal "", Liquid::Template.parse('{{ product | map: "pretty_inspect" }}').render!('product' => ProductDrop.new)
145
+ assert_equal "", Liquid::Template.parse('{{ product | map: "whatever" }}').render!('product' => ProductDrop.new)
146
+ end
147
+
148
+ def test_drops_respond_to_to_liquid
149
+ assert_equal "text1", Liquid::Template.parse("{{ product.to_liquid.texts.text }}").render!('product' => ProductDrop.new)
150
+ assert_equal "text1", Liquid::Template.parse('{{ product | map: "to_liquid" | map: "texts" | map: "text" }}').render!('product' => ProductDrop.new)
151
+ end
152
+
153
+ def test_text_drop
154
+ output = Liquid::Template.parse( ' {{ product.texts.text }} ' ).render!('product' => ProductDrop.new)
155
+ assert_equal ' text1 ', output
156
+ end
157
+
158
+ def test_unknown_method
159
+ output = Liquid::Template.parse( ' {{ product.catchall.unknown }} ' ).render!('product' => ProductDrop.new)
160
+ assert_equal ' method: unknown ', output
161
+ end
162
+
163
+ def test_integer_argument_drop
164
+ output = Liquid::Template.parse( ' {{ product.catchall[8] }} ' ).render!('product' => ProductDrop.new)
165
+ assert_equal ' method: 8 ', output
166
+ end
167
+
168
+ def test_text_array_drop
169
+ output = Liquid::Template.parse( '{% for text in product.texts.array %} {{text}} {% endfor %}' ).render!('product' => ProductDrop.new)
170
+ assert_equal ' text1 text2 ', output
171
+ end
172
+
173
+ def test_context_drop
174
+ output = Liquid::Template.parse( ' {{ context.bar }} ' ).render!('context' => ContextDrop.new, 'bar' => "carrot")
175
+ assert_equal ' carrot ', output
176
+ end
177
+
178
+ def test_nested_context_drop
179
+ output = Liquid::Template.parse( ' {{ product.context.foo }} ' ).render!('product' => ProductDrop.new, 'foo' => "monkey")
180
+ assert_equal ' monkey ', output
181
+ end
182
+
183
+ def test_protected
184
+ output = Liquid::Template.parse( ' {{ product.callmenot }} ' ).render!('product' => ProductDrop.new)
185
+ assert_equal ' ', output
186
+ end
187
+
188
+ def test_object_methods_not_allowed
189
+ [:dup, :clone, :singleton_class, :eval, :class_eval, :inspect].each do |method|
190
+ output = Liquid::Template.parse(" {{ product.#{method} }} ").render!('product' => ProductDrop.new)
191
+ assert_equal ' ', output
192
+ end
193
+ end
194
+
195
+ def test_scope
196
+ assert_equal '1', Liquid::Template.parse( '{{ context.scopes }}' ).render!('context' => ContextDrop.new)
197
+ assert_equal '2', Liquid::Template.parse( '{%for i in dummy%}{{ context.scopes }}{%endfor%}' ).render!('context' => ContextDrop.new, 'dummy' => [1])
198
+ assert_equal '3', Liquid::Template.parse( '{%for i in dummy%}{%for i in dummy%}{{ context.scopes }}{%endfor%}{%endfor%}' ).render!('context' => ContextDrop.new, 'dummy' => [1])
199
+ end
200
+
201
+ def test_scope_though_proc
202
+ assert_equal '1', Liquid::Template.parse( '{{ s }}' ).render!('context' => ContextDrop.new, 's' => Proc.new{|c| c['context.scopes'] })
203
+ assert_equal '2', Liquid::Template.parse( '{%for i in dummy%}{{ s }}{%endfor%}' ).render!('context' => ContextDrop.new, 's' => Proc.new{|c| c['context.scopes'] }, 'dummy' => [1])
204
+ assert_equal '3', Liquid::Template.parse( '{%for i in dummy%}{%for i in dummy%}{{ s }}{%endfor%}{%endfor%}' ).render!('context' => ContextDrop.new, 's' => Proc.new{|c| c['context.scopes'] }, 'dummy' => [1])
205
+ end
206
+
207
+ def test_scope_with_assigns
208
+ assert_equal 'variable', Liquid::Template.parse( '{% assign a = "variable"%}{{a}}' ).render!('context' => ContextDrop.new)
209
+ assert_equal 'variable', Liquid::Template.parse( '{% assign a = "variable"%}{%for i in dummy%}{{a}}{%endfor%}' ).render!('context' => ContextDrop.new, 'dummy' => [1])
210
+ assert_equal 'test', Liquid::Template.parse( '{% assign header_gif = "test"%}{{header_gif}}' ).render!('context' => ContextDrop.new)
211
+ assert_equal 'test', Liquid::Template.parse( "{% assign header_gif = 'test'%}{{header_gif}}" ).render!('context' => ContextDrop.new)
212
+ end
213
+
214
+ def test_scope_from_tags
215
+ assert_equal '1', Liquid::Template.parse( '{% for i in context.scopes_as_array %}{{i}}{% endfor %}' ).render!('context' => ContextDrop.new, 'dummy' => [1])
216
+ assert_equal '12', Liquid::Template.parse( '{%for a in dummy%}{% for i in context.scopes_as_array %}{{i}}{% endfor %}{% endfor %}' ).render!('context' => ContextDrop.new, 'dummy' => [1])
217
+ assert_equal '123', Liquid::Template.parse( '{%for a in dummy%}{%for a in dummy%}{% for i in context.scopes_as_array %}{{i}}{% endfor %}{% endfor %}{% endfor %}' ).render!('context' => ContextDrop.new, 'dummy' => [1])
218
+ end
219
+
220
+ def test_access_context_from_drop
221
+ assert_equal '123', Liquid::Template.parse( '{%for a in dummy%}{{ context.loop_pos }}{% endfor %}' ).render!('context' => ContextDrop.new, 'dummy' => [1,2,3])
222
+ end
223
+
224
+ def test_enumerable_drop
225
+ assert_equal '123', Liquid::Template.parse( '{% for c in collection %}{{c}}{% endfor %}').render!('collection' => EnumerableDrop.new)
226
+ end
227
+
228
+ def test_enumerable_drop_size
229
+ assert_equal '3', Liquid::Template.parse( '{{collection.size}}').render!('collection' => EnumerableDrop.new)
230
+ end
231
+
232
+ def test_enumerable_drop_will_invoke_before_method_for_clashing_method_names
233
+ ["select", "each", "map", "cycle"].each do |method|
234
+ assert_equal method.to_s, Liquid::Template.parse("{{collection.#{method}}}").render!('collection' => EnumerableDrop.new)
235
+ assert_equal method.to_s, Liquid::Template.parse("{{collection[\"#{method}\"]}}").render!('collection' => EnumerableDrop.new)
236
+ assert_equal method.to_s, Liquid::Template.parse("{{collection.#{method}}}").render!('collection' => RealEnumerableDrop.new)
237
+ assert_equal method.to_s, Liquid::Template.parse("{{collection[\"#{method}\"]}}").render!('collection' => RealEnumerableDrop.new)
238
+ end
239
+ end
240
+
241
+ def test_some_enumerable_methods_still_get_invoked
242
+ [ :count, :max ].each do |method|
243
+ assert_equal "3", Liquid::Template.parse("{{collection.#{method}}}").render!('collection' => RealEnumerableDrop.new)
244
+ assert_equal "3", Liquid::Template.parse("{{collection[\"#{method}\"]}}").render!('collection' => RealEnumerableDrop.new)
245
+ assert_equal "3", Liquid::Template.parse("{{collection.#{method}}}").render!('collection' => EnumerableDrop.new)
246
+ assert_equal "3", Liquid::Template.parse("{{collection[\"#{method}\"]}}").render!('collection' => EnumerableDrop.new)
247
+ end
248
+
249
+ assert_equal "yes", Liquid::Template.parse("{% if collection contains 3 %}yes{% endif %}").render!('collection' => RealEnumerableDrop.new)
250
+
251
+ [ :min, :first ].each do |method|
252
+ assert_equal "1", Liquid::Template.parse("{{collection.#{method}}}").render!('collection' => RealEnumerableDrop.new)
253
+ assert_equal "1", Liquid::Template.parse("{{collection[\"#{method}\"]}}").render!('collection' => RealEnumerableDrop.new)
254
+ assert_equal "1", Liquid::Template.parse("{{collection.#{method}}}").render!('collection' => EnumerableDrop.new)
255
+ assert_equal "1", Liquid::Template.parse("{{collection[\"#{method}\"]}}").render!('collection' => EnumerableDrop.new)
256
+ end
257
+ end
258
+
259
+ def test_empty_string_value_access
260
+ assert_equal '', Liquid::Template.parse('{{ product[value] }}').render!('product' => ProductDrop.new, 'value' => '')
261
+ end
262
+
263
+ def test_nil_value_access
264
+ assert_equal '', Liquid::Template.parse('{{ product[value] }}').render!('product' => ProductDrop.new, 'value' => nil)
265
+ end
266
+
267
+ def test_default_to_s_on_drops
268
+ assert_equal 'ProductDrop', Liquid::Template.parse("{{ product }}").render!('product' => ProductDrop.new)
269
+ assert_equal 'EnumerableDrop', Liquid::Template.parse('{{ collection }}').render!('collection' => EnumerableDrop.new)
270
+ end
271
+ end # DropsTest
@@ -0,0 +1,207 @@
1
+ require 'test_helper'
2
+
3
+ class ErrorDrop < Liquid::Drop
4
+ def standard_error
5
+ raise Liquid::StandardError, 'standard error'
6
+ end
7
+
8
+ def argument_error
9
+ raise Liquid::ArgumentError, 'argument error'
10
+ end
11
+
12
+ def syntax_error
13
+ raise Liquid::SyntaxError, 'syntax error'
14
+ end
15
+
16
+ def exception
17
+ raise Exception, 'exception'
18
+ end
19
+
20
+ end
21
+
22
+ class ErrorHandlingTest < Minitest::Test
23
+ include Liquid
24
+
25
+ def test_templates_parsed_with_line_numbers_renders_them_in_errors
26
+ template = <<-LIQUID
27
+ Hello,
28
+
29
+ {{ errors.standard_error }} will raise a standard error.
30
+
31
+ Bla bla test.
32
+
33
+ {{ errors.syntax_error }} will raise a syntax error.
34
+
35
+ This is an argument error: {{ errors.argument_error }}
36
+
37
+ Bla.
38
+ LIQUID
39
+
40
+ expected = <<-TEXT
41
+ Hello,
42
+
43
+ Liquid error (line 3): standard error will raise a standard error.
44
+
45
+ Bla bla test.
46
+
47
+ Liquid syntax error (line 7): syntax error will raise a syntax error.
48
+
49
+ This is an argument error: Liquid error (line 9): argument error
50
+
51
+ Bla.
52
+ TEXT
53
+
54
+ output = Liquid::Template.parse(template, line_numbers: true).render('errors' => ErrorDrop.new)
55
+ assert_equal expected, output
56
+ end
57
+
58
+ def test_standard_error
59
+ template = Liquid::Template.parse( ' {{ errors.standard_error }} ' )
60
+ assert_equal ' Liquid error: standard error ', template.render('errors' => ErrorDrop.new)
61
+
62
+ assert_equal 1, template.errors.size
63
+ assert_equal StandardError, template.errors.first.class
64
+ end
65
+
66
+ def test_syntax
67
+ template = Liquid::Template.parse( ' {{ errors.syntax_error }} ' )
68
+ assert_equal ' Liquid syntax error: syntax error ', template.render('errors' => ErrorDrop.new)
69
+
70
+ assert_equal 1, template.errors.size
71
+ assert_equal SyntaxError, template.errors.first.class
72
+ end
73
+
74
+ def test_argument
75
+ template = Liquid::Template.parse( ' {{ errors.argument_error }} ' )
76
+ assert_equal ' Liquid error: argument error ', template.render('errors' => ErrorDrop.new)
77
+
78
+ assert_equal 1, template.errors.size
79
+ assert_equal ArgumentError, template.errors.first.class
80
+ end
81
+
82
+ def test_missing_endtag_parse_time_error
83
+ assert_raises(Liquid::SyntaxError) do
84
+ Liquid::Template.parse(' {% for a in b %} ... ')
85
+ end
86
+ end
87
+
88
+ def test_unrecognized_operator
89
+ with_error_mode(:strict) do
90
+ assert_raises(SyntaxError) do
91
+ Liquid::Template.parse(' {% if 1 =! 2 %}ok{% endif %} ')
92
+ end
93
+ end
94
+ end
95
+
96
+ def test_lax_unrecognized_operator
97
+ template = Liquid::Template.parse(' {% if 1 =! 2 %}ok{% endif %} ', :error_mode => :lax)
98
+ assert_equal ' Liquid error: Unknown operator =! ', template.render
99
+ assert_equal 1, template.errors.size
100
+ assert_equal Liquid::ArgumentError, template.errors.first.class
101
+ end
102
+
103
+ def test_with_line_numbers_adds_numbers_to_parser_errors
104
+ err = assert_raises(SyntaxError) do
105
+ template = Liquid::Template.parse(%q{
106
+ foobar
107
+
108
+ {% "cat" | foobar %}
109
+
110
+ bla
111
+ },
112
+ :line_numbers => true
113
+ )
114
+ end
115
+
116
+ assert_match /Liquid syntax error \(line 4\)/, err.message
117
+ end
118
+
119
+ def test_parsing_warn_with_line_numbers_adds_numbers_to_lexer_errors
120
+ template = Liquid::Template.parse(%q{
121
+ foobar
122
+
123
+ {% if 1 =! 2 %}ok{% endif %}
124
+
125
+ bla
126
+ },
127
+ :error_mode => :warn,
128
+ :line_numbers => true
129
+ )
130
+
131
+ assert_equal ['Liquid syntax error (line 4): Unexpected character = in "1 =! 2"'],
132
+ template.warnings.map(&:message)
133
+ end
134
+
135
+ def test_parsing_strict_with_line_numbers_adds_numbers_to_lexer_errors
136
+ err = assert_raises(SyntaxError) do
137
+ Liquid::Template.parse(%q{
138
+ foobar
139
+
140
+ {% if 1 =! 2 %}ok{% endif %}
141
+
142
+ bla
143
+ },
144
+ :error_mode => :strict,
145
+ :line_numbers => true
146
+ )
147
+ end
148
+
149
+ assert_equal 'Liquid syntax error (line 4): Unexpected character = in "1 =! 2"', err.message
150
+ end
151
+
152
+ def test_syntax_errors_in_nested_blocks_have_correct_line_number
153
+ err = assert_raises(SyntaxError) do
154
+ Liquid::Template.parse(%q{
155
+ foobar
156
+
157
+ {% if 1 != 2 %}
158
+ {% foo %}
159
+ {% endif %}
160
+
161
+ bla
162
+ },
163
+ :line_numbers => true
164
+ )
165
+ end
166
+
167
+ assert_equal "Liquid syntax error (line 5): Unknown tag 'foo'", err.message
168
+ end
169
+
170
+ def test_strict_error_messages
171
+ err = assert_raises(SyntaxError) do
172
+ Liquid::Template.parse(' {% if 1 =! 2 %}ok{% endif %} ', :error_mode => :strict)
173
+ end
174
+ assert_equal 'Liquid syntax error: Unexpected character = in "1 =! 2"', err.message
175
+
176
+ err = assert_raises(SyntaxError) do
177
+ Liquid::Template.parse('{{%%%}}', :error_mode => :strict)
178
+ end
179
+ assert_equal 'Liquid syntax error: Unexpected character % in "{{%%%}}"', err.message
180
+ end
181
+
182
+ def test_warnings
183
+ template = Liquid::Template.parse('{% if ~~~ %}{{%%%}}{% else %}{{ hello. }}{% endif %}', :error_mode => :warn)
184
+ assert_equal 3, template.warnings.size
185
+ assert_equal 'Unexpected character ~ in "~~~"', template.warnings[0].to_s(false)
186
+ assert_equal 'Unexpected character % in "{{%%%}}"', template.warnings[1].to_s(false)
187
+ assert_equal 'Expected id but found end_of_string in "{{ hello. }}"', template.warnings[2].to_s(false)
188
+ assert_equal '', template.render
189
+ end
190
+
191
+ def test_warning_line_numbers
192
+ template = Liquid::Template.parse("{% if ~~~ %}\n{{%%%}}{% else %}\n{{ hello. }}{% endif %}", :error_mode => :warn, :line_numbers => true)
193
+ assert_equal 'Liquid syntax error (line 1): Unexpected character ~ in "~~~"', template.warnings[0].message
194
+ assert_equal 'Liquid syntax error (line 2): Unexpected character % in "{{%%%}}"', template.warnings[1].message
195
+ assert_equal 'Liquid syntax error (line 3): Expected id but found end_of_string in "{{ hello. }}"', template.warnings[2].message
196
+ assert_equal 3, template.warnings.size
197
+ assert_equal [1,2,3], template.warnings.map(&:line_number)
198
+ end
199
+
200
+ # Liquid should not catch Exceptions that are not subclasses of StandardError, like Interrupt and NoMemoryError
201
+ def test_exceptions_propagate
202
+ assert_raises Exception do
203
+ template = Liquid::Template.parse('{{ errors.exception }}')
204
+ template.render('errors' => ErrorDrop.new)
205
+ end
206
+ end
207
+ end