liquid 4.0.0 → 5.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (117) hide show
  1. checksums.yaml +5 -5
  2. data/History.md +235 -2
  3. data/README.md +58 -8
  4. data/lib/liquid/block.rb +51 -20
  5. data/lib/liquid/block_body.rb +216 -82
  6. data/lib/liquid/condition.rb +83 -32
  7. data/lib/liquid/const.rb +8 -0
  8. data/lib/liquid/context.rb +130 -59
  9. data/lib/liquid/deprecations.rb +22 -0
  10. data/lib/liquid/document.rb +47 -9
  11. data/lib/liquid/drop.rb +8 -2
  12. data/lib/liquid/environment.rb +159 -0
  13. data/lib/liquid/errors.rb +23 -20
  14. data/lib/liquid/expression.rb +114 -31
  15. data/lib/liquid/extensions.rb +8 -0
  16. data/lib/liquid/file_system.rb +6 -4
  17. data/lib/liquid/forloop_drop.rb +51 -4
  18. data/lib/liquid/i18n.rb +5 -3
  19. data/lib/liquid/interrupts.rb +3 -1
  20. data/lib/liquid/lexer.rb +165 -39
  21. data/lib/liquid/locales/en.yml +16 -6
  22. data/lib/liquid/parse_context.rb +62 -7
  23. data/lib/liquid/parse_tree_visitor.rb +42 -0
  24. data/lib/liquid/parser.rb +31 -19
  25. data/lib/liquid/parser_switching.rb +42 -3
  26. data/lib/liquid/partial_cache.rb +33 -0
  27. data/lib/liquid/profiler/hooks.rb +26 -14
  28. data/lib/liquid/profiler.rb +67 -86
  29. data/lib/liquid/range_lookup.rb +26 -6
  30. data/lib/liquid/registers.rb +51 -0
  31. data/lib/liquid/resource_limits.rb +47 -8
  32. data/lib/liquid/snippet_drop.rb +22 -0
  33. data/lib/liquid/standardfilters.rb +813 -137
  34. data/lib/liquid/strainer_template.rb +62 -0
  35. data/lib/liquid/tablerowloop_drop.rb +64 -5
  36. data/lib/liquid/tag/disableable.rb +22 -0
  37. data/lib/liquid/tag/disabler.rb +13 -0
  38. data/lib/liquid/tag.rb +42 -6
  39. data/lib/liquid/tags/assign.rb +46 -18
  40. data/lib/liquid/tags/break.rb +15 -4
  41. data/lib/liquid/tags/capture.rb +26 -18
  42. data/lib/liquid/tags/case.rb +108 -32
  43. data/lib/liquid/tags/comment.rb +76 -4
  44. data/lib/liquid/tags/continue.rb +15 -13
  45. data/lib/liquid/tags/cycle.rb +117 -34
  46. data/lib/liquid/tags/decrement.rb +30 -23
  47. data/lib/liquid/tags/doc.rb +81 -0
  48. data/lib/liquid/tags/echo.rb +39 -0
  49. data/lib/liquid/tags/for.rb +109 -96
  50. data/lib/liquid/tags/if.rb +72 -41
  51. data/lib/liquid/tags/ifchanged.rb +10 -11
  52. data/lib/liquid/tags/include.rb +89 -63
  53. data/lib/liquid/tags/increment.rb +31 -20
  54. data/lib/liquid/tags/inline_comment.rb +28 -0
  55. data/lib/liquid/tags/raw.rb +25 -13
  56. data/lib/liquid/tags/render.rb +151 -0
  57. data/lib/liquid/tags/snippet.rb +45 -0
  58. data/lib/liquid/tags/table_row.rb +104 -21
  59. data/lib/liquid/tags/unless.rb +37 -20
  60. data/lib/liquid/tags.rb +51 -0
  61. data/lib/liquid/template.rb +90 -106
  62. data/lib/liquid/template_factory.rb +9 -0
  63. data/lib/liquid/tokenizer.rb +143 -13
  64. data/lib/liquid/usage.rb +8 -0
  65. data/lib/liquid/utils.rb +114 -5
  66. data/lib/liquid/variable.rb +119 -45
  67. data/lib/liquid/variable_lookup.rb +35 -13
  68. data/lib/liquid/version.rb +3 -1
  69. data/lib/liquid.rb +31 -18
  70. metadata +56 -107
  71. data/lib/liquid/strainer.rb +0 -66
  72. data/test/fixtures/en_locale.yml +0 -9
  73. data/test/integration/assign_test.rb +0 -48
  74. data/test/integration/blank_test.rb +0 -106
  75. data/test/integration/capture_test.rb +0 -50
  76. data/test/integration/context_test.rb +0 -32
  77. data/test/integration/document_test.rb +0 -19
  78. data/test/integration/drop_test.rb +0 -273
  79. data/test/integration/error_handling_test.rb +0 -260
  80. data/test/integration/filter_test.rb +0 -178
  81. data/test/integration/hash_ordering_test.rb +0 -23
  82. data/test/integration/output_test.rb +0 -123
  83. data/test/integration/parsing_quirks_test.rb +0 -118
  84. data/test/integration/render_profiling_test.rb +0 -154
  85. data/test/integration/security_test.rb +0 -66
  86. data/test/integration/standard_filter_test.rb +0 -535
  87. data/test/integration/tags/break_tag_test.rb +0 -15
  88. data/test/integration/tags/continue_tag_test.rb +0 -15
  89. data/test/integration/tags/for_tag_test.rb +0 -410
  90. data/test/integration/tags/if_else_tag_test.rb +0 -188
  91. data/test/integration/tags/include_tag_test.rb +0 -238
  92. data/test/integration/tags/increment_tag_test.rb +0 -23
  93. data/test/integration/tags/raw_tag_test.rb +0 -31
  94. data/test/integration/tags/standard_tag_test.rb +0 -296
  95. data/test/integration/tags/statements_test.rb +0 -111
  96. data/test/integration/tags/table_row_test.rb +0 -64
  97. data/test/integration/tags/unless_else_tag_test.rb +0 -26
  98. data/test/integration/template_test.rb +0 -323
  99. data/test/integration/trim_mode_test.rb +0 -525
  100. data/test/integration/variable_test.rb +0 -92
  101. data/test/test_helper.rb +0 -117
  102. data/test/unit/block_unit_test.rb +0 -58
  103. data/test/unit/condition_unit_test.rb +0 -158
  104. data/test/unit/context_unit_test.rb +0 -483
  105. data/test/unit/file_system_unit_test.rb +0 -35
  106. data/test/unit/i18n_unit_test.rb +0 -37
  107. data/test/unit/lexer_unit_test.rb +0 -51
  108. data/test/unit/parser_unit_test.rb +0 -82
  109. data/test/unit/regexp_unit_test.rb +0 -44
  110. data/test/unit/strainer_unit_test.rb +0 -148
  111. data/test/unit/tag_unit_test.rb +0 -21
  112. data/test/unit/tags/case_tag_unit_test.rb +0 -10
  113. data/test/unit/tags/for_tag_unit_test.rb +0 -13
  114. data/test/unit/tags/if_tag_unit_test.rb +0 -8
  115. data/test/unit/template_unit_test.rb +0 -78
  116. data/test/unit/tokenizer_unit_test.rb +0 -55
  117. data/test/unit/variable_unit_test.rb +0 -162
@@ -1,535 +0,0 @@
1
- # encoding: utf-8
2
-
3
- require 'test_helper'
4
-
5
- class Filters
6
- include Liquid::StandardFilters
7
- end
8
-
9
- class TestThing
10
- attr_reader :foo
11
-
12
- def initialize
13
- @foo = 0
14
- end
15
-
16
- def to_s
17
- "woot: #{@foo}"
18
- end
19
-
20
- def [](whatever)
21
- to_s
22
- end
23
-
24
- def to_liquid
25
- @foo += 1
26
- self
27
- end
28
- end
29
-
30
- class TestDrop < Liquid::Drop
31
- def test
32
- "testfoo"
33
- end
34
- end
35
-
36
- class TestEnumerable < Liquid::Drop
37
- include Enumerable
38
-
39
- def each(&block)
40
- [ { "foo" => 1, "bar" => 2 }, { "foo" => 2, "bar" => 1 }, { "foo" => 3, "bar" => 3 } ].each(&block)
41
- end
42
- end
43
-
44
- class NumberLikeThing < Liquid::Drop
45
- def initialize(amount)
46
- @amount = amount
47
- end
48
-
49
- def to_number
50
- @amount
51
- end
52
- end
53
-
54
- class StandardFiltersTest < Minitest::Test
55
- include Liquid
56
-
57
- def setup
58
- @filters = Filters.new
59
- end
60
-
61
- def test_size
62
- assert_equal 3, @filters.size([1, 2, 3])
63
- assert_equal 0, @filters.size([])
64
- assert_equal 0, @filters.size(nil)
65
- end
66
-
67
- def test_downcase
68
- assert_equal 'testing', @filters.downcase("Testing")
69
- assert_equal '', @filters.downcase(nil)
70
- end
71
-
72
- def test_upcase
73
- assert_equal 'TESTING', @filters.upcase("Testing")
74
- assert_equal '', @filters.upcase(nil)
75
- end
76
-
77
- def test_slice
78
- assert_equal 'oob', @filters.slice('foobar', 1, 3)
79
- assert_equal 'oobar', @filters.slice('foobar', 1, 1000)
80
- assert_equal '', @filters.slice('foobar', 1, 0)
81
- assert_equal 'o', @filters.slice('foobar', 1, 1)
82
- assert_equal 'bar', @filters.slice('foobar', 3, 3)
83
- assert_equal 'ar', @filters.slice('foobar', -2, 2)
84
- assert_equal 'ar', @filters.slice('foobar', -2, 1000)
85
- assert_equal 'r', @filters.slice('foobar', -1)
86
- assert_equal '', @filters.slice(nil, 0)
87
- assert_equal '', @filters.slice('foobar', 100, 10)
88
- assert_equal '', @filters.slice('foobar', -100, 10)
89
- assert_equal 'oob', @filters.slice('foobar', '1', '3')
90
- assert_raises(Liquid::ArgumentError) do
91
- @filters.slice('foobar', nil)
92
- end
93
- assert_raises(Liquid::ArgumentError) do
94
- @filters.slice('foobar', 0, "")
95
- end
96
- end
97
-
98
- def test_slice_on_arrays
99
- input = 'foobar'.split(//)
100
- assert_equal %w(o o b), @filters.slice(input, 1, 3)
101
- assert_equal %w(o o b a r), @filters.slice(input, 1, 1000)
102
- assert_equal %w(), @filters.slice(input, 1, 0)
103
- assert_equal %w(o), @filters.slice(input, 1, 1)
104
- assert_equal %w(b a r), @filters.slice(input, 3, 3)
105
- assert_equal %w(a r), @filters.slice(input, -2, 2)
106
- assert_equal %w(a r), @filters.slice(input, -2, 1000)
107
- assert_equal %w(r), @filters.slice(input, -1)
108
- assert_equal %w(), @filters.slice(input, 100, 10)
109
- assert_equal %w(), @filters.slice(input, -100, 10)
110
- end
111
-
112
- def test_truncate
113
- assert_equal '1234...', @filters.truncate('1234567890', 7)
114
- assert_equal '1234567890', @filters.truncate('1234567890', 20)
115
- assert_equal '...', @filters.truncate('1234567890', 0)
116
- assert_equal '1234567890', @filters.truncate('1234567890')
117
- assert_equal "测试...", @filters.truncate("测试测试测试测试", 5)
118
- assert_equal '12341', @filters.truncate("1234567890", 5, 1)
119
- end
120
-
121
- def test_split
122
- assert_equal ['12', '34'], @filters.split('12~34', '~')
123
- assert_equal ['A? ', ' ,Z'], @filters.split('A? ~ ~ ~ ,Z', '~ ~ ~')
124
- assert_equal ['A?Z'], @filters.split('A?Z', '~')
125
- assert_equal [], @filters.split(nil, ' ')
126
- assert_equal ['A', 'Z'], @filters.split('A1Z', 1)
127
- end
128
-
129
- def test_escape
130
- assert_equal '&lt;strong&gt;', @filters.escape('<strong>')
131
- assert_equal nil, @filters.escape(nil)
132
- assert_equal '&lt;strong&gt;', @filters.h('<strong>')
133
- end
134
-
135
- def test_escape_once
136
- assert_equal '&lt;strong&gt;Hulk&lt;/strong&gt;', @filters.escape_once('&lt;strong&gt;Hulk</strong>')
137
- end
138
-
139
- def test_url_encode
140
- assert_equal 'foo%2B1%40example.com', @filters.url_encode('foo+1@example.com')
141
- assert_equal nil, @filters.url_encode(nil)
142
- end
143
-
144
- def test_url_decode
145
- assert_equal 'foo bar', @filters.url_decode('foo+bar')
146
- assert_equal 'foo bar', @filters.url_decode('foo%20bar')
147
- assert_equal 'foo+1@example.com', @filters.url_decode('foo%2B1%40example.com')
148
- assert_equal nil, @filters.url_decode(nil)
149
- end
150
-
151
- def test_truncatewords
152
- assert_equal 'one two three', @filters.truncatewords('one two three', 4)
153
- assert_equal 'one two...', @filters.truncatewords('one two three', 2)
154
- assert_equal 'one two three', @filters.truncatewords('one two three')
155
- assert_equal 'Two small (13&#8221; x 5.5&#8221; x 10&#8221; high) baskets fit inside one large basket (13&#8221;...', @filters.truncatewords('Two small (13&#8221; x 5.5&#8221; x 10&#8221; high) baskets fit inside one large basket (13&#8221; x 16&#8221; x 10.5&#8221; high) with cover.', 15)
156
- assert_equal "测试测试测试测试", @filters.truncatewords('测试测试测试测试', 5)
157
- assert_equal 'one two1', @filters.truncatewords("one two three", 2, 1)
158
- end
159
-
160
- def test_strip_html
161
- assert_equal 'test', @filters.strip_html("<div>test</div>")
162
- assert_equal 'test', @filters.strip_html("<div id='test'>test</div>")
163
- assert_equal '', @filters.strip_html("<script type='text/javascript'>document.write('some stuff');</script>")
164
- assert_equal '', @filters.strip_html("<style type='text/css'>foo bar</style>")
165
- assert_equal 'test', @filters.strip_html("<div\nclass='multiline'>test</div>")
166
- assert_equal 'test', @filters.strip_html("<!-- foo bar \n test -->test")
167
- assert_equal '', @filters.strip_html(nil)
168
- end
169
-
170
- def test_join
171
- assert_equal '1 2 3 4', @filters.join([1, 2, 3, 4])
172
- assert_equal '1 - 2 - 3 - 4', @filters.join([1, 2, 3, 4], ' - ')
173
- end
174
-
175
- def test_sort
176
- assert_equal [1, 2, 3, 4], @filters.sort([4, 3, 2, 1])
177
- assert_equal [{ "a" => 1 }, { "a" => 2 }, { "a" => 3 }, { "a" => 4 }], @filters.sort([{ "a" => 4 }, { "a" => 3 }, { "a" => 1 }, { "a" => 2 }], "a")
178
- end
179
-
180
- def test_sort_when_property_is_sometimes_missing_puts_nils_last
181
- input = [
182
- { "price" => 4, "handle" => "alpha" },
183
- { "handle" => "beta" },
184
- { "price" => 1, "handle" => "gamma" },
185
- { "handle" => "delta" },
186
- { "price" => 2, "handle" => "epsilon" }
187
- ]
188
- expectation = [
189
- { "price" => 1, "handle" => "gamma" },
190
- { "price" => 2, "handle" => "epsilon" },
191
- { "price" => 4, "handle" => "alpha" },
192
- { "handle" => "delta" },
193
- { "handle" => "beta" }
194
- ]
195
- assert_equal expectation, @filters.sort(input, "price")
196
- end
197
-
198
- def test_sort_empty_array
199
- assert_equal [], @filters.sort([], "a")
200
- end
201
-
202
- def test_sort_natural_empty_array
203
- assert_equal [], @filters.sort_natural([], "a")
204
- end
205
-
206
- def test_legacy_sort_hash
207
- assert_equal [{ a: 1, b: 2 }], @filters.sort({ a: 1, b: 2 })
208
- end
209
-
210
- def test_numerical_vs_lexicographical_sort
211
- assert_equal [2, 10], @filters.sort([10, 2])
212
- assert_equal [{ "a" => 2 }, { "a" => 10 }], @filters.sort([{ "a" => 10 }, { "a" => 2 }], "a")
213
- assert_equal ["10", "2"], @filters.sort(["10", "2"])
214
- assert_equal [{ "a" => "10" }, { "a" => "2" }], @filters.sort([{ "a" => "10" }, { "a" => "2" }], "a")
215
- end
216
-
217
- def test_uniq
218
- assert_equal ["foo"], @filters.uniq("foo")
219
- assert_equal [1, 3, 2, 4], @filters.uniq([1, 1, 3, 2, 3, 1, 4, 3, 2, 1])
220
- assert_equal [{ "a" => 1 }, { "a" => 3 }, { "a" => 2 }], @filters.uniq([{ "a" => 1 }, { "a" => 3 }, { "a" => 1 }, { "a" => 2 }], "a")
221
- testdrop = TestDrop.new
222
- assert_equal [testdrop], @filters.uniq([testdrop, TestDrop.new], 'test')
223
- end
224
-
225
- def test_uniq_empty_array
226
- assert_equal [], @filters.uniq([], "a")
227
- end
228
-
229
- def test_compact_empty_array
230
- assert_equal [], @filters.compact([], "a")
231
- end
232
-
233
- def test_reverse
234
- assert_equal [4, 3, 2, 1], @filters.reverse([1, 2, 3, 4])
235
- end
236
-
237
- def test_legacy_reverse_hash
238
- assert_equal [{ a: 1, b: 2 }], @filters.reverse(a: 1, b: 2)
239
- end
240
-
241
- def test_map
242
- assert_equal [1, 2, 3, 4], @filters.map([{ "a" => 1 }, { "a" => 2 }, { "a" => 3 }, { "a" => 4 }], 'a')
243
- assert_template_result 'abc', "{{ ary | map:'foo' | map:'bar' }}",
244
- 'ary' => [{ 'foo' => { 'bar' => 'a' } }, { 'foo' => { 'bar' => 'b' } }, { 'foo' => { 'bar' => 'c' } }]
245
- end
246
-
247
- def test_map_doesnt_call_arbitrary_stuff
248
- assert_template_result "", '{{ "foo" | map: "__id__" }}'
249
- assert_template_result "", '{{ "foo" | map: "inspect" }}'
250
- end
251
-
252
- def test_map_calls_to_liquid
253
- t = TestThing.new
254
- assert_template_result "woot: 1", '{{ foo | map: "whatever" }}', "foo" => [t]
255
- end
256
-
257
- def test_map_on_hashes
258
- assert_template_result "4217", '{{ thing | map: "foo" | map: "bar" }}',
259
- "thing" => { "foo" => [ { "bar" => 42 }, { "bar" => 17 } ] }
260
- end
261
-
262
- def test_legacy_map_on_hashes_with_dynamic_key
263
- template = "{% assign key = 'foo' %}{{ thing | map: key | map: 'bar' }}"
264
- hash = { "foo" => { "bar" => 42 } }
265
- assert_template_result "42", template, "thing" => hash
266
- end
267
-
268
- def test_sort_calls_to_liquid
269
- t = TestThing.new
270
- Liquid::Template.parse('{{ foo | sort: "whatever" }}').render("foo" => [t])
271
- assert t.foo > 0
272
- end
273
-
274
- def test_map_over_proc
275
- drop = TestDrop.new
276
- p = proc{ drop }
277
- templ = '{{ procs | map: "test" }}'
278
- assert_template_result "testfoo", templ, "procs" => [p]
279
- end
280
-
281
- def test_map_over_drops_returning_procs
282
- drops = [
283
- {
284
- "proc" => ->{ "foo" },
285
- },
286
- {
287
- "proc" => ->{ "bar" },
288
- },
289
- ]
290
- templ = '{{ drops | map: "proc" }}'
291
- assert_template_result "foobar", templ, "drops" => drops
292
- end
293
-
294
- def test_map_works_on_enumerables
295
- assert_template_result "123", '{{ foo | map: "foo" }}', "foo" => TestEnumerable.new
296
- end
297
-
298
- def test_sort_works_on_enumerables
299
- assert_template_result "213", '{{ foo | sort: "bar" | map: "foo" }}', "foo" => TestEnumerable.new
300
- end
301
-
302
- def test_first_and_last_call_to_liquid
303
- assert_template_result 'foobar', '{{ foo | first }}', 'foo' => [ThingWithToLiquid.new]
304
- assert_template_result 'foobar', '{{ foo | last }}', 'foo' => [ThingWithToLiquid.new]
305
- end
306
-
307
- def test_truncate_calls_to_liquid
308
- assert_template_result "wo...", '{{ foo | truncate: 5 }}', "foo" => TestThing.new
309
- end
310
-
311
- def test_date
312
- assert_equal 'May', @filters.date(Time.parse("2006-05-05 10:00:00"), "%B")
313
- assert_equal 'June', @filters.date(Time.parse("2006-06-05 10:00:00"), "%B")
314
- assert_equal 'July', @filters.date(Time.parse("2006-07-05 10:00:00"), "%B")
315
-
316
- assert_equal 'May', @filters.date("2006-05-05 10:00:00", "%B")
317
- assert_equal 'June', @filters.date("2006-06-05 10:00:00", "%B")
318
- assert_equal 'July', @filters.date("2006-07-05 10:00:00", "%B")
319
-
320
- assert_equal '2006-07-05 10:00:00', @filters.date("2006-07-05 10:00:00", "")
321
- assert_equal '2006-07-05 10:00:00', @filters.date("2006-07-05 10:00:00", "")
322
- assert_equal '2006-07-05 10:00:00', @filters.date("2006-07-05 10:00:00", "")
323
- assert_equal '2006-07-05 10:00:00', @filters.date("2006-07-05 10:00:00", nil)
324
-
325
- assert_equal '07/05/2006', @filters.date("2006-07-05 10:00:00", "%m/%d/%Y")
326
-
327
- assert_equal "07/16/2004", @filters.date("Fri Jul 16 01:00:00 2004", "%m/%d/%Y")
328
- assert_equal "#{Date.today.year}", @filters.date('now', '%Y')
329
- assert_equal "#{Date.today.year}", @filters.date('today', '%Y')
330
- assert_equal "#{Date.today.year}", @filters.date('Today', '%Y')
331
-
332
- assert_equal nil, @filters.date(nil, "%B")
333
-
334
- assert_equal '', @filters.date('', "%B")
335
-
336
- with_timezone("UTC") do
337
- assert_equal "07/05/2006", @filters.date(1152098955, "%m/%d/%Y")
338
- assert_equal "07/05/2006", @filters.date("1152098955", "%m/%d/%Y")
339
- end
340
- end
341
-
342
- def test_first_last
343
- assert_equal 1, @filters.first([1, 2, 3])
344
- assert_equal 3, @filters.last([1, 2, 3])
345
- assert_equal nil, @filters.first([])
346
- assert_equal nil, @filters.last([])
347
- end
348
-
349
- def test_replace
350
- assert_equal '2 2 2 2', @filters.replace('1 1 1 1', '1', 2)
351
- assert_equal '2 2 2 2', @filters.replace('1 1 1 1', 1, 2)
352
- assert_equal '2 1 1 1', @filters.replace_first('1 1 1 1', '1', 2)
353
- assert_equal '2 1 1 1', @filters.replace_first('1 1 1 1', 1, 2)
354
- assert_template_result '2 1 1 1', "{{ '1 1 1 1' | replace_first: '1', 2 }}"
355
- end
356
-
357
- def test_remove
358
- assert_equal ' ', @filters.remove("a a a a", 'a')
359
- assert_equal ' ', @filters.remove("1 1 1 1", 1)
360
- assert_equal 'a a a', @filters.remove_first("a a a a", 'a ')
361
- assert_equal ' 1 1 1', @filters.remove_first("1 1 1 1", 1)
362
- assert_template_result 'a a a', "{{ 'a a a a' | remove_first: 'a ' }}"
363
- end
364
-
365
- def test_pipes_in_string_arguments
366
- assert_template_result 'foobar', "{{ 'foo|bar' | remove: '|' }}"
367
- end
368
-
369
- def test_strip
370
- assert_template_result 'ab c', "{{ source | strip }}", 'source' => " ab c "
371
- assert_template_result 'ab c', "{{ source | strip }}", 'source' => " \tab c \n \t"
372
- end
373
-
374
- def test_lstrip
375
- assert_template_result 'ab c ', "{{ source | lstrip }}", 'source' => " ab c "
376
- assert_template_result "ab c \n \t", "{{ source | lstrip }}", 'source' => " \tab c \n \t"
377
- end
378
-
379
- def test_rstrip
380
- assert_template_result " ab c", "{{ source | rstrip }}", 'source' => " ab c "
381
- assert_template_result " \tab c", "{{ source | rstrip }}", 'source' => " \tab c \n \t"
382
- end
383
-
384
- def test_strip_newlines
385
- assert_template_result 'abc', "{{ source | strip_newlines }}", 'source' => "a\nb\nc"
386
- assert_template_result 'abc', "{{ source | strip_newlines }}", 'source' => "a\r\nb\nc"
387
- end
388
-
389
- def test_newlines_to_br
390
- assert_template_result "a<br />\nb<br />\nc", "{{ source | newline_to_br }}", 'source' => "a\nb\nc"
391
- end
392
-
393
- def test_plus
394
- assert_template_result "2", "{{ 1 | plus:1 }}"
395
- assert_template_result "2.0", "{{ '1' | plus:'1.0' }}"
396
-
397
- assert_template_result "5", "{{ price | plus:'2' }}", 'price' => NumberLikeThing.new(3)
398
- end
399
-
400
- def test_minus
401
- assert_template_result "4", "{{ input | minus:operand }}", 'input' => 5, 'operand' => 1
402
- assert_template_result "2.3", "{{ '4.3' | minus:'2' }}"
403
-
404
- assert_template_result "5", "{{ price | minus:'2' }}", 'price' => NumberLikeThing.new(7)
405
- end
406
-
407
- def test_abs
408
- assert_template_result "17", "{{ 17 | abs }}"
409
- assert_template_result "17", "{{ -17 | abs }}"
410
- assert_template_result "17", "{{ '17' | abs }}"
411
- assert_template_result "17", "{{ '-17' | abs }}"
412
- assert_template_result "0", "{{ 0 | abs }}"
413
- assert_template_result "0", "{{ '0' | abs }}"
414
- assert_template_result "17.42", "{{ 17.42 | abs }}"
415
- assert_template_result "17.42", "{{ -17.42 | abs }}"
416
- assert_template_result "17.42", "{{ '17.42' | abs }}"
417
- assert_template_result "17.42", "{{ '-17.42' | abs }}"
418
- end
419
-
420
- def test_times
421
- assert_template_result "12", "{{ 3 | times:4 }}"
422
- assert_template_result "0", "{{ 'foo' | times:4 }}"
423
- assert_template_result "6", "{{ '2.1' | times:3 | replace: '.','-' | plus:0}}"
424
- assert_template_result "7.25", "{{ 0.0725 | times:100 }}"
425
- assert_template_result "-7.25", '{{ "-0.0725" | times:100 }}'
426
- assert_template_result "7.25", '{{ "-0.0725" | times: -100 }}'
427
- assert_template_result "4", "{{ price | times:2 }}", 'price' => NumberLikeThing.new(2)
428
- end
429
-
430
- def test_divided_by
431
- assert_template_result "4", "{{ 12 | divided_by:3 }}"
432
- assert_template_result "4", "{{ 14 | divided_by:3 }}"
433
-
434
- assert_template_result "5", "{{ 15 | divided_by:3 }}"
435
- assert_equal "Liquid error: divided by 0", Template.parse("{{ 5 | divided_by:0 }}").render
436
-
437
- assert_template_result "0.5", "{{ 2.0 | divided_by:4 }}"
438
- assert_raises(Liquid::ZeroDivisionError) do
439
- assert_template_result "4", "{{ 1 | modulo: 0 }}"
440
- end
441
-
442
- assert_template_result "5", "{{ price | divided_by:2 }}", 'price' => NumberLikeThing.new(10)
443
- end
444
-
445
- def test_modulo
446
- assert_template_result "1", "{{ 3 | modulo:2 }}"
447
- assert_raises(Liquid::ZeroDivisionError) do
448
- assert_template_result "4", "{{ 1 | modulo: 0 }}"
449
- end
450
-
451
- assert_template_result "1", "{{ price | modulo:2 }}", 'price' => NumberLikeThing.new(3)
452
- end
453
-
454
- def test_round
455
- assert_template_result "5", "{{ input | round }}", 'input' => 4.6
456
- assert_template_result "4", "{{ '4.3' | round }}"
457
- assert_template_result "4.56", "{{ input | round: 2 }}", 'input' => 4.5612
458
- assert_raises(Liquid::FloatDomainError) do
459
- assert_template_result "4", "{{ 1.0 | divided_by: 0.0 | round }}"
460
- end
461
-
462
- assert_template_result "5", "{{ price | round }}", 'price' => NumberLikeThing.new(4.6)
463
- assert_template_result "4", "{{ price | round }}", 'price' => NumberLikeThing.new(4.3)
464
- end
465
-
466
- def test_ceil
467
- assert_template_result "5", "{{ input | ceil }}", 'input' => 4.6
468
- assert_template_result "5", "{{ '4.3' | ceil }}"
469
- assert_raises(Liquid::FloatDomainError) do
470
- assert_template_result "4", "{{ 1.0 | divided_by: 0.0 | ceil }}"
471
- end
472
-
473
- assert_template_result "5", "{{ price | ceil }}", 'price' => NumberLikeThing.new(4.6)
474
- end
475
-
476
- def test_floor
477
- assert_template_result "4", "{{ input | floor }}", 'input' => 4.6
478
- assert_template_result "4", "{{ '4.3' | floor }}"
479
- assert_raises(Liquid::FloatDomainError) do
480
- assert_template_result "4", "{{ 1.0 | divided_by: 0.0 | floor }}"
481
- end
482
-
483
- assert_template_result "5", "{{ price | floor }}", 'price' => NumberLikeThing.new(5.4)
484
- end
485
-
486
- def test_append
487
- assigns = { 'a' => 'bc', 'b' => 'd' }
488
- assert_template_result('bcd', "{{ a | append: 'd'}}", assigns)
489
- assert_template_result('bcd', "{{ a | append: b}}", assigns)
490
- end
491
-
492
- def test_concat
493
- assert_equal [1, 2, 3, 4], @filters.concat([1, 2], [3, 4])
494
- assert_equal [1, 2, 'a'], @filters.concat([1, 2], ['a'])
495
- assert_equal [1, 2, 10], @filters.concat([1, 2], [10])
496
-
497
- assert_raises(Liquid::ArgumentError, "concat filter requires an array argument") do
498
- @filters.concat([1, 2], 10)
499
- end
500
- end
501
-
502
- def test_prepend
503
- assigns = { 'a' => 'bc', 'b' => 'a' }
504
- assert_template_result('abc', "{{ a | prepend: 'a'}}", assigns)
505
- assert_template_result('abc', "{{ a | prepend: b}}", assigns)
506
- end
507
-
508
- def test_default
509
- assert_equal "foo", @filters.default("foo", "bar")
510
- assert_equal "bar", @filters.default(nil, "bar")
511
- assert_equal "bar", @filters.default("", "bar")
512
- assert_equal "bar", @filters.default(false, "bar")
513
- assert_equal "bar", @filters.default([], "bar")
514
- assert_equal "bar", @filters.default({}, "bar")
515
- end
516
-
517
- def test_cannot_access_private_methods
518
- assert_template_result('a', "{{ 'a' | to_number }}")
519
- end
520
-
521
- def test_date_raises_nothing
522
- assert_template_result('', "{{ '' | date: '%D' }}")
523
- assert_template_result('abc', "{{ 'abc' | date: '%D' }}")
524
- end
525
-
526
- private
527
-
528
- def with_timezone(tz)
529
- old_tz = ENV['TZ']
530
- ENV['TZ'] = tz
531
- yield
532
- ensure
533
- ENV['TZ'] = old_tz
534
- end
535
- end # StandardFiltersTest
@@ -1,15 +0,0 @@
1
- require 'test_helper'
2
-
3
- class BreakTagTest < Minitest::Test
4
- include Liquid
5
-
6
- # tests that no weird errors are raised if break is called outside of a
7
- # block
8
- def test_break_with_no_block
9
- assigns = { 'i' => 1 }
10
- markup = '{% break %}'
11
- expected = ''
12
-
13
- assert_template_result(expected, markup, assigns)
14
- end
15
- end
@@ -1,15 +0,0 @@
1
- require 'test_helper'
2
-
3
- class ContinueTagTest < Minitest::Test
4
- include Liquid
5
-
6
- # tests that no weird errors are raised if continue is called outside of a
7
- # block
8
- def test_continue_with_no_block
9
- assigns = {}
10
- markup = '{% continue %}'
11
- expected = ''
12
-
13
- assert_template_result(expected, markup, assigns)
14
- end
15
- end