Back to Repositories

Testing Liquid Template Expression Handling in Shopify/liquid

This integration test suite validates expression handling in Liquid templating, focusing on parsing and evaluating different data types and literals. The tests ensure proper handling of strings, numbers, ranges, and keyword literals within Liquid templates.

Test Coverage Overview

The test suite provides comprehensive coverage of Liquid expression parsing and evaluation:

  • Keyword literals (true/false)
  • String handling with single and double quotes
  • Integer and float number processing
  • Range expressions and their validation
  • Error handling for invalid range expressions

Implementation Analysis

The testing approach utilizes Minitest framework with custom assertion helpers for template evaluation. The implementation follows a systematic pattern of testing each data type separately, using assert_template_result and assert_expression_result helpers for verification.

The tests employ both direct template rendering and expression evaluation comparisons.

Technical Details

Testing infrastructure includes:

  • Minitest as the testing framework
  • Custom assertion helpers for template validation
  • Syntax error checking mechanisms
  • Template result verification methods
  • Expression evaluation comparisons

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Isolated test cases for each data type
  • Clear test method naming conventions
  • Comprehensive error case coverage
  • DRY principle through helper methods
  • Consistent assertion patterns

shopify/liquid

test/integration/expression_test.rb

            
# frozen_string_literal: true

require 'test_helper'

class ExpressionTest < Minitest::Test
  def test_keyword_literals
    assert_template_result("true", "{{ true }}")
    assert_expression_result(true, "true")
  end

  def test_string
    assert_template_result("single quoted", "{{'single quoted'}}")
    assert_template_result("double quoted", '{{"double quoted"}}')
    assert_template_result("spaced", "{{ 'spaced' }}")
    assert_template_result("spaced2", "{{ 'spaced2' }}")
  end

  def test_int
    assert_template_result("456", "{{ 456 }}")
    assert_expression_result(123, "123")
    assert_expression_result(12, "012")
  end

  def test_float
    assert_template_result("2.5", "{{ 2.5 }}")
    assert_expression_result(1.5, "1.5")
  end

  def test_range
    assert_template_result("3..4", "{{ ( 3 .. 4 ) }}")
    assert_expression_result(1..2, "(1..2)")

    assert_match_syntax_error(
      "Liquid syntax error (line 1): Invalid expression type 'false' in range expression",
      "{{ (false..true) }}",
    )
    assert_match_syntax_error(
      "Liquid syntax error (line 1): Invalid expression type '(1..2)' in range expression",
      "{{ ((1..2)..3) }}",
    )
  end

  private

  def assert_expression_result(expect, markup, **assigns)
    liquid = "{% if expect == #{markup} %}pass{% else %}got {{ #{markup} }}{% endif %}"
    assert_template_result("pass", liquid, { "expect" => expect, **assigns })
  end
end