Skip to content

Commit

Permalink
skip mocha tests when mocha is not installed
Browse files Browse the repository at this point in the history
  • Loading branch information
Sven Fuchs committed Dec 12, 2009
1 parent cb83519 commit d68b9aa
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 41 deletions.
8 changes: 5 additions & 3 deletions test/cases/backend/active_record_test.rb
Expand Up @@ -40,9 +40,11 @@ def test_store_translations_does_not_allow_ambigous_keys_2
assert_equal 'foo', I18n.t(:foo)
end

def test_missing_translations_table_does_not_cause_available_locales_to_error
I18n::Backend::ActiveRecord::Translation.expects(:available_locales).raises(::ActiveRecord::StatementInvalid)
assert_equal [], I18n.backend.available_locales
with_mocha do
def test_missing_translations_table_does_not_cause_available_locales_to_error
I18n::Backend::ActiveRecord::Translation.expects(:available_locales).raises(::ActiveRecord::StatementInvalid)
assert_equal [], I18n.backend.available_locales
end
end

def test_expand_keys
Expand Down
28 changes: 15 additions & 13 deletions test/cases/backend/cache_test.rb
Expand Up @@ -29,22 +29,24 @@ 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)
with_mocha do
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).never
assert_equal 'Foo', I18n.t(:foo)

I18n.backend.expects(:lookup).returns('Bar')
assert_equal 'Bar', I18n.t(:bar)
end
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) }
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
end

define_method "test uses 'i18n' as a cache key namespace by default" do
Expand Down
46 changes: 25 additions & 21 deletions test/cases/i18n_test.rb
Expand Up @@ -71,26 +71,28 @@ def test_can_set_exception_handler
I18n.exception_handler = :default_exception_handler # revert it
end

def test_uses_custom_exception_handler
I18n.exception_handler = :custom_exception_handler
I18n.expects(:custom_exception_handler)
I18n.translate :bogus
I18n.exception_handler = :default_exception_handler # revert it
end
with_mocha do
def test_uses_custom_exception_handler
I18n.exception_handler = :custom_exception_handler
I18n.expects(:custom_exception_handler)
I18n.translate :bogus
I18n.exception_handler = :default_exception_handler # revert it
end

def test_delegates_translate_to_backend
I18n.backend.expects(:translate).with 'de', :foo, {}
I18n.translate :foo, :locale => 'de'
end
def test_delegates_translate_to_backend
I18n.backend.expects(:translate).with 'de', :foo, {}
I18n.translate :foo, :locale => 'de'
end

def test_delegates_localize_to_backend
I18n.backend.expects(:localize).with 'de', :whatever, :default
I18n.localize :whatever, :locale => 'de'
end
def test_delegates_localize_to_backend
I18n.backend.expects(:localize).with 'de', :whatever, :default
I18n.localize :whatever, :locale => 'de'
end

def test_translate_given_no_locale_uses_i18n_locale
I18n.backend.expects(:translate).with :en, :foo, {}
I18n.translate :foo
def test_translate_given_no_locale_uses_i18n_locale
I18n.backend.expects(:translate).with :en, :foo, {}
I18n.translate :foo
end
end

def test_translate_on_nested_symbol_keys_works
Expand All @@ -117,10 +119,12 @@ def test_translate_with_dot_separated_key_array_and_scope_works
assert_equal [".", ","], I18n.t(%w(format.separator format.delimiter), :scope => 'currency')
end

def test_translate_with_options_using_scope_works
I18n.backend.expects(:translate).with('de', :precision, :scope => :"currency.format")
I18n.with_options :locale => 'de', :scope => :'currency.format' do |locale|
locale.t :precision
with_mocha do
def test_translate_with_options_using_scope_works
I18n.backend.expects(:translate).with('de', :precision, :scope => :"currency.format")
I18n.with_options :locale => 'de', :scope => :'currency.format' do |locale|
locale.t :precision
end
end
end

Expand Down
18 changes: 14 additions & 4 deletions test/test_helper.rb
Expand Up @@ -5,12 +5,18 @@

require 'rubygems'
require 'test/unit'
require 'mocha'
require 'i18n'
require 'i18n/core_ext/object/meta_class'
require 'time'
require 'yaml'

begin
require 'mocha'
rescue LoadError
puts "skipping tests using mocha as mocha can't be found"
end


Dir[File.dirname(__FILE__) + '/api/**/*.rb'].each do |filename|
require filename
end
Expand All @@ -24,11 +30,15 @@
# end
# end

class Test::Unit::TestCase
class Test::Unit::TestCase
def self.test(name, &block)
define_method("test: " + name, &block)
end

def self.with_mocha
yield if Object.respond_to?(:expects)
end

def teardown
I18n.locale = nil
I18n.default_locale = :en
Expand All @@ -40,13 +50,13 @@ def teardown
def translations
I18n.backend.instance_variable_get(:@translations)
end

def store_translations(*args)
data = args.pop
locale = args.pop || :en
I18n.backend.store_translations(locale, data)
end

def locales_dir
File.dirname(__FILE__) + '/fixtures/locales'
end
Expand Down

0 comments on commit d68b9aa

Please sign in to comment.