wordify_liquid 2.5.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (69) hide show
  1. data/History.md +75 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.md +44 -0
  4. data/lib/extras/liquid_view.rb +51 -0
  5. data/lib/liquid.rb +68 -0
  6. data/lib/liquid/block.rb +115 -0
  7. data/lib/liquid/condition.rb +120 -0
  8. data/lib/liquid/context.rb +259 -0
  9. data/lib/liquid/document.rb +17 -0
  10. data/lib/liquid/drop.rb +61 -0
  11. data/lib/liquid/errors.rb +11 -0
  12. data/lib/liquid/extensions.rb +62 -0
  13. data/lib/liquid/file_system.rb +62 -0
  14. data/lib/liquid/htmltags.rb +74 -0
  15. data/lib/liquid/interrupts.rb +17 -0
  16. data/lib/liquid/module_ex.rb +62 -0
  17. data/lib/liquid/standardfilters.rb +245 -0
  18. data/lib/liquid/strainer.rb +53 -0
  19. data/lib/liquid/tag.rb +26 -0
  20. data/lib/liquid/tags/assign.rb +33 -0
  21. data/lib/liquid/tags/break.rb +21 -0
  22. data/lib/liquid/tags/capture.rb +35 -0
  23. data/lib/liquid/tags/case.rb +79 -0
  24. data/lib/liquid/tags/comment.rb +9 -0
  25. data/lib/liquid/tags/continue.rb +21 -0
  26. data/lib/liquid/tags/cycle.rb +59 -0
  27. data/lib/liquid/tags/decrement.rb +39 -0
  28. data/lib/liquid/tags/for.rb +142 -0
  29. data/lib/liquid/tags/if.rb +79 -0
  30. data/lib/liquid/tags/ifchanged.rb +20 -0
  31. data/lib/liquid/tags/include.rb +65 -0
  32. data/lib/liquid/tags/increment.rb +35 -0
  33. data/lib/liquid/tags/raw.rb +21 -0
  34. data/lib/liquid/tags/unless.rb +33 -0
  35. data/lib/liquid/template.rb +150 -0
  36. data/lib/liquid/utils.rb +31 -0
  37. data/lib/liquid/variable.rb +57 -0
  38. data/lib/wordify_liquid.rb +1 -0
  39. data/test/liquid/assign_test.rb +21 -0
  40. data/test/liquid/block_test.rb +58 -0
  41. data/test/liquid/capture_test.rb +40 -0
  42. data/test/liquid/condition_test.rb +127 -0
  43. data/test/liquid/context_test.rb +478 -0
  44. data/test/liquid/drop_test.rb +169 -0
  45. data/test/liquid/error_handling_test.rb +81 -0
  46. data/test/liquid/file_system_test.rb +29 -0
  47. data/test/liquid/filter_test.rb +125 -0
  48. data/test/liquid/module_ex_test.rb +87 -0
  49. data/test/liquid/output_test.rb +116 -0
  50. data/test/liquid/parsing_quirks_test.rb +52 -0
  51. data/test/liquid/regexp_test.rb +44 -0
  52. data/test/liquid/security_test.rb +64 -0
  53. data/test/liquid/standard_filter_test.rb +195 -0
  54. data/test/liquid/strainer_test.rb +52 -0
  55. data/test/liquid/tags/break_tag_test.rb +16 -0
  56. data/test/liquid/tags/continue_tag_test.rb +16 -0
  57. data/test/liquid/tags/for_tag_test.rb +284 -0
  58. data/test/liquid/tags/html_tag_test.rb +63 -0
  59. data/test/liquid/tags/if_else_tag_test.rb +160 -0
  60. data/test/liquid/tags/include_tag_test.rb +139 -0
  61. data/test/liquid/tags/increment_tag_test.rb +24 -0
  62. data/test/liquid/tags/raw_tag_test.rb +15 -0
  63. data/test/liquid/tags/standard_tag_test.rb +295 -0
  64. data/test/liquid/tags/statements_test.rb +134 -0
  65. data/test/liquid/tags/unless_else_tag_test.rb +26 -0
  66. data/test/liquid/template_test.rb +74 -0
  67. data/test/liquid/variable_test.rb +180 -0
  68. data/test/test_helper.rb +29 -0
  69. metadata +145 -0
@@ -0,0 +1,52 @@
1
+ require 'test_helper'
2
+
3
+ class StrainerTest < Test::Unit::TestCase
4
+ include Liquid
5
+
6
+ module AccessScopeFilters
7
+ def public_filter
8
+ "public"
9
+ end
10
+
11
+ def private_filter
12
+ "private"
13
+ end
14
+ private :private_filter
15
+ end
16
+
17
+ Strainer.global_filter(AccessScopeFilters)
18
+
19
+ def test_strainer
20
+ strainer = Strainer.create(nil)
21
+ assert_equal 5, strainer.invoke('size', 'input')
22
+ assert_equal "public", strainer.invoke("public_filter")
23
+ end
24
+
25
+ def test_strainer_only_invokes_public_filter_methods
26
+ strainer = Strainer.create(nil)
27
+ assert_equal false, strainer.invokable?('__test__')
28
+ assert_equal false, strainer.invokable?('test')
29
+ assert_equal false, strainer.invokable?('instance_eval')
30
+ assert_equal false, strainer.invokable?('__send__')
31
+ assert_equal true, strainer.invokable?('size') # from the standard lib
32
+ end
33
+
34
+ def test_strainer_returns_nil_if_no_filter_method_found
35
+ strainer = Strainer.create(nil)
36
+ assert_nil strainer.invoke("private_filter")
37
+ assert_nil strainer.invoke("undef_the_filter")
38
+ end
39
+
40
+ def test_strainer_returns_first_argument_if_no_method_and_arguments_given
41
+ strainer = Strainer.create(nil)
42
+ assert_equal "password", strainer.invoke("undef_the_method", "password")
43
+ end
44
+
45
+ def test_strainer_only_allows_methods_defined_in_filters
46
+ strainer = Strainer.create(nil)
47
+ assert_equal "1 + 1", strainer.invoke("instance_eval", "1 + 1")
48
+ assert_equal "puts", strainer.invoke("__send__", "puts", "Hi Mom")
49
+ assert_equal "has_method?", strainer.invoke("invoke", "has_method?", "invoke")
50
+ end
51
+
52
+ end # StrainerTest
@@ -0,0 +1,16 @@
1
+ require 'test_helper'
2
+
3
+ class BreakTagTest < Test::Unit::TestCase
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
+
16
+ end
@@ -0,0 +1,16 @@
1
+ require 'test_helper'
2
+
3
+ class ContinueTagTest < Test::Unit::TestCase
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
+
16
+ end
@@ -0,0 +1,284 @@
1
+ require 'test_helper'
2
+
3
+ class ForTagTest < Test::Unit::TestCase
4
+ include Liquid
5
+
6
+ def test_for
7
+ assert_template_result(' yo yo yo yo ','{%for item in array%} yo {%endfor%}','array' => [1,2,3,4])
8
+ assert_template_result('yoyo','{%for item in array%}yo{%endfor%}','array' => [1,2])
9
+ assert_template_result(' yo ','{%for item in array%} yo {%endfor%}','array' => [1])
10
+ assert_template_result('','{%for item in array%}{%endfor%}','array' => [1,2])
11
+ expected = <<HERE
12
+
13
+ yo
14
+
15
+ yo
16
+
17
+ yo
18
+
19
+ HERE
20
+ template = <<HERE
21
+ {%for item in array%}
22
+ yo
23
+ {%endfor%}
24
+ HERE
25
+ assert_template_result(expected,template,'array' => [1,2,3])
26
+ end
27
+
28
+ def test_for_reversed
29
+ assigns = {'array' => [ 1, 2, 3] }
30
+ assert_template_result('321','{%for item in array reversed %}{{item}}{%endfor%}',assigns)
31
+ end
32
+
33
+ def test_for_with_range
34
+ assert_template_result(' 1 2 3 ','{%for item in (1..3) %} {{item}} {%endfor%}')
35
+ end
36
+
37
+ def test_for_with_variable
38
+ assert_template_result(' 1 2 3 ','{%for item in array%} {{item}} {%endfor%}','array' => [1,2,3])
39
+ assert_template_result('123','{%for item in array%}{{item}}{%endfor%}','array' => [1,2,3])
40
+ assert_template_result('123','{% for item in array %}{{item}}{% endfor %}','array' => [1,2,3])
41
+ assert_template_result('abcd','{%for item in array%}{{item}}{%endfor%}','array' => ['a','b','c','d'])
42
+ assert_template_result('a b c','{%for item in array%}{{item}}{%endfor%}','array' => ['a',' ','b',' ','c'])
43
+ assert_template_result('abc','{%for item in array%}{{item}}{%endfor%}','array' => ['a','','b','','c'])
44
+ end
45
+
46
+ def test_for_helpers
47
+ assigns = {'array' => [1,2,3] }
48
+ assert_template_result(' 1/3 2/3 3/3 ',
49
+ '{%for item in array%} {{forloop.index}}/{{forloop.length}} {%endfor%}',
50
+ assigns)
51
+ assert_template_result(' 1 2 3 ', '{%for item in array%} {{forloop.index}} {%endfor%}', assigns)
52
+ assert_template_result(' 0 1 2 ', '{%for item in array%} {{forloop.index0}} {%endfor%}', assigns)
53
+ assert_template_result(' 2 1 0 ', '{%for item in array%} {{forloop.rindex0}} {%endfor%}', assigns)
54
+ assert_template_result(' 3 2 1 ', '{%for item in array%} {{forloop.rindex}} {%endfor%}', assigns)
55
+ assert_template_result(' true false false ', '{%for item in array%} {{forloop.first}} {%endfor%}', assigns)
56
+ assert_template_result(' false false true ', '{%for item in array%} {{forloop.last}} {%endfor%}', assigns)
57
+ end
58
+
59
+ def test_for_and_if
60
+ assigns = {'array' => [1,2,3] }
61
+ assert_template_result('+--',
62
+ '{%for item in array%}{% if forloop.first %}+{% else %}-{% endif %}{%endfor%}',
63
+ assigns)
64
+ end
65
+
66
+ def test_for_else
67
+ assert_template_result('+++', '{%for item in array%}+{%else%}-{%endfor%}', 'array'=>[1,2,3])
68
+ assert_template_result('-', '{%for item in array%}+{%else%}-{%endfor%}', 'array'=>[])
69
+ assert_template_result('-', '{%for item in array%}+{%else%}-{%endfor%}', 'array'=>nil)
70
+ end
71
+
72
+ def test_limiting
73
+ assigns = {'array' => [1,2,3,4,5,6,7,8,9,0]}
74
+ assert_template_result('12', '{%for i in array limit:2 %}{{ i }}{%endfor%}', assigns)
75
+ assert_template_result('1234', '{%for i in array limit:4 %}{{ i }}{%endfor%}', assigns)
76
+ assert_template_result('3456', '{%for i in array limit:4 offset:2 %}{{ i }}{%endfor%}', assigns)
77
+ assert_template_result('3456', '{%for i in array limit: 4 offset: 2 %}{{ i }}{%endfor%}', assigns)
78
+ end
79
+
80
+ def test_dynamic_variable_limiting
81
+ assigns = {'array' => [1,2,3,4,5,6,7,8,9,0]}
82
+ assigns['limit'] = 2
83
+ assigns['offset'] = 2
84
+
85
+ assert_template_result('34', '{%for i in array limit: limit offset: offset %}{{ i }}{%endfor%}', assigns)
86
+ end
87
+
88
+ def test_nested_for
89
+ assigns = {'array' => [[1,2],[3,4],[5,6]] }
90
+ assert_template_result('123456', '{%for item in array%}{%for i in item%}{{ i }}{%endfor%}{%endfor%}', assigns)
91
+ end
92
+
93
+ def test_offset_only
94
+ assigns = {'array' => [1,2,3,4,5,6,7,8,9,0]}
95
+ assert_template_result('890', '{%for i in array offset:7 %}{{ i }}{%endfor%}', assigns)
96
+ end
97
+
98
+ def test_pause_resume
99
+ assigns = {'array' => {'items' => [1,2,3,4,5,6,7,8,9,0]}}
100
+ markup = <<-MKUP
101
+ {%for i in array.items limit: 3 %}{{i}}{%endfor%}
102
+ next
103
+ {%for i in array.items offset:continue limit: 3 %}{{i}}{%endfor%}
104
+ next
105
+ {%for i in array.items offset:continue limit: 3 %}{{i}}{%endfor%}
106
+ MKUP
107
+ expected = <<-XPCTD
108
+ 123
109
+ next
110
+ 456
111
+ next
112
+ 789
113
+ XPCTD
114
+ assert_template_result(expected,markup,assigns)
115
+ end
116
+
117
+ def test_pause_resume_limit
118
+ assigns = {'array' => {'items' => [1,2,3,4,5,6,7,8,9,0]}}
119
+ markup = <<-MKUP
120
+ {%for i in array.items limit:3 %}{{i}}{%endfor%}
121
+ next
122
+ {%for i in array.items offset:continue limit:3 %}{{i}}{%endfor%}
123
+ next
124
+ {%for i in array.items offset:continue limit:1 %}{{i}}{%endfor%}
125
+ MKUP
126
+ expected = <<-XPCTD
127
+ 123
128
+ next
129
+ 456
130
+ next
131
+ 7
132
+ XPCTD
133
+ assert_template_result(expected,markup,assigns)
134
+ end
135
+
136
+ def test_pause_resume_BIG_limit
137
+ assigns = {'array' => {'items' => [1,2,3,4,5,6,7,8,9,0]}}
138
+ markup = <<-MKUP
139
+ {%for i in array.items limit:3 %}{{i}}{%endfor%}
140
+ next
141
+ {%for i in array.items offset:continue limit:3 %}{{i}}{%endfor%}
142
+ next
143
+ {%for i in array.items offset:continue limit:1000 %}{{i}}{%endfor%}
144
+ MKUP
145
+ expected = <<-XPCTD
146
+ 123
147
+ next
148
+ 456
149
+ next
150
+ 7890
151
+ XPCTD
152
+ assert_template_result(expected,markup,assigns)
153
+ end
154
+
155
+
156
+ def test_pause_resume_BIG_offset
157
+ assigns = {'array' => {'items' => [1,2,3,4,5,6,7,8,9,0]}}
158
+ markup = %q({%for i in array.items limit:3 %}{{i}}{%endfor%}
159
+ next
160
+ {%for i in array.items offset:continue limit:3 %}{{i}}{%endfor%}
161
+ next
162
+ {%for i in array.items offset:continue limit:3 offset:1000 %}{{i}}{%endfor%})
163
+ expected = %q(123
164
+ next
165
+ 456
166
+ next
167
+ )
168
+ assert_template_result(expected,markup,assigns)
169
+ end
170
+
171
+ def test_for_with_break
172
+ assigns = {'array' => {'items' => [1,2,3,4,5,6,7,8,9,10]}}
173
+
174
+ markup = '{% for i in array.items %}{% break %}{% endfor %}'
175
+ expected = ""
176
+ assert_template_result(expected,markup,assigns)
177
+
178
+ markup = '{% for i in array.items %}{{ i }}{% break %}{% endfor %}'
179
+ expected = "1"
180
+ assert_template_result(expected,markup,assigns)
181
+
182
+ markup = '{% for i in array.items %}{% break %}{{ i }}{% endfor %}'
183
+ expected = ""
184
+ assert_template_result(expected,markup,assigns)
185
+
186
+ markup = '{% for i in array.items %}{{ i }}{% if i > 3 %}{% break %}{% endif %}{% endfor %}'
187
+ expected = "1234"
188
+ assert_template_result(expected,markup,assigns)
189
+
190
+ # tests to ensure it only breaks out of the local for loop
191
+ # and not all of them.
192
+ assigns = {'array' => [[1,2],[3,4],[5,6]] }
193
+ markup = '{% for item in array %}' +
194
+ '{% for i in item %}' +
195
+ '{% if i == 1 %}' +
196
+ '{% break %}' +
197
+ '{% endif %}' +
198
+ '{{ i }}' +
199
+ '{% endfor %}' +
200
+ '{% endfor %}'
201
+ expected = '3456'
202
+ assert_template_result(expected, markup, assigns)
203
+
204
+ # test break does nothing when unreached
205
+ assigns = {'array' => {'items' => [1,2,3,4,5]}}
206
+ markup = '{% for i in array.items %}{% if i == 9999 %}{% break %}{% endif %}{{ i }}{% endfor %}'
207
+ expected = '12345'
208
+ assert_template_result(expected, markup, assigns)
209
+ end
210
+
211
+ def test_for_with_continue
212
+ assigns = {'array' => {'items' => [1,2,3,4,5]}}
213
+
214
+ markup = '{% for i in array.items %}{% continue %}{% endfor %}'
215
+ expected = ""
216
+ assert_template_result(expected,markup,assigns)
217
+
218
+ markup = '{% for i in array.items %}{{ i }}{% continue %}{% endfor %}'
219
+ expected = "12345"
220
+ assert_template_result(expected,markup,assigns)
221
+
222
+ markup = '{% for i in array.items %}{% continue %}{{ i }}{% endfor %}'
223
+ expected = ""
224
+ assert_template_result(expected,markup,assigns)
225
+
226
+ markup = '{% for i in array.items %}{% if i > 3 %}{% continue %}{% endif %}{{ i }}{% endfor %}'
227
+ expected = "123"
228
+ assert_template_result(expected,markup,assigns)
229
+
230
+ markup = '{% for i in array.items %}{% if i == 3 %}{% continue %}{% else %}{{ i }}{% endif %}{% endfor %}'
231
+ expected = "1245"
232
+ assert_template_result(expected,markup,assigns)
233
+
234
+ # tests to ensure it only continues the local for loop and not all of them.
235
+ assigns = {'array' => [[1,2],[3,4],[5,6]] }
236
+ markup = '{% for item in array %}' +
237
+ '{% for i in item %}' +
238
+ '{% if i == 1 %}' +
239
+ '{% continue %}' +
240
+ '{% endif %}' +
241
+ '{{ i }}' +
242
+ '{% endfor %}' +
243
+ '{% endfor %}'
244
+ expected = '23456'
245
+ assert_template_result(expected, markup, assigns)
246
+
247
+ # test continue does nothing when unreached
248
+ assigns = {'array' => {'items' => [1,2,3,4,5]}}
249
+ markup = '{% for i in array.items %}{% if i == 9999 %}{% continue %}{% endif %}{{ i }}{% endfor %}'
250
+ expected = '12345'
251
+ assert_template_result(expected, markup, assigns)
252
+ end
253
+
254
+ def test_for_tag_string
255
+ # ruby 1.8.7 "String".each => Enumerator with single "String" element.
256
+ # ruby 1.9.3 no longer supports .each on String though we mimic
257
+ # the functionality for backwards compatibility
258
+
259
+ assert_template_result('test string',
260
+ '{%for val in string%}{{val}}{%endfor%}',
261
+ 'string' => "test string")
262
+
263
+ assert_template_result('test string',
264
+ '{%for val in string limit:1%}{{val}}{%endfor%}',
265
+ 'string' => "test string")
266
+
267
+ assert_template_result('val-string-1-1-0-1-0-true-true-test string',
268
+ '{%for val in string%}' +
269
+ '{{forloop.name}}-' +
270
+ '{{forloop.index}}-' +
271
+ '{{forloop.length}}-' +
272
+ '{{forloop.index0}}-' +
273
+ '{{forloop.rindex}}-' +
274
+ '{{forloop.rindex0}}-' +
275
+ '{{forloop.first}}-' +
276
+ '{{forloop.last}}-' +
277
+ '{{val}}{%endfor%}',
278
+ 'string' => "test string")
279
+ end
280
+
281
+ def test_blank_string_not_iterable
282
+ assert_template_result('', "{% for char in characters %}I WILL NOT BE OUTPUT{% endfor %}", 'characters' => '')
283
+ end
284
+ end
@@ -0,0 +1,63 @@
1
+ require 'test_helper'
2
+
3
+ class HtmlTagTest < Test::Unit::TestCase
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_html_table
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_html_table_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_html_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 # HtmlTagTest
@@ -0,0 +1,160 @@
1
+ require 'test_helper'
2
+
3
+ class IfElseTagTest < Test::Unit::TestCase
4
+ include Liquid
5
+
6
+ def test_if
7
+ assert_template_result(' ',' {% if false %} this text should not go into the output {% endif %} ')
8
+ assert_template_result(' this text should go into the output ',
9
+ ' {% if true %} this text should go into the output {% endif %} ')
10
+ assert_template_result(' you rock ?','{% if false %} you suck {% endif %} {% if true %} you rock {% endif %}?')
11
+ end
12
+
13
+ def test_if_else
14
+ assert_template_result(' YES ','{% if false %} NO {% else %} YES {% endif %}')
15
+ assert_template_result(' YES ','{% if true %} YES {% else %} NO {% endif %}')
16
+ assert_template_result(' YES ','{% if "foo" %} YES {% else %} NO {% endif %}')
17
+ end
18
+
19
+ def test_if_boolean
20
+ assert_template_result(' YES ','{% if var %} YES {% endif %}', 'var' => true)
21
+ end
22
+
23
+ def test_if_or
24
+ assert_template_result(' YES ','{% if a or b %} YES {% endif %}', 'a' => true, 'b' => true)
25
+ assert_template_result(' YES ','{% if a or b %} YES {% endif %}', 'a' => true, 'b' => false)
26
+ assert_template_result(' YES ','{% if a or b %} YES {% endif %}', 'a' => false, 'b' => true)
27
+ assert_template_result('', '{% if a or b %} YES {% endif %}', 'a' => false, 'b' => false)
28
+
29
+ assert_template_result(' YES ','{% if a or b or c %} YES {% endif %}', 'a' => false, 'b' => false, 'c' => true)
30
+ assert_template_result('', '{% if a or b or c %} YES {% endif %}', 'a' => false, 'b' => false, 'c' => false)
31
+ end
32
+
33
+ def test_if_or_with_operators
34
+ assert_template_result(' YES ','{% if a == true or b == true %} YES {% endif %}', 'a' => true, 'b' => true)
35
+ assert_template_result(' YES ','{% if a == true or b == false %} YES {% endif %}', 'a' => true, 'b' => true)
36
+ assert_template_result('','{% if a == false or b == false %} YES {% endif %}', 'a' => true, 'b' => true)
37
+ end
38
+
39
+ def test_comparison_of_strings_containing_and_or_or
40
+ assert_nothing_raised do
41
+ awful_markup = "a == 'and' and b == 'or' and c == 'foo and bar' and d == 'bar or baz' and e == 'foo' and foo and bar"
42
+ assigns = {'a' => 'and', 'b' => 'or', 'c' => 'foo and bar', 'd' => 'bar or baz', 'e' => 'foo', 'foo' => true, 'bar' => true}
43
+ assert_template_result(' YES ',"{% if #{awful_markup} %} YES {% endif %}", assigns)
44
+ end
45
+ end
46
+
47
+ def test_comparison_of_expressions_starting_with_and_or_or
48
+ assigns = {'order' => {'items_count' => 0}, 'android' => {'name' => 'Roy'}}
49
+ assert_nothing_raised do
50
+ assert_template_result( "YES",
51
+ "{% if android.name == 'Roy' %}YES{% endif %}",
52
+ assigns)
53
+ end
54
+ assert_nothing_raised do
55
+ assert_template_result( "YES",
56
+ "{% if order.items_count == 0 %}YES{% endif %}",
57
+ assigns)
58
+ end
59
+ end
60
+
61
+ def test_if_and
62
+ assert_template_result(' YES ','{% if true and true %} YES {% endif %}')
63
+ assert_template_result('','{% if false and true %} YES {% endif %}')
64
+ assert_template_result('','{% if false and true %} YES {% endif %}')
65
+ end
66
+
67
+
68
+ def test_hash_miss_generates_false
69
+ assert_template_result('','{% if foo.bar %} NO {% endif %}', 'foo' => {})
70
+ end
71
+
72
+ def test_if_from_variable
73
+ assert_template_result('','{% if var %} NO {% endif %}', 'var' => false)
74
+ assert_template_result('','{% if var %} NO {% endif %}', 'var' => nil)
75
+ assert_template_result('','{% if foo.bar %} NO {% endif %}', 'foo' => {'bar' => false})
76
+ assert_template_result('','{% if foo.bar %} NO {% endif %}', 'foo' => {})
77
+ assert_template_result('','{% if foo.bar %} NO {% endif %}', 'foo' => nil)
78
+ assert_template_result('','{% if foo.bar %} NO {% endif %}', 'foo' => true)
79
+
80
+ assert_template_result(' YES ','{% if var %} YES {% endif %}', 'var' => "text")
81
+ assert_template_result(' YES ','{% if var %} YES {% endif %}', 'var' => true)
82
+ assert_template_result(' YES ','{% if var %} YES {% endif %}', 'var' => 1)
83
+ assert_template_result(' YES ','{% if var %} YES {% endif %}', 'var' => {})
84
+ assert_template_result(' YES ','{% if var %} YES {% endif %}', 'var' => [])
85
+ assert_template_result(' YES ','{% if "foo" %} YES {% endif %}')
86
+ assert_template_result(' YES ','{% if foo.bar %} YES {% endif %}', 'foo' => {'bar' => true})
87
+ assert_template_result(' YES ','{% if foo.bar %} YES {% endif %}', 'foo' => {'bar' => "text"})
88
+ assert_template_result(' YES ','{% if foo.bar %} YES {% endif %}', 'foo' => {'bar' => 1 })
89
+ assert_template_result(' YES ','{% if foo.bar %} YES {% endif %}', 'foo' => {'bar' => {} })
90
+ assert_template_result(' YES ','{% if foo.bar %} YES {% endif %}', 'foo' => {'bar' => [] })
91
+
92
+ assert_template_result(' YES ','{% if var %} NO {% else %} YES {% endif %}', 'var' => false)
93
+ assert_template_result(' YES ','{% if var %} NO {% else %} YES {% endif %}', 'var' => nil)
94
+ assert_template_result(' YES ','{% if var %} YES {% else %} NO {% endif %}', 'var' => true)
95
+ assert_template_result(' YES ','{% if "foo" %} YES {% else %} NO {% endif %}', 'var' => "text")
96
+
97
+ assert_template_result(' YES ','{% if foo.bar %} NO {% else %} YES {% endif %}', 'foo' => {'bar' => false})
98
+ assert_template_result(' YES ','{% if foo.bar %} YES {% else %} NO {% endif %}', 'foo' => {'bar' => true})
99
+ assert_template_result(' YES ','{% if foo.bar %} YES {% else %} NO {% endif %}', 'foo' => {'bar' => "text"})
100
+ assert_template_result(' YES ','{% if foo.bar %} NO {% else %} YES {% endif %}', 'foo' => {'notbar' => true})
101
+ assert_template_result(' YES ','{% if foo.bar %} NO {% else %} YES {% endif %}', 'foo' => {})
102
+ assert_template_result(' YES ','{% if foo.bar %} NO {% else %} YES {% endif %}', 'notfoo' => {'bar' => true})
103
+ end
104
+
105
+ def test_nested_if
106
+ assert_template_result('', '{% if false %}{% if false %} NO {% endif %}{% endif %}')
107
+ assert_template_result('', '{% if false %}{% if true %} NO {% endif %}{% endif %}')
108
+ assert_template_result('', '{% if true %}{% if false %} NO {% endif %}{% endif %}')
109
+ assert_template_result(' YES ', '{% if true %}{% if true %} YES {% endif %}{% endif %}')
110
+
111
+ assert_template_result(' YES ', '{% if true %}{% if true %} YES {% else %} NO {% endif %}{% else %} NO {% endif %}')
112
+ assert_template_result(' YES ', '{% if true %}{% if false %} NO {% else %} YES {% endif %}{% else %} NO {% endif %}')
113
+ assert_template_result(' YES ', '{% if false %}{% if true %} NO {% else %} NONO {% endif %}{% else %} YES {% endif %}')
114
+
115
+ end
116
+
117
+ def test_comparisons_on_null
118
+ assert_template_result('','{% if null < 10 %} NO {% endif %}')
119
+ assert_template_result('','{% if null <= 10 %} NO {% endif %}')
120
+ assert_template_result('','{% if null >= 10 %} NO {% endif %}')
121
+ assert_template_result('','{% if null > 10 %} NO {% endif %}')
122
+
123
+ assert_template_result('','{% if 10 < null %} NO {% endif %}')
124
+ assert_template_result('','{% if 10 <= null %} NO {% endif %}')
125
+ assert_template_result('','{% if 10 >= null %} NO {% endif %}')
126
+ assert_template_result('','{% if 10 > null %} NO {% endif %}')
127
+ end
128
+
129
+ def test_else_if
130
+ assert_template_result('0','{% if 0 == 0 %}0{% elsif 1 == 1%}1{% else %}2{% endif %}')
131
+ assert_template_result('1','{% if 0 != 0 %}0{% elsif 1 == 1%}1{% else %}2{% endif %}')
132
+ assert_template_result('2','{% if 0 != 0 %}0{% elsif 1 != 1%}1{% else %}2{% endif %}')
133
+
134
+ assert_template_result('elsif','{% if false %}if{% elsif true %}elsif{% endif %}')
135
+ end
136
+
137
+ def test_syntax_error_no_variable
138
+ assert_raise(SyntaxError){ assert_template_result('', '{% if jerry == 1 %}')}
139
+ end
140
+
141
+ def test_syntax_error_no_expression
142
+ assert_raise(SyntaxError) { assert_template_result('', '{% if %}') }
143
+ end
144
+
145
+ def test_if_with_custom_condition
146
+ Condition.operators['contains'] = :[]
147
+
148
+ assert_template_result('yes', %({% if 'bob' contains 'o' %}yes{% endif %}))
149
+ assert_template_result('no', %({% if 'bob' contains 'f' %}yes{% else %}no{% endif %}))
150
+ ensure
151
+ Condition.operators.delete 'contains'
152
+ end
153
+
154
+ def test_operators_are_ignored_unless_isolated
155
+ Condition.operators['contains'] = :[]
156
+
157
+ assert_template_result('yes',
158
+ %({% if 'gnomeslab-and-or-liquid' contains 'gnomeslab-and-or-liquid' %}yes{% endif %}))
159
+ end
160
+ end # IfElseTest