File: variable_test.rb

package info (click to toggle)
ruby-liquid 5.11.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,372 kB
  • sloc: ruby: 14,164; makefile: 6
file content (277 lines) | stat: -rw-r--r-- 9,530 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# frozen_string_literal: true

require 'test_helper'
require 'timeout'

class VariableTest < Minitest::Test
  include Liquid

  def test_simple_variable
    assert_template_result('worked', "{{test}}", { 'test' => 'worked' })
    assert_template_result('worked wonderfully', "{{test}}", { 'test' => 'worked wonderfully' })
  end

  def test_variable_render_calls_to_liquid
    assert_template_result('foobar', '{{ foo }}', { 'foo' => ThingWithToLiquid.new })
  end

  def test_variable_lookup_calls_to_liquid_value
    assert_template_result('1', '{{ foo }}', { 'foo' => IntegerDrop.new('1') })
    assert_template_result('2', '{{ list[foo] }}', { 'foo' => IntegerDrop.new('1'), 'list' => [1, 2, 3] })
    assert_template_result('one', '{{ list[foo] }}', { 'foo' => IntegerDrop.new('1'), 'list' => { 1 => 'one' } })
    assert_template_result('Yay', '{{ foo }}', { 'foo' => BooleanDrop.new(true) })
    assert_template_result('YAY', '{{ foo | upcase }}', { 'foo' => BooleanDrop.new(true) })
  end

  def test_if_tag_calls_to_liquid_value
    assert_template_result('one', '{% if foo == 1 %}one{% endif %}', { 'foo' => IntegerDrop.new('1') })
    assert_template_result('one', '{% if foo == eqv %}one{% endif %}', { 'foo' => IntegerDrop.new(1), 'eqv' => IntegerDrop.new(1) })
    assert_template_result('one', '{% if 0 < foo %}one{% endif %}', { 'foo' => IntegerDrop.new('1') })
    assert_template_result('one', '{% if foo > 0 %}one{% endif %}', { 'foo' => IntegerDrop.new('1') })
    assert_template_result('one', '{% if b > a %}one{% endif %}', { 'b' => IntegerDrop.new(1), 'a' => IntegerDrop.new(0) })
    assert_template_result('true', '{% if foo == true %}true{% endif %}', { 'foo' => BooleanDrop.new(true) })
    assert_template_result('true', '{% if foo %}true{% endif %}', { 'foo' => BooleanDrop.new(true) })

    assert_template_result('', '{% if foo %}true{% endif %}', { 'foo' => BooleanDrop.new(false) })
    assert_template_result('', '{% if foo == true %}True{% endif %}', { 'foo' => BooleanDrop.new(false) })
    assert_template_result('', '{% if foo and true %}SHOULD NOT HAPPEN{% endif %}', { 'foo' => BooleanDrop.new(false) })

    assert_template_result('one', '{% if a contains x %}one{% endif %}', { 'a' => [1], 'x' => IntegerDrop.new(1) })
  end

  def test_unless_tag_calls_to_liquid_value
    assert_template_result('', '{% unless foo %}true{% endunless %}', { 'foo' => BooleanDrop.new(true) })
    assert_template_result('true', '{% unless foo %}true{% endunless %}', { 'foo' => BooleanDrop.new(false) })
  end

  def test_case_tag_calls_to_liquid_value
    assert_template_result('One', '{% case foo %}{% when 1 %}One{% endcase %}', { 'foo' => IntegerDrop.new('1') })
  end

  def test_simple_with_whitespaces
    assert_template_result("  worked  ", "  {{ test }}  ", { "test" => "worked" })
    assert_template_result("  worked wonderfully  ", "  {{ test }}  ", { "test" => "worked wonderfully" })
  end

  def test_expression_with_whitespace_in_square_brackets
    assert_template_result('result', "{{ a[ 'b' ] }}", { 'a' => { 'b' => 'result' } })
    assert_template_result('result', "{{ a[ [ 'b' ] ] }}", { 'b' => 'c', 'a' => { 'c' => 'result' } })
  end

  def test_ignore_unknown
    assert_template_result("", "{{ test }}")
  end

  def test_using_blank_as_variable_name
    assert_template_result("", "{% assign foo = blank %}{{ foo }}")
  end

  def test_using_empty_as_variable_name
    assert_template_result("", "{% assign foo = empty %}{{ foo }}")
  end

  def test_hash_scoping
    assert_template_result('worked', "{{ test.test }}", { 'test' => { 'test' => 'worked' } })
    assert_template_result('worked', "{{ test . test }}", { 'test' => { 'test' => 'worked' } })
  end

  def test_false_renders_as_false
    assert_template_result("false", "{{ foo }}", { 'foo' => false })
    assert_template_result("false", "{{ false }}")
  end

  def test_nil_renders_as_empty_string
    assert_template_result("", "{{ nil }}")
    assert_template_result("cat", "{{ nil | append: 'cat' }}")
  end

  def test_preset_assigns
    template                 = Template.parse(%({{ test }}))
    template.assigns['test'] = 'worked'
    assert_equal('worked', template.render!)
  end

  def test_reuse_parsed_template
    template                     = Template.parse(%({{ greeting }} {{ name }}))
    template.assigns['greeting'] = 'Goodbye'
    assert_equal('Hello Tobi', template.render!('greeting' => 'Hello', 'name' => 'Tobi'))
    assert_equal('Hello ', template.render!('greeting' => 'Hello', 'unknown' => 'Tobi'))
    assert_equal('Hello Brian', template.render!('greeting' => 'Hello', 'name' => 'Brian'))
    assert_equal('Goodbye Brian', template.render!('name' => 'Brian'))
    assert_equal({ 'greeting' => 'Goodbye' }, template.assigns)
  end

  def test_assigns_not_polluted_from_template
    template                 = Template.parse(%({{ test }}{% assign test = 'bar' %}{{ test }}))
    template.assigns['test'] = 'baz'
    assert_equal('bazbar', template.render!)
    assert_equal('bazbar', template.render!)
    assert_equal('foobar', template.render!('test' => 'foo'))
    assert_equal('bazbar', template.render!)
  end

  def test_hash_with_default_proc
    template        = Template.parse(%(Hello {{ test }}))
    assigns         = Hash.new { |_h, k| raise "Unknown variable '#{k}'" }
    assigns['test'] = 'Tobi'
    assert_equal('Hello Tobi', template.render!(assigns))
    assigns.delete('test')
    e = assert_raises(RuntimeError) do
      template.render!(assigns)
    end
    assert_equal("Unknown variable 'test'", e.message)
  end

  def test_multiline_variable
    assert_template_result("worked", "{{\ntest\n}}", { "test" => "worked" })
  end

  def test_render_symbol
    assert_template_result('bar', '{{ foo }}', { 'foo' => :bar })
  end

  def test_nested_array
    assert_template_result('', '{{ foo }}', { 'foo' => [[nil]] })
  end

  def test_dynamic_find_var
    assert_template_result('bar', '{{ [key] }}', { 'key' => 'foo', 'foo' => 'bar' })
  end

  def test_raw_value_variable
    assert_template_result('bar', '{{ [key] }}', { 'key' => 'foo', 'foo' => 'bar' })
  end

  def test_dynamic_find_var_with_drop
    assert_template_result(
      'bar',
      '{{ [list[settings.zero]] }}',
      {
        'list' => ['foo'],
        'settings' => SettingsDrop.new("zero" => 0),
        'foo' => 'bar',
      },
    )

    assert_template_result(
      'foo',
      '{{ [list[settings.zero]["foo"]] }}',
      {
        'list' => [{ 'foo' => 'bar' }],
        'settings' => SettingsDrop.new("zero" => 0),
        'bar' => 'foo',
      },
    )
  end

  def test_double_nested_variable_lookup
    assert_template_result(
      'bar',
      '{{ list[list[settings.zero]]["foo"] }}',
      {
        'list' => [1, { 'foo' => 'bar' }],
        'settings' => SettingsDrop.new("zero" => 0),
        'bar' => 'foo',
      },
    )
  end

  def test_variable_lookup_should_not_hang_with_invalid_syntax
    Timeout.timeout(1) do
      assert_template_result(
        'bar',
        "{{['foo'}}",
        {
          'foo' => 'bar',
        },
        error_mode: :lax,
      )
    end

    very_long_key = "1234567890" * 100

    template_list = [
      "{{['#{very_long_key}']}}", # valid
      "{{['#{very_long_key}'}}", # missing closing bracket
      "{{[['#{very_long_key}']}}", # extra open bracket
    ]

    template_list.each do |template|
      Timeout.timeout(1) do
        assert_template_result(
          'bar',
          template,
          {
            very_long_key => 'bar',
          },
          error_mode: :lax,
        )
      end
    end
  end

  def test_filter_with_single_trailing_comma
    template = '{{ "hello" | append: "world", }}'

    with_error_modes(:strict) do
      error = assert_raises(Liquid::SyntaxError) { Template.parse(template) }
      assert_match(/is not a valid expression/, error.message)
    end

    with_error_modes(:strict2) do
      assert_template_result('helloworld', template)
    end
  end

  def test_multiple_filters_with_trailing_commas
    template = '{{ "hello" | append: "1", | append: "2", }}'

    with_error_modes(:strict) do
      error = assert_raises(Liquid::SyntaxError) { Template.parse(template) }
      assert_match(/is not a valid expression/, error.message)
    end

    with_error_modes(:strict2) do
      assert_template_result('hello12', template)
    end
  end

  def test_filter_with_colon_but_no_arguments
    template = '{{ "test" | upcase: }}'

    with_error_modes(:strict) do
      error = assert_raises(Liquid::SyntaxError) { Template.parse(template) }
      assert_match(/is not a valid expression/, error.message)
    end

    with_error_modes(:strict2) do
      assert_template_result('TEST', template)
    end
  end

  def test_filter_chain_with_colon_no_args
    template = '{{ "test" | append: "x" | upcase: }}'

    with_error_modes(:strict) do
      error = assert_raises(Liquid::SyntaxError) { Template.parse(template) }
      assert_match(/is not a valid expression/, error.message)
    end

    with_error_modes(:strict2) do
      assert_template_result('TESTX', template)
    end
  end

  def test_combining_trailing_comma_and_empty_args
    template = '{{ "test" | append: "x", | upcase: }}'

    with_error_modes(:strict) do
      error = assert_raises(Liquid::SyntaxError) { Template.parse(template) }
      assert_match(/is not a valid expression/, error.message)
    end

    with_error_modes(:strict2) do
      assert_template_result('TESTX', template)
    end
  end
end