drnic-liquid 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (113) hide show
  1. data/CHANGELOG +44 -0
  2. data/History.txt +44 -0
  3. data/MIT-LICENSE +20 -0
  4. data/Manifest.txt +34 -0
  5. data/README.rdoc +44 -0
  6. data/Rakefile +30 -0
  7. data/example/server/example_servlet.rb +37 -0
  8. data/example/server/liquid_servlet.rb +28 -0
  9. data/example/server/server.rb +12 -0
  10. data/example/server/templates/index.liquid +6 -0
  11. data/example/server/templates/products.liquid +45 -0
  12. data/init.rb +8 -0
  13. data/lib/extras/liquid_view.rb +51 -0
  14. data/lib/liquid.rb +70 -0
  15. data/lib/liquid/block.rb +102 -0
  16. data/lib/liquid/condition.rb +120 -0
  17. data/lib/liquid/context.rb +221 -0
  18. data/lib/liquid/document.rb +17 -0
  19. data/lib/liquid/drop.rb +51 -0
  20. data/lib/liquid/errors.rb +11 -0
  21. data/lib/liquid/extensions.rb +56 -0
  22. data/lib/liquid/file_system.rb +62 -0
  23. data/lib/liquid/htmltags.rb +74 -0
  24. data/lib/liquid/module_ex.rb +62 -0
  25. data/lib/liquid/standardfilters.rb +209 -0
  26. data/lib/liquid/strainer.rb +51 -0
  27. data/lib/liquid/tag.rb +26 -0
  28. data/lib/liquid/tags/assign.rb +33 -0
  29. data/lib/liquid/tags/capture.rb +35 -0
  30. data/lib/liquid/tags/case.rb +83 -0
  31. data/lib/liquid/tags/comment.rb +9 -0
  32. data/lib/liquid/tags/cycle.rb +59 -0
  33. data/lib/liquid/tags/for.rb +136 -0
  34. data/lib/liquid/tags/if.rb +79 -0
  35. data/lib/liquid/tags/ifchanged.rb +20 -0
  36. data/lib/liquid/tags/include.rb +55 -0
  37. data/lib/liquid/tags/unless.rb +33 -0
  38. data/lib/liquid/template.rb +147 -0
  39. data/lib/liquid/variable.rb +49 -0
  40. data/liquid.gemspec +40 -0
  41. data/performance/shopify.rb +92 -0
  42. data/performance/shopify/comment_form.rb +33 -0
  43. data/performance/shopify/database.rb +45 -0
  44. data/performance/shopify/json_filter.rb +7 -0
  45. data/performance/shopify/liquid.rb +18 -0
  46. data/performance/shopify/money_filter.rb +18 -0
  47. data/performance/shopify/paginate.rb +93 -0
  48. data/performance/shopify/shop_filter.rb +98 -0
  49. data/performance/shopify/tag_filter.rb +25 -0
  50. data/performance/shopify/vision.database.yml +945 -0
  51. data/performance/shopify/weight_filter.rb +11 -0
  52. data/performance/tests/dropify/article.liquid +74 -0
  53. data/performance/tests/dropify/blog.liquid +33 -0
  54. data/performance/tests/dropify/cart.liquid +66 -0
  55. data/performance/tests/dropify/collection.liquid +22 -0
  56. data/performance/tests/dropify/index.liquid +47 -0
  57. data/performance/tests/dropify/page.liquid +8 -0
  58. data/performance/tests/dropify/product.liquid +68 -0
  59. data/performance/tests/dropify/theme.liquid +105 -0
  60. data/performance/tests/ripen/article.liquid +74 -0
  61. data/performance/tests/ripen/blog.liquid +13 -0
  62. data/performance/tests/ripen/cart.liquid +54 -0
  63. data/performance/tests/ripen/collection.liquid +29 -0
  64. data/performance/tests/ripen/index.liquid +32 -0
  65. data/performance/tests/ripen/page.liquid +4 -0
  66. data/performance/tests/ripen/product.liquid +75 -0
  67. data/performance/tests/ripen/theme.liquid +85 -0
  68. data/performance/tests/tribble/404.liquid +56 -0
  69. data/performance/tests/tribble/article.liquid +98 -0
  70. data/performance/tests/tribble/blog.liquid +41 -0
  71. data/performance/tests/tribble/cart.liquid +134 -0
  72. data/performance/tests/tribble/collection.liquid +70 -0
  73. data/performance/tests/tribble/index.liquid +94 -0
  74. data/performance/tests/tribble/page.liquid +56 -0
  75. data/performance/tests/tribble/product.liquid +116 -0
  76. data/performance/tests/tribble/search.liquid +51 -0
  77. data/performance/tests/tribble/theme.liquid +90 -0
  78. data/performance/tests/vogue/article.liquid +66 -0
  79. data/performance/tests/vogue/blog.liquid +32 -0
  80. data/performance/tests/vogue/cart.liquid +58 -0
  81. data/performance/tests/vogue/collection.liquid +19 -0
  82. data/performance/tests/vogue/index.liquid +22 -0
  83. data/performance/tests/vogue/page.liquid +3 -0
  84. data/performance/tests/vogue/product.liquid +62 -0
  85. data/performance/tests/vogue/theme.liquid +122 -0
  86. data/test/assign_test.rb +11 -0
  87. data/test/block_test.rb +58 -0
  88. data/test/condition_test.rb +109 -0
  89. data/test/context_test.rb +482 -0
  90. data/test/drop_test.rb +162 -0
  91. data/test/error_handling_test.rb +89 -0
  92. data/test/extra/breakpoint.rb +547 -0
  93. data/test/extra/caller.rb +80 -0
  94. data/test/file_system_test.rb +30 -0
  95. data/test/filter_test.rb +95 -0
  96. data/test/helper.rb +20 -0
  97. data/test/html_tag_test.rb +31 -0
  98. data/test/if_else_test.rb +131 -0
  99. data/test/include_tag_test.rb +115 -0
  100. data/test/module_ex_test.rb +89 -0
  101. data/test/output_test.rb +121 -0
  102. data/test/parsing_quirks_test.rb +41 -0
  103. data/test/regexp_test.rb +45 -0
  104. data/test/security_test.rb +41 -0
  105. data/test/standard_filter_test.rb +161 -0
  106. data/test/standard_tag_test.rb +400 -0
  107. data/test/statements_test.rb +137 -0
  108. data/test/strainer_test.rb +21 -0
  109. data/test/template_test.rb +26 -0
  110. data/test/test_helper.rb +20 -0
  111. data/test/unless_else_test.rb +27 -0
  112. data/test/variable_test.rb +172 -0
  113. metadata +187 -0
@@ -0,0 +1,400 @@
1
+ require File.dirname(__FILE__) + '/helper'
2
+
3
+
4
+ class StandardTagTest < Test::Unit::TestCase
5
+ include Liquid
6
+
7
+
8
+ def test_tag
9
+ tag = Tag.new('tag', [], [])
10
+ assert_equal 'liquid::tag', tag.name
11
+ assert_equal '', tag.render(Context.new)
12
+ end
13
+
14
+ def test_no_transform
15
+ assert_template_result('this text should come out of the template without change...',
16
+ 'this text should come out of the template without change...')
17
+ assert_template_result('blah','blah')
18
+ assert_template_result('<blah>','<blah>')
19
+ assert_template_result('|,.:','|,.:')
20
+ assert_template_result('','')
21
+
22
+ text = %|this shouldnt see any transformation either but has multiple lines
23
+ as you can clearly see here ...|
24
+ assert_template_result(text,text)
25
+ end
26
+
27
+ def test_has_a_block_which_does_nothing
28
+ assert_template_result(%|the comment block should be removed .. right?|,
29
+ %|the comment block should be removed {%comment%} be gone.. {%end%} .. right?|)
30
+
31
+ assert_template_result('','{%comment%}{%end%}')
32
+ assert_template_result('','{%comment%}{% end %}')
33
+ assert_template_result('','{% comment %}{%end%}')
34
+ assert_template_result('','{% comment %}{% end %}')
35
+ assert_template_result('','{%comment%}comment{%end%}')
36
+ assert_template_result('','{% comment %}comment{% end %}')
37
+
38
+ assert_template_result('foobar','foo{%comment%}comment{%end%}bar')
39
+ assert_template_result('foobar','foo{% comment %}comment{% end %}bar')
40
+ assert_template_result('foobar','foo{%comment%} comment {%end%}bar')
41
+ assert_template_result('foobar','foo{% comment %} comment {% end %}bar')
42
+
43
+ assert_template_result('foo bar','foo {%comment%} {%end%} bar')
44
+ assert_template_result('foo bar','foo {%comment%}comment{%end%} bar')
45
+ assert_template_result('foo bar','foo {%comment%} comment {%end%} bar')
46
+
47
+ assert_template_result('foobar','foo{%comment%}
48
+ {%end%}bar')
49
+ end
50
+
51
+ def test_for
52
+ assert_template_result(' yo yo yo yo ','{%for item in array%} yo {%end%}','array' => [1,2,3,4])
53
+ assert_template_result('yoyo','{%for item in array%}yo{%end%}','array' => [1,2])
54
+ assert_template_result(' yo ','{%for item in array%} yo {%end%}','array' => [1])
55
+ assert_template_result('','{%for item in array%}{%end%}','array' => [1,2])
56
+ expected = <<HERE
57
+
58
+ yo
59
+
60
+ yo
61
+
62
+ yo
63
+
64
+ HERE
65
+ template = <<HERE
66
+ {%for item in array%}
67
+ yo
68
+ {%end%}
69
+ HERE
70
+ assert_template_result(expected,template,'array' => [1,2,3])
71
+ end
72
+
73
+ def test_for_with_range
74
+ assert_template_result(' 1 2 3 ','{%for item in (1..3) %} {{item}} {%end%}')
75
+ end
76
+
77
+ def test_for_with_variable
78
+ assert_template_result(' 1 2 3 ','{%for item in array%} {{item}} {%end%}','array' => [1,2,3])
79
+ assert_template_result('123','{%for item in array%}{{item}}{%end%}','array' => [1,2,3])
80
+ assert_template_result('123','{% for item in array %}{{item}}{% end %}','array' => [1,2,3])
81
+ assert_template_result('abcd','{%for item in array%}{{item}}{%end%}','array' => ['a','b','c','d'])
82
+ assert_template_result('a b c','{%for item in array%}{{item}}{%end%}','array' => ['a',' ','b',' ','c'])
83
+ assert_template_result('abc','{%for item in array%}{{item}}{%end%}','array' => ['a','','b','','c'])
84
+ end
85
+
86
+ def test_for_helpers
87
+ assigns = {'array' => [1,2,3] }
88
+ assert_template_result(' 1/3 2/3 3/3 ','{%for item in array%} {{forloop.index}}/{{forloop.length}} {%end%}',assigns)
89
+ assert_template_result(' 1 2 3 ','{%for item in array%} {{forloop.index}} {%end%}',assigns)
90
+ assert_template_result(' 0 1 2 ','{%for item in array%} {{forloop.index0}} {%end%}',assigns)
91
+ assert_template_result(' 2 1 0 ','{%for item in array%} {{forloop.rindex0}} {%end%}',assigns)
92
+ assert_template_result(' 3 2 1 ','{%for item in array%} {{forloop.rindex}} {%end%}',assigns)
93
+ assert_template_result(' true false false ','{%for item in array%} {{forloop.first}} {%end%}',assigns)
94
+ assert_template_result(' false false true ','{%for item in array%} {{forloop.last}} {%end%}',assigns)
95
+ end
96
+
97
+ def test_for_and_if
98
+ assigns = {'array' => [1,2,3] }
99
+ assert_template_result('+--', '{%for item in array%}{% if forloop.first %}+{% else %}-{% end %}{%end%}', assigns)
100
+ end
101
+
102
+ def test_limiting
103
+ assigns = {'array' => [1,2,3,4,5,6,7,8,9,0]}
104
+ assert_template_result('12','{%for i in array limit:2 %}{{ i }}{%end%}',assigns)
105
+ assert_template_result('1234','{%for i in array limit:4 %}{{ i }}{%end%}',assigns)
106
+ assert_template_result('3456','{%for i in array limit:4 offset:2 %}{{ i }}{%end%}',assigns)
107
+ assert_template_result('3456','{%for i in array limit: 4 offset: 2 %}{{ i }}{%end%}',assigns)
108
+ end
109
+
110
+ def test_dynamic_variable_limiting
111
+ assigns = {'array' => [1,2,3,4,5,6,7,8,9,0]}
112
+ assigns['limit'] = 2
113
+ assigns['offset'] = 2
114
+ assert_template_result('34','{%for i in array limit: limit offset: offset %}{{ i }}{%end%}',assigns)
115
+ end
116
+
117
+ def test_nested_for
118
+ assigns = {'array' => [[1,2],[3,4],[5,6]] }
119
+ assert_template_result('123456','{%for item in array%}{%for i in item%}{{ i }}{%end%}{%end%}',assigns)
120
+ end
121
+
122
+ def test_offset_only
123
+ assigns = {'array' => [1,2,3,4,5,6,7,8,9,0]}
124
+ assert_template_result('890','{%for i in array offset:7 %}{{ i }}{%end%}',assigns)
125
+ end
126
+
127
+ def test_pause_resume
128
+ assigns = {'array' => {'items' => [1,2,3,4,5,6,7,8,9,0]}}
129
+ markup = <<-MKUP
130
+ {%for i in array.items limit: 3 %}{{i}}{%end%}
131
+ next
132
+ {%for i in array.items offset:continue limit: 3 %}{{i}}{%end%}
133
+ next
134
+ {%for i in array.items offset:continue limit: 3 %}{{i}}{%end%}
135
+ MKUP
136
+ expected = <<-XPCTD
137
+ 123
138
+ next
139
+ 456
140
+ next
141
+ 789
142
+ XPCTD
143
+ assert_template_result(expected,markup,assigns)
144
+ end
145
+
146
+ def test_pause_resume_limit
147
+ assigns = {'array' => {'items' => [1,2,3,4,5,6,7,8,9,0]}}
148
+ markup = <<-MKUP
149
+ {%for i in array.items limit:3 %}{{i}}{%end%}
150
+ next
151
+ {%for i in array.items offset:continue limit:3 %}{{i}}{%end%}
152
+ next
153
+ {%for i in array.items offset:continue limit:1 %}{{i}}{%end%}
154
+ MKUP
155
+ expected = <<-XPCTD
156
+ 123
157
+ next
158
+ 456
159
+ next
160
+ 7
161
+ XPCTD
162
+ assert_template_result(expected,markup,assigns)
163
+ end
164
+
165
+ def test_pause_resume_BIG_limit
166
+ assigns = {'array' => {'items' => [1,2,3,4,5,6,7,8,9,0]}}
167
+ markup = <<-MKUP
168
+ {%for i in array.items limit:3 %}{{i}}{%end%}
169
+ next
170
+ {%for i in array.items offset:continue limit:3 %}{{i}}{%end%}
171
+ next
172
+ {%for i in array.items offset:continue limit:1000 %}{{i}}{%end%}
173
+ MKUP
174
+ expected = <<-XPCTD
175
+ 123
176
+ next
177
+ 456
178
+ next
179
+ 7890
180
+ XPCTD
181
+ assert_template_result(expected,markup,assigns)
182
+ end
183
+
184
+
185
+ def test_pause_resume_BIG_offset
186
+ assigns = {'array' => {'items' => [1,2,3,4,5,6,7,8,9,0]}}
187
+ markup = %q({%for i in array.items limit:3 %}{{i}}{%end%}
188
+ next
189
+ {%for i in array.items offset:continue limit:3 %}{{i}}{%end%}
190
+ next
191
+ {%for i in array.items offset:continue limit:3 offset:1000 %}{{i}}{%end%})
192
+ expected = %q(123
193
+ next
194
+ 456
195
+ next
196
+ )
197
+ assert_template_result(expected,markup,assigns)
198
+ end
199
+
200
+ def test_assign
201
+ assigns = {'var' => 'content' }
202
+ assert_template_result('var2: var2:content','var2:{{var2}} {%assign var2 = var%} var2:{{var2}}',assigns)
203
+
204
+ end
205
+
206
+ def test_hyphenated_assign
207
+ assigns = {'a-b' => '1' }
208
+ assert_template_result('a-b:1 a-b:2','a-b:{{a-b}} {%assign a-b = 2 %}a-b:{{a-b}}',assigns)
209
+
210
+ end
211
+
212
+ def test_assign_with_colon_and_spaces
213
+ assigns = {'var' => {'a:b c' => {'paged' => '1' }}}
214
+ assert_template_result('var2: 1','{%assign var2 = var["a:b c"].paged %}var2: {{var2}}',assigns)
215
+ end
216
+
217
+ def test_capture
218
+ assigns = {'var' => 'content' }
219
+ assert_template_result('content foo content foo ','{{ var2 }}{% capture var2 %}{{ var }} foo {% end %}{{ var2 }}{{ var2 }}', assigns)
220
+ end
221
+
222
+ def test_capture_detects_bad_syntax
223
+ assert_raise(SyntaxError) do
224
+ assert_template_result('content foo content foo ','{{ var2 }}{% capture %}{{ var }} foo {% end %}{{ var2 }}{{ var2 }}', {'var' => 'content' })
225
+ end
226
+ end
227
+
228
+ def test_case
229
+ assigns = {'condition' => 2 }
230
+ assert_template_result(' its 2 ','{% case condition %}{% when 1 %} its 1 {% when 2 %} its 2 {% end %}', assigns)
231
+
232
+ assigns = {'condition' => 1 }
233
+ assert_template_result(' its 1 ','{% case condition %}{% when 1 %} its 1 {% when 2 %} its 2 {% end %}', assigns)
234
+
235
+ assigns = {'condition' => 3 }
236
+ assert_template_result('','{% case condition %}{% when 1 %} its 1 {% when 2 %} its 2 {% end %}', assigns)
237
+
238
+ assigns = {'condition' => "string here" }
239
+ assert_template_result(' hit ','{% case condition %}{% when "string here" %} hit {% end %}', assigns)
240
+
241
+ assigns = {'condition' => "bad string here" }
242
+ assert_template_result('','{% case condition %}{% when "string here" %} hit {% end %}', assigns)
243
+ end
244
+
245
+ def test_case_with_else
246
+
247
+ assigns = {'condition' => 5 }
248
+ assert_template_result(' hit ','{% case condition %}{% when 5 %} hit {% else %} else {% end %}', assigns)
249
+
250
+ assigns = {'condition' => 6 }
251
+ assert_template_result(' else ','{% case condition %}{% when 5 %} hit {% else %} else {% end %}', assigns)
252
+
253
+ assigns = {'condition' => 6 }
254
+ assert_template_result(' else ','{% case condition %} {% when 5 %} hit {% else %} else {% end %}', assigns)
255
+
256
+
257
+ end
258
+
259
+ def test_case_on_size
260
+ assert_template_result('', '{% case a.size %}{% when 1 %}1{% when 2 %}2{% end %}', 'a' => [])
261
+ assert_template_result('1' , '{% case a.size %}{% when 1 %}1{% when 2 %}2{% end %}', 'a' => [1])
262
+ assert_template_result('2' , '{% case a.size %}{% when 1 %}1{% when 2 %}2{% end %}', 'a' => [1, 1])
263
+ assert_template_result('', '{% case a.size %}{% when 1 %}1{% when 2 %}2{% end %}', 'a' => [1, 1, 1])
264
+ assert_template_result('', '{% case a.size %}{% when 1 %}1{% when 2 %}2{% end %}', 'a' => [1, 1, 1, 1])
265
+ assert_template_result('', '{% case a.size %}{% when 1 %}1{% when 2 %}2{% end %}', 'a' => [1, 1, 1, 1, 1])
266
+ end
267
+
268
+ def test_case_on_size_with_else
269
+ assert_template_result('else', '{% case a.size %}{% when 1 %}1{% when 2 %}2{% else %}else{% end %}', 'a' => [])
270
+ assert_template_result('1', '{% case a.size %}{% when 1 %}1{% when 2 %}2{% else %}else{% end %}', 'a' => [1])
271
+ assert_template_result('2', '{% case a.size %}{% when 1 %}1{% when 2 %}2{% else %}else{% end %}', 'a' => [1, 1])
272
+ assert_template_result('else', '{% case a.size %}{% when 1 %}1{% when 2 %}2{% else %}else{% end %}', 'a' => [1, 1, 1])
273
+ assert_template_result('else', '{% case a.size %}{% when 1 %}1{% when 2 %}2{% else %}else{% end %}', 'a' => [1, 1, 1, 1])
274
+ assert_template_result('else', '{% case a.size %}{% when 1 %}1{% when 2 %}2{% else %}else{% end %}', 'a' => [1, 1, 1, 1, 1])
275
+ end
276
+
277
+ def test_case_on_length_with_else
278
+ assert_template_result('else', '{% case a.empty? %}{% when true %}true{% when false %}false{% else %}else{% end %}', {})
279
+ assert_template_result('false', '{% case false %}{% when true %}true{% when false %}false{% else %}else{% end %}', {})
280
+ assert_template_result('true', '{% case true %}{% when true %}true{% when false %}false{% else %}else{% end %}', {})
281
+ assert_template_result('else', '{% case NULL %}{% when true %}true{% when false %}false{% else %}else{% end %}', {})
282
+ end
283
+
284
+ def test_assign_from_case
285
+ # Example from the shopify forums
286
+ code = %q({% case collection.handle %}{% when 'menswear-jackets' %}{% assign ptitle = 'menswear' %}{% when 'menswear-t-shirts' %}{% assign ptitle = 'menswear' %}{% else %}{% assign ptitle = 'womenswear' %}{% end %}{{ ptitle }})
287
+ template = Liquid::Template.parse(code)
288
+ assert_equal "menswear", template.render("collection" => {'handle' => 'menswear-jackets'})
289
+ assert_equal "menswear", template.render("collection" => {'handle' => 'menswear-t-shirts'})
290
+ assert_equal "womenswear", template.render("collection" => {'handle' => 'x'})
291
+ assert_equal "womenswear", template.render("collection" => {'handle' => 'y'})
292
+ assert_equal "womenswear", template.render("collection" => {'handle' => 'z'})
293
+ end
294
+
295
+ def test_case_when_or
296
+ code = '{% case condition %}{% when 1 or 2 or 3 %} its 1 or 2 or 3 {% when 4 %} its 4 {% end %}'
297
+ assert_template_result(' its 1 or 2 or 3 ', code, {'condition' => 1 })
298
+ assert_template_result(' its 1 or 2 or 3 ', code, {'condition' => 2 })
299
+ assert_template_result(' its 1 or 2 or 3 ', code, {'condition' => 3 })
300
+ assert_template_result(' its 4 ', code, {'condition' => 4 })
301
+ assert_template_result('', code, {'condition' => 5 })
302
+
303
+ code = '{% case condition %}{% when 1 or "string" or null %} its 1 or 2 or 3 {% when 4 %} its 4 {% end %}'
304
+ assert_template_result(' its 1 or 2 or 3 ', code, {'condition' => 1 })
305
+ assert_template_result(' its 1 or 2 or 3 ', code, {'condition' => 'string' })
306
+ assert_template_result(' its 1 or 2 or 3 ', code, {'condition' => nil })
307
+ assert_template_result('', code, {'condition' => 'something else' })
308
+ end
309
+
310
+ def test_case_when_comma
311
+ code = '{% case condition %}{% when 1, 2, 3 %} its 1 or 2 or 3 {% when 4 %} its 4 {% end %}'
312
+ assert_template_result(' its 1 or 2 or 3 ', code, {'condition' => 1 })
313
+ assert_template_result(' its 1 or 2 or 3 ', code, {'condition' => 2 })
314
+ assert_template_result(' its 1 or 2 or 3 ', code, {'condition' => 3 })
315
+ assert_template_result(' its 4 ', code, {'condition' => 4 })
316
+ assert_template_result('', code, {'condition' => 5 })
317
+
318
+ code = '{% case condition %}{% when 1, "string", null %} its 1 or 2 or 3 {% when 4 %} its 4 {% end %}'
319
+ assert_template_result(' its 1 or 2 or 3 ', code, {'condition' => 1 })
320
+ assert_template_result(' its 1 or 2 or 3 ', code, {'condition' => 'string' })
321
+ assert_template_result(' its 1 or 2 or 3 ', code, {'condition' => nil })
322
+ assert_template_result('', code, {'condition' => 'something else' })
323
+ end
324
+
325
+ def test_assign
326
+ assert_equal 'variable', Liquid::Template.parse( '{% assign a = "variable"%}{{a}}' ).render
327
+ end
328
+
329
+ def test_assign_is_global
330
+ assert_equal 'variable', Liquid::Template.parse( '{%for i in (1..2) %}{% assign a = "variable"%}{% end %}{{a}}' ).render
331
+ end
332
+
333
+ def test_case_detects_bad_syntax
334
+ assert_raise(SyntaxError) do
335
+ assert_template_result('', '{% case false %}{% when %}true{% end %}', {})
336
+ end
337
+
338
+ assert_raise(SyntaxError) do
339
+ assert_template_result('', '{% case false %}{% huh %}true{% end %}', {})
340
+ end
341
+
342
+ end
343
+
344
+
345
+
346
+ def test_cycle
347
+
348
+ assert_template_result('one','{%cycle "one", "two"%}')
349
+ assert_template_result('one two','{%cycle "one", "two"%} {%cycle "one", "two"%}')
350
+
351
+ assert_template_result('one two one','{%cycle "one", "two"%} {%cycle "one", "two"%} {%cycle "one", "two"%}')
352
+
353
+ assert_template_result('text-align: left text-align: right','{%cycle "text-align: left", "text-align: right" %} {%cycle "text-align: left", "text-align: right"%}')
354
+
355
+ end
356
+
357
+ def test_multiple_cycles
358
+ assert_template_result('1 2 1 1 2 3 1','{%cycle 1,2%} {%cycle 1,2%} {%cycle 1,2%} {%cycle 1,2,3%} {%cycle 1,2,3%} {%cycle 1,2,3%} {%cycle 1,2,3%}')
359
+ end
360
+
361
+ def test_multiple_named_cycles
362
+ assert_template_result('one one two two one one','{%cycle 1: "one", "two" %} {%cycle 2: "one", "two" %} {%cycle 1: "one", "two" %} {%cycle 2: "one", "two" %} {%cycle 1: "one", "two" %} {%cycle 2: "one", "two" %}')
363
+ end
364
+
365
+ def test_multiple_named_cycles_with_names_from_context
366
+ assigns = {"var1" => 1, "var2" => 2 }
367
+ assert_template_result('one one two two one one','{%cycle var1: "one", "two" %} {%cycle var2: "one", "two" %} {%cycle var1: "one", "two" %} {%cycle var2: "one", "two" %} {%cycle var1: "one", "two" %} {%cycle var2: "one", "two" %}', assigns)
368
+ end
369
+
370
+ def test_size_of_array
371
+ assigns = {"array" => [1,2,3,4]}
372
+ assert_template_result('array has 4 elements', "array has {{ array.size }} elements", assigns)
373
+ end
374
+
375
+ def test_size_of_hash
376
+ assigns = {"hash" => {:a => 1, :b => 2, :c=> 3, :d => 4}}
377
+ assert_template_result('hash has 4 elements', "hash has {{ hash.size }} elements", assigns)
378
+ end
379
+
380
+ def test_illegal_symbols
381
+ assert_template_result('', '{% if true == empty %}?{% end %}', {})
382
+ assert_template_result('', '{% if true == null %}?{% end %}', {})
383
+ assert_template_result('', '{% if empty == true %}?{% end %}', {})
384
+ assert_template_result('', '{% if null == true %}?{% end %}', {})
385
+ end
386
+
387
+ def test_for_reversed
388
+ assigns = {'array' => [ 1, 2, 3] }
389
+ assert_template_result('321','{%for item in array reversed %}{{item}}{%end%}',assigns)
390
+ end
391
+
392
+
393
+ def test_ifchanged
394
+ assigns = {'array' => [ 1, 1, 2, 2, 3, 3] }
395
+ assert_template_result('123','{%for item in array%}{%ifchanged%}{{item}}{% end %}{%end%}',assigns)
396
+
397
+ assigns = {'array' => [ 1, 1, 1, 1] }
398
+ assert_template_result('1','{%for item in array%}{%ifchanged%}{{item}}{% end %}{%end%}',assigns)
399
+ end
400
+ end
@@ -0,0 +1,137 @@
1
+ #!/usr/bin/env ruby
2
+ require File.dirname(__FILE__) + '/helper'
3
+
4
+ class StatementsTest < Test::Unit::TestCase
5
+ include Liquid
6
+
7
+
8
+ def test_true_eql_true
9
+ text = %| {% if true == true %} true {% else %} false {% end %} |
10
+ expected = %| true |
11
+ assert_equal expected, Template.parse(text).render
12
+ end
13
+
14
+ def test_true_not_eql_true
15
+ text = %| {% if true != true %} true {% else %} false {% end %} |
16
+ expected = %| false |
17
+ assert_equal expected, Template.parse(text).render
18
+ end
19
+
20
+ def test_true_lq_true
21
+ text = %| {% if 0 > 0 %} true {% else %} false {% end %} |
22
+ expected = %| false |
23
+ assert_equal expected, Template.parse(text).render
24
+ end
25
+
26
+ def test_one_lq_zero
27
+ text = %| {% if 1 > 0 %} true {% else %} false {% end %} |
28
+ expected = %| true |
29
+ assert_equal expected, Template.parse(text).render
30
+ end
31
+
32
+ def test_zero_lq_one
33
+ text = %| {% if 0 < 1 %} true {% else %} false {% end %} |
34
+ expected = %| true |
35
+ assert_equal expected, Template.parse(text).render
36
+ end
37
+
38
+ def test_zero_lq_or_equal_one
39
+ text = %| {% if 0 <= 0 %} true {% else %} false {% end %} |
40
+ expected = %| true |
41
+ assert_equal expected, Template.parse(text).render
42
+ end
43
+
44
+ def test_zero_lq_or_equal_one_involving_nil
45
+ text = %| {% if null <= 0 %} true {% else %} false {% end %} |
46
+ expected = %| false |
47
+ assert_equal expected, Template.parse(text).render
48
+
49
+
50
+ text = %| {% if 0 <= null %} true {% else %} false {% end %} |
51
+ expected = %| false |
52
+ assert_equal expected, Template.parse(text).render
53
+ end
54
+
55
+ def test_zero_lqq_or_equal_one
56
+ text = %| {% if 0 >= 0 %} true {% else %} false {% end %} |
57
+ expected = %| true |
58
+ assert_equal expected, Template.parse(text).render
59
+ end
60
+
61
+ def test_strings
62
+ text = %| {% if 'test' == 'test' %} true {% else %} false {% end %} |
63
+ expected = %| true |
64
+ assert_equal expected, Template.parse(text).render
65
+ end
66
+
67
+ def test_strings_not_equal
68
+ text = %| {% if 'test' != 'test' %} true {% else %} false {% end %} |
69
+ expected = %| false |
70
+ assert_equal expected, Template.parse(text).render
71
+ end
72
+
73
+ def test_var_strings_equal
74
+ text = %| {% if var == "hello there!" %} true {% else %} false {% end %} |
75
+ expected = %| true |
76
+ assert_equal expected, Template.parse(text).render('var' => 'hello there!')
77
+ end
78
+
79
+ def test_var_strings_are_not_equal
80
+ text = %| {% if "hello there!" == var %} true {% else %} false {% end %} |
81
+ expected = %| true |
82
+ assert_equal expected, Template.parse(text).render('var' => 'hello there!')
83
+ end
84
+
85
+ def test_var_and_long_string_are_equal
86
+ text = %| {% if var == 'hello there!' %} true {% else %} false {% end %} |
87
+ expected = %| true |
88
+ assert_equal expected, Template.parse(text).render('var' => 'hello there!')
89
+ end
90
+
91
+
92
+ def test_var_and_long_string_are_equal_backwards
93
+ text = %| {% if 'hello there!' == var %} true {% else %} false {% end %} |
94
+ expected = %| true |
95
+ assert_equal expected, Template.parse(text).render('var' => 'hello there!')
96
+ end
97
+
98
+ #def test_is_nil
99
+ # text = %| {% if var != nil %} true {% else %} false {% end %} |
100
+ # @template.assigns = { 'var' => 'hello there!'}
101
+ # expected = %| true |
102
+ # assert_equal expected, @template.parse(text)
103
+ #end
104
+
105
+ def test_is_collection_empty
106
+ text = %| {% if array == empty %} true {% else %} false {% end %} |
107
+ expected = %| true |
108
+ assert_equal expected, Template.parse(text).render('array' => [])
109
+ end
110
+
111
+ def test_is_not_collection_empty
112
+ text = %| {% if array == empty %} true {% else %} false {% end %} |
113
+ expected = %| false |
114
+ assert_equal expected, Template.parse(text).render('array' => [1,2,3])
115
+ end
116
+
117
+ def test_nil
118
+ text = %| {% if var == nil %} true {% else %} false {% end %} |
119
+ expected = %| true |
120
+ assert_equal expected, Template.parse(text).render('var' => nil)
121
+
122
+ text = %| {% if var == null %} true {% else %} false {% end %} |
123
+ expected = %| true |
124
+ assert_equal expected, Template.parse(text).render('var' => nil)
125
+ end
126
+
127
+ def test_not_nil
128
+ text = %| {% if var != nil %} true {% else %} false {% end %} |
129
+ expected = %| true |
130
+ assert_equal expected, Template.parse(text).render('var' => 1 )
131
+
132
+ text = %| {% if var != null %} true {% else %} false {% end %} |
133
+ expected = %| true |
134
+ assert_equal expected, Template.parse(text).render('var' => 1 )
135
+ end
136
+
137
+ end