Sha256: 20cc2ca39f5c23a18629b09c0dda268c95fc3ec514361d2422799f6dd08e6410

Contents?: true

Size: 1.64 KB

Versions: 6

Compression:

Stored size: 1.64 KB

Contents

# encoding: utf-8

require File.expand_path(File.dirname(__FILE__) + '/../../test_helper')
require 'i18n/backend/cache'
require 'activesupport'

class I18nCacheBackendTest < Test::Unit::TestCase
  def setup
    super
    I18n.backend.meta_class.send(:include, I18n::Backend::Cache)
    I18n.cache_store = ActiveSupport::Cache.lookup_store(:memory_store)
  end

  def teardown
    I18n.cache_store = nil
  end

  def test_uses_cache
    assert I18n.cache_store.is_a?(ActiveSupport::Cache::MemoryStore)
  end

  define_method :"test translate hits the backend and caches the response" do
    I18n.backend.expects(:lookup).returns('Foo')
    assert_equal 'Foo', I18n.t(:foo)

    I18n.backend.expects(:lookup).never
    assert_equal 'Foo', I18n.t(:foo)

    I18n.backend.expects(:lookup).returns('Bar')
    assert_equal 'Bar', I18n.t(:bar)
  end

  define_method :"test still raises MissingTranslationData but also caches it" do
    I18n.backend.expects(:lookup).returns(nil)
    assert_raises(I18n::MissingTranslationData) { I18n.t(:missing, :raise => true) }
    I18n.backend.expects(:lookup).never
    assert_raises(I18n::MissingTranslationData) { I18n.t(:missing, :raise => true) }
  end

  define_method :"test uses 'i18n' as a cache key namespace by default" do
    assert_equal 0, I18n.backend.send(:cache_key, :foo).index('i18n')
  end

  define_method :"test adds a custom cache key namespace" do
    with_cache_namespace('bar') do
      assert_equal 0, I18n.backend.send(:cache_key, :foo).index('i18n-bar')
    end
  end

  protected

    def with_cache_namespace(namespace)
      I18n.cache_namespace = namespace
      yield
      I18n.cache_namespace = nil
    end
end

Version data entries

6 entries across 6 versions & 2 rubygems

Version Path
pepe-i18n-0.2.0 test/backend/cache/cache_test.rb
polish-0.0.5 lib/vendor/i18n/test/backend/cache/cache_test.rb
polish-0.0.4 lib/vendor/i18n/test/backend/cache/cache_test.rb
polish-0.0.3 lib/vendor/i18n/test/backend/cache/cache_test.rb
polish-0.0.2 lib/vendor/i18n/test/backend/cache/cache_test.rb
polish-0.0.1 lib/vendor/i18n/test/backend/cache/cache_test.rb