Back to Repositories

Testing I18n Translation Implementation in Shopify/liquid

This test suite validates the internationalization (i18n) functionality in Liquid’s Ruby implementation. It ensures proper translation string handling, nested translations, and interpolation capabilities while verifying error conditions and default behaviors.

Test Coverage Overview

The test suite provides comprehensive coverage of i18n functionality in Liquid:

  • Basic string translation verification
  • Nested translation path handling
  • String interpolation with variables
  • Error handling for missing translations
  • Default locale path validation

Implementation Analysis

The testing approach utilizes Minitest framework with focused unit tests for the I18n class. It employs fixture-based testing using YAML locale files and validates both successful translation scenarios and error conditions through assertion-based testing patterns.

Technical Details

  • Testing Framework: Minitest
  • Fixture Usage: en_locale.yml for test data
  • Setup: Creates new I18n instance for each test
  • Error Handling: Tests TranslationError raising
  • Assertion Methods: assert_equal and assert_raises

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Isolated test cases with clear purpose
  • Proper setup and fixture usage
  • Comprehensive error condition testing
  • Consistent assertion patterns
  • Clear test naming conventions

shopify/liquid

test/unit/i18n_unit_test.rb

            
# frozen_string_literal: true

require 'test_helper'

class I18nUnitTest < Minitest::Test
  include Liquid

  def setup
    @i18n = I18n.new(fixture("en_locale.yml"))
  end

  def test_simple_translate_string
    assert_equal("less is more", @i18n.translate("simple"))
  end

  def test_nested_translate_string
    assert_equal("something wasn't right", @i18n.translate("errors.syntax.oops"))
  end

  def test_single_string_interpolation
    assert_equal("something different", @i18n.translate("whatever", something: "different"))
  end

  # def test_raises_translation_error_on_undefined_interpolation_key
  #   assert_raises I18n::TranslationError do
  #     @i18n.translate("whatever", :oopstypos => "yes")
  #   end
  # end

  def test_raises_unknown_translation
    assert_raises(I18n::TranslationError) do
      @i18n.translate("doesnt_exist")
    end
  end

  def test_sets_default_path_to_en
    assert_equal(I18n::DEFAULT_LOCALE, I18n.new.path)
  end
end