liquor 0.1.1 → 0.9.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (176) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +17 -0
  3. data/.rspec +2 -0
  4. data/.travis.yml +3 -9
  5. data/Gemfile +7 -0
  6. data/Guardfile +11 -0
  7. data/MIT-LICENSE +6 -2
  8. data/README.md +4 -122
  9. data/Rakefile +20 -23
  10. data/doc/language-spec.html +768 -0
  11. data/doc/language-spec.md +698 -0
  12. data/lib/liquor.rb +39 -68
  13. data/lib/liquor/ast_tools.rb +28 -0
  14. data/lib/liquor/compiler.rb +110 -0
  15. data/lib/liquor/context.rb +76 -254
  16. data/lib/liquor/diagnostics.rb +151 -0
  17. data/lib/liquor/drop/drop.rb +168 -0
  18. data/lib/liquor/drop/drop_delegation.rb +24 -0
  19. data/lib/liquor/drop/drop_scope.rb +118 -0
  20. data/lib/liquor/drop/dropable.rb +17 -0
  21. data/lib/liquor/emitter.rb +313 -0
  22. data/lib/liquor/extensions/kaminari.rb +14 -0
  23. data/lib/liquor/extensions/pagination.rb +235 -0
  24. data/lib/liquor/extensions/rails.rb +97 -0
  25. data/lib/liquor/extensions/thinking_sphinx.rb +14 -0
  26. data/lib/liquor/extensions/tire.rb +30 -0
  27. data/lib/liquor/external.rb +79 -0
  28. data/lib/liquor/function.rb +94 -0
  29. data/lib/liquor/grammar/lexer.rb +1223 -0
  30. data/lib/liquor/grammar/lexer.rl +297 -0
  31. data/lib/liquor/grammar/parser.racc +288 -0
  32. data/lib/liquor/grammar/parser.rb +885 -0
  33. data/lib/liquor/library.rb +41 -0
  34. data/lib/liquor/manager.rb +146 -0
  35. data/lib/liquor/runtime.rb +167 -0
  36. data/lib/liquor/stdlib/builtin_functions.rb +315 -0
  37. data/lib/liquor/stdlib/builtin_tags.rb +228 -0
  38. data/lib/liquor/stdlib/html_truncater.rb +162 -0
  39. data/lib/liquor/stdlib/partial_tags.rb +76 -0
  40. data/lib/liquor/tag.rb +83 -14
  41. data/lib/liquor/version.rb +1 -1
  42. data/liquor.gemspec +29 -6
  43. data/spec/builtins_spec.rb +264 -0
  44. data/spec/compiler_spec.rb +136 -0
  45. data/spec/context_spec.rb +49 -0
  46. data/spec/drop_delegation_spec.rb +21 -0
  47. data/spec/drop_spec.rb +222 -0
  48. data/spec/errors_spec.rb +40 -0
  49. data/spec/external_spec.rb +207 -0
  50. data/spec/function_spec.rb +80 -0
  51. data/spec/lexer_spec.rb +173 -0
  52. data/spec/library_spec.rb +18 -0
  53. data/spec/manager_spec.rb +84 -0
  54. data/spec/parser_spec.rb +381 -0
  55. data/spec/partials_spec.rb +74 -0
  56. data/spec/runtime_spec.rb +97 -0
  57. data/spec/spec_helper.rb +94 -0
  58. data/spec/tag_spec.rb +7 -0
  59. metadata +216 -173
  60. data/AUTHORS +0 -2
  61. data/CHANGELOG +0 -48
  62. data/Gemfile.lock +0 -91
  63. data/History.txt +0 -44
  64. data/LICENSE +0 -23
  65. data/example/server/example_servlet.rb +0 -37
  66. data/example/server/liquid_servlet.rb +0 -28
  67. data/example/server/liquor_servlet.rb +0 -28
  68. data/example/server/server.rb +0 -12
  69. data/example/server/templates/index.liquid +0 -6
  70. data/example/server/templates/index.liquor +0 -6
  71. data/example/server/templates/products.liquid +0 -45
  72. data/example/server/templates/products.liquor +0 -45
  73. data/init.rb +0 -8
  74. data/lib/extras/liquid_view.rb +0 -51
  75. data/lib/extras/liquor_view.rb +0 -51
  76. data/lib/liquor/block.rb +0 -101
  77. data/lib/liquor/condition.rb +0 -120
  78. data/lib/liquor/document.rb +0 -17
  79. data/lib/liquor/drop.rb +0 -256
  80. data/lib/liquor/errors.rb +0 -11
  81. data/lib/liquor/extensions.rb +0 -72
  82. data/lib/liquor/file_system.rb +0 -62
  83. data/lib/liquor/htmltags.rb +0 -74
  84. data/lib/liquor/module_ex.rb +0 -60
  85. data/lib/liquor/standardfilters.rb +0 -315
  86. data/lib/liquor/strainer.rb +0 -58
  87. data/lib/liquor/tags/assign.rb +0 -33
  88. data/lib/liquor/tags/capture.rb +0 -35
  89. data/lib/liquor/tags/case.rb +0 -83
  90. data/lib/liquor/tags/comment.rb +0 -9
  91. data/lib/liquor/tags/content_for.rb +0 -54
  92. data/lib/liquor/tags/cycle.rb +0 -59
  93. data/lib/liquor/tags/for.rb +0 -136
  94. data/lib/liquor/tags/if.rb +0 -80
  95. data/lib/liquor/tags/ifchanged.rb +0 -20
  96. data/lib/liquor/tags/include.rb +0 -56
  97. data/lib/liquor/tags/unless.rb +0 -33
  98. data/lib/liquor/tags/yield.rb +0 -49
  99. data/lib/liquor/template.rb +0 -181
  100. data/lib/liquor/variable.rb +0 -52
  101. data/performance/shopify.rb +0 -92
  102. data/performance/shopify/comment_form.rb +0 -33
  103. data/performance/shopify/database.rb +0 -45
  104. data/performance/shopify/json_filter.rb +0 -7
  105. data/performance/shopify/liquid.rb +0 -18
  106. data/performance/shopify/liquor.rb +0 -18
  107. data/performance/shopify/money_filter.rb +0 -18
  108. data/performance/shopify/paginate.rb +0 -93
  109. data/performance/shopify/shop_filter.rb +0 -98
  110. data/performance/shopify/tag_filter.rb +0 -25
  111. data/performance/shopify/vision.database.yml +0 -945
  112. data/performance/shopify/weight_filter.rb +0 -11
  113. data/performance/tests/dropify/article.liquid +0 -74
  114. data/performance/tests/dropify/blog.liquid +0 -33
  115. data/performance/tests/dropify/cart.liquid +0 -66
  116. data/performance/tests/dropify/collection.liquid +0 -22
  117. data/performance/tests/dropify/index.liquid +0 -47
  118. data/performance/tests/dropify/page.liquid +0 -8
  119. data/performance/tests/dropify/product.liquid +0 -68
  120. data/performance/tests/dropify/theme.liquid +0 -105
  121. data/performance/tests/ripen/article.liquid +0 -74
  122. data/performance/tests/ripen/blog.liquid +0 -13
  123. data/performance/tests/ripen/cart.liquid +0 -54
  124. data/performance/tests/ripen/collection.liquid +0 -29
  125. data/performance/tests/ripen/index.liquid +0 -32
  126. data/performance/tests/ripen/page.liquid +0 -4
  127. data/performance/tests/ripen/product.liquid +0 -75
  128. data/performance/tests/ripen/theme.liquid +0 -85
  129. data/performance/tests/tribble/404.liquid +0 -56
  130. data/performance/tests/tribble/article.liquid +0 -98
  131. data/performance/tests/tribble/blog.liquid +0 -41
  132. data/performance/tests/tribble/cart.liquid +0 -134
  133. data/performance/tests/tribble/collection.liquid +0 -70
  134. data/performance/tests/tribble/index.liquid +0 -94
  135. data/performance/tests/tribble/page.liquid +0 -56
  136. data/performance/tests/tribble/product.liquid +0 -116
  137. data/performance/tests/tribble/search.liquid +0 -51
  138. data/performance/tests/tribble/theme.liquid +0 -90
  139. data/performance/tests/vogue/article.liquid +0 -66
  140. data/performance/tests/vogue/blog.liquid +0 -32
  141. data/performance/tests/vogue/cart.liquid +0 -58
  142. data/performance/tests/vogue/collection.liquid +0 -19
  143. data/performance/tests/vogue/index.liquid +0 -22
  144. data/performance/tests/vogue/page.liquid +0 -3
  145. data/performance/tests/vogue/product.liquid +0 -62
  146. data/performance/tests/vogue/theme.liquid +0 -122
  147. data/test/assign_test.rb +0 -11
  148. data/test/block_test.rb +0 -58
  149. data/test/capture_test.rb +0 -41
  150. data/test/condition_test.rb +0 -115
  151. data/test/content_for_test.rb +0 -15
  152. data/test/context_test.rb +0 -479
  153. data/test/drop_test.rb +0 -162
  154. data/test/error_handling_test.rb +0 -89
  155. data/test/extra/breakpoint.rb +0 -547
  156. data/test/extra/caller.rb +0 -80
  157. data/test/file_system_test.rb +0 -30
  158. data/test/filter_test.rb +0 -147
  159. data/test/helper.rb +0 -24
  160. data/test/html_tag_test.rb +0 -31
  161. data/test/if_else_test.rb +0 -139
  162. data/test/include_tag_test.rb +0 -129
  163. data/test/module_ex_test.rb +0 -89
  164. data/test/output_test.rb +0 -121
  165. data/test/parsing_quirks_test.rb +0 -54
  166. data/test/regexp_test.rb +0 -45
  167. data/test/security_test.rb +0 -41
  168. data/test/standard_filter_test.rb +0 -170
  169. data/test/standard_tag_test.rb +0 -405
  170. data/test/statements_test.rb +0 -137
  171. data/test/strainer_test.rb +0 -27
  172. data/test/template_test.rb +0 -82
  173. data/test/test_helper.rb +0 -28
  174. data/test/unless_else_test.rb +0 -27
  175. data/test/variable_test.rb +0 -173
  176. data/test/yield_test.rb +0 -24
@@ -1,170 +0,0 @@
1
- #!/usr/bin/env ruby
2
- require File.dirname(__FILE__) + '/helper'
3
-
4
-
5
- class Filters
6
- include Liquor::StandardFilters
7
- end
8
-
9
-
10
- class StandardFiltersTest < Test::Unit::TestCase
11
- include Liquor
12
-
13
- def setup
14
- @filters = Filters.new
15
- end
16
-
17
- def test_size
18
- assert_equal 3, @filters.size([1,2,3])
19
- assert_equal 0, @filters.size([])
20
- assert_equal 0, @filters.size(nil)
21
- end
22
-
23
- def test_downcase
24
- assert_equal 'testing', @filters.downcase("Testing")
25
- assert_equal '', @filters.downcase(nil)
26
- end
27
-
28
- def test_upcase
29
- assert_equal 'TESTING', @filters.upcase("Testing")
30
- assert_equal '', @filters.upcase(nil)
31
- end
32
-
33
- def test_upcase
34
- assert_equal 'TESTING', @filters.upcase("Testing")
35
- assert_equal '', @filters.upcase(nil)
36
- end
37
-
38
- def test_truncate
39
- assert_equal '1234...', @filters.truncate('1234567890', 7)
40
- assert_equal '1234567890', @filters.truncate('1234567890', 20)
41
- assert_equal '...', @filters.truncate('1234567890', 0)
42
- assert_equal '1234567890', @filters.truncate('1234567890')
43
- end
44
-
45
- def test_escape
46
- assert_equal '&lt;strong&gt;', @filters.escape('<strong>')
47
- assert_equal '&lt;strong&gt;', @filters.h('<strong>')
48
- end
49
-
50
- def test_escape_once
51
- assert_equal '&lt;strong&gt;', @filters.escape_once(@filters.escape('<strong>'))
52
- end
53
-
54
- def test_truncatewords
55
- assert_equal 'one two three', @filters.truncatewords('one two three', 4)
56
- assert_equal 'one two...', @filters.truncatewords('one two three', 2)
57
- assert_equal 'one two three', @filters.truncatewords('one two three')
58
- 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)
59
- end
60
-
61
- def test_strip_html
62
- assert_equal 'test', @filters.strip_html("<div>test</div>")
63
- assert_equal 'test', @filters.strip_html("<div id='test'>test</div>")
64
- assert_equal '', @filters.strip_html("<script type='text/javascript'>document.write('some stuff');</script>")
65
- assert_equal '', @filters.strip_html(nil)
66
- end
67
-
68
- def test_join
69
- assert_equal '1 2 3 4', @filters.join([1,2,3,4])
70
- assert_equal '1 - 2 - 3 - 4', @filters.join([1,2,3,4], ' - ')
71
- end
72
-
73
- def test_sort
74
- assert_equal [1,2,3,4], @filters.sort([4,3,2,1])
75
- assert_equal [{"a" => 1}, {"a" => 2}, {"a" => 3}, {"a" => 4}], @filters.sort([{"a" => 4}, {"a" => 3}, {"a" => 1}, {"a" => 2}], "a")
76
- end
77
-
78
- def test_map
79
- assert_equal [1,2,3,4], @filters.map([{"a" => 1}, {"a" => 2}, {"a" => 3}, {"a" => 4}], 'a')
80
- assert_template_result 'abc', "{{ ary | map:'foo' | map:'bar' }}",
81
- 'ary' => [{'foo' => {'bar' => 'a'}}, {'foo' => {'bar' => 'b'}}, {'foo' => {'bar' => 'c'}}]
82
- end
83
-
84
- def test_date
85
- assert_equal 'May', @filters.date(Time.parse("2006-05-05 10:00:00"), "%B")
86
- assert_equal 'June', @filters.date(Time.parse("2006-06-05 10:00:00"), "%B")
87
- assert_equal 'July', @filters.date(Time.parse("2006-07-05 10:00:00"), "%B")
88
-
89
- assert_equal 'May', @filters.date("2006-05-05 10:00:00", "%B")
90
- assert_equal 'June', @filters.date("2006-06-05 10:00:00", "%B")
91
- assert_equal 'July', @filters.date("2006-07-05 10:00:00", "%B")
92
-
93
- assert_equal '2006-07-05 10:00:00', @filters.date("2006-07-05 10:00:00", "")
94
- assert_equal '2006-07-05 10:00:00', @filters.date("2006-07-05 10:00:00", "")
95
- assert_equal '2006-07-05 10:00:00', @filters.date("2006-07-05 10:00:00", "")
96
- assert_equal '2006-07-05 10:00:00', @filters.date("2006-07-05 10:00:00", nil)
97
-
98
- assert_equal '07/05/2006', @filters.date("2006-07-05 10:00:00", "%m/%d/%Y")
99
-
100
- assert_equal "07/16/2004", @filters.date("Fri Jul 16 01:00:00 2004", "%m/%d/%Y")
101
-
102
- assert_equal nil, @filters.date(nil, "%B")
103
- end
104
-
105
-
106
- def test_first_last
107
- assert_equal 1, @filters.first([1,2,3])
108
- assert_equal 3, @filters.last([1,2,3])
109
- assert_equal nil, @filters.first([])
110
- assert_equal nil, @filters.last([])
111
- end
112
-
113
- def test_replace
114
- assert_equal 'b b b b', @filters.replace("a a a a", 'a', 'b')
115
- assert_equal 'b a a a', @filters.replace_first("a a a a", 'a', 'b')
116
- assert_template_result 'b a a a', "{{ 'a a a a' | replace_first: 'a', 'b' }}"
117
- end
118
-
119
- def test_remove
120
- assert_equal ' ', @filters.remove("a a a a", 'a')
121
- assert_equal 'a a a', @filters.remove_first("a a a a", 'a ')
122
- assert_template_result 'a a a', "{{ 'a a a a' | remove_first: 'a ' }}"
123
- end
124
-
125
- def test_pipes_in_string_arguments
126
- assert_template_result 'foobar', "{{ 'foo|bar' | remove: '|' }}"
127
- end
128
-
129
- def test_strip_newlines
130
- assert_template_result 'abc', "{{ source | strip_newlines }}", 'source' => "a\nb\nc"
131
- end
132
-
133
- def test_newlines_to_br
134
- assert_template_result "a<br />\nb<br />\nc", "{{ source | newline_to_br }}", 'source' => "a\nb\nc"
135
- end
136
-
137
- def test_plus
138
- assert_template_result "2", "{{ 1 | plus:1 }}"
139
- assert_template_result "11", "{{ '1' | plus:'1' }}"
140
- end
141
-
142
- def test_minus
143
- assert_template_result "4", "{{ input | minus:operand }}", 'input' => 5, 'operand' => 1
144
- end
145
-
146
- def test_times
147
- assert_template_result "12", "{{ 3 | times:4 }}"
148
- assert_template_result "foofoofoofoo", "{{ 'foo' | times:4 }}"
149
- end
150
-
151
- def test_append
152
- assigns = {'a' => 'bc', 'b' => 'd' }
153
- assert_template_result('bcd',"{{ a | append: 'd'}}",assigns)
154
- assert_template_result('bcd',"{{ a | append: b}}",assigns)
155
- end
156
-
157
- def test_prepend
158
- assigns = {'a' => 'bc', 'b' => 'a' }
159
- assert_template_result('abc',"{{ a | prepend: 'a'}}",assigns)
160
- assert_template_result('abc',"{{ a | prepend: b}}",assigns)
161
- end
162
-
163
- def test_divided_by
164
- assert_template_result "4", "{{ 12 | divided_by:3 }}"
165
- assert_template_result "4", "{{ 14 | divided_by:3 }}"
166
- assert_template_result "5", "{{ 15 | divided_by:3 }}"
167
- end
168
-
169
- end
170
-
@@ -1,405 +0,0 @@
1
- require File.dirname(__FILE__) + '/helper'
2
-
3
-
4
- class StandardTagTest < Test::Unit::TestCase
5
- include Liquor
6
-
7
-
8
- def test_tag
9
- tag = Tag.new('tag', [], [])
10
- assert_equal 'liquor::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.. {%endcomment%} .. right?|)
30
-
31
- assert_template_result('','{%comment%}{%endcomment%}')
32
- assert_template_result('','{%comment%}{% endcomment %}')
33
- assert_template_result('','{% comment %}{%endcomment%}')
34
- assert_template_result('','{% comment %}{% endcomment %}')
35
- assert_template_result('','{%comment%}comment{%endcomment%}')
36
- assert_template_result('','{% comment %}comment{% endcomment %}')
37
-
38
- assert_template_result('foobar','foo{%comment%}comment{%endcomment%}bar')
39
- assert_template_result('foobar','foo{% comment %}comment{% endcomment %}bar')
40
- assert_template_result('foobar','foo{%comment%} comment {%endcomment%}bar')
41
- assert_template_result('foobar','foo{% comment %} comment {% endcomment %}bar')
42
-
43
- assert_template_result('foo bar','foo {%comment%} {%endcomment%} bar')
44
- assert_template_result('foo bar','foo {%comment%}comment{%endcomment%} bar')
45
- assert_template_result('foo bar','foo {%comment%} comment {%endcomment%} bar')
46
-
47
- assert_template_result('foobar','foo{%comment%}
48
- {%endcomment%}bar')
49
- end
50
-
51
- def test_for
52
- assert_template_result(' yo yo yo yo ','{%for item in array%} yo {%endfor%}','array' => [1,2,3,4])
53
- assert_template_result('yoyo','{%for item in array%}yo{%endfor%}','array' => [1,2])
54
- assert_template_result(' yo ','{%for item in array%} yo {%endfor%}','array' => [1])
55
- assert_template_result('','{%for item in array%}{%endfor%}','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
- {%endfor%}
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}} {%endfor%}')
75
- end
76
-
77
- def test_for_with_variable
78
- assert_template_result(' 1 2 3 ','{%for item in array%} {{item}} {%endfor%}','array' => [1,2,3])
79
- assert_template_result('123','{%for item in array%}{{item}}{%endfor%}','array' => [1,2,3])
80
- assert_template_result('123','{% for item in array %}{{item}}{% endfor %}','array' => [1,2,3])
81
- assert_template_result('abcd','{%for item in array%}{{item}}{%endfor%}','array' => ['a','b','c','d'])
82
- assert_template_result('a b c','{%for item in array%}{{item}}{%endfor%}','array' => ['a',' ','b',' ','c'])
83
- assert_template_result('abc','{%for item in array%}{{item}}{%endfor%}','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}} {%endfor%}',assigns)
89
- assert_template_result(' 1 2 3 ','{%for item in array%} {{forloop.index}} {%endfor%}',assigns)
90
- assert_template_result(' 0 1 2 ','{%for item in array%} {{forloop.index0}} {%endfor%}',assigns)
91
- assert_template_result(' 2 1 0 ','{%for item in array%} {{forloop.rindex0}} {%endfor%}',assigns)
92
- assert_template_result(' 3 2 1 ','{%for item in array%} {{forloop.rindex}} {%endfor%}',assigns)
93
- assert_template_result(' true false false ','{%for item in array%} {{forloop.first}} {%endfor%}',assigns)
94
- assert_template_result(' false false true ','{%for item in array%} {{forloop.last}} {%endfor%}',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 %}-{% endif %}{%endfor%}', 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 }}{%endfor%}',assigns)
105
- assert_template_result('1234','{%for i in array limit:4 %}{{ i }}{%endfor%}',assigns)
106
- assert_template_result('3456','{%for i in array limit:4 offset:2 %}{{ i }}{%endfor%}',assigns)
107
- assert_template_result('3456','{%for i in array limit: 4 offset: 2 %}{{ i }}{%endfor%}',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 }}{%endfor%}',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 }}{%endfor%}{%endfor%}',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 }}{%endfor%}',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}}{%endfor%}
131
- next
132
- {%for i in array.items offset:continue limit: 3 %}{{i}}{%endfor%}
133
- next
134
- {%for i in array.items offset:continue limit: 3 %}{{i}}{%endfor%}
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}}{%endfor%}
150
- next
151
- {%for i in array.items offset:continue limit:3 %}{{i}}{%endfor%}
152
- next
153
- {%for i in array.items offset:continue limit:1 %}{{i}}{%endfor%}
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}}{%endfor%}
169
- next
170
- {%for i in array.items offset:continue limit:3 %}{{i}}{%endfor%}
171
- next
172
- {%for i in array.items offset:continue limit:1000 %}{{i}}{%endfor%}
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}}{%endfor%}
188
- next
189
- {%for i in array.items offset:continue limit:3 %}{{i}}{%endfor%}
190
- next
191
- {%for i in array.items offset:continue limit:3 offset:1000 %}{{i}}{%endfor%})
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 {% endcapture %}{{ 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 {% endcapture %}{{ 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 {% endcase %}', assigns)
231
-
232
- assigns = {'condition' => 1 }
233
- assert_template_result(' its 1 ','{% case condition %}{% when 1 %} its 1 {% when 2 %} its 2 {% endcase %}', assigns)
234
-
235
- assigns = {'condition' => 3 }
236
- assert_template_result('','{% case condition %}{% when 1 %} its 1 {% when 2 %} its 2 {% endcase %}', assigns)
237
-
238
- assigns = {'condition' => "string here" }
239
- assert_template_result(' hit ','{% case condition %}{% when "string here" %} hit {% endcase %}', assigns)
240
-
241
- assigns = {'condition' => "bad string here" }
242
- assert_template_result('','{% case condition %}{% when "string here" %} hit {% endcase %}', 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 {% endcase %}', assigns)
249
-
250
- assigns = {'condition' => 6 }
251
- assert_template_result(' else ','{% case condition %}{% when 5 %} hit {% else %} else {% endcase %}', assigns)
252
-
253
- assigns = {'condition' => 6 }
254
- assert_template_result(' else ','{% case condition %} {% when 5 %} hit {% else %} else {% endcase %}', 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{% endcase %}', 'a' => [])
261
- assert_template_result('1' , '{% case a.size %}{% when 1 %}1{% when 2 %}2{% endcase %}', 'a' => [1])
262
- assert_template_result('2' , '{% case a.size %}{% when 1 %}1{% when 2 %}2{% endcase %}', 'a' => [1, 1])
263
- assert_template_result('', '{% case a.size %}{% when 1 %}1{% when 2 %}2{% endcase %}', 'a' => [1, 1, 1])
264
- assert_template_result('', '{% case a.size %}{% when 1 %}1{% when 2 %}2{% endcase %}', 'a' => [1, 1, 1, 1])
265
- assert_template_result('', '{% case a.size %}{% when 1 %}1{% when 2 %}2{% endcase %}', '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{% endcase %}', 'a' => [])
270
- assert_template_result('1', '{% case a.size %}{% when 1 %}1{% when 2 %}2{% else %}else{% endcase %}', 'a' => [1])
271
- assert_template_result('2', '{% case a.size %}{% when 1 %}1{% when 2 %}2{% else %}else{% endcase %}', 'a' => [1, 1])
272
- assert_template_result('else', '{% case a.size %}{% when 1 %}1{% when 2 %}2{% else %}else{% endcase %}', 'a' => [1, 1, 1])
273
- assert_template_result('else', '{% case a.size %}{% when 1 %}1{% when 2 %}2{% else %}else{% endcase %}', 'a' => [1, 1, 1, 1])
274
- assert_template_result('else', '{% case a.size %}{% when 1 %}1{% when 2 %}2{% else %}else{% endcase %}', '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{% endcase %}', {})
279
- assert_template_result('false', '{% case false %}{% when true %}true{% when false %}false{% else %}else{% endcase %}', {})
280
- assert_template_result('true', '{% case true %}{% when true %}true{% when false %}false{% else %}else{% endcase %}', {})
281
- assert_template_result('else', '{% case NULL %}{% when true %}true{% when false %}false{% else %}else{% endcase %}', {})
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' %}{% endcase %}{{ ptitle }})
287
- template = Liquor::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 {% endcase %}'
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 {% endcase %}'
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 {% endcase %}'
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 {% endcase %}'
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', Liquor::Template.parse( '{% assign a = "variable"%}{{a}}' ).render
327
- end
328
-
329
- def test_assign_an_empty_string
330
- assert_equal '', Liquor::Template.parse( '{% assign a = ""%}{{a}}' ).render
331
- end
332
-
333
- def test_assign_is_global
334
- assert_equal 'variable', Liquor::Template.parse( '{%for i in (1..2) %}{% assign a = "variable"%}{% endfor %}{{a}}' ).render
335
- end
336
-
337
- def test_case_detects_bad_syntax
338
- assert_raise(SyntaxError) do
339
- assert_template_result('', '{% case false %}{% when %}true{% endcase %}', {})
340
- end
341
-
342
- assert_raise(SyntaxError) do
343
- assert_template_result('', '{% case false %}{% huh %}true{% endcase %}', {})
344
- end
345
-
346
- end
347
-
348
-
349
-
350
- def test_cycle
351
-
352
- assert_template_result('one','{%cycle "one", "two"%}')
353
- assert_template_result('one two','{%cycle "one", "two"%} {%cycle "one", "two"%}')
354
- assert_template_result(' two','{%cycle "", "two"%} {%cycle "", "two"%}')
355
-
356
- assert_template_result('one two one','{%cycle "one", "two"%} {%cycle "one", "two"%} {%cycle "one", "two"%}')
357
-
358
- assert_template_result('text-align: left text-align: right','{%cycle "text-align: left", "text-align: right" %} {%cycle "text-align: left", "text-align: right"%}')
359
-
360
- end
361
-
362
- def test_multiple_cycles
363
- 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%}')
364
- end
365
-
366
- def test_multiple_named_cycles
367
- 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" %}')
368
- end
369
-
370
- def test_multiple_named_cycles_with_names_from_context
371
- assigns = {"var1" => 1, "var2" => 2 }
372
- 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)
373
- end
374
-
375
- def test_size_of_array
376
- assigns = {"array" => [1,2,3,4]}
377
- assert_template_result('array has 4 elements', "array has {{ array.size }} elements", assigns)
378
- end
379
-
380
- def test_size_of_hash
381
- assigns = {"hash" => {:a => 1, :b => 2, :c=> 3, :d => 4}}
382
- assert_template_result('hash has 4 elements', "hash has {{ hash.size }} elements", assigns)
383
- end
384
-
385
- def test_illegal_symbols
386
- assert_template_result('', '{% if true == empty %}?{% endif %}', {})
387
- assert_template_result('', '{% if true == null %}?{% endif %}', {})
388
- assert_template_result('', '{% if empty == true %}?{% endif %}', {})
389
- assert_template_result('', '{% if null == true %}?{% endif %}', {})
390
- end
391
-
392
- def test_for_reversed
393
- assigns = {'array' => [ 1, 2, 3] }
394
- assert_template_result('321','{%for item in array reversed %}{{item}}{%endfor%}',assigns)
395
- end
396
-
397
-
398
- def test_ifchanged
399
- assigns = {'array' => [ 1, 1, 2, 2, 3, 3] }
400
- assert_template_result('123','{%for item in array%}{%ifchanged%}{{item}}{% endifchanged %}{%endfor%}',assigns)
401
-
402
- assigns = {'array' => [ 1, 1, 1, 1] }
403
- assert_template_result('1','{%for item in array%}{%ifchanged%}{{item}}{% endifchanged %}{%endfor%}',assigns)
404
- end
405
- end