Permalink
Browse files

split up big simple_backend_test file to smaller files

  • Loading branch information...
1 parent 172914a commit a7d692d9e46f92617b463b74ccf26d0c35346c0f Sven Fuchs committed May 1, 2009
View
@@ -1,5 +1,5 @@
dir = File.dirname(__FILE__)
require dir + '/i18n_test.rb'
-require dir + '/simple_backend_test.rb'
+require dir + '/backend/simple/all.rb'
require dir + '/i18n_exceptions_test.rb'
# *require* dir + '/custom_backend_test.rb'
@@ -0,0 +1,3 @@
+Dir[File.dirname(__FILE__) + '/*_test.rb'].each do |file|
+ require file
+end
@@ -0,0 +1,135 @@
+# encoding: utf-8
+require File.expand_path(File.dirname(__FILE__) + '/test_helper')
+
+class I18nSimpleBackendTranslationsTest < Test::Unit::TestCase
+ include I18nSimpleBackendTestSetup
+
+ def test_store_translations_adds_translations # no, really :-)
+ @backend.store_translations :'en', :foo => 'bar'
+ assert_equal Hash[:'en', {:foo => 'bar'}], backend_get_translations
+ end
+
+ def test_store_translations_deep_merges_translations
+ @backend.store_translations :'en', :foo => {:bar => 'bar'}
+ @backend.store_translations :'en', :foo => {:baz => 'baz'}
+ assert_equal Hash[:'en', {:foo => {:bar => 'bar', :baz => 'baz'}}], backend_get_translations
+ end
+
+ def test_store_translations_forces_locale_to_sym
+ @backend.store_translations 'en', :foo => 'bar'
+ assert_equal Hash[:'en', {:foo => 'bar'}], backend_get_translations
+ end
+
+ def test_store_translations_converts_keys_to_symbols
+ # backend_reset_translations!
+ @backend.store_translations 'en', 'foo' => {'bar' => 'bar', 'baz' => 'baz'}
+ assert_equal Hash[:'en', {:foo => {:bar => 'bar', :baz => 'baz'}}], backend_get_translations
+ end
+end
+
+class I18nSimpleBackendAvailableLocalesTest < Test::Unit::TestCase
+ def test_available_locales
+ @backend = I18n::Backend::Simple.new
+ @backend.store_translations 'de', :foo => 'bar'
+ @backend.store_translations 'en', :foo => 'foo'
+
+ assert_equal ['de', 'en'], @backend.available_locales.map{|locale| locale.to_s }.sort
+ end
+end
+
+class I18nSimpleBackendHelperMethodsTest < Test::Unit::TestCase
+ def setup
+ @backend = I18n::Backend::Simple.new
+ end
+
+ def test_deep_symbolize_keys_works
+ result = @backend.send :deep_symbolize_keys, 'foo' => {'bar' => {'baz' => 'bar'}}
+ expected = {:foo => {:bar => {:baz => 'bar'}}}
+ assert_equal expected, result
+ end
+end
+
+class I18nSimpleBackendLoadTranslationsTest < Test::Unit::TestCase
+ include I18nSimpleBackendTestSetup
+
+ def test_load_translations_with_unknown_file_type_raises_exception
+ assert_raises(I18n::UnknownFileType) { @backend.load_translations "#{@locale_dir}/en.xml" }
+ end
+
+ def test_load_translations_with_ruby_file_type_does_not_raise_exception
+ assert_nothing_raised { @backend.load_translations "#{@locale_dir}/en.rb" }
+ end
+
+ def test_load_rb_loads_data_from_ruby_file
+ data = @backend.send :load_rb, "#{@locale_dir}/en.rb"
+ assert_equal({:'en-Ruby' => {:foo => {:bar => "baz"}}}, data)
+ end
+
+ def test_load_rb_loads_data_from_yaml_file
+ data = @backend.send :load_yml, "#{@locale_dir}/en.yml"
+ assert_equal({'en-Yaml' => {'foo' => {'bar' => 'baz'}}}, data)
+ end
+
+ def test_load_translations_loads_from_different_file_formats
+ @backend = I18n::Backend::Simple.new
+ @backend.load_translations "#{@locale_dir}/en.rb", "#{@locale_dir}/en.yml"
+ expected = {
+ :'en-Ruby' => {:foo => {:bar => "baz"}},
+ :'en-Yaml' => {:foo => {:bar => "baz"}}
+ }
+ assert_equal expected, backend_get_translations
+ end
+end
+
+class I18nSimpleBackendLoadPathTest < Test::Unit::TestCase
+ include I18nSimpleBackendTestSetup
+
+ def teardown
+ I18n.load_path = []
+ end
+
+ def test_nested_load_paths_do_not_break_locale_loading
+ @backend = I18n::Backend::Simple.new
+ I18n.load_path = [[File.dirname(__FILE__) + '/../../locale/en.yml']]
+ assert_nil backend_get_translations
+ assert_nothing_raised { @backend.send :init_translations }
+ assert_not_nil backend_get_translations
+ end
+
+ def test_adding_arrays_of_filenames_to_load_path_do_not_break_locale_loading
+ @backend = I18n::Backend::Simple.new
+ I18n.load_path << Dir[File.dirname(__FILE__) + '/../../locale/*.{rb,yml}']
+ assert_nil backend_get_translations
+ assert_nothing_raised { @backend.send :init_translations }
+ assert_not_nil backend_get_translations
+ end
+end
+
+class I18nSimpleBackendReloadTranslationsTest < Test::Unit::TestCase
+ include I18nSimpleBackendTestSetup
+
+ def setup
+ @backend = I18n::Backend::Simple.new
+ I18n.load_path = [File.dirname(__FILE__) + '/../../locale/en.yml']
+ assert_nil backend_get_translations
+ @backend.send :init_translations
+ end
+
+ def teardown
+ I18n.load_path = []
+ end
+
+ def test_setup
+ assert_not_nil backend_get_translations
+ end
+
+ def test_reload_translations_unloads_translations
+ @backend.reload!
+ assert_nil backend_get_translations
+ end
+
+ def test_reload_translations_uninitializes_translations
+ @backend.reload!
+ assert_equal @backend.initialized?, false
+ end
+end
@@ -0,0 +1,67 @@
+# encoding: utf-8
+require File.expand_path(File.dirname(__FILE__) + '/test_helper')
+
+class I18nSimpleBackendInterpolateTest < Test::Unit::TestCase
+ include I18nSimpleBackendTestSetup
+
+ def test_interpolate_given_a_value_hash_interpolates_the_values_to_the_string
+ assert_equal 'Hi David!', @backend.send(:interpolate, nil, 'Hi {{name}}!', :name => 'David')
+ end
+
+ def test_interpolate_given_a_value_hash_interpolates_into_unicode_string
+ assert_equal 'Häi David!', @backend.send(:interpolate, nil, 'Häi {{name}}!', :name => 'David')
+ end
+
+ def test_interpolate_given_an_unicode_value_hash_interpolates_to_the_string
+ assert_equal 'Hi ゆきひろ!', @backend.send(:interpolate, nil, 'Hi {{name}}!', :name => 'ゆきひろ')
+ end
+
+ def test_interpolate_given_an_unicode_value_hash_interpolates_into_unicode_string
+ assert_equal 'こんにちは、ゆきひろさん!', @backend.send(:interpolate, nil, 'こんにちは、{{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 ゆきひろ!'), @backend.send(:interpolate, nil, 'Hi {{name}}!', :name => euc_jp('ゆきひろ'))
+ end
+
+ def test_interpolate_given_an_unicode_value_hash_into_a_non_unicode_multibyte_string_raises_encoding_compatibility_error
+ assert_raises(Encoding::CompatibilityError) do
+ @backend.send(:interpolate, nil, 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
+ @backend.send(:interpolate, nil, 'こんにちは、{{name}}さん!', :name => euc_jp('ゆきひろ'))
+ end
+ end
+ end
+
+ def test_interpolate_given_nil_as_a_string_returns_nil
+ assert_nil @backend.send(:interpolate, nil, nil, :name => 'David')
+ end
+
+ def test_interpolate_given_an_non_string_as_a_string_returns_nil
+ assert_equal [], @backend.send(:interpolate, nil, [], :name => 'David')
+ end
+
+ def test_interpolate_given_a_values_hash_with_nil_values_interpolates_the_string
+ assert_equal 'Hi !', @backend.send(:interpolate, nil, 'Hi {{name}}!', {:name => nil})
+ end
+
+ def test_interpolate_given_an_empty_values_hash_raises_missing_interpolation_argument
+ assert_raises(I18n::MissingInterpolationArgument) { @backend.send(:interpolate, nil, 'Hi {{name}}!', {}) }
+ end
+
+ def test_interpolate_given_a_string_containing_a_reserved_key_raises_reserved_interpolation_key
+ assert_raises(I18n::ReservedInterpolationKey) { @backend.send(:interpolate, nil, '{{default}}', {:default => nil}) }
+ end
+
+ private
+
+ def euc_jp(string)
+ string.encode!(Encoding::EUC_JP)
+ end
+end
+
@@ -0,0 +1,49 @@
+# encoding: utf-8
+require File.expand_path(File.dirname(__FILE__) + '/test_helper')
+
+class I18nSimpleBackendLambdaTest < Test::Unit::TestCase
+ include I18nSimpleBackendTestSetup
+
+ def test_translate_simple_proc
+ setup_proc_translation
+ assert_equal 'bar=bar, baz=baz, foo=foo', @backend.translate('en', :a_lambda, :foo => 'foo', :bar => 'bar', :baz => 'baz')
+ end
+
+ def test_translate_proc_in_defaults
+ setup_proc_translation
+ assert_equal 'bar=bar, baz=baz, foo=foo', @backend.translate('en', :does_not_exist, :default => :a_lambda, :foo => 'foo', :bar => 'bar', :baz => 'baz')
+ assert_equal 'bar=bar, baz=baz, foo=foo', @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_translation
+ params = { :zero => 'zero', :one => 'one', :other => 'other' }
+ assert_equal 'zero', @backend.translate('en', :lambda_for_pluralize, params.merge(:count => 0))
+ assert_equal 'one', @backend.translate('en', :lambda_for_pluralize, params.merge(:count => 1))
+ assert_equal 'other', @backend.translate('en', :lambda_for_pluralize, params.merge(:count => 2))
+ end
+
+ def test_translate_proc_with_interpolate
+ setup_proc_translation
+ assert_equal 'bar baz foo', @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, @backend.translate(:en, :'does not exist', :default => lambda { expected })
+ end
+
+ private
+ def setup_proc_translation
+ @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
+
@@ -0,0 +1,37 @@
+# encoding: utf-8
+require File.expand_path(File.dirname(__FILE__) + '/test_helper')
+
+class I18nSimpleBackendTranslateLinkedTest < Test::Unit::TestCase
+ def setup
+ @backend = I18n::Backend::Simple.new
+ @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
+
+ def test_translate_calls_translate_if_resolves_to_a_symbol
+ assert_equal 'foo', @backend.translate('en', :link_to_foo)
+ end
+
+ def test_translate_calls_translate_if_resolves_to_a_symbol2
+ assert_equal('baz', @backend.translate('en', :link_to_baz))
+ end
+
+ def test_translate_calls_translate_if_resolves_to_a_symbol3
+ assert @backend.translate('en', :link_to_bar).key?(:baz)
+ end
+
+ def test_translate_calls_translate_if_resolves_to_a_symbol_with_scope_1
+ assert_equal('baz', @backend.translate('en', :link_to_baz, :scope => :bar))
+ end
+
+ def test_translate_calls_translate_if_resolves_to_a_symbol_with_scope_1
+ assert_equal('buz', @backend.translate('en', :'bar.link_to_buz'))
+ end
+end
+
Oops, something went wrong.

0 comments on commit a7d692d

Please sign in to comment.