liquid 4.0.3 → 5.0.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 +33 -0
- data/README.md +6 -0
- data/lib/liquid.rb +17 -5
- data/lib/liquid/block.rb +31 -14
- data/lib/liquid/block_body.rb +164 -54
- data/lib/liquid/condition.rb +39 -18
- data/lib/liquid/context.rb +106 -51
- data/lib/liquid/document.rb +47 -9
- data/lib/liquid/drop.rb +4 -2
- data/lib/liquid/errors.rb +20 -18
- data/lib/liquid/expression.rb +29 -34
- data/lib/liquid/extensions.rb +2 -0
- data/lib/liquid/file_system.rb +6 -4
- data/lib/liquid/forloop_drop.rb +11 -4
- data/lib/liquid/i18n.rb +5 -3
- data/lib/liquid/interrupts.rb +3 -1
- data/lib/liquid/lexer.rb +30 -23
- data/lib/liquid/locales/en.yml +3 -1
- data/lib/liquid/parse_context.rb +16 -4
- data/lib/liquid/parse_tree_visitor.rb +2 -2
- data/lib/liquid/parser.rb +30 -18
- data/lib/liquid/parser_switching.rb +17 -3
- data/lib/liquid/partial_cache.rb +24 -0
- data/lib/liquid/profiler.rb +67 -86
- data/lib/liquid/profiler/hooks.rb +26 -14
- data/lib/liquid/range_lookup.rb +5 -3
- data/lib/liquid/register.rb +6 -0
- data/lib/liquid/resource_limits.rb +47 -8
- data/lib/liquid/standardfilters.rb +63 -44
- data/lib/liquid/static_registers.rb +44 -0
- data/lib/liquid/strainer_factory.rb +36 -0
- data/lib/liquid/strainer_template.rb +53 -0
- data/lib/liquid/tablerowloop_drop.rb +6 -4
- data/lib/liquid/tag.rb +28 -6
- data/lib/liquid/tag/disableable.rb +22 -0
- data/lib/liquid/tag/disabler.rb +21 -0
- data/lib/liquid/tags/assign.rb +24 -10
- data/lib/liquid/tags/break.rb +8 -3
- data/lib/liquid/tags/capture.rb +11 -8
- data/lib/liquid/tags/case.rb +33 -27
- data/lib/liquid/tags/comment.rb +5 -3
- data/lib/liquid/tags/continue.rb +8 -3
- data/lib/liquid/tags/cycle.rb +25 -14
- data/lib/liquid/tags/decrement.rb +6 -3
- data/lib/liquid/tags/echo.rb +26 -0
- data/lib/liquid/tags/for.rb +68 -44
- data/lib/liquid/tags/if.rb +35 -23
- data/lib/liquid/tags/ifchanged.rb +11 -10
- data/lib/liquid/tags/include.rb +34 -47
- data/lib/liquid/tags/increment.rb +7 -3
- data/lib/liquid/tags/raw.rb +14 -11
- data/lib/liquid/tags/render.rb +84 -0
- data/lib/liquid/tags/table_row.rb +23 -19
- data/lib/liquid/tags/unless.rb +15 -15
- data/lib/liquid/template.rb +55 -71
- data/lib/liquid/template_factory.rb +9 -0
- data/lib/liquid/tokenizer.rb +17 -9
- data/lib/liquid/usage.rb +8 -0
- data/lib/liquid/utils.rb +5 -3
- data/lib/liquid/variable.rb +46 -41
- data/lib/liquid/variable_lookup.rb +8 -6
- data/lib/liquid/version.rb +2 -1
- data/test/integration/assign_test.rb +74 -5
- data/test/integration/blank_test.rb +11 -8
- data/test/integration/block_test.rb +47 -1
- data/test/integration/capture_test.rb +18 -10
- data/test/integration/context_test.rb +608 -5
- data/test/integration/document_test.rb +4 -2
- data/test/integration/drop_test.rb +67 -83
- data/test/integration/error_handling_test.rb +73 -61
- data/test/integration/expression_test.rb +46 -0
- data/test/integration/filter_test.rb +53 -42
- data/test/integration/hash_ordering_test.rb +5 -3
- data/test/integration/output_test.rb +26 -24
- data/test/integration/parsing_quirks_test.rb +19 -7
- data/test/integration/{render_profiling_test.rb → profiler_test.rb} +84 -25
- data/test/integration/security_test.rb +30 -21
- data/test/integration/standard_filter_test.rb +339 -281
- data/test/integration/tag/disableable_test.rb +59 -0
- data/test/integration/tag_test.rb +45 -0
- data/test/integration/tags/break_tag_test.rb +4 -2
- data/test/integration/tags/continue_tag_test.rb +4 -2
- data/test/integration/tags/echo_test.rb +13 -0
- data/test/integration/tags/for_tag_test.rb +107 -51
- data/test/integration/tags/if_else_tag_test.rb +5 -3
- data/test/integration/tags/include_tag_test.rb +70 -54
- data/test/integration/tags/increment_tag_test.rb +4 -2
- data/test/integration/tags/liquid_tag_test.rb +116 -0
- data/test/integration/tags/raw_tag_test.rb +14 -11
- data/test/integration/tags/render_tag_test.rb +213 -0
- data/test/integration/tags/standard_tag_test.rb +38 -31
- data/test/integration/tags/statements_test.rb +23 -21
- data/test/integration/tags/table_row_test.rb +2 -0
- data/test/integration/tags/unless_else_tag_test.rb +4 -2
- data/test/integration/template_test.rb +118 -124
- data/test/integration/trim_mode_test.rb +78 -44
- data/test/integration/variable_test.rb +43 -32
- data/test/test_helper.rb +75 -22
- data/test/unit/block_unit_test.rb +19 -24
- data/test/unit/condition_unit_test.rb +79 -77
- data/test/unit/file_system_unit_test.rb +6 -4
- data/test/unit/i18n_unit_test.rb +7 -5
- data/test/unit/lexer_unit_test.rb +11 -9
- data/test/{integration → unit}/parse_tree_visitor_test.rb +2 -2
- data/test/unit/parser_unit_test.rb +37 -35
- data/test/unit/partial_cache_unit_test.rb +128 -0
- data/test/unit/regexp_unit_test.rb +17 -15
- data/test/unit/static_registers_unit_test.rb +156 -0
- data/test/unit/strainer_factory_unit_test.rb +100 -0
- data/test/unit/strainer_template_unit_test.rb +82 -0
- data/test/unit/tag_unit_test.rb +5 -3
- data/test/unit/tags/case_tag_unit_test.rb +3 -1
- data/test/unit/tags/for_tag_unit_test.rb +4 -2
- data/test/unit/tags/if_tag_unit_test.rb +3 -1
- data/test/unit/template_factory_unit_test.rb +12 -0
- data/test/unit/template_unit_test.rb +19 -10
- data/test/unit/tokenizer_unit_test.rb +19 -17
- data/test/unit/variable_unit_test.rb +51 -49
- metadata +73 -47
- data/lib/liquid/strainer.rb +0 -66
- data/lib/liquid/truffle.rb +0 -5
- data/test/truffle/truffle_test.rb +0 -9
- data/test/unit/context_unit_test.rb +0 -489
- data/test/unit/strainer_unit_test.rb +0 -164
@@ -0,0 +1,213 @@
|
|
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,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'test_helper'
|
2
4
|
|
3
5
|
class StandardTagTest < Minitest::Test
|
@@ -69,7 +71,7 @@ class StandardTagTest < Minitest::Test
|
|
69
71
|
assert_raises(SyntaxError) do
|
70
72
|
assert_template_result('content foo content foo ',
|
71
73
|
'{{ var2 }}{% capture %}{{ var }} foo {% endcapture %}{{ var2 }}{{ var2 }}',
|
72
|
-
|
74
|
+
'var' => 'content')
|
73
75
|
end
|
74
76
|
end
|
75
77
|
|
@@ -172,47 +174,52 @@ class StandardTagTest < Minitest::Test
|
|
172
174
|
|
173
175
|
def test_assign_from_case
|
174
176
|
# Example from the shopify forums
|
175
|
-
code
|
177
|
+
code = "{% case collection.handle %}{% when 'menswear-jackets' %}{% assign ptitle = 'menswear' %}{% when 'menswear-t-shirts' %}{% assign ptitle = 'menswear' %}{% else %}{% assign ptitle = 'womenswear' %}{% endcase %}{{ ptitle }}"
|
176
178
|
template = Liquid::Template.parse(code)
|
177
|
-
assert_equal
|
178
|
-
assert_equal
|
179
|
-
assert_equal
|
180
|
-
assert_equal
|
181
|
-
assert_equal
|
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' }))
|
182
184
|
end
|
183
185
|
|
184
186
|
def test_case_when_or
|
185
187
|
code = '{% case condition %}{% when 1 or 2 or 3 %} its 1 or 2 or 3 {% when 4 %} its 4 {% endcase %}'
|
186
|
-
assert_template_result(' its 1 or 2 or 3 ', code,
|
187
|
-
assert_template_result(' its 1 or 2 or 3 ', code,
|
188
|
-
assert_template_result(' its 1 or 2 or 3 ', code,
|
189
|
-
assert_template_result(' its 4 ', code,
|
190
|
-
assert_template_result('', code,
|
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)
|
191
193
|
|
192
194
|
code = '{% case condition %}{% when 1 or "string" or null %} its 1 or 2 or 3 {% when 4 %} its 4 {% endcase %}'
|
193
|
-
assert_template_result(' its 1 or 2 or 3 ', code,
|
194
|
-
assert_template_result(' its 1 or 2 or 3 ', code,
|
195
|
-
assert_template_result(' its 1 or 2 or 3 ', code,
|
196
|
-
assert_template_result('', code,
|
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')
|
197
199
|
end
|
198
200
|
|
199
201
|
def test_case_when_comma
|
200
202
|
code = '{% case condition %}{% when 1, 2, 3 %} its 1 or 2 or 3 {% when 4 %} its 4 {% endcase %}'
|
201
|
-
assert_template_result(' its 1 or 2 or 3 ', code,
|
202
|
-
assert_template_result(' its 1 or 2 or 3 ', code,
|
203
|
-
assert_template_result(' its 1 or 2 or 3 ', code,
|
204
|
-
assert_template_result(' its 4 ', code,
|
205
|
-
assert_template_result('', code,
|
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)
|
206
208
|
|
207
209
|
code = '{% case condition %}{% when 1, "string", null %} its 1 or 2 or 3 {% when 4 %} its 4 {% endcase %}'
|
208
|
-
assert_template_result(' its 1 or 2 or 3 ', code,
|
209
|
-
assert_template_result(' its 1 or 2 or 3 ', code,
|
210
|
-
assert_template_result(' its 1 or 2 or 3 ', code,
|
211
|
-
assert_template_result('', code,
|
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)
|
212
219
|
end
|
213
220
|
|
214
221
|
def test_assign
|
215
|
-
assert_template_result
|
222
|
+
assert_template_result('variable', '{% assign a = "variable"%}{{a}}')
|
216
223
|
end
|
217
224
|
|
218
225
|
def test_assign_unassigned
|
@@ -221,11 +228,11 @@ class StandardTagTest < Minitest::Test
|
|
221
228
|
end
|
222
229
|
|
223
230
|
def test_assign_an_empty_string
|
224
|
-
assert_template_result
|
231
|
+
assert_template_result('', '{% assign a = ""%}{{a}}')
|
225
232
|
end
|
226
233
|
|
227
234
|
def test_assign_is_global
|
228
|
-
assert_template_result
|
235
|
+
assert_template_result('variable', '{%for i in (1..2) %}{% assign a = "variable"%}{% endfor %}{{a}}')
|
229
236
|
end
|
230
237
|
|
231
238
|
def test_case_detects_bad_syntax
|
@@ -283,14 +290,14 @@ class StandardTagTest < Minitest::Test
|
|
283
290
|
end
|
284
291
|
|
285
292
|
def test_ifchanged
|
286
|
-
assigns = { 'array' => [
|
293
|
+
assigns = { 'array' => [1, 1, 2, 2, 3, 3] }
|
287
294
|
assert_template_result('123', '{%for item in array%}{%ifchanged%}{{item}}{% endifchanged %}{%endfor%}', assigns)
|
288
295
|
|
289
|
-
assigns = { 'array' => [
|
296
|
+
assigns = { 'array' => [1, 1, 1, 1] }
|
290
297
|
assert_template_result('1', '{%for item in array%}{%ifchanged%}{{item}}{% endifchanged %}{%endfor%}', assigns)
|
291
298
|
end
|
292
299
|
|
293
300
|
def test_multiline_tag
|
294
|
-
assert_template_result
|
301
|
+
assert_template_result('0 1 2 3', "0{%\nfor i in (1..3)\n%} {{\ni\n}}{%\nendfor\n%}")
|
295
302
|
end
|
296
303
|
end # StandardTagTest
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'test_helper'
|
2
4
|
|
3
5
|
class StatementsTest < Minitest::Test
|
@@ -5,75 +7,75 @@ class StatementsTest < Minitest::Test
|
|
5
7
|
|
6
8
|
def test_true_eql_true
|
7
9
|
text = ' {% if true == true %} true {% else %} false {% endif %} '
|
8
|
-
assert_template_result
|
10
|
+
assert_template_result(' true ', text)
|
9
11
|
end
|
10
12
|
|
11
13
|
def test_true_not_eql_true
|
12
14
|
text = ' {% if true != true %} true {% else %} false {% endif %} '
|
13
|
-
assert_template_result
|
15
|
+
assert_template_result(' false ', text)
|
14
16
|
end
|
15
17
|
|
16
18
|
def test_true_lq_true
|
17
19
|
text = ' {% if 0 > 0 %} true {% else %} false {% endif %} '
|
18
|
-
assert_template_result
|
20
|
+
assert_template_result(' false ', text)
|
19
21
|
end
|
20
22
|
|
21
23
|
def test_one_lq_zero
|
22
24
|
text = ' {% if 1 > 0 %} true {% else %} false {% endif %} '
|
23
|
-
assert_template_result
|
25
|
+
assert_template_result(' true ', text)
|
24
26
|
end
|
25
27
|
|
26
28
|
def test_zero_lq_one
|
27
29
|
text = ' {% if 0 < 1 %} true {% else %} false {% endif %} '
|
28
|
-
assert_template_result
|
30
|
+
assert_template_result(' true ', text)
|
29
31
|
end
|
30
32
|
|
31
33
|
def test_zero_lq_or_equal_one
|
32
34
|
text = ' {% if 0 <= 0 %} true {% else %} false {% endif %} '
|
33
|
-
assert_template_result
|
35
|
+
assert_template_result(' true ', text)
|
34
36
|
end
|
35
37
|
|
36
38
|
def test_zero_lq_or_equal_one_involving_nil
|
37
39
|
text = ' {% if null <= 0 %} true {% else %} false {% endif %} '
|
38
|
-
assert_template_result
|
40
|
+
assert_template_result(' false ', text)
|
39
41
|
|
40
42
|
text = ' {% if 0 <= null %} true {% else %} false {% endif %} '
|
41
|
-
assert_template_result
|
43
|
+
assert_template_result(' false ', text)
|
42
44
|
end
|
43
45
|
|
44
46
|
def test_zero_lqq_or_equal_one
|
45
47
|
text = ' {% if 0 >= 0 %} true {% else %} false {% endif %} '
|
46
|
-
assert_template_result
|
48
|
+
assert_template_result(' true ', text)
|
47
49
|
end
|
48
50
|
|
49
51
|
def test_strings
|
50
52
|
text = " {% if 'test' == 'test' %} true {% else %} false {% endif %} "
|
51
|
-
assert_template_result
|
53
|
+
assert_template_result(' true ', text)
|
52
54
|
end
|
53
55
|
|
54
56
|
def test_strings_not_equal
|
55
57
|
text = " {% if 'test' != 'test' %} true {% else %} false {% endif %} "
|
56
|
-
assert_template_result
|
58
|
+
assert_template_result(' false ', text)
|
57
59
|
end
|
58
60
|
|
59
61
|
def test_var_strings_equal
|
60
62
|
text = ' {% if var == "hello there!" %} true {% else %} false {% endif %} '
|
61
|
-
assert_template_result
|
63
|
+
assert_template_result(' true ', text, 'var' => 'hello there!')
|
62
64
|
end
|
63
65
|
|
64
66
|
def test_var_strings_are_not_equal
|
65
67
|
text = ' {% if "hello there!" == var %} true {% else %} false {% endif %} '
|
66
|
-
assert_template_result
|
68
|
+
assert_template_result(' true ', text, 'var' => 'hello there!')
|
67
69
|
end
|
68
70
|
|
69
71
|
def test_var_and_long_string_are_equal
|
70
72
|
text = " {% if var == 'hello there!' %} true {% else %} false {% endif %} "
|
71
|
-
assert_template_result
|
73
|
+
assert_template_result(' true ', text, 'var' => 'hello there!')
|
72
74
|
end
|
73
75
|
|
74
76
|
def test_var_and_long_string_are_equal_backwards
|
75
77
|
text = " {% if 'hello there!' == var %} true {% else %} false {% endif %} "
|
76
|
-
assert_template_result
|
78
|
+
assert_template_result(' true ', text, 'var' => 'hello there!')
|
77
79
|
end
|
78
80
|
|
79
81
|
# def test_is_nil
|
@@ -85,27 +87,27 @@ class StatementsTest < Minitest::Test
|
|
85
87
|
|
86
88
|
def test_is_collection_empty
|
87
89
|
text = ' {% if array == empty %} true {% else %} false {% endif %} '
|
88
|
-
assert_template_result
|
90
|
+
assert_template_result(' true ', text, 'array' => [])
|
89
91
|
end
|
90
92
|
|
91
93
|
def test_is_not_collection_empty
|
92
94
|
text = ' {% if array == empty %} true {% else %} false {% endif %} '
|
93
|
-
assert_template_result
|
95
|
+
assert_template_result(' false ', text, 'array' => [1, 2, 3])
|
94
96
|
end
|
95
97
|
|
96
98
|
def test_nil
|
97
99
|
text = ' {% if var == nil %} true {% else %} false {% endif %} '
|
98
|
-
assert_template_result
|
100
|
+
assert_template_result(' true ', text, 'var' => nil)
|
99
101
|
|
100
102
|
text = ' {% if var == null %} true {% else %} false {% endif %} '
|
101
|
-
assert_template_result
|
103
|
+
assert_template_result(' true ', text, 'var' => nil)
|
102
104
|
end
|
103
105
|
|
104
106
|
def test_not_nil
|
105
107
|
text = ' {% if var != nil %} true {% else %} false {% endif %} '
|
106
|
-
assert_template_result
|
108
|
+
assert_template_result(' true ', text, 'var' => 1)
|
107
109
|
|
108
110
|
text = ' {% if var != null %} true {% else %} false {% endif %} '
|
109
|
-
assert_template_result
|
111
|
+
assert_template_result(' true ', text, 'var' => 1)
|
110
112
|
end
|
111
113
|
end # StatementsTest
|