liquid 5.1.0 → 5.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/History.md +35 -0
- data/README.md +4 -4
- data/lib/liquid/block_body.rb +6 -6
- data/lib/liquid/condition.rb +7 -1
- data/lib/liquid/context.rb +6 -2
- data/lib/liquid/expression.rb +11 -10
- data/lib/liquid/forloop_drop.rb +44 -1
- data/lib/liquid/locales/en.yml +6 -5
- data/lib/liquid/partial_cache.rb +3 -3
- data/lib/liquid/registers.rb +51 -0
- data/lib/liquid/standardfilters.rb +463 -75
- data/lib/liquid/strainer_factory.rb +15 -10
- data/lib/liquid/strainer_template.rb +9 -0
- data/lib/liquid/tablerowloop_drop.rb +58 -1
- data/lib/liquid/tags/assign.rb +12 -8
- data/lib/liquid/tags/break.rb +8 -0
- data/lib/liquid/tags/capture.rb +13 -10
- data/lib/liquid/tags/case.rb +21 -0
- data/lib/liquid/tags/comment.rb +13 -0
- data/lib/liquid/tags/continue.rb +8 -9
- data/lib/liquid/tags/cycle.rb +12 -11
- data/lib/liquid/tags/decrement.rb +16 -17
- data/lib/liquid/tags/echo.rb +16 -9
- data/lib/liquid/tags/for.rb +22 -43
- data/lib/liquid/tags/if.rb +11 -9
- data/lib/liquid/tags/include.rb +15 -13
- data/lib/liquid/tags/increment.rb +16 -14
- data/lib/liquid/tags/inline_comment.rb +43 -0
- data/lib/liquid/tags/raw.rb +11 -0
- data/lib/liquid/tags/render.rb +29 -4
- data/lib/liquid/tags/table_row.rb +22 -0
- data/lib/liquid/tags/unless.rb +15 -4
- data/lib/liquid/template.rb +2 -3
- data/lib/liquid/variable.rb +4 -4
- data/lib/liquid/variable_lookup.rb +10 -7
- data/lib/liquid/version.rb +1 -1
- data/lib/liquid.rb +4 -4
- metadata +7 -121
- data/lib/liquid/register.rb +0 -6
- data/lib/liquid/static_registers.rb +0 -44
- data/test/fixtures/en_locale.yml +0 -9
- data/test/integration/assign_test.rb +0 -117
- data/test/integration/blank_test.rb +0 -109
- data/test/integration/block_test.rb +0 -58
- data/test/integration/capture_test.rb +0 -58
- data/test/integration/context_test.rb +0 -636
- data/test/integration/document_test.rb +0 -21
- data/test/integration/drop_test.rb +0 -257
- data/test/integration/error_handling_test.rb +0 -272
- data/test/integration/expression_test.rb +0 -46
- data/test/integration/filter_test.rb +0 -189
- data/test/integration/hash_ordering_test.rb +0 -25
- data/test/integration/output_test.rb +0 -125
- data/test/integration/parsing_quirks_test.rb +0 -134
- data/test/integration/profiler_test.rb +0 -213
- data/test/integration/security_test.rb +0 -89
- data/test/integration/standard_filter_test.rb +0 -880
- data/test/integration/tag/disableable_test.rb +0 -59
- data/test/integration/tag_test.rb +0 -45
- data/test/integration/tags/break_tag_test.rb +0 -17
- data/test/integration/tags/continue_tag_test.rb +0 -17
- data/test/integration/tags/echo_test.rb +0 -13
- data/test/integration/tags/for_tag_test.rb +0 -466
- data/test/integration/tags/if_else_tag_test.rb +0 -190
- data/test/integration/tags/include_tag_test.rb +0 -269
- data/test/integration/tags/increment_tag_test.rb +0 -25
- data/test/integration/tags/liquid_tag_test.rb +0 -116
- data/test/integration/tags/raw_tag_test.rb +0 -34
- data/test/integration/tags/render_tag_test.rb +0 -213
- data/test/integration/tags/standard_tag_test.rb +0 -303
- data/test/integration/tags/statements_test.rb +0 -113
- data/test/integration/tags/table_row_test.rb +0 -66
- data/test/integration/tags/unless_else_tag_test.rb +0 -28
- data/test/integration/template_test.rb +0 -340
- data/test/integration/trim_mode_test.rb +0 -563
- data/test/integration/variable_test.rb +0 -138
- data/test/test_helper.rb +0 -207
- data/test/unit/block_unit_test.rb +0 -53
- data/test/unit/condition_unit_test.rb +0 -168
- data/test/unit/file_system_unit_test.rb +0 -37
- data/test/unit/i18n_unit_test.rb +0 -39
- data/test/unit/lexer_unit_test.rb +0 -53
- data/test/unit/parse_tree_visitor_test.rb +0 -261
- data/test/unit/parser_unit_test.rb +0 -84
- data/test/unit/partial_cache_unit_test.rb +0 -128
- data/test/unit/regexp_unit_test.rb +0 -46
- data/test/unit/static_registers_unit_test.rb +0 -156
- data/test/unit/strainer_factory_unit_test.rb +0 -100
- data/test/unit/strainer_template_unit_test.rb +0 -82
- data/test/unit/tag_unit_test.rb +0 -23
- data/test/unit/tags/case_tag_unit_test.rb +0 -12
- data/test/unit/tags/for_tag_unit_test.rb +0 -15
- data/test/unit/tags/if_tag_unit_test.rb +0 -10
- data/test/unit/template_factory_unit_test.rb +0 -12
- data/test/unit/template_unit_test.rb +0 -87
- data/test/unit/tokenizer_unit_test.rb +0 -62
- data/test/unit/variable_unit_test.rb +0 -164
@@ -1,213 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'test_helper'
|
4
|
-
|
5
|
-
class RenderTagTest < Minitest::Test
|
6
|
-
include Liquid
|
7
|
-
|
8
|
-
def test_render_with_no_arguments
|
9
|
-
Liquid::Template.file_system = StubFileSystem.new('source' => 'rendered content')
|
10
|
-
assert_template_result('rendered content', '{% render "source" %}')
|
11
|
-
end
|
12
|
-
|
13
|
-
def test_render_tag_looks_for_file_system_in_registers_first
|
14
|
-
file_system = StubFileSystem.new('pick_a_source' => 'from register file system')
|
15
|
-
assert_equal('from register file system',
|
16
|
-
Template.parse('{% render "pick_a_source" %}').render!({}, registers: { file_system: file_system }))
|
17
|
-
end
|
18
|
-
|
19
|
-
def test_render_passes_named_arguments_into_inner_scope
|
20
|
-
Liquid::Template.file_system = StubFileSystem.new('product' => '{{ inner_product.title }}')
|
21
|
-
assert_template_result('My Product', '{% render "product", inner_product: outer_product %}',
|
22
|
-
'outer_product' => { 'title' => 'My Product' })
|
23
|
-
end
|
24
|
-
|
25
|
-
def test_render_accepts_literals_as_arguments
|
26
|
-
Liquid::Template.file_system = StubFileSystem.new('snippet' => '{{ price }}')
|
27
|
-
assert_template_result('123', '{% render "snippet", price: 123 %}')
|
28
|
-
end
|
29
|
-
|
30
|
-
def test_render_accepts_multiple_named_arguments
|
31
|
-
Liquid::Template.file_system = StubFileSystem.new('snippet' => '{{ one }} {{ two }}')
|
32
|
-
assert_template_result('1 2', '{% render "snippet", one: 1, two: 2 %}')
|
33
|
-
end
|
34
|
-
|
35
|
-
def test_render_does_not_inherit_parent_scope_variables
|
36
|
-
Liquid::Template.file_system = StubFileSystem.new('snippet' => '{{ outer_variable }}')
|
37
|
-
assert_template_result('', '{% assign outer_variable = "should not be visible" %}{% render "snippet" %}')
|
38
|
-
end
|
39
|
-
|
40
|
-
def test_render_does_not_inherit_variable_with_same_name_as_snippet
|
41
|
-
Liquid::Template.file_system = StubFileSystem.new('snippet' => '{{ snippet }}')
|
42
|
-
assert_template_result('', "{% assign snippet = 'should not be visible' %}{% render 'snippet' %}")
|
43
|
-
end
|
44
|
-
|
45
|
-
def test_render_does_not_mutate_parent_scope
|
46
|
-
Liquid::Template.file_system = StubFileSystem.new('snippet' => '{% assign inner = 1 %}')
|
47
|
-
assert_template_result('', "{% render 'snippet' %}{{ inner }}")
|
48
|
-
end
|
49
|
-
|
50
|
-
def test_nested_render_tag
|
51
|
-
Liquid::Template.file_system = StubFileSystem.new(
|
52
|
-
'one' => "one {% render 'two' %}",
|
53
|
-
'two' => 'two'
|
54
|
-
)
|
55
|
-
assert_template_result('one two', "{% render 'one' %}")
|
56
|
-
end
|
57
|
-
|
58
|
-
def test_recursively_rendered_template_does_not_produce_endless_loop
|
59
|
-
Liquid::Template.file_system = StubFileSystem.new('loop' => '{% render "loop" %}')
|
60
|
-
|
61
|
-
assert_raises(Liquid::StackLevelError) do
|
62
|
-
Template.parse('{% render "loop" %}').render!
|
63
|
-
end
|
64
|
-
end
|
65
|
-
|
66
|
-
def test_sub_contexts_count_towards_the_same_recursion_limit
|
67
|
-
Liquid::Template.file_system = StubFileSystem.new(
|
68
|
-
'loop_render' => '{% render "loop_render" %}',
|
69
|
-
)
|
70
|
-
assert_raises(Liquid::StackLevelError) do
|
71
|
-
Template.parse('{% render "loop_render" %}').render!
|
72
|
-
end
|
73
|
-
end
|
74
|
-
|
75
|
-
def test_dynamically_choosen_templates_are_not_allowed
|
76
|
-
Liquid::Template.file_system = StubFileSystem.new('snippet' => 'should not be rendered')
|
77
|
-
|
78
|
-
assert_raises(Liquid::SyntaxError) do
|
79
|
-
Liquid::Template.parse("{% assign name = 'snippet' %}{% render name %}")
|
80
|
-
end
|
81
|
-
end
|
82
|
-
|
83
|
-
def test_include_tag_caches_second_read_of_same_partial
|
84
|
-
file_system = StubFileSystem.new('snippet' => 'echo')
|
85
|
-
assert_equal('echoecho',
|
86
|
-
Template.parse('{% render "snippet" %}{% render "snippet" %}')
|
87
|
-
.render!({}, registers: { file_system: file_system }))
|
88
|
-
assert_equal(1, file_system.file_read_count)
|
89
|
-
end
|
90
|
-
|
91
|
-
def test_render_tag_doesnt_cache_partials_across_renders
|
92
|
-
file_system = StubFileSystem.new('snippet' => 'my message')
|
93
|
-
|
94
|
-
assert_equal('my message',
|
95
|
-
Template.parse('{% include "snippet" %}').render!({}, registers: { file_system: file_system }))
|
96
|
-
assert_equal(1, file_system.file_read_count)
|
97
|
-
|
98
|
-
assert_equal('my message',
|
99
|
-
Template.parse('{% include "snippet" %}').render!({}, registers: { file_system: file_system }))
|
100
|
-
assert_equal(2, file_system.file_read_count)
|
101
|
-
end
|
102
|
-
|
103
|
-
def test_render_tag_within_if_statement
|
104
|
-
Liquid::Template.file_system = StubFileSystem.new('snippet' => 'my message')
|
105
|
-
assert_template_result('my message', '{% if true %}{% render "snippet" %}{% endif %}')
|
106
|
-
end
|
107
|
-
|
108
|
-
def test_break_through_render
|
109
|
-
Liquid::Template.file_system = StubFileSystem.new('break' => '{% break %}')
|
110
|
-
assert_template_result('1', '{% for i in (1..3) %}{{ i }}{% break %}{{ i }}{% endfor %}')
|
111
|
-
assert_template_result('112233', '{% for i in (1..3) %}{{ i }}{% render "break" %}{{ i }}{% endfor %}')
|
112
|
-
end
|
113
|
-
|
114
|
-
def test_increment_is_isolated_between_renders
|
115
|
-
Liquid::Template.file_system = StubFileSystem.new('incr' => '{% increment %}')
|
116
|
-
assert_template_result('010', '{% increment %}{% increment %}{% render "incr" %}')
|
117
|
-
end
|
118
|
-
|
119
|
-
def test_decrement_is_isolated_between_renders
|
120
|
-
Liquid::Template.file_system = StubFileSystem.new('decr' => '{% decrement %}')
|
121
|
-
assert_template_result('-1-2-1', '{% decrement %}{% decrement %}{% render "decr" %}')
|
122
|
-
end
|
123
|
-
|
124
|
-
def test_includes_will_not_render_inside_render_tag
|
125
|
-
Liquid::Template.file_system = StubFileSystem.new(
|
126
|
-
'foo' => 'bar',
|
127
|
-
'test_include' => '{% include "foo" %}'
|
128
|
-
)
|
129
|
-
|
130
|
-
exc = assert_raises(Liquid::DisabledError) do
|
131
|
-
Liquid::Template.parse('{% render "test_include" %}').render!
|
132
|
-
end
|
133
|
-
assert_equal('Liquid error: include usage is not allowed in this context', exc.message)
|
134
|
-
end
|
135
|
-
|
136
|
-
def test_includes_will_not_render_inside_nested_sibling_tags
|
137
|
-
Liquid::Template.file_system = StubFileSystem.new(
|
138
|
-
'foo' => 'bar',
|
139
|
-
'nested_render_with_sibling_include' => '{% render "test_include" %}{% include "foo" %}',
|
140
|
-
'test_include' => '{% include "foo" %}'
|
141
|
-
)
|
142
|
-
|
143
|
-
output = Liquid::Template.parse('{% render "nested_render_with_sibling_include" %}').render
|
144
|
-
assert_equal('Liquid error: include usage is not allowed in this contextLiquid error: include usage is not allowed in this context', output)
|
145
|
-
end
|
146
|
-
|
147
|
-
def test_render_tag_with
|
148
|
-
Liquid::Template.file_system = StubFileSystem.new(
|
149
|
-
'product' => "Product: {{ product.title }} ",
|
150
|
-
'product_alias' => "Product: {{ product.title }} ",
|
151
|
-
)
|
152
|
-
|
153
|
-
assert_template_result("Product: Draft 151cm ",
|
154
|
-
"{% render 'product' with products[0] %}", "products" => [{ 'title' => 'Draft 151cm' }, { 'title' => 'Element 155cm' }])
|
155
|
-
end
|
156
|
-
|
157
|
-
def test_render_tag_with_alias
|
158
|
-
Liquid::Template.file_system = StubFileSystem.new(
|
159
|
-
'product' => "Product: {{ product.title }} ",
|
160
|
-
'product_alias' => "Product: {{ product.title }} ",
|
161
|
-
)
|
162
|
-
|
163
|
-
assert_template_result("Product: Draft 151cm ",
|
164
|
-
"{% render 'product_alias' with products[0] as product %}", "products" => [{ 'title' => 'Draft 151cm' }, { 'title' => 'Element 155cm' }])
|
165
|
-
end
|
166
|
-
|
167
|
-
def test_render_tag_for_alias
|
168
|
-
Liquid::Template.file_system = StubFileSystem.new(
|
169
|
-
'product' => "Product: {{ product.title }} ",
|
170
|
-
'product_alias' => "Product: {{ product.title }} ",
|
171
|
-
)
|
172
|
-
|
173
|
-
assert_template_result("Product: Draft 151cm Product: Element 155cm ",
|
174
|
-
"{% render 'product_alias' for products as product %}", "products" => [{ 'title' => 'Draft 151cm' }, { 'title' => 'Element 155cm' }])
|
175
|
-
end
|
176
|
-
|
177
|
-
def test_render_tag_for
|
178
|
-
Liquid::Template.file_system = StubFileSystem.new(
|
179
|
-
'product' => "Product: {{ product.title }} ",
|
180
|
-
'product_alias' => "Product: {{ product.title }} ",
|
181
|
-
)
|
182
|
-
|
183
|
-
assert_template_result("Product: Draft 151cm Product: Element 155cm ",
|
184
|
-
"{% render 'product' for products %}", "products" => [{ 'title' => 'Draft 151cm' }, { 'title' => 'Element 155cm' }])
|
185
|
-
end
|
186
|
-
|
187
|
-
def test_render_tag_forloop
|
188
|
-
Liquid::Template.file_system = StubFileSystem.new(
|
189
|
-
'product' => "Product: {{ product.title }} {% if forloop.first %}first{% endif %} {% if forloop.last %}last{% endif %} index:{{ forloop.index }} ",
|
190
|
-
)
|
191
|
-
|
192
|
-
assert_template_result("Product: Draft 151cm first index:1 Product: Element 155cm last index:2 ",
|
193
|
-
"{% render 'product' for products %}", "products" => [{ 'title' => 'Draft 151cm' }, { 'title' => 'Element 155cm' }])
|
194
|
-
end
|
195
|
-
|
196
|
-
def test_render_tag_for_drop
|
197
|
-
Liquid::Template.file_system = StubFileSystem.new(
|
198
|
-
'loop' => "{{ value.foo }}",
|
199
|
-
)
|
200
|
-
|
201
|
-
assert_template_result("123",
|
202
|
-
"{% render 'loop' for loop as value %}", "loop" => TestEnumerable.new)
|
203
|
-
end
|
204
|
-
|
205
|
-
def test_render_tag_with_drop
|
206
|
-
Liquid::Template.file_system = StubFileSystem.new(
|
207
|
-
'loop' => "{{ value }}",
|
208
|
-
)
|
209
|
-
|
210
|
-
assert_template_result("TestEnumerable",
|
211
|
-
"{% render 'loop' with loop as value %}", "loop" => TestEnumerable.new)
|
212
|
-
end
|
213
|
-
end
|
@@ -1,303 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'test_helper'
|
4
|
-
|
5
|
-
class StandardTagTest < Minitest::Test
|
6
|
-
include Liquid
|
7
|
-
|
8
|
-
def test_no_transform
|
9
|
-
assert_template_result('this text should come out of the template without change...',
|
10
|
-
'this text should come out of the template without change...')
|
11
|
-
|
12
|
-
assert_template_result('blah', 'blah')
|
13
|
-
assert_template_result('<blah>', '<blah>')
|
14
|
-
assert_template_result('|,.:', '|,.:')
|
15
|
-
assert_template_result('', '')
|
16
|
-
|
17
|
-
text = %(this shouldnt see any transformation either but has multiple lines
|
18
|
-
as you can clearly see here ...)
|
19
|
-
assert_template_result(text, text)
|
20
|
-
end
|
21
|
-
|
22
|
-
def test_has_a_block_which_does_nothing
|
23
|
-
assert_template_result(%(the comment block should be removed .. right?),
|
24
|
-
%(the comment block should be removed {%comment%} be gone.. {%endcomment%} .. right?))
|
25
|
-
|
26
|
-
assert_template_result('', '{%comment%}{%endcomment%}')
|
27
|
-
assert_template_result('', '{%comment%}{% endcomment %}')
|
28
|
-
assert_template_result('', '{% comment %}{%endcomment%}')
|
29
|
-
assert_template_result('', '{% comment %}{% endcomment %}')
|
30
|
-
assert_template_result('', '{%comment%}comment{%endcomment%}')
|
31
|
-
assert_template_result('', '{% comment %}comment{% endcomment %}')
|
32
|
-
assert_template_result('', '{% comment %} 1 {% comment %} 2 {% endcomment %} 3 {% endcomment %}')
|
33
|
-
|
34
|
-
assert_template_result('', '{%comment%}{%blabla%}{%endcomment%}')
|
35
|
-
assert_template_result('', '{% comment %}{% blabla %}{% endcomment %}')
|
36
|
-
assert_template_result('', '{%comment%}{% endif %}{%endcomment%}')
|
37
|
-
assert_template_result('', '{% comment %}{% endwhatever %}{% endcomment %}')
|
38
|
-
assert_template_result('', '{% comment %}{% raw %} {{%%%%}} }} { {% endcomment %} {% comment {% endraw %} {% endcomment %}')
|
39
|
-
|
40
|
-
assert_template_result('foobar', 'foo{%comment%}comment{%endcomment%}bar')
|
41
|
-
assert_template_result('foobar', 'foo{% comment %}comment{% endcomment %}bar')
|
42
|
-
assert_template_result('foobar', 'foo{%comment%} comment {%endcomment%}bar')
|
43
|
-
assert_template_result('foobar', 'foo{% comment %} comment {% endcomment %}bar')
|
44
|
-
|
45
|
-
assert_template_result('foo bar', 'foo {%comment%} {%endcomment%} bar')
|
46
|
-
assert_template_result('foo bar', 'foo {%comment%}comment{%endcomment%} bar')
|
47
|
-
assert_template_result('foo bar', 'foo {%comment%} comment {%endcomment%} bar')
|
48
|
-
|
49
|
-
assert_template_result('foobar', 'foo{%comment%}
|
50
|
-
{%endcomment%}bar')
|
51
|
-
end
|
52
|
-
|
53
|
-
def test_hyphenated_assign
|
54
|
-
assigns = { 'a-b' => '1' }
|
55
|
-
assert_template_result('a-b:1 a-b:2', 'a-b:{{a-b}} {%assign a-b = 2 %}a-b:{{a-b}}', assigns)
|
56
|
-
end
|
57
|
-
|
58
|
-
def test_assign_with_colon_and_spaces
|
59
|
-
assigns = { 'var' => { 'a:b c' => { 'paged' => '1' } } }
|
60
|
-
assert_template_result('var2: 1', '{%assign var2 = var["a:b c"].paged %}var2: {{var2}}', assigns)
|
61
|
-
end
|
62
|
-
|
63
|
-
def test_capture
|
64
|
-
assigns = { 'var' => 'content' }
|
65
|
-
assert_template_result('content foo content foo ',
|
66
|
-
'{{ var2 }}{% capture var2 %}{{ var }} foo {% endcapture %}{{ var2 }}{{ var2 }}',
|
67
|
-
assigns)
|
68
|
-
end
|
69
|
-
|
70
|
-
def test_capture_detects_bad_syntax
|
71
|
-
assert_raises(SyntaxError) do
|
72
|
-
assert_template_result('content foo content foo ',
|
73
|
-
'{{ var2 }}{% capture %}{{ var }} foo {% endcapture %}{{ var2 }}{{ var2 }}',
|
74
|
-
'var' => 'content')
|
75
|
-
end
|
76
|
-
end
|
77
|
-
|
78
|
-
def test_case
|
79
|
-
assigns = { 'condition' => 2 }
|
80
|
-
assert_template_result(' its 2 ',
|
81
|
-
'{% case condition %}{% when 1 %} its 1 {% when 2 %} its 2 {% endcase %}',
|
82
|
-
assigns)
|
83
|
-
|
84
|
-
assigns = { 'condition' => 1 }
|
85
|
-
assert_template_result(' its 1 ',
|
86
|
-
'{% case condition %}{% when 1 %} its 1 {% when 2 %} its 2 {% endcase %}',
|
87
|
-
assigns)
|
88
|
-
|
89
|
-
assigns = { 'condition' => 3 }
|
90
|
-
assert_template_result('',
|
91
|
-
'{% case condition %}{% when 1 %} its 1 {% when 2 %} its 2 {% endcase %}',
|
92
|
-
assigns)
|
93
|
-
|
94
|
-
assigns = { 'condition' => "string here" }
|
95
|
-
assert_template_result(' hit ',
|
96
|
-
'{% case condition %}{% when "string here" %} hit {% endcase %}',
|
97
|
-
assigns)
|
98
|
-
|
99
|
-
assigns = { 'condition' => "bad string here" }
|
100
|
-
assert_template_result('',
|
101
|
-
'{% case condition %}{% when "string here" %} hit {% endcase %}',\
|
102
|
-
assigns)
|
103
|
-
end
|
104
|
-
|
105
|
-
def test_case_with_else
|
106
|
-
assigns = { 'condition' => 5 }
|
107
|
-
assert_template_result(' hit ',
|
108
|
-
'{% case condition %}{% when 5 %} hit {% else %} else {% endcase %}',
|
109
|
-
assigns)
|
110
|
-
|
111
|
-
assigns = { 'condition' => 6 }
|
112
|
-
assert_template_result(' else ',
|
113
|
-
'{% case condition %}{% when 5 %} hit {% else %} else {% endcase %}',
|
114
|
-
assigns)
|
115
|
-
|
116
|
-
assigns = { 'condition' => 6 }
|
117
|
-
assert_template_result(' else ',
|
118
|
-
'{% case condition %} {% when 5 %} hit {% else %} else {% endcase %}',
|
119
|
-
assigns)
|
120
|
-
end
|
121
|
-
|
122
|
-
def test_case_on_size
|
123
|
-
assert_template_result('', '{% case a.size %}{% when 1 %}1{% when 2 %}2{% endcase %}', 'a' => [])
|
124
|
-
assert_template_result('1', '{% case a.size %}{% when 1 %}1{% when 2 %}2{% endcase %}', 'a' => [1])
|
125
|
-
assert_template_result('2', '{% case a.size %}{% when 1 %}1{% when 2 %}2{% endcase %}', 'a' => [1, 1])
|
126
|
-
assert_template_result('', '{% case a.size %}{% when 1 %}1{% when 2 %}2{% endcase %}', 'a' => [1, 1, 1])
|
127
|
-
assert_template_result('', '{% case a.size %}{% when 1 %}1{% when 2 %}2{% endcase %}', 'a' => [1, 1, 1, 1])
|
128
|
-
assert_template_result('', '{% case a.size %}{% when 1 %}1{% when 2 %}2{% endcase %}', 'a' => [1, 1, 1, 1, 1])
|
129
|
-
end
|
130
|
-
|
131
|
-
def test_case_on_size_with_else
|
132
|
-
assert_template_result('else',
|
133
|
-
'{% case a.size %}{% when 1 %}1{% when 2 %}2{% else %}else{% endcase %}',
|
134
|
-
'a' => [])
|
135
|
-
|
136
|
-
assert_template_result('1',
|
137
|
-
'{% case a.size %}{% when 1 %}1{% when 2 %}2{% else %}else{% endcase %}',
|
138
|
-
'a' => [1])
|
139
|
-
|
140
|
-
assert_template_result('2',
|
141
|
-
'{% case a.size %}{% when 1 %}1{% when 2 %}2{% else %}else{% endcase %}',
|
142
|
-
'a' => [1, 1])
|
143
|
-
|
144
|
-
assert_template_result('else',
|
145
|
-
'{% case a.size %}{% when 1 %}1{% when 2 %}2{% else %}else{% endcase %}',
|
146
|
-
'a' => [1, 1, 1])
|
147
|
-
|
148
|
-
assert_template_result('else',
|
149
|
-
'{% case a.size %}{% when 1 %}1{% when 2 %}2{% else %}else{% endcase %}',
|
150
|
-
'a' => [1, 1, 1, 1])
|
151
|
-
|
152
|
-
assert_template_result('else',
|
153
|
-
'{% case a.size %}{% when 1 %}1{% when 2 %}2{% else %}else{% endcase %}',
|
154
|
-
'a' => [1, 1, 1, 1, 1])
|
155
|
-
end
|
156
|
-
|
157
|
-
def test_case_on_length_with_else
|
158
|
-
assert_template_result('else',
|
159
|
-
'{% case a.empty? %}{% when true %}true{% when false %}false{% else %}else{% endcase %}',
|
160
|
-
{})
|
161
|
-
|
162
|
-
assert_template_result('false',
|
163
|
-
'{% case false %}{% when true %}true{% when false %}false{% else %}else{% endcase %}',
|
164
|
-
{})
|
165
|
-
|
166
|
-
assert_template_result('true',
|
167
|
-
'{% case true %}{% when true %}true{% when false %}false{% else %}else{% endcase %}',
|
168
|
-
{})
|
169
|
-
|
170
|
-
assert_template_result('else',
|
171
|
-
'{% case NULL %}{% when true %}true{% when false %}false{% else %}else{% endcase %}',
|
172
|
-
{})
|
173
|
-
end
|
174
|
-
|
175
|
-
def test_assign_from_case
|
176
|
-
# Example from the shopify forums
|
177
|
-
code = "{% case collection.handle %}{% when 'menswear-jackets' %}{% assign ptitle = 'menswear' %}{% when 'menswear-t-shirts' %}{% assign ptitle = 'menswear' %}{% else %}{% assign ptitle = 'womenswear' %}{% endcase %}{{ ptitle }}"
|
178
|
-
template = Liquid::Template.parse(code)
|
179
|
-
assert_equal("menswear", template.render!("collection" => { 'handle' => 'menswear-jackets' }))
|
180
|
-
assert_equal("menswear", template.render!("collection" => { 'handle' => 'menswear-t-shirts' }))
|
181
|
-
assert_equal("womenswear", template.render!("collection" => { 'handle' => 'x' }))
|
182
|
-
assert_equal("womenswear", template.render!("collection" => { 'handle' => 'y' }))
|
183
|
-
assert_equal("womenswear", template.render!("collection" => { 'handle' => 'z' }))
|
184
|
-
end
|
185
|
-
|
186
|
-
def test_case_when_or
|
187
|
-
code = '{% case condition %}{% when 1 or 2 or 3 %} its 1 or 2 or 3 {% when 4 %} its 4 {% endcase %}'
|
188
|
-
assert_template_result(' its 1 or 2 or 3 ', code, 'condition' => 1)
|
189
|
-
assert_template_result(' its 1 or 2 or 3 ', code, 'condition' => 2)
|
190
|
-
assert_template_result(' its 1 or 2 or 3 ', code, 'condition' => 3)
|
191
|
-
assert_template_result(' its 4 ', code, 'condition' => 4)
|
192
|
-
assert_template_result('', code, 'condition' => 5)
|
193
|
-
|
194
|
-
code = '{% case condition %}{% when 1 or "string" or null %} its 1 or 2 or 3 {% when 4 %} its 4 {% endcase %}'
|
195
|
-
assert_template_result(' its 1 or 2 or 3 ', code, 'condition' => 1)
|
196
|
-
assert_template_result(' its 1 or 2 or 3 ', code, 'condition' => 'string')
|
197
|
-
assert_template_result(' its 1 or 2 or 3 ', code, 'condition' => nil)
|
198
|
-
assert_template_result('', code, 'condition' => 'something else')
|
199
|
-
end
|
200
|
-
|
201
|
-
def test_case_when_comma
|
202
|
-
code = '{% case condition %}{% when 1, 2, 3 %} its 1 or 2 or 3 {% when 4 %} its 4 {% endcase %}'
|
203
|
-
assert_template_result(' its 1 or 2 or 3 ', code, 'condition' => 1)
|
204
|
-
assert_template_result(' its 1 or 2 or 3 ', code, 'condition' => 2)
|
205
|
-
assert_template_result(' its 1 or 2 or 3 ', code, 'condition' => 3)
|
206
|
-
assert_template_result(' its 4 ', code, 'condition' => 4)
|
207
|
-
assert_template_result('', code, 'condition' => 5)
|
208
|
-
|
209
|
-
code = '{% case condition %}{% when 1, "string", null %} its 1 or 2 or 3 {% when 4 %} its 4 {% endcase %}'
|
210
|
-
assert_template_result(' its 1 or 2 or 3 ', code, 'condition' => 1)
|
211
|
-
assert_template_result(' its 1 or 2 or 3 ', code, 'condition' => 'string')
|
212
|
-
assert_template_result(' its 1 or 2 or 3 ', code, 'condition' => nil)
|
213
|
-
assert_template_result('', code, 'condition' => 'something else')
|
214
|
-
end
|
215
|
-
|
216
|
-
def test_case_when_comma_and_blank_body
|
217
|
-
code = '{% case condition %}{% when 1, 2 %} {% assign r = "result" %} {% endcase %}{{ r }}'
|
218
|
-
assert_template_result('result', code, 'condition' => 2)
|
219
|
-
end
|
220
|
-
|
221
|
-
def test_assign
|
222
|
-
assert_template_result('variable', '{% assign a = "variable"%}{{a}}')
|
223
|
-
end
|
224
|
-
|
225
|
-
def test_assign_unassigned
|
226
|
-
assigns = { 'var' => 'content' }
|
227
|
-
assert_template_result('var2: var2:content', 'var2:{{var2}} {%assign var2 = var%} var2:{{var2}}', assigns)
|
228
|
-
end
|
229
|
-
|
230
|
-
def test_assign_an_empty_string
|
231
|
-
assert_template_result('', '{% assign a = ""%}{{a}}')
|
232
|
-
end
|
233
|
-
|
234
|
-
def test_assign_is_global
|
235
|
-
assert_template_result('variable', '{%for i in (1..2) %}{% assign a = "variable"%}{% endfor %}{{a}}')
|
236
|
-
end
|
237
|
-
|
238
|
-
def test_case_detects_bad_syntax
|
239
|
-
assert_raises(SyntaxError) do
|
240
|
-
assert_template_result('', '{% case false %}{% when %}true{% endcase %}', {})
|
241
|
-
end
|
242
|
-
|
243
|
-
assert_raises(SyntaxError) do
|
244
|
-
assert_template_result('', '{% case false %}{% huh %}true{% endcase %}', {})
|
245
|
-
end
|
246
|
-
end
|
247
|
-
|
248
|
-
def test_cycle
|
249
|
-
assert_template_result('one', '{%cycle "one", "two"%}')
|
250
|
-
assert_template_result('one two', '{%cycle "one", "two"%} {%cycle "one", "two"%}')
|
251
|
-
assert_template_result(' two', '{%cycle "", "two"%} {%cycle "", "two"%}')
|
252
|
-
|
253
|
-
assert_template_result('one two one', '{%cycle "one", "two"%} {%cycle "one", "two"%} {%cycle "one", "two"%}')
|
254
|
-
|
255
|
-
assert_template_result('text-align: left text-align: right',
|
256
|
-
'{%cycle "text-align: left", "text-align: right" %} {%cycle "text-align: left", "text-align: right"%}')
|
257
|
-
end
|
258
|
-
|
259
|
-
def test_multiple_cycles
|
260
|
-
assert_template_result('1 2 1 1 2 3 1',
|
261
|
-
'{%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%}')
|
262
|
-
end
|
263
|
-
|
264
|
-
def test_multiple_named_cycles
|
265
|
-
assert_template_result('one one two two one one',
|
266
|
-
'{%cycle 1: "one", "two" %} {%cycle 2: "one", "two" %} {%cycle 1: "one", "two" %} {%cycle 2: "one", "two" %} {%cycle 1: "one", "two" %} {%cycle 2: "one", "two" %}')
|
267
|
-
end
|
268
|
-
|
269
|
-
def test_multiple_named_cycles_with_names_from_context
|
270
|
-
assigns = { "var1" => 1, "var2" => 2 }
|
271
|
-
assert_template_result('one one two two one one',
|
272
|
-
'{%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)
|
273
|
-
end
|
274
|
-
|
275
|
-
def test_size_of_array
|
276
|
-
assigns = { "array" => [1, 2, 3, 4] }
|
277
|
-
assert_template_result('array has 4 elements', "array has {{ array.size }} elements", assigns)
|
278
|
-
end
|
279
|
-
|
280
|
-
def test_size_of_hash
|
281
|
-
assigns = { "hash" => { a: 1, b: 2, c: 3, d: 4 } }
|
282
|
-
assert_template_result('hash has 4 elements', "hash has {{ hash.size }} elements", assigns)
|
283
|
-
end
|
284
|
-
|
285
|
-
def test_illegal_symbols
|
286
|
-
assert_template_result('', '{% if true == empty %}?{% endif %}', {})
|
287
|
-
assert_template_result('', '{% if true == null %}?{% endif %}', {})
|
288
|
-
assert_template_result('', '{% if empty == true %}?{% endif %}', {})
|
289
|
-
assert_template_result('', '{% if null == true %}?{% endif %}', {})
|
290
|
-
end
|
291
|
-
|
292
|
-
def test_ifchanged
|
293
|
-
assigns = { 'array' => [1, 1, 2, 2, 3, 3] }
|
294
|
-
assert_template_result('123', '{%for item in array%}{%ifchanged%}{{item}}{% endifchanged %}{%endfor%}', assigns)
|
295
|
-
|
296
|
-
assigns = { 'array' => [1, 1, 1, 1] }
|
297
|
-
assert_template_result('1', '{%for item in array%}{%ifchanged%}{{item}}{% endifchanged %}{%endfor%}', assigns)
|
298
|
-
end
|
299
|
-
|
300
|
-
def test_multiline_tag
|
301
|
-
assert_template_result('0 1 2 3', "0{%\nfor i in (1..3)\n%} {{\ni\n}}{%\nendfor\n%}")
|
302
|
-
end
|
303
|
-
end # StandardTagTest
|
@@ -1,113 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'test_helper'
|
4
|
-
|
5
|
-
class StatementsTest < Minitest::Test
|
6
|
-
include Liquid
|
7
|
-
|
8
|
-
def test_true_eql_true
|
9
|
-
text = ' {% if true == true %} true {% else %} false {% endif %} '
|
10
|
-
assert_template_result(' true ', text)
|
11
|
-
end
|
12
|
-
|
13
|
-
def test_true_not_eql_true
|
14
|
-
text = ' {% if true != true %} true {% else %} false {% endif %} '
|
15
|
-
assert_template_result(' false ', text)
|
16
|
-
end
|
17
|
-
|
18
|
-
def test_true_lq_true
|
19
|
-
text = ' {% if 0 > 0 %} true {% else %} false {% endif %} '
|
20
|
-
assert_template_result(' false ', text)
|
21
|
-
end
|
22
|
-
|
23
|
-
def test_one_lq_zero
|
24
|
-
text = ' {% if 1 > 0 %} true {% else %} false {% endif %} '
|
25
|
-
assert_template_result(' true ', text)
|
26
|
-
end
|
27
|
-
|
28
|
-
def test_zero_lq_one
|
29
|
-
text = ' {% if 0 < 1 %} true {% else %} false {% endif %} '
|
30
|
-
assert_template_result(' true ', text)
|
31
|
-
end
|
32
|
-
|
33
|
-
def test_zero_lq_or_equal_one
|
34
|
-
text = ' {% if 0 <= 0 %} true {% else %} false {% endif %} '
|
35
|
-
assert_template_result(' true ', text)
|
36
|
-
end
|
37
|
-
|
38
|
-
def test_zero_lq_or_equal_one_involving_nil
|
39
|
-
text = ' {% if null <= 0 %} true {% else %} false {% endif %} '
|
40
|
-
assert_template_result(' false ', text)
|
41
|
-
|
42
|
-
text = ' {% if 0 <= null %} true {% else %} false {% endif %} '
|
43
|
-
assert_template_result(' false ', text)
|
44
|
-
end
|
45
|
-
|
46
|
-
def test_zero_lqq_or_equal_one
|
47
|
-
text = ' {% if 0 >= 0 %} true {% else %} false {% endif %} '
|
48
|
-
assert_template_result(' true ', text)
|
49
|
-
end
|
50
|
-
|
51
|
-
def test_strings
|
52
|
-
text = " {% if 'test' == 'test' %} true {% else %} false {% endif %} "
|
53
|
-
assert_template_result(' true ', text)
|
54
|
-
end
|
55
|
-
|
56
|
-
def test_strings_not_equal
|
57
|
-
text = " {% if 'test' != 'test' %} true {% else %} false {% endif %} "
|
58
|
-
assert_template_result(' false ', text)
|
59
|
-
end
|
60
|
-
|
61
|
-
def test_var_strings_equal
|
62
|
-
text = ' {% if var == "hello there!" %} true {% else %} false {% endif %} '
|
63
|
-
assert_template_result(' true ', text, 'var' => 'hello there!')
|
64
|
-
end
|
65
|
-
|
66
|
-
def test_var_strings_are_not_equal
|
67
|
-
text = ' {% if "hello there!" == var %} true {% else %} false {% endif %} '
|
68
|
-
assert_template_result(' true ', text, 'var' => 'hello there!')
|
69
|
-
end
|
70
|
-
|
71
|
-
def test_var_and_long_string_are_equal
|
72
|
-
text = " {% if var == 'hello there!' %} true {% else %} false {% endif %} "
|
73
|
-
assert_template_result(' true ', text, 'var' => 'hello there!')
|
74
|
-
end
|
75
|
-
|
76
|
-
def test_var_and_long_string_are_equal_backwards
|
77
|
-
text = " {% if 'hello there!' == var %} true {% else %} false {% endif %} "
|
78
|
-
assert_template_result(' true ', text, 'var' => 'hello there!')
|
79
|
-
end
|
80
|
-
|
81
|
-
# def test_is_nil
|
82
|
-
# text = %| {% if var != nil %} true {% else %} false {% end %} |
|
83
|
-
# @template.assigns = { 'var' => 'hello there!'}
|
84
|
-
# expected = %| true |
|
85
|
-
# assert_equal expected, @template.parse(text)
|
86
|
-
# end
|
87
|
-
|
88
|
-
def test_is_collection_empty
|
89
|
-
text = ' {% if array == empty %} true {% else %} false {% endif %} '
|
90
|
-
assert_template_result(' true ', text, 'array' => [])
|
91
|
-
end
|
92
|
-
|
93
|
-
def test_is_not_collection_empty
|
94
|
-
text = ' {% if array == empty %} true {% else %} false {% endif %} '
|
95
|
-
assert_template_result(' false ', text, 'array' => [1, 2, 3])
|
96
|
-
end
|
97
|
-
|
98
|
-
def test_nil
|
99
|
-
text = ' {% if var == nil %} true {% else %} false {% endif %} '
|
100
|
-
assert_template_result(' true ', text, 'var' => nil)
|
101
|
-
|
102
|
-
text = ' {% if var == null %} true {% else %} false {% endif %} '
|
103
|
-
assert_template_result(' true ', text, 'var' => nil)
|
104
|
-
end
|
105
|
-
|
106
|
-
def test_not_nil
|
107
|
-
text = ' {% if var != nil %} true {% else %} false {% endif %} '
|
108
|
-
assert_template_result(' true ', text, 'var' => 1)
|
109
|
-
|
110
|
-
text = ' {% if var != null %} true {% else %} false {% endif %} '
|
111
|
-
assert_template_result(' true ', text, 'var' => 1)
|
112
|
-
end
|
113
|
-
end # StatementsTest
|