Sha256: ef02e3e39b27fb26e8fb1b029659a87d2feb7044d47f68ccb69c885579add2f4

Contents?: true

Size: 1.44 KB

Versions: 21

Compression:

Stored size: 1.44 KB

Contents

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
    template = Template.parse(template_source)
    rendered = template.render!
    assert_equal "Print this-thing", rendered.strip
  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
    template = Template.parse(template_source)
    rendered = template.render!
    assert_equal "test-string", rendered.gsub(/\s/, '')
  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
    template = Template.parse(template_source)
    rendered = template.render!
    assert_equal "3-3", rendered.gsub(/\s/, '')
  end
end # CaptureTest

Version data entries

21 entries across 21 versions & 4 rubygems

Version Path
liquid-4.0.4 test/integration/capture_test.rb
metanorma-cli-1.3.4 gems/ruby/2.6.0/gems/liquid-4.0.3/test/integration/capture_test.rb
metanorma-cli-1.3.3.1 gems/ruby/2.6.0/gems/liquid-4.0.3/test/integration/capture_test.rb
liquid-4.0.3 test/integration/capture_test.rb
liquid-4.0.2 test/integration/capture_test.rb
liquid-4-0-2-4.0.2 test/integration/capture_test.rb
liquid-4.0.1 test/integration/capture_test.rb
liquid-4.0.0 test/integration/capture_test.rb
liquid-4.0.0.rc3 test/integration/capture_test.rb
liquid-4.0.0.rc2 test/integration/capture_test.rb
liquid-4.0.0.rc1 test/integration/capture_test.rb
locomotivecms-liquid-4.0.0 test/integration/capture_test.rb
liquid-3.0.6 test/integration/capture_test.rb
liquid-3.0.5 test/integration/capture_test.rb
liquid-3.0.4 test/integration/capture_test.rb
liquid-3.0.3 test/integration/capture_test.rb
liquid-3.0.2 test/integration/capture_test.rb
locomotivecms-liquid-4.0.0.alpha2 test/integration/capture_test.rb
locomotivecms-liquid-4.0.0.alpha1 test/integration/capture_test.rb
locomotivecms-liquid-4.0.0.alpha test/integration/capture_test.rb