Permalink
Browse files

reorganize tests to modules so that we can reuse the API tests for di…

…fferent backend implementations
  • Loading branch information...
1 parent dd38888 commit 298b98254b1cfc40d8681df0fb5a5d59aaca861b Sven Fuchs committed Jul 11, 2009
View
@@ -0,0 +1,13 @@
+module Tests
+ module Backend
+ module Api
+ module Basics
+ def test_available_locales
+ backend_store_translations 'de', :foo => 'bar'
+ backend_store_translations 'en', :foo => 'foo'
+ assert_equal ['de', 'en'], I18n.backend.available_locales.map{|locale| locale.to_s }.sort
+ end
+ end
+ end
+ end
+end
@@ -0,0 +1,63 @@
+module Tests
+ module Backend
+ module Api
+ module Interpolation
+ def interpolate(options)
+ I18n.backend.translate('en', nil, options)
+ end
+
+ def test_interpolation_given_no_interpolation_values_it_does_not_alter_the_string
+ assert_equal 'Hi {{name}}!', interpolate(:default => 'Hi {{name}}!')
+ end
+
+ def test_interpolation_given_interpolation_values_it_interpolates_the_values_to_the_string
+ assert_equal 'Hi David!', interpolate(:default => 'Hi {{name}}!', :name => 'David')
+ end
+
+ def test_interpolation_given_interpolation_values_with_nil_values_it_interpolates_the_values_to_the_string
+ assert_equal 'Hi !', interpolate(:default => 'Hi {{name}}!', :name => nil)
+ end
+
+ def test_interpolate_with_ruby_1_9_syntax
+ assert_equal 'Hi David!', interpolate(:default => 'Hi %{name}!', :name => 'David')
+ end
+
+ def test_interpolate_given_a_value_hash_interpolates_into_unicode_string
+ assert_equal 'Häi David!', interpolate(:default => 'Häi {{name}}!', :name => 'David')
+ end
+
+ def test_interpolate_given_a_unicode_value_hash_interpolates_to_the_string
+ assert_equal 'Hi ゆきひろ!', interpolate(:default => 'Hi {{name}}!', :name => 'ゆきひろ')
+ end
+
+ def test_interpolate_given_a_unicode_value_hash_interpolates_into_unicode_string
+ assert_equal 'こんにちは、ゆきひろさん!', interpolate(:default => 'こんにちは、{{name}}さん!', :name => 'ゆきひろ')
+ end
+
+ if Kernel.const_defined?(:Encoding)
+ def test_interpolate_given_a_non_unicode_multibyte_value_hash_interpolates_into_a_string_with_the_same_encoding
+ assert_equal euc_jp('Hi ゆきひろ!'), interpolate(:default => 'Hi {{name}}!', :name => euc_jp('ゆきひろ'))
+ end
+
+ def test_interpolate_given_a_unicode_value_hash_into_a_non_unicode_multibyte_string_raises_encoding_compatibility_error
+ assert_raises(Encoding::CompatibilityError) do
+ interpolate(:default => euc_jp('こんにちは、{{name}}さん!'), :name => 'ゆきひろ')
+ end
+ end
+
+ def test_interpolate_given_a_non_unicode_multibyte_value_hash_into_an_unicode_string_raises_encoding_compatibility_error
+ assert_raises(Encoding::CompatibilityError) do
+ interpolate(:default => 'こんにちは、{{name}}さん!', :name => euc_jp('ゆきひろ'))
+ end
+ end
+ end
+
+ def test_interpolate_given_a_string_containing_a_reserved_key_raises_reserved_interpolation_key
+ assert_raises(I18n::ReservedInterpolationKey) { interpolate(:default => '{{default}}', :foo => :bar) }
+ assert_raises(I18n::ReservedInterpolationKey) { interpolate(:default => '{{scope}}', :foo => :bar) }
+ assert_raises(I18n::ReservedInterpolationKey) { interpolate(:default => '{{separator}}', :foo => :bar) }
+ end
+ end
+ end
+ end
+end
View
@@ -0,0 +1,50 @@
+module Tests
+ module Backend
+ module Api
+ module Lambda
+ def test_translate_simple_proc
+ setup_proc_translations
+ assert_equal 'bar=bar, baz=baz, foo=foo', I18n.backend.translate('en', :a_lambda, :foo => 'foo', :bar => 'bar', :baz => 'baz')
+ end
+
+ def test_translate_proc_in_defaults
+ setup_proc_translations
+ assert_equal 'bar=bar, baz=baz, foo=foo', I18n.backend.translate('en', :does_not_exist, :default => :a_lambda, :foo => 'foo', :bar => 'bar', :baz => 'baz')
+ assert_equal 'bar=bar, baz=baz, foo=foo', I18n.backend.translate('en', :does_not_exist, :default => [:does_not_exist_2, :does_not_exist_3, :a_lambda], :foo => 'foo', :bar => 'bar', :baz => 'baz')
+ end
+
+ def test_translate_proc_with_pluralize
+ setup_proc_translations
+ params = { :zero => 'zero', :one => 'one', :other => 'other' }
+ assert_equal 'zero', I18n.backend.translate('en', :lambda_for_pluralize, params.merge(:count => 0))
+ assert_equal 'one', I18n.backend.translate('en', :lambda_for_pluralize, params.merge(:count => 1))
+ assert_equal 'other', I18n.backend.translate('en', :lambda_for_pluralize, params.merge(:count => 2))
+ end
+
+ def test_translate_proc_with_interpolate
+ setup_proc_translations
+ assert_equal 'bar baz foo', I18n.backend.translate('en', :lambda_for_interpolate, :foo => 'foo', :bar => 'bar', :baz => 'baz')
+ end
+
+ def test_translate_with_proc_as_default
+ expected = 'result from lambda'
+ assert_equal expected, I18n.backend.translate(:en, :'does not exist', :default => lambda { |key, values| expected })
+ end
+
+ private
+
+ def setup_proc_translations
+ I18n.backend.store_translations 'en', {
+ :a_lambda => lambda { |key, values|
+ values.keys.sort_by(&:to_s).collect { |key| "#{key}=#{values[key]}"}.join(', ')
+ },
+ :lambda_for_pluralize => lambda { |key, values| values },
+ :lambda_for_interpolate => lambda { |key, values|
+ "{{#{values.keys.sort_by(&:to_s).join('}} {{')}}}"
+ }
+ }
+ end
+ end
+ end
+ end
+end
View
@@ -0,0 +1,45 @@
+module Tests
+ module Backend
+ module Api
+ module Link
+ def test_translate_calls_translate_if_resolves_to_a_symbol
+ setup_linked_translations
+ assert_equal 'foo', I18n.backend.translate('en', :link_to_foo)
+ end
+
+ def test_translate_calls_translate_if_resolves_to_a_symbol2
+ setup_linked_translations
+ assert_equal('baz', I18n.backend.translate('en', :link_to_baz))
+ end
+
+ def test_translate_calls_translate_if_resolves_to_a_symbol3
+ setup_linked_translations
+ assert I18n.backend.translate('en', :link_to_bar).key?(:baz)
+ end
+
+ def test_translate_calls_translate_if_resolves_to_a_symbol_with_scope_1
+ setup_linked_translations
+ assert_equal('baz', I18n.backend.translate('en', :link_to_baz, :scope => :bar))
+ end
+
+ def test_translate_calls_translate_if_resolves_to_a_symbol_with_scope_1
+ setup_linked_translations
+ assert_equal('buz', I18n.backend.translate('en', :'bar.link_to_buz'))
+ end
+
+ private
+
+ def setup_linked_translations
+ I18n.backend.store_translations 'en', {
+ :foo => 'foo',
+ :bar => { :baz => 'baz', :link_to_baz => :baz, :link_to_buz => :'boz.buz' },
+ :boz => { :buz => 'buz' },
+ :link_to_foo => :foo,
+ :link_to_bar => :bar,
+ :link_to_baz => :'bar.baz'
+ }
+ end
+ end
+ end
+ end
+end
@@ -0,0 +1,63 @@
+module Tests
+ module Backend
+ module Api
+ module Localization
+ module Date
+ # TODO should be Mrz, shouldn't it?
+ def test_localize_given_the_short_format_it_uses_it
+ assert_equal '01. Mar', I18n.backend.localize('de', date, :short)
+ end
+
+ def test_localize_given_the_long_format_it_uses_it
+ assert_equal '01. März 2008', I18n.backend.localize('de', date, :long)
+ end
+
+ def test_localize_given_the_default_format_it_uses_it
+ assert_equal '01.03.2008', I18n.backend.localize('de', date, :default)
+ end
+
+ def test_localize_given_a_day_name_format_it_returns_a_day_name
+ assert_equal 'Samstag', I18n.backend.localize('de', date, '%A')
+ end
+
+ def test_localize_given_an_abbr_day_name_format_it_returns_an_abbrevated_day_name
+ assert_equal 'Sa', I18n.backend.localize('de', date, '%a')
+ end
+
+ def test_localize_given_a_month_name_format_it_returns_a_month_name
+ assert_equal 'März', I18n.backend.localize('de', date, '%B')
+ end
+
+ # TODO should be Mrz, shouldn't it?
+ def test_localize_given_an_abbr_month_name_format_it_returns_an_abbrevated_month_name
+ assert_equal 'Mar', I18n.backend.localize('de', date, '%b')
+ end
+
+ def test_localize_given_a_format_specified_as_a_proc
+ assert_equal '1ter März 2008', I18n.backend.localize('de', date, :long_ordinalized)
+ end
+
+ def test_localize_given_a_format_specified_as_a_proc_with_additional_options
+ assert_equal '1ter März 2008 (MEZ)', I18n.backend.localize('de', date, :long_ordinalized, :timezone => 'MEZ')
+ end
+
+ def test_localize_given_no_format_it_does_not_fail
+ assert_nothing_raised{ I18n.backend.localize 'de', date }
+ end
+
+ def test_localize_given_an_unknown_format_it_does_not_fail
+ assert_nothing_raised{ I18n.backend.localize 'de', date, '%x' }
+ end
+
+ def test_localize_nil_raises_argument_error
+ assert_raises(I18n::ArgumentError) { I18n.backend.localize 'de', nil }
+ end
+
+ def test_localize_object_raises_argument_error
+ assert_raises(I18n::ArgumentError) { I18n.backend.localize 'de', Object.new }
+ end
+ end
+ end
+ end
+ end
+end
@@ -0,0 +1,61 @@
+module Tests
+ module Backend
+ module Api
+ module Localization
+ module DateTime
+ # TODO should be Mrz, shouldn't it?
+ def test_localize_given_the_short_format_it_uses_it
+ assert_equal '01. Mar 06:00', I18n.backend.localize('de', morning_datetime, :short)
+ end
+
+ def test_localize_given_the_long_format_it_uses_it
+ assert_equal '01. März 2008 06:00', I18n.backend.localize('de', morning_datetime, :long)
+ end
+
+ # TODO should be Mrz, shouldn't it?
+ def test_localize_given_the_default_format_it_uses_it
+ assert_equal 'Sa, 01. Mar 2008 06:00:00 +0000', I18n.backend.localize('de', morning_datetime, :default)
+ end
+
+ def test_localize_given_a_day_name_format_it_returns_the_correct_day_name
+ assert_equal 'Samstag', I18n.backend.localize('de', morning_datetime, '%A')
+ end
+
+ def test_localize_given_an_abbr_day_name_format_it_returns_the_correct_abbrevated_day_name
+ assert_equal 'Sa', I18n.backend.localize('de', morning_datetime, '%a')
+ end
+
+ def test_localize_given_a_month_name_format_it_returns_the_correct_month_name
+ assert_equal 'März', I18n.backend.localize('de', morning_datetime, '%B')
+ end
+
+ # TODO should be Mrz, shouldn't it?
+ def test_localize_given_an_abbr_month_name_format_it_returns_the_correct_abbrevated_month_name
+ assert_equal 'Mar', I18n.backend.localize('de', morning_datetime, '%b')
+ end
+
+ def test_localize_given_a_meridian_indicator_format_it_returns_the_correct_meridian_indicator
+ assert_equal 'am', I18n.backend.localize('de', morning_datetime, '%p')
+ assert_equal 'pm', I18n.backend.localize('de', evening_datetime, '%p')
+ end
+
+ def test_localize_given_a_format_specified_as_a_proc
+ assert_equal '1ter März 2008, 06:00 Uhr', I18n.backend.localize('de', morning_datetime, :long_ordinalized)
+ end
+
+ def test_localize_given_a_format_specified_as_a_proc_with_additional_options
+ assert_equal '1ter März 2008, 06:00 Uhr (MEZ)', I18n.backend.localize('de', morning_datetime, :long_ordinalized, :timezone => 'MEZ')
+ end
+
+ def test_localize_given_no_format_it_does_not_fail
+ assert_nothing_raised{ I18n.backend.localize 'de', morning_datetime }
+ end
+
+ def test_localize_given_an_unknown_format_it_does_not_fail
+ assert_nothing_raised{ I18n.backend.localize 'de', morning_datetime, '%x' }
+ end
+ end
+ end
+ end
+ end
+end
@@ -0,0 +1,24 @@
+module Tests
+ module Backend
+ module Api
+ module Localization
+ module Lambda
+ def test_localize_uses_lambda_day_names
+ assert_match /Суббота/, I18n.backend.localize('ru', time, "%A, %d %B")
+ assert_match /суббота/, I18n.backend.localize('ru', time, "%d %B (%A)")
+ end
+
+ def test_localize_uses_lambda_month_names
+ assert_match /марта/, I18n.backend.localize('ru', time, "%d %B %Y")
+ assert_match /Март/, I18n.backend.localize('ru', time, "%B %Y")
+ end
+
+ def test_localize_uses_lambda_abbr_day_names
+ assert_match /марта/, I18n.backend.localize('ru', time, "%d %b %Y")
+ assert_match /март/, I18n.backend.localize('ru', time, "%b %Y")
+ end
+ end
+ end
+ end
+ end
+end
@@ -0,0 +1,61 @@
+module Tests
+ module Backend
+ module Api
+ module Localization
+ module Time
+ # TODO should be Mrz, shouldn't it?
+ def test_localize_given_the_short_format_it_uses_it
+ assert_equal '01. Mar 06:00', I18n.backend.localize('de', morning_time, :short)
+ end
+
+ def test_localize_given_the_long_format_it_uses_it
+ assert_equal '01. März 2008 06:00', I18n.backend.localize('de', morning_time, :long)
+ end
+
+ # TODO Seems to break on Windows because ENV['TZ'] is ignored. What's a better way to do this?
+ # def test_localize_given_the_default_format_it_uses_it
+ # assert_equal 'Sa, 01. Mar 2008 06:00:00 +0000', I18n.backend.localize('de', morning_time, :default)
+ # end
+
+ def test_localize_given_a_day_name_format_it_returns_the_correct_day_name
+ assert_equal 'Samstag', I18n.backend.localize('de', morning_time, '%A')
+ end
+
+ def test_localize_given_an_abbr_day_name_format_it_returns_the_correct_abbrevated_day_name
+ assert_equal 'Sa', I18n.backend.localize('de', morning_time, '%a')
+ end
+
+ def test_localize_given_a_month_name_format_it_returns_the_correct_month_name
+ assert_equal 'März', I18n.backend.localize('de', morning_time, '%B')
+ end
+
+ # TODO should be Mrz, shouldn't it?
+ def test_localize_given_an_abbr_month_name_format_it_returns_the_correct_abbrevated_month_name
+ assert_equal 'Mar', I18n.backend.localize('de', morning_time, '%b')
+ end
+
+ def test_localize_given_a_meridian_indicator_format_it_returns_the_correct_meridian_indicator
+ assert_equal 'am', I18n.backend.localize('de', morning_time, '%p')
+ assert_equal 'pm', I18n.backend.localize('de', evening_time, '%p')
+ end
+
+ def test_localize_given_a_format_specified_as_a_proc
+ assert_equal '1ter März 2008, 06:00 Uhr', I18n.backend.localize('de', morning_time, :long_ordinalized)
+ end
+
+ def test_localize_given_a_format_specified_as_a_proc_with_additional_options
+ assert_equal '1ter März 2008, 06:00 Uhr (MEZ)', I18n.backend.localize('de', morning_time, :long_ordinalized, :timezone => 'MEZ')
+ end
+
+ def test_localize_given_no_format_it_does_not_fail
+ assert_nothing_raised{ I18n.backend.localize 'de', morning_time }
+ end
+
+ def test_localize_given_an_unknown_format_it_does_not_fail
+ assert_nothing_raised{ I18n.backend.localize 'de', morning_time, '%x' }
+ end
+ end
+ end
+ end
+ end
+end
Oops, something went wrong. Retry.

0 comments on commit 298b982

Please sign in to comment.