File: capture_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 (52 lines) | stat: -rw-r--r-- 1,536 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
# frozen_string_literal: true

require 'test_helper'

class CaptureTest < Minitest::Test
  include Liquid

  def test_captures_block_content_in_variable
    assert_template_result("test string", "{% capture 'var' %}test string{% endcapture %}{{var}}", {})
  end

  def test_capture_with_hyphen_in_variable_name
    template_source = <<~END_TEMPLATE
      {% capture this-thing %}Print this-thing{% endcapture -%}
      {{ this-thing -}}
    END_TEMPLATE
    assert_template_result("Print this-thing", template_source)
  end

  def test_capture_to_variable_from_outer_scope_if_existing
    template_source = <<~END_TEMPLATE
      {% assign var = '' -%}
      {% if true -%}
        {% capture var %}first-block-string{% endcapture -%}
      {% endif -%}
      {% if true -%}
        {% capture var %}test-string{% endcapture -%}
      {% endif -%}
      {{var-}}
    END_TEMPLATE
    assert_template_result("test-string", template_source)
  end

  def test_assigning_from_capture
    template_source = <<~END_TEMPLATE
      {% assign first = '' -%}
      {% assign second = '' -%}
      {% for number in (1..3) -%}
        {% capture first %}{{number}}{% endcapture -%}
        {% assign second = first -%}
      {% endfor -%}
      {{ first }}-{{ second -}}
    END_TEMPLATE
    assert_template_result("3-3", template_source)
  end

  def test_increment_assign_score_by_bytes_not_characters
    t = Template.parse("{% capture foo %}すごい{% endcapture %}")
    t.render!
    assert_equal(9, t.resource_limits.assign_score)
  end
end