Back to Repositories

Testing Inline Comment Functionality in Shopify/liquid

This test suite validates Liquid’s inline comment functionality, ensuring proper handling of comments within templates. It covers various comment syntax patterns and verifies comment behavior in different contexts.

Test Coverage Overview

The test suite comprehensively covers inline comment functionality in Liquid templates.

Key areas tested include:
  • Basic inline comment syntax with different spacing patterns
  • Multi-line comment blocks within liquid tags
  • Comment formatting variations and style options
  • Error handling for incorrect comment syntax
  • Nested tag behavior within comments

Implementation Analysis

The testing approach uses Minitest framework with systematic validation of comment parsing and rendering.

Implementation focuses on:
  • Template result assertions using assert_template_result
  • Syntax error validation with assert_match_syntax_error
  • Multiple comment formatting scenarios
  • Edge case handling for nested tags and malformed comments

Technical Details

Testing infrastructure includes:
  • Minitest as the testing framework
  • Liquid template engine integration
  • Custom assertions for template validation
  • Heredoc syntax for multi-line test cases
  • Frozen string literal pragma for optimization

Best Practices Demonstrated

The test suite exemplifies strong testing practices through:

  • Comprehensive coverage of different comment syntaxes
  • Clear test case organization and naming
  • Thorough validation of error conditions
  • Explicit test cases for edge scenarios
  • Consistent assertion patterns

shopify/liquid

test/integration/tags/inline_comment_test.rb

            
# frozen_string_literal: true

require 'test_helper'

class InlineCommentTest < Minitest::Test
  include Liquid

  def test_inline_comment_returns_nothing
    assert_template_result('', '{%- # this is an inline comment -%}')
    assert_template_result('', '{%-# this is an inline comment -%}')
    assert_template_result('', '{% # this is an inline comment %}')
    assert_template_result('', '{%# this is an inline comment %}')
  end

  def test_inline_comment_does_not_require_a_space_after_the_pound_sign
    assert_template_result('', '{%#this is an inline comment%}')
  end

  def test_liquid_inline_comment_returns_nothing
    assert_template_result('Hey there, how are you doing today?', <<~LIQUID)
      {%- liquid
        # This is how you'd write a block comment in a liquid tag.
        # It looks a lot like what you'd have in ruby.

        # You can use it as inline documentation in your
        # liquid blocks to explain why you're doing something.
        echo "Hey there, "

        # It won't affect the output.
        echo "how are you doing today?"
      -%}
    LIQUID
  end

  def test_inline_comment_can_be_written_on_multiple_lines
    assert_template_result('', <<~LIQUID)
      {%-
        # That kind of block comment is also allowed.
        # It would only be a stylistic difference.

        # Much like JavaScript's /* */ comments and their
        # leading * on new lines.
      -%}
    LIQUID
  end

  def test_inline_comment_multiple_pound_signs
    assert_template_result('', <<~LIQUID)
      {%- liquid
        ######################################
        # We support comments like this too. #
        ######################################
      -%}
    LIQUID
  end

  def test_inline_comments_require_the_pound_sign_on_every_new_line
    assert_match_syntax_error("Each line of comments must be prefixed by the '#' character", <<~LIQUID)
      {%-
        # some comment
        echo 'hello world'
      -%}
    LIQUID
  end

  def test_inline_comment_does_not_support_nested_tags
    assert_template_result(' -%}', "{%- # {% echo 'hello world' %} -%}")
  end
end