Back to Repositories

Testing Template Class Implementation in Shopify/Liquid

A comprehensive unit test suite for the Liquid templating engine’s Template class, focusing on localization handling, tag registration, and template inheritance verification. This test file ensures core template parsing and rendering functionality works as expected across different initialization scenarios.

Test Coverage Overview

The test suite provides thorough coverage of Template class functionality in Liquid.

Key areas tested include:
  • Default localization initialization in document parsing
  • Context-specific localization with custom locale files
  • Tag registration and iteration capabilities
  • Template inheritance behavior
Edge cases covered include quick initialization scenarios and custom tag implementations.

Implementation Analysis

The testing approach utilizes Minitest’s assertion framework with focused unit tests for isolated component verification. The implementation follows Ruby’s testing patterns with test helper integration and module inclusion.

Technical patterns include:
  • Custom tag registration testing
  • Fixture-based locale file loading
  • Class inheritance verification
  • Instance type checking

Technical Details

Testing tools and configuration:
  • Test Framework: Minitest
  • Helper Modules: Liquid module inclusion
  • Fixture Support: YAML locale files
  • Custom Components: FakeTag implementation
  • Setup Requirements: test_helper integration

Best Practices Demonstrated

The test suite exemplifies high-quality testing practices through focused test methods and clear assertions.

Notable practices include:
  • Isolated test cases with single responsibility
  • Proper setup and teardown management
  • Clear test naming conventions
  • Effective use of fixtures and helpers
  • Comprehensive edge case coverage

shopify/liquid

test/unit/template_unit_test.rb

            
# frozen_string_literal: true

require 'test_helper'

class TemplateUnitTest < Minitest::Test
  include Liquid

  def test_sets_default_localization_in_document
    t = Template.new
    t.parse('{%comment%}{%endcomment%}')
    assert_instance_of(I18n, t.root.nodelist[0].options[:locale])
  end

  def test_sets_default_localization_in_context_with_quick_initialization
    t = Template.new
    t.parse('{%comment%}{%endcomment%}', locale: I18n.new(fixture("en_locale.yml")))

    locale = t.root.nodelist[0].options[:locale]
    assert_instance_of(I18n, locale)
    assert_equal(fixture("en_locale.yml"), locale.path)
  end

  class FakeTag; end

  def test_tags_can_be_looped_over
    with_custom_tag('fake', FakeTag) do
      result = Template.tags.map { |name, klass| [name, klass] }
      assert(result.include?(["fake", TemplateUnitTest::FakeTag]))
    end
  end

  class TemplateSubclass < Liquid::Template
  end

  def test_template_inheritance
    assert_equal("foo", TemplateSubclass.parse("foo").render)
  end
end