Back to Repositories

Testing Unless-Else Tag Conditional Logic in Shopify/liquid

This test suite validates the behavior of Liquid’s unless-else tag functionality in template rendering. It verifies conditional logic execution and template output generation through various test scenarios including loops and boolean conditions.

Test Coverage Overview

The test suite provides comprehensive coverage of the unless-else tag implementation in Liquid templating.

  • Basic unless condition evaluation
  • Unless-else branch handling
  • Integration with loop constructs
  • Boolean and nil value handling

Implementation Analysis

The testing approach uses Minitest framework with focused assertion methods for template rendering validation. Tests systematically verify template output against expected results using assert_template_result helper.

Key patterns include:
  • Template string evaluation
  • Conditional logic verification
  • Loop context integration testing

Technical Details

Testing infrastructure includes:
  • Minitest as the testing framework
  • Liquid module inclusion for template processing
  • Custom assertion helpers for template validation
  • Ruby’s frozen_string_literal pragma for optimization

Best Practices Demonstrated

The test suite exemplifies strong testing practices through systematic validation of feature behavior.

  • Isolated test cases for specific functionality
  • Clear test method naming conventions
  • Comprehensive edge case coverage
  • Integration scenario testing

shopify/liquid

test/integration/tags/unless_else_tag_test.rb

            
# frozen_string_literal: true

require 'test_helper'

class UnlessElseTagTest < Minitest::Test
  include Liquid

  def test_unless
    assert_template_result('  ', ' {% unless true %} this text should not go into the output {% endunless %} ')
    assert_template_result(
      '  this text should go into the output  ',
      ' {% unless false %} this text should go into the output {% endunless %} ',
    )
    assert_template_result('  you rock ?', '{% unless true %} you suck {% endunless %} {% unless false %} you rock {% endunless %}?')
  end

  def test_unless_else
    assert_template_result(' YES ', '{% unless true %} NO {% else %} YES {% endunless %}')
    assert_template_result(' YES ', '{% unless false %} YES {% else %} NO {% endunless %}')
    assert_template_result(' YES ', '{% unless "foo" %} NO {% else %} YES {% endunless %}')
  end

  def test_unless_in_loop
    assert_template_result('23', '{% for i in choices %}{% unless i %}{{ forloop.index }}{% endunless %}{% endfor %}', { 'choices' => [1, nil, false] })
  end

  def test_unless_else_in_loop
    assert_template_result(' TRUE  2  3 ', '{% for i in choices %}{% unless i %} {{ forloop.index }} {% else %} TRUE {% endunless %}{% endfor %}', { 'choices' => [1, nil, false] })
  end
end # UnlessElseTest