Back to Repositories

Testing Increment/Decrement Tag Operations in Shopify/Liquid

This test suite validates the increment and decrement tag functionality in Liquid templating, focusing on variable manipulation within template rendering. The tests verify proper counter behavior and variable scope handling.

Test Coverage Overview

The test suite provides comprehensive coverage of increment/decrement operations in Liquid templates.
  • Tests basic increment operations and variable access
  • Verifies multiple increment operations in sequence
  • Tests decrement functionality with initial values
  • Validates interaction between multiple counter variables

Implementation Analysis

The testing approach uses Minitest framework with assertion-based validation. Tests employ assert_template_result to compare expected and actual template rendering outputs. Complex scenarios combine multiple increment/decrement operations to verify proper state management.

Technical Details

  • Testing Framework: Minitest
  • Template Engine: Liquid
  • Test Helper Integration
  • Custom Assertion Methods
  • Template Result Validation

Best Practices Demonstrated

The test suite demonstrates strong testing practices including isolated test cases, clear input/output validation, and comprehensive edge case coverage. Each test method focuses on specific functionality aspects while maintaining readability and maintainability.

shopify/liquid

test/integration/tags/increment_tag_test.rb

            
# frozen_string_literal: true

require 'test_helper'

class IncrementTagTest < Minitest::Test
  include Liquid

  def test_inc
    assert_template_result('0 1', '{%increment port %} {{ port }}')
    assert_template_result(' 0 1 2', '{{port}} {%increment port %} {%increment port%} {{port}}')
    assert_template_result(
      '0 0 1 2 1',
      '{%increment port %} {%increment starboard%} ' \
      '{%increment port %} {%increment port%} ' \
      '{%increment starboard %}',
    )
  end

  def test_dec
    assert_template_result('-1 -1', '{%decrement port %} {{ port }}', { 'port' => 10 })
    assert_template_result(' -1 -2 -2', '{{port}} {%decrement port %} {%decrement port%} {{port}}')
    assert_template_result(
      '0 1 2 0 3 1 1 3',
      '{%increment starboard %} {%increment starboard%} {%increment starboard%} ' \
      '{%increment port %} {%increment starboard%} ' \
      '{%increment port %} {%decrement port%} ' \
      '{%decrement starboard %}',
    )
  end
end