Back to Repositories

Testing Tag Disabling Implementation in Shopify/liquid

This integration test suite validates the tag disabling functionality in Liquid templates, focusing on the ability to selectively disable custom tags within specific block contexts. It demonstrates how Liquid’s tag system handles permission restrictions and error messaging for disabled tags.

Test Coverage Overview

The test suite covers tag disabling mechanisms in Liquid templates with comprehensive scenarios:

  • Single tag disabling within block contexts
  • Multiple tag disabling simultaneously
  • Error message generation for disabled tags
  • Nested tag behavior validation

Implementation Analysis

The testing approach uses Minitest framework with custom tag implementations that leverage Liquid’s Disableable module. The tests utilize helper methods for tag registration and context management, demonstrating modular test organization.

Key patterns include:
  • Tag class inheritance and module inclusion
  • Block-scoped tag registration
  • Custom rendering behavior through module prepending

Technical Details

Testing infrastructure includes:
  • Minitest as the testing framework
  • Custom tag classes with Liquid::Tag inheritance
  • Helper methods for test setup and teardown
  • Module-based behavior injection
  • Template parsing and rendering validation

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Isolated test contexts using helper methods
  • Clear test case naming conventions
  • Modular test setup with reusable components
  • Comprehensive error case validation
  • Clean separation of concerns in test implementation

shopify/liquid

test/integration/tag/disableable_test.rb

            
# frozen_string_literal: true

require 'test_helper'

class TagDisableableTest < Minitest::Test
  include Liquid

  module RenderTagName
    def render(_context)
      tag_name
    end
  end

  class Custom < Tag
    prepend Liquid::Tag::Disableable
    include RenderTagName
  end

  class Custom2 < Tag
    prepend Liquid::Tag::Disableable
    include RenderTagName
  end

  class DisableCustom < Block
    disable_tags "custom"
  end

  class DisableBoth < Block
    disable_tags "custom", "custom2"
  end

  def test_block_tag_disabling_nested_tag
    with_disableable_tags do
      with_custom_tag('disable', DisableCustom) do
        output = Template.parse('{% disable %}{% custom %};{% custom2 %}{% enddisable %}').render
        assert_equal('Liquid error: custom usage is not allowed in this context;custom2', output)
      end
    end
  end

  def test_block_tag_disabling_multiple_nested_tags
    with_disableable_tags do
      with_custom_tag('disable', DisableBoth) do
        output = Template.parse('{% disable %}{% custom %};{% custom2 %}{% enddisable %}').render
        assert_equal('Liquid error: custom usage is not allowed in this context;Liquid error: custom2 usage is not allowed in this context', output)
      end
    end
  end

  private

  def with_disableable_tags
    with_custom_tag('custom', Custom) do
      with_custom_tag('custom2', Custom2) do
        yield
      end
    end
  end
end