spinto-liquid 2.3.0.1

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 (62) hide show
  1. data/History.md +56 -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/block.rb +101 -0
  6. data/lib/liquid/condition.rb +120 -0
  7. data/lib/liquid/context.rb +245 -0
  8. data/lib/liquid/document.rb +17 -0
  9. data/lib/liquid/drop.rb +49 -0
  10. data/lib/liquid/errors.rb +11 -0
  11. data/lib/liquid/extensions.rb +62 -0
  12. data/lib/liquid/file_system.rb +62 -0
  13. data/lib/liquid/htmltags.rb +75 -0
  14. data/lib/liquid/module_ex.rb +62 -0
  15. data/lib/liquid/standardfilters.rb +241 -0
  16. data/lib/liquid/strainer.rb +54 -0
  17. data/lib/liquid/tag.rb +26 -0
  18. data/lib/liquid/tags/assign.rb +33 -0
  19. data/lib/liquid/tags/capture.rb +35 -0
  20. data/lib/liquid/tags/case.rb +79 -0
  21. data/lib/liquid/tags/comment.rb +9 -0
  22. data/lib/liquid/tags/cycle.rb +59 -0
  23. data/lib/liquid/tags/decrement.rb +39 -0
  24. data/lib/liquid/tags/for.rb +190 -0
  25. data/lib/liquid/tags/if.rb +79 -0
  26. data/lib/liquid/tags/ifchanged.rb +20 -0
  27. data/lib/liquid/tags/include.rb +65 -0
  28. data/lib/liquid/tags/increment.rb +35 -0
  29. data/lib/liquid/tags/raw.rb +21 -0
  30. data/lib/liquid/tags/unless.rb +33 -0
  31. data/lib/liquid/template.rb +150 -0
  32. data/lib/liquid/variable.rb +50 -0
  33. data/lib/liquid.rb +66 -0
  34. data/test/liquid/assign_test.rb +21 -0
  35. data/test/liquid/block_test.rb +58 -0
  36. data/test/liquid/capture_test.rb +40 -0
  37. data/test/liquid/condition_test.rb +127 -0
  38. data/test/liquid/context_test.rb +478 -0
  39. data/test/liquid/drop_test.rb +162 -0
  40. data/test/liquid/error_handling_test.rb +81 -0
  41. data/test/liquid/file_system_test.rb +29 -0
  42. data/test/liquid/filter_test.rb +106 -0
  43. data/test/liquid/module_ex_test.rb +87 -0
  44. data/test/liquid/output_test.rb +116 -0
  45. data/test/liquid/parsing_quirks_test.rb +52 -0
  46. data/test/liquid/regexp_test.rb +44 -0
  47. data/test/liquid/security_test.rb +41 -0
  48. data/test/liquid/standard_filter_test.rb +195 -0
  49. data/test/liquid/strainer_test.rb +25 -0
  50. data/test/liquid/tags/for_tag_test.rb +215 -0
  51. data/test/liquid/tags/html_tag_test.rb +39 -0
  52. data/test/liquid/tags/if_else_tag_test.rb +160 -0
  53. data/test/liquid/tags/include_tag_test.rb +139 -0
  54. data/test/liquid/tags/increment_tag_test.rb +24 -0
  55. data/test/liquid/tags/raw_tag_test.rb +15 -0
  56. data/test/liquid/tags/standard_tag_test.rb +295 -0
  57. data/test/liquid/tags/statements_test.rb +134 -0
  58. data/test/liquid/tags/unless_else_tag_test.rb +26 -0
  59. data/test/liquid/template_test.rb +74 -0
  60. data/test/liquid/variable_test.rb +170 -0
  61. data/test/test_helper.rb +29 -0
  62. metadata +136 -0
@@ -0,0 +1,25 @@
1
+ require 'test_helper'
2
+
3
+ class StrainerTest < Test::Unit::TestCase
4
+ include Liquid
5
+
6
+ def test_strainer
7
+ strainer = Strainer.create(nil)
8
+ assert_equal false, strainer.respond_to?('__test__')
9
+ assert_equal false, strainer.respond_to?('test')
10
+ assert_equal false, strainer.respond_to?('instance_eval')
11
+ assert_equal false, strainer.respond_to?('__send__')
12
+ assert_equal true, strainer.respond_to?('size') # from the standard lib
13
+ end
14
+
15
+ def test_should_respond_to_two_parameters
16
+ strainer = Strainer.create(nil)
17
+ assert_equal true, strainer.respond_to?('size', false)
18
+ end
19
+
20
+ # Asserts that Object#respond_to_missing? is not being undefined in Ruby versions where it has been implemented
21
+ # Currently this method is only present in Ruby v1.9.2, or higher
22
+ def test_object_respond_to_missing
23
+ assert_equal Object.respond_to?(:respond_to_missing?), Strainer.create(nil).respond_to?(:respond_to_missing?)
24
+ end
25
+ end # StrainerTest
@@ -0,0 +1,215 @@
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_sorting
73
+ assigns = {'array' => [1,2,3,4,5,6,7,8,9,0]}
74
+ assert_template_result('0123456789', '{%for i in array order:ascending %}{{ i }}{%endfor%}', assigns)
75
+ assert_template_result('9876543210', '{%for i in array order:descending %}{{ i }}{%endfor%}', assigns)
76
+ assert_template_result('765', '{%for i in array order:descending offset:2 limit:3 %}{{ i }}{%endfor%}', assigns)
77
+
78
+ assigns = {'array' => [{"name" => 'A', "count" => '3'}, {"name" => 'C', "count" => '1'}, {"name" => 'B', "count" => '2'}]}
79
+ assert_template_result('A|B|C|', '{%for i in array sort_by:name %}{{ i["name"] }}|{%endfor%}', assigns)
80
+ assert_template_result('C|B|A|', '{%for i in array sort_by:name order:descending %}{{ i["name"] }}|{%endfor%}', assigns)
81
+ assert_template_result('1|2|3|', '{%for i in array sort_by:count %}{{ i["count"] }}|{%endfor%}', assigns)
82
+ assert_template_result('A|C|B|', '{%for i in array sort_by:missing_attribute %}{{ i["name"] }}|{%endfor%}', assigns)
83
+ end
84
+
85
+ def test_limiting
86
+ assigns = {'array' => [1,2,3,4,5,6,7,8,9,0]}
87
+ assert_template_result('12', '{%for i in array limit:2 %}{{ i }}{%endfor%}', assigns)
88
+ assert_template_result('1234', '{%for i in array limit:4 %}{{ i }}{%endfor%}', assigns)
89
+ assert_template_result('3456', '{%for i in array limit:4 offset:2 %}{{ i }}{%endfor%}', assigns)
90
+ assert_template_result('3456', '{%for i in array limit: 4 offset: 2 %}{{ i }}{%endfor%}', assigns)
91
+ end
92
+
93
+ def test_dynamic_variable_limiting
94
+ assigns = {'array' => [1,2,3,4,5,6,7,8,9,0]}
95
+ assigns['limit'] = 2
96
+ assigns['offset'] = 2
97
+
98
+ assert_template_result('34', '{%for i in array limit: limit offset: offset %}{{ i }}{%endfor%}', assigns)
99
+ end
100
+
101
+ def test_nested_for
102
+ assigns = {'array' => [[1,2],[3,4],[5,6]] }
103
+ assert_template_result('123456', '{%for item in array%}{%for i in item%}{{ i }}{%endfor%}{%endfor%}', assigns)
104
+ end
105
+
106
+ def test_offset_only
107
+ assigns = {'array' => [1,2,3,4,5,6,7,8,9,0]}
108
+ assert_template_result('890', '{%for i in array offset:7 %}{{ i }}{%endfor%}', assigns)
109
+ end
110
+
111
+ def test_pause_resume
112
+ assigns = {'array' => {'items' => [1,2,3,4,5,6,7,8,9,0]}}
113
+ markup = <<-MKUP
114
+ {%for i in array.items limit: 3 %}{{i}}{%endfor%}
115
+ next
116
+ {%for i in array.items offset:continue limit: 3 %}{{i}}{%endfor%}
117
+ next
118
+ {%for i in array.items offset:continue limit: 3 %}{{i}}{%endfor%}
119
+ MKUP
120
+ expected = <<-XPCTD
121
+ 123
122
+ next
123
+ 456
124
+ next
125
+ 789
126
+ XPCTD
127
+ assert_template_result(expected,markup,assigns)
128
+ end
129
+
130
+ def test_pause_resume_limit
131
+ assigns = {'array' => {'items' => [1,2,3,4,5,6,7,8,9,0]}}
132
+ markup = <<-MKUP
133
+ {%for i in array.items limit:3 %}{{i}}{%endfor%}
134
+ next
135
+ {%for i in array.items offset:continue limit:3 %}{{i}}{%endfor%}
136
+ next
137
+ {%for i in array.items offset:continue limit:1 %}{{i}}{%endfor%}
138
+ MKUP
139
+ expected = <<-XPCTD
140
+ 123
141
+ next
142
+ 456
143
+ next
144
+ 7
145
+ XPCTD
146
+ assert_template_result(expected,markup,assigns)
147
+ end
148
+
149
+ def test_pause_resume_BIG_limit
150
+ assigns = {'array' => {'items' => [1,2,3,4,5,6,7,8,9,0]}}
151
+ markup = <<-MKUP
152
+ {%for i in array.items limit:3 %}{{i}}{%endfor%}
153
+ next
154
+ {%for i in array.items offset:continue limit:3 %}{{i}}{%endfor%}
155
+ next
156
+ {%for i in array.items offset:continue limit:1000 %}{{i}}{%endfor%}
157
+ MKUP
158
+ expected = <<-XPCTD
159
+ 123
160
+ next
161
+ 456
162
+ next
163
+ 7890
164
+ XPCTD
165
+ assert_template_result(expected,markup,assigns)
166
+ end
167
+
168
+
169
+ def test_pause_resume_BIG_offset
170
+ assigns = {'array' => {'items' => [1,2,3,4,5,6,7,8,9,0]}}
171
+ markup = %q({%for i in array.items limit:3 %}{{i}}{%endfor%}
172
+ next
173
+ {%for i in array.items offset:continue limit:3 %}{{i}}{%endfor%}
174
+ next
175
+ {%for i in array.items offset:continue limit:3 offset:1000 %}{{i}}{%endfor%})
176
+ expected = %q(123
177
+ next
178
+ 456
179
+ next
180
+ )
181
+ assert_template_result(expected,markup,assigns)
182
+ end
183
+
184
+
185
+ def test_for_tag_string
186
+ # ruby 1.8.7 "String".each => Enumerator with single "String" element.
187
+ # ruby 1.9.3 no longer supports .each on String though we mimic
188
+ # the functionality for backwards compatibility
189
+
190
+ assert_template_result('test string',
191
+ '{%for val in string%}{{val}}{%endfor%}',
192
+ 'string' => "test string")
193
+
194
+ assert_template_result('test string',
195
+ '{%for val in string limit:1%}{{val}}{%endfor%}',
196
+ 'string' => "test string")
197
+
198
+ assert_template_result('val-string-1-1-0-1-0-true-true-test string',
199
+ '{%for val in string%}' +
200
+ '{{forloop.name}}-' +
201
+ '{{forloop.index}}-' +
202
+ '{{forloop.length}}-' +
203
+ '{{forloop.index0}}-' +
204
+ '{{forloop.rindex}}-' +
205
+ '{{forloop.rindex0}}-' +
206
+ '{{forloop.first}}-' +
207
+ '{{forloop.last}}-' +
208
+ '{{val}}{%endfor%}',
209
+ 'string' => "test string")
210
+ end
211
+
212
+ def test_blank_string_not_iterable
213
+ assert_template_result('', "{% for char in characters %}I WILL NOT BE OUTPUT{% endfor %}", 'characters' => '')
214
+ end
215
+ end
@@ -0,0 +1,39 @@
1
+ require 'test_helper'
2
+
3
+ class HtmlTagTest < Test::Unit::TestCase
4
+ include Liquid
5
+
6
+ def test_html_table
7
+
8
+ 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",
9
+ '{% tablerow n in numbers cols:3%} {{n}} {% endtablerow %}',
10
+ 'numbers' => [1,2,3,4,5,6])
11
+
12
+ assert_template_result("<tr class=\"row1\">\n</tr>\n",
13
+ '{% tablerow n in numbers cols:3%} {{n}} {% endtablerow %}',
14
+ 'numbers' => [])
15
+ end
16
+
17
+ def test_html_table_with_different_cols
18
+ 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",
19
+ '{% tablerow n in numbers cols:5%} {{n}} {% endtablerow %}',
20
+ 'numbers' => [1,2,3,4,5,6])
21
+
22
+ end
23
+
24
+ def test_html_col_counter
25
+ 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",
26
+ '{% tablerow n in numbers cols:2%}{{tablerowloop.col}}{% endtablerow %}',
27
+ 'numbers' => [1,2,3,4,5,6])
28
+ end
29
+
30
+ def test_quoted_fragment
31
+ 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",
32
+ "{% tablerow n in collections.frontpage cols:3%} {{n}} {% endtablerow %}",
33
+ 'collections' => {'frontpage' => [1,2,3,4,5,6]})
34
+ 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",
35
+ "{% tablerow n in collections['frontpage'] cols:3%} {{n}} {% endtablerow %}",
36
+ 'collections' => {'frontpage' => [1,2,3,4,5,6]})
37
+
38
+ end
39
+ 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
@@ -0,0 +1,139 @@
1
+ require 'test_helper'
2
+
3
+ class TestFileSystem
4
+ def read_template_file(template_path, context)
5
+ case template_path
6
+ when "product"
7
+ "Product: {{ product.title }} "
8
+
9
+ when "locale_variables"
10
+ "Locale: {{echo1}} {{echo2}}"
11
+
12
+ when "variant"
13
+ "Variant: {{ variant.title }}"
14
+
15
+ when "nested_template"
16
+ "{% include 'header' %} {% include 'body' %} {% include 'footer' %}"
17
+
18
+ when "body"
19
+ "body {% include 'body_detail' %}"
20
+
21
+ when "nested_product_template"
22
+ "Product: {{ nested_product_template.title }} {%include 'details'%} "
23
+
24
+ when "recursively_nested_template"
25
+ "-{% include 'recursively_nested_template' %}"
26
+
27
+ when "pick_a_source"
28
+ "from TestFileSystem"
29
+
30
+ else
31
+ template_path
32
+ end
33
+ end
34
+ end
35
+
36
+ class OtherFileSystem
37
+ def read_template_file(template_path, context)
38
+ 'from OtherFileSystem'
39
+ end
40
+ end
41
+
42
+ class IncludeTagTest < Test::Unit::TestCase
43
+ include Liquid
44
+
45
+ def setup
46
+ Liquid::Template.file_system = TestFileSystem.new
47
+ end
48
+
49
+ def test_include_tag_looks_for_file_system_in_registers_first
50
+ assert_equal 'from OtherFileSystem',
51
+ Template.parse("{% include 'pick_a_source' %}").render({}, :registers => {:file_system => OtherFileSystem.new})
52
+ end
53
+
54
+
55
+ def test_include_tag_with
56
+ assert_equal "Product: Draft 151cm ",
57
+ Template.parse("{% include 'product' with products[0] %}").render( "products" => [ {'title' => 'Draft 151cm'}, {'title' => 'Element 155cm'} ] )
58
+ end
59
+
60
+ def test_include_tag_with_default_name
61
+ assert_equal "Product: Draft 151cm ",
62
+ Template.parse("{% include 'product' %}").render( "product" => {'title' => 'Draft 151cm'} )
63
+ end
64
+
65
+ def test_include_tag_for
66
+
67
+ assert_equal "Product: Draft 151cm Product: Element 155cm ",
68
+ Template.parse("{% include 'product' for products %}").render( "products" => [ {'title' => 'Draft 151cm'}, {'title' => 'Element 155cm'} ] )
69
+ end
70
+
71
+ def test_include_tag_with_local_variables
72
+ assert_equal "Locale: test123 ",
73
+ Template.parse("{% include 'locale_variables' echo1: 'test123' %}").render
74
+ end
75
+
76
+ def test_include_tag_with_multiple_local_variables
77
+ assert_equal "Locale: test123 test321",
78
+ Template.parse("{% include 'locale_variables' echo1: 'test123', echo2: 'test321' %}").render
79
+ end
80
+
81
+ def test_include_tag_with_multiple_local_variables_from_context
82
+ assert_equal "Locale: test123 test321",
83
+ Template.parse("{% include 'locale_variables' echo1: echo1, echo2: more_echos.echo2 %}").render('echo1' => 'test123', 'more_echos' => { "echo2" => 'test321'})
84
+ end
85
+
86
+ def test_nested_include_tag
87
+ assert_equal "body body_detail",
88
+ Template.parse("{% include 'body' %}").render
89
+
90
+ assert_equal "header body body_detail footer",
91
+ Template.parse("{% include 'nested_template' %}").render
92
+ end
93
+
94
+ def test_nested_include_with_variable
95
+
96
+ assert_equal "Product: Draft 151cm details ",
97
+ Template.parse("{% include 'nested_product_template' with product %}").render("product" => {"title" => 'Draft 151cm'})
98
+
99
+ assert_equal "Product: Draft 151cm details Product: Element 155cm details ",
100
+ Template.parse("{% include 'nested_product_template' for products %}").render("products" => [{"title" => 'Draft 151cm'}, {"title" => 'Element 155cm'}])
101
+
102
+ end
103
+
104
+ def test_recursively_included_template_does_not_produce_endless_loop
105
+
106
+ infinite_file_system = Class.new do
107
+ def read_template_file(template_path, context)
108
+ "-{% include 'loop' %}"
109
+ end
110
+ end
111
+
112
+ Liquid::Template.file_system = infinite_file_system.new
113
+
114
+ assert_raise(Liquid::StackLevelError) do
115
+ Template.parse("{% include 'loop' %}").render!
116
+ end
117
+
118
+ end
119
+
120
+ def test_backwards_compatability_support_for_overridden_read_template_file
121
+ infinite_file_system = Class.new do
122
+ def read_template_file(template_path) # testing only one argument here.
123
+ "- hi mom"
124
+ end
125
+ end
126
+
127
+ Liquid::Template.file_system = infinite_file_system.new
128
+
129
+ Template.parse("{% include 'hi_mom' %}").render!
130
+ end
131
+
132
+ def test_dynamically_choosen_template
133
+
134
+ assert_equal "Test123", Template.parse("{% include template %}").render("template" => 'Test123')
135
+ assert_equal "Test321", Template.parse("{% include template %}").render("template" => 'Test321')
136
+
137
+ assert_equal "Product: Draft 151cm ", Template.parse("{% include template for product %}").render("template" => 'product', 'product' => { 'title' => 'Draft 151cm'})
138
+ end
139
+ end # IncludeTagTest