summaryrefslogtreecommitdiff
path: root/vendor/bundle/ruby/3.4.0/gems/liquid-4.0.4/test/integration/assign_test.rb
blob: 55022899998112f207cca8f3f70380ff778823f5 (plain)
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
require 'test_helper'

class AssignTest < Minitest::Test
  include Liquid

  def test_assign_with_hyphen_in_variable_name
    template_source = <<-END_TEMPLATE
    {% assign this-thing = 'Print this-thing' %}
    {{ this-thing }}
    END_TEMPLATE
    template = Template.parse(template_source)
    rendered = template.render!
    assert_equal "Print this-thing", rendered.strip
  end

  def test_assigned_variable
    assert_template_result('.foo.',
      '{% assign foo = values %}.{{ foo[0] }}.',
      'values' => %w(foo bar baz))

    assert_template_result('.bar.',
      '{% assign foo = values %}.{{ foo[1] }}.',
      'values' => %w(foo bar baz))
  end

  def test_assign_with_filter
    assert_template_result('.bar.',
      '{% assign foo = values | split: "," %}.{{ foo[1] }}.',
      'values' => "foo,bar,baz")
  end

  def test_assign_syntax_error
    assert_match_syntax_error(/assign/,
      '{% assign foo not values %}.',
      'values' => "foo,bar,baz")
  end

  def test_assign_uses_error_mode
    with_error_mode(:strict) do
      assert_raises(SyntaxError) do
        Template.parse("{% assign foo = ('X' | downcase) %}")
      end
    end
    with_error_mode(:lax) do
      assert Template.parse("{% assign foo = ('X' | downcase) %}")
    end
  end
end # AssignTest