Back to Repositories

Validating Tag Class Implementation in Shopify/liquid

This test suite validates core functionality of Liquid’s Tag class implementation, focusing on parsing, rendering, and custom tag behavior. The tests ensure proper tag name handling, raw text preservation, and output buffer management.

Test Coverage Overview

The test suite provides comprehensive coverage of Liquid’s Tag class functionality.

Key areas tested include:
  • Basic tag parsing and rendering
  • Raw text preservation in tags with parameters
  • Tag name extraction and validation
  • Custom tag implementation and output buffer handling

Implementation Analysis

The testing approach utilizes Minitest framework with focused unit tests for Tag class methods. The implementation demonstrates clear separation of concerns between parsing, rendering, and custom tag behavior, using Ruby’s class inheritance for extensibility.

Technical patterns include:
  • Tag parsing with tokenizer and context objects
  • Custom tag class inheritance testing
  • Output buffer manipulation verification

Technical Details

Testing infrastructure includes:
  • Minitest as the testing framework
  • Liquid module integration
  • Custom tag class definition for inheritance testing
  • ParseContext and Tokenizer objects for parsing simulation

Best Practices Demonstrated

The test suite exemplifies solid testing practices with isolated test cases and clear assertions. Each test focuses on a specific aspect of tag functionality, from basic parsing to custom implementations.

Notable practices include:
  • Isolated test cases with single responsibility
  • Clear test naming conventions
  • Comprehensive edge case coverage
  • Proper test setup and teardown

shopify/liquid

test/unit/tag_unit_test.rb

            
# frozen_string_literal: true

require 'test_helper'

class TagUnitTest < Minitest::Test
  include Liquid

  def test_tag
    tag = Tag.parse('tag', "", Tokenizer.new(""), ParseContext.new)
    assert_equal('liquid::tag', tag.name)
    assert_equal('', tag.render(Context.new))
  end

  def test_return_raw_text_of_tag
    tag = Tag.parse("long_tag", "param1, param2, param3", Tokenizer.new(""), ParseContext.new)
    assert_equal("long_tag param1, param2, param3", tag.raw)
  end

  def test_tag_name_should_return_name_of_the_tag
    tag = Tag.parse("some_tag", "", Tokenizer.new(""), ParseContext.new)
    assert_equal('some_tag', tag.tag_name)
  end

  class CustomTag < Liquid::Tag
    def render(_context); end
  end

  def test_tag_render_to_output_buffer_nil_value
    custom_tag = CustomTag.parse("some_tag", "", Tokenizer.new(""), ParseContext.new)
    assert_equal('some string', custom_tag.render_to_output_buffer(Context.new, "some string"))
  end
end