Back to Repositories

Testing Comment Tag Implementation in Shopify/liquid

This test suite validates the Liquid template engine’s comment tag functionality, ensuring proper handling of nested comments, whitespace control, and syntax error detection. The tests verify both basic and complex comment tag scenarios in Liquid templates.

Test Coverage Overview

The test suite provides comprehensive coverage of Liquid’s comment tag functionality.

Key areas tested include:
  • Comment tags inside Liquid tags
  • Nested comment handling
  • Whitespace control behavior
  • Syntax error detection
  • Child tag closure requirements

Implementation Analysis

The testing approach uses Minitest framework to systematically verify comment tag behavior. Tests employ assert_template_result and assert_raises methods to validate both successful template rendering and proper error handling.

Notable patterns include testing of:
  • Nested comment structures
  • Whitespace control modifiers
  • Error line number accuracy
  • Comment delimiter variations

Technical Details

Testing infrastructure includes:
  • Minitest as the testing framework
  • Custom assert_template_result helper
  • ErrorDrop test helper class
  • Line number tracking configuration
  • Liquid::Template parser integration

Best Practices Demonstrated

The test suite exemplifies robust testing practices through comprehensive edge case coverage and systematic validation approaches.

Notable practices include:
  • Isolated test cases for specific functionality
  • Explicit error condition testing
  • Consistent assertion patterns
  • Clear test case naming
  • Thorough documentation of test scenarios

shopify/liquid

test/unit/tags/comment_tag_unit_test.rb

            
# frozen_string_literal: true

require 'test_helper'

class CommentTagUnitTest < Minitest::Test
  def test_comment_inside_liquid_tag
    assert_template_result("", <<~LIQUID.chomp)
      {% liquid
        if 1 != 1
        comment
        else
          echo 123
        endcomment
        endif
      %}
    LIQUID
  end

  def test_does_not_parse_nodes_inside_a_comment
    assert_template_result("", <<~LIQUID.chomp)
      {% comment %}
        {% if true %}
        {% if ... %}
        {%- for ? -%}
        {% while true %}
        {%
          unless if
        %}
        {% endcase %}
      {% endcomment %}
    LIQUID
  end

  def test_allows_unclosed_tags
    assert_template_result('', <<~LIQUID.chomp)
      {% comment %}
        {% if true %}
      {% endcomment %}
    LIQUID
  end

  def test_open_tags_in_comment
    assert_template_result('', <<~LIQUID.chomp)
      {% comment %}
        {% assign a = 123 {% comment %}
      {% endcomment %}
    LIQUID

    assert_raises(Liquid::SyntaxError) do
      assert_template_result("", <<~LIQUID.chomp)
        {% comment %}
          {% assign foo = "1"
        {% endcomment %}
      LIQUID
    end

    assert_raises(Liquid::SyntaxError) do
      assert_template_result("", <<~LIQUID.chomp)
        {% comment %}
          {% comment %}
            {% invalid
          {% endcomment %}
        {% endcomment %}
      LIQUID
    end

    assert_raises(Liquid::SyntaxError) do
      assert_template_result("", <<~LIQUID.chomp)
        {% comment %}
        {% {{ {%- endcomment %}
      LIQUID
    end
  end

  def test_child_comment_tags_need_to_be_closed
    assert_template_result("", <<~LIQUID.chomp)
      {% comment %}
        {% comment %}
          {% comment %}{%    endcomment     %}
        {% endcomment %}
      {% endcomment %}
    LIQUID

    assert_raises(Liquid::SyntaxError) do
      assert_template_result("", <<~LIQUID.chomp)
        {% comment %}
          {% comment %}
            {% comment %}
          {% endcomment %}
        {% endcomment %}
      LIQUID
    end
  end

  def test_child_raw_tags_need_to_be_closed
    assert_template_result("", <<~LIQUID.chomp)
      {% comment %}
        {% raw %}
          {% endcomment %}
        {% endraw %}
      {% endcomment %}
    LIQUID

    assert_raises(Liquid::SyntaxError) do
      Liquid::Template.parse(<<~LIQUID.chomp)
        {% comment %}
          {% raw %}
          {% endcomment %}
        {% endcomment %}
      LIQUID
    end
  end

  def test_error_line_number_is_correct
    template = Liquid::Template.parse(<<~LIQUID.chomp, line_numbers: true)
      {% comment %}
        {% if true %}
      {% endcomment %}
      {{ errors.standard_error }}
    LIQUID

    output = template.render('errors' => ErrorDrop.new)
    expected = <<~TEXT.chomp

      Liquid error (line 4): standard error
    TEXT

    assert_equal(expected, output)
  end

  def test_comment_tag_delimiter_with_extra_strings
    assert_template_result(
      '',
      <<~LIQUID.chomp,
        {% comment %}
          {% comment %}
          {% endcomment
          {% if true %}
          {% endif %}
        {% endcomment %}
      LIQUID
    )
  end

  def test_nested_comment_tag_with_extra_strings
    assert_template_result(
      '',
      <<~LIQUID.chomp,
        {% comment %}
          {% comment
            {% assign foo = 1 %}
          {% endcomment
          {% assign foo = 1 %}
        {% endcomment %}
      LIQUID
    )
  end

  def test_ignores_delimiter_with_extra_strings
    assert_template_result(
      '',
      <<~LIQUID.chomp,
        {% if true %}
          {% comment %}
            {% commentXXXXX %}wut{% endcommentXXXXX %}
          {% endcomment %}
        {% endif %}
      LIQUID
    )
  end

  def test_delimiter_can_have_extra_strings
    assert_template_result('', "{% comment %}123{% endcomment xyz %}")
    assert_template_result('', "{% comment %}123{% endcomment\txyz %}")
    assert_template_result('', "{% comment %}123{% endcomment\nxyz %}")
    assert_template_result('', "{% comment %}123{% endcomment\n   xyz  endcomment %}")
    assert_template_result('', "{%comment}{% assign a = 1 %}{%endcomment}{% endif %}")
  end

  def test_with_whitespace_control
    assert_template_result("Hello!", "      {%- comment -%}123{%- endcomment -%}Hello!")
    assert_template_result("Hello!", "{%- comment -%}123{%- endcomment -%}     Hello!")
    assert_template_result("Hello!", "      {%- comment -%}123{%- endcomment -%}     Hello!")

    assert_template_result("Hello!", <<~LIQUID.chomp)
      {%- comment %}Whitespace control!{% endcomment -%}
      Hello!
    LIQUID
  end

  def test_dont_override_liquid_tag_whitespace_control
    assert_template_result("Hello!World!", <<~LIQUID.chomp)
      Hello!
      {%- liquid
        comment
         this is inside a liquid tag
        endcomment
      -%}
      World!
    LIQUID
  end
end