10
module I18nSimpleBackendTestSetup
12
# backend_reset_translations!
13
@backend = I18n::Backend::Simple.new
14
@backend.store_translations 'en', :foo => {:bar => 'bar', :baz => 'baz'}
15
@locale_dir = File.dirname(__FILE__) + '/locale'
17
alias :setup :setup_backend
19
# def backend_reset_translations!
20
# I18n::Backend::Simple::ClassMethods.send :class_variable_set, :@@translations, {}
23
def backend_get_translations
24
# I18n::Backend::Simple::ClassMethods.send :class_variable_get, :@@translations
25
@backend.instance_variable_get :@translations
28
def add_datetime_translations
29
@backend.store_translations :'de', {
32
:default => "%d.%m.%Y",
36
:day_names => %w(Sonntag Montag Dienstag Mittwoch Donnerstag Freitag Samstag),
37
:abbr_day_names => %w(So Mo Di Mi Do Fr Sa),
38
:month_names => %w(Januar Februar März April Mai Juni Juli August September Oktober November Dezember).unshift(nil),
39
:abbr_month_names => %w(Jan Feb Mar Apr Mai Jun Jul Aug Sep Okt Nov Dez).unshift(nil),
40
:order => [:day, :month, :year]
44
:default => "%a, %d. %b %Y %H:%M:%S %z",
45
:short => "%d. %b %H:%M",
46
:long => "%d. %B %Y %H:%M",
52
:distance_in_words => {
53
:half_a_minute => 'half a minute',
54
:less_than_x_seconds => {
55
:one => 'less than 1 second',
56
:other => 'less than {{count}} seconds'
60
:other => '{{count}} seconds'
62
:less_than_x_minutes => {
63
:one => 'less than a minute',
64
:other => 'less than {{count}} minutes'
68
:other => '{{count}} minutes'
71
:one => 'about 1 hour',
72
:other => 'about {{count}} hours'
76
:other => '{{count}} days'
79
:one => 'about 1 month',
80
:other => 'about {{count}} months'
84
:other => '{{count}} months'
87
:one => 'about 1 year',
88
:other => 'about {{count}} year'
91
:one => 'over 1 year',
92
:other => 'over {{count}} years'
100
class I18nSimpleBackendTranslationsTest < Test::Unit::TestCase
101
include I18nSimpleBackendTestSetup
103
def test_store_translations_adds_translations # no, really :-)
104
@backend.store_translations :'en', :foo => 'bar'
105
assert_equal Hash[:'en', {:foo => 'bar'}], backend_get_translations
108
def test_store_translations_deep_merges_translations
109
@backend.store_translations :'en', :foo => {:bar => 'bar'}
110
@backend.store_translations :'en', :foo => {:baz => 'baz'}
111
assert_equal Hash[:'en', {:foo => {:bar => 'bar', :baz => 'baz'}}], backend_get_translations
114
def test_store_translations_forces_locale_to_sym
115
@backend.store_translations 'en', :foo => 'bar'
116
assert_equal Hash[:'en', {:foo => 'bar'}], backend_get_translations
119
def test_store_translations_converts_keys_to_symbols
120
# backend_reset_translations!
121
@backend.store_translations 'en', 'foo' => {'bar' => 'bar', 'baz' => 'baz'}
122
assert_equal Hash[:'en', {:foo => {:bar => 'bar', :baz => 'baz'}}], backend_get_translations
126
class I18nSimpleBackendAvailableLocalesTest < Test::Unit::TestCase
127
def test_available_locales
128
@backend = I18n::Backend::Simple.new
129
@backend.store_translations 'de', :foo => 'bar'
130
@backend.store_translations 'en', :foo => 'foo'
132
assert_equal ['de', 'en'], @backend.available_locales.map{|locale| locale.to_s }.sort
136
class I18nSimpleBackendTranslateTest < Test::Unit::TestCase
137
include I18nSimpleBackendTestSetup
139
def test_translate_calls_lookup_with_locale_given
140
@backend.expects(:lookup).with('de', :bar, [:foo]).returns 'bar'
141
@backend.translate 'de', :bar, :scope => [:foo]
144
def test_given_no_keys_it_returns_the_default
145
assert_equal 'default', @backend.translate('en', nil, :default => 'default')
148
def test_translate_given_a_symbol_as_a_default_translates_the_symbol
149
assert_equal 'bar', @backend.translate('en', nil, :scope => [:foo], :default => :bar)
152
def test_translate_given_an_array_as_default_uses_the_first_match
153
assert_equal 'bar', @backend.translate('en', :does_not_exist, :scope => [:foo], :default => [:does_not_exist_2, :bar])
156
def test_translate_given_an_array_of_inexistent_keys_it_raises_missing_translation_data
157
assert_raise I18n::MissingTranslationData do
158
@backend.translate('en', :does_not_exist, :scope => [:foo], :default => [:does_not_exist_2, :does_not_exist_3])
162
def test_translate_an_array_of_keys_translates_all_of_them
163
assert_equal %w(bar baz), @backend.translate('en', [:bar, :baz], :scope => [:foo])
166
def test_translate_calls_pluralize
167
@backend.expects(:pluralize).with 'en', 'bar', 1
168
@backend.translate 'en', :bar, :scope => [:foo], :count => 1
171
def test_translate_calls_interpolate
172
@backend.expects(:interpolate).with 'en', 'bar', {}
173
@backend.translate 'en', :bar, :scope => [:foo]
176
def test_translate_calls_interpolate_including_count_as_a_value
177
@backend.expects(:interpolate).with 'en', 'bar', {:count => 1}
178
@backend.translate 'en', :bar, :scope => [:foo], :count => 1
181
def test_translate_given_nil_as_a_locale_raises_an_argument_error
182
assert_raise(I18n::InvalidLocale){ @backend.translate nil, :bar }
185
def test_translate_with_a_bogus_key_and_no_default_raises_missing_translation_data
186
assert_raise(I18n::MissingTranslationData){ @backend.translate 'de', :bogus }
190
class I18nSimpleBackendLookupTest < Test::Unit::TestCase
191
include I18nSimpleBackendTestSetup
193
# useful because this way we can use the backend with no key for interpolation/pluralization
194
def test_lookup_given_nil_as_a_key_returns_nil
195
assert_nil @backend.send(:lookup, 'en', nil)
198
def test_lookup_given_nested_keys_looks_up_a_nested_hash_value
199
assert_equal 'bar', @backend.send(:lookup, 'en', :bar, [:foo])
203
class I18nSimpleBackendPluralizeTest < Test::Unit::TestCase
204
include I18nSimpleBackendTestSetup
206
def test_pluralize_given_nil_returns_the_given_entry
207
entry = {:one => 'bar', :other => 'bars'}
208
assert_equal entry, @backend.send(:pluralize, nil, entry, nil)
211
def test_pluralize_given_0_returns_zero_string_if_zero_key_given
212
assert_equal 'zero', @backend.send(:pluralize, nil, {:zero => 'zero', :one => 'bar', :other => 'bars'}, 0)
215
def test_pluralize_given_0_returns_plural_string_if_no_zero_key_given
216
assert_equal 'bars', @backend.send(:pluralize, nil, {:one => 'bar', :other => 'bars'}, 0)
219
def test_pluralize_given_1_returns_singular_string
220
assert_equal 'bar', @backend.send(:pluralize, nil, {:one => 'bar', :other => 'bars'}, 1)
223
def test_pluralize_given_2_returns_plural_string
224
assert_equal 'bars', @backend.send(:pluralize, nil, {:one => 'bar', :other => 'bars'}, 2)
227
def test_pluralize_given_3_returns_plural_string
228
assert_equal 'bars', @backend.send(:pluralize, nil, {:one => 'bar', :other => 'bars'}, 3)
231
def test_interpolate_given_incomplete_pluralization_data_raises_invalid_pluralization_data
232
assert_raise(I18n::InvalidPluralizationData){ @backend.send(:pluralize, nil, {:one => 'bar'}, 2) }
235
# def test_interpolate_given_a_string_raises_invalid_pluralization_data
236
# assert_raise(I18n::InvalidPluralizationData){ @backend.send(:pluralize, nil, 'bar', 2) }
239
# def test_interpolate_given_an_array_raises_invalid_pluralization_data
240
# assert_raise(I18n::InvalidPluralizationData){ @backend.send(:pluralize, nil, ['bar'], 2) }
244
class I18nSimpleBackendInterpolateTest < Test::Unit::TestCase
245
include I18nSimpleBackendTestSetup
247
def test_interpolate_given_a_value_hash_interpolates_the_values_to_the_string
248
assert_equal 'Hi David!', @backend.send(:interpolate, nil, 'Hi {{name}}!', :name => 'David')
251
def test_interpolate_given_a_value_hash_interpolates_into_unicode_string
252
assert_equal 'Häi David!', @backend.send(:interpolate, nil, 'Häi {{name}}!', :name => 'David')
255
def test_interpolate_given_an_unicode_value_hash_interpolates_to_the_string
256
assert_equal 'Hi ゆきひろ!', @backend.send(:interpolate, nil, 'Hi {{name}}!', :name => 'ゆきひろ')
259
def test_interpolate_given_an_unicode_value_hash_interpolates_into_unicode_string
260
assert_equal 'こんにちは、ゆきひろさん!', @backend.send(:interpolate, nil, 'こんにちは、{{name}}さん!', :name => 'ゆきひろ')
263
if Kernel.const_defined?(:Encoding)
264
def test_interpolate_given_a_non_unicode_multibyte_value_hash_interpolates_into_a_string_with_the_same_encoding
265
assert_equal euc_jp('Hi ゆきひろ!'), @backend.send(:interpolate, nil, 'Hi {{name}}!', :name => euc_jp('ゆきひろ'))
268
def test_interpolate_given_an_unicode_value_hash_into_a_non_unicode_multibyte_string_raises_encoding_compatibility_error
269
assert_raise(Encoding::CompatibilityError) do
270
@backend.send(:interpolate, nil, euc_jp('こんにちは、{{name}}さん!'), :name => 'ゆきひろ')
274
def test_interpolate_given_a_non_unicode_multibyte_value_hash_into_an_unicode_string_raises_encoding_compatibility_error
275
assert_raise(Encoding::CompatibilityError) do
276
@backend.send(:interpolate, nil, 'こんにちは、{{name}}さん!', :name => euc_jp('ゆきひろ'))
281
def test_interpolate_given_nil_as_a_string_returns_nil
282
assert_nil @backend.send(:interpolate, nil, nil, :name => 'David')
285
def test_interpolate_given_an_non_string_as_a_string_returns_nil
286
assert_equal [], @backend.send(:interpolate, nil, [], :name => 'David')
289
def test_interpolate_given_a_values_hash_with_nil_values_interpolates_the_string
290
assert_equal 'Hi !', @backend.send(:interpolate, nil, 'Hi {{name}}!', {:name => nil})
293
def test_interpolate_given_an_empty_values_hash_raises_missing_interpolation_argument
294
assert_raise(I18n::MissingInterpolationArgument) { @backend.send(:interpolate, nil, 'Hi {{name}}!', {}) }
297
def test_interpolate_given_a_string_containing_a_reserved_key_raises_reserved_interpolation_key
298
assert_raise(I18n::ReservedInterpolationKey) { @backend.send(:interpolate, nil, '{{default}}', {:default => nil}) }
304
string.encode!(Encoding::EUC_JP)
308
class I18nSimpleBackendLocalizeDateTest < Test::Unit::TestCase
309
include I18nSimpleBackendTestSetup
312
@backend = I18n::Backend::Simple.new
313
add_datetime_translations
314
@date = Date.new 2008, 1, 1
317
def test_translate_given_the_short_format_it_uses_it
318
assert_equal '01. Jan', @backend.localize('de', @date, :short)
321
def test_translate_given_the_long_format_it_uses_it
322
assert_equal '01. Januar 2008', @backend.localize('de', @date, :long)
325
def test_translate_given_the_default_format_it_uses_it
326
assert_equal '01.01.2008', @backend.localize('de', @date, :default)
329
def test_translate_given_a_day_name_format_it_returns_a_day_name
330
assert_equal 'Dienstag', @backend.localize('de', @date, '%A')
333
def test_translate_given_an_abbr_day_name_format_it_returns_an_abbrevated_day_name
334
assert_equal 'Di', @backend.localize('de', @date, '%a')
337
def test_translate_given_a_month_name_format_it_returns_a_month_name
338
assert_equal 'Januar', @backend.localize('de', @date, '%B')
341
def test_translate_given_an_abbr_month_name_format_it_returns_an_abbrevated_month_name
342
assert_equal 'Jan', @backend.localize('de', @date, '%b')
345
def test_translate_given_no_format_it_does_not_fail
346
assert_nothing_raised{ @backend.localize 'de', @date }
349
def test_translate_given_an_unknown_format_it_does_not_fail
350
assert_nothing_raised{ @backend.localize 'de', @date, '%x' }
353
def test_localize_nil_raises_argument_error
354
assert_raise(I18n::ArgumentError) { @backend.localize 'de', nil }
357
def test_localize_object_raises_argument_error
358
assert_raise(I18n::ArgumentError) { @backend.localize 'de', Object.new }
362
class I18nSimpleBackendLocalizeDateTimeTest < Test::Unit::TestCase
363
include I18nSimpleBackendTestSetup
366
@backend = I18n::Backend::Simple.new
367
add_datetime_translations
368
@morning = DateTime.new 2008, 1, 1, 6
369
@evening = DateTime.new 2008, 1, 1, 18
372
def test_translate_given_the_short_format_it_uses_it
373
assert_equal '01. Jan 06:00', @backend.localize('de', @morning, :short)
376
def test_translate_given_the_long_format_it_uses_it
377
assert_equal '01. Januar 2008 06:00', @backend.localize('de', @morning, :long)
380
def test_translate_given_the_default_format_it_uses_it
381
assert_equal 'Di, 01. Jan 2008 06:00:00 +0000', @backend.localize('de', @morning, :default)
384
def test_translate_given_a_day_name_format_it_returns_the_correct_day_name
385
assert_equal 'Dienstag', @backend.localize('de', @morning, '%A')
388
def test_translate_given_an_abbr_day_name_format_it_returns_the_correct_abbrevated_day_name
389
assert_equal 'Di', @backend.localize('de', @morning, '%a')
392
def test_translate_given_a_month_name_format_it_returns_the_correct_month_name
393
assert_equal 'Januar', @backend.localize('de', @morning, '%B')
396
def test_translate_given_an_abbr_month_name_format_it_returns_the_correct_abbrevated_month_name
397
assert_equal 'Jan', @backend.localize('de', @morning, '%b')
400
def test_translate_given_a_meridian_indicator_format_it_returns_the_correct_meridian_indicator
401
assert_equal 'am', @backend.localize('de', @morning, '%p')
402
assert_equal 'pm', @backend.localize('de', @evening, '%p')
405
def test_translate_given_no_format_it_does_not_fail
406
assert_nothing_raised{ @backend.localize 'de', @morning }
409
def test_translate_given_an_unknown_format_it_does_not_fail
410
assert_nothing_raised{ @backend.localize 'de', @morning, '%x' }
414
class I18nSimpleBackendLocalizeTimeTest < Test::Unit::TestCase
415
include I18nSimpleBackendTestSetup
418
@old_timezone, ENV['TZ'] = ENV['TZ'], 'UTC'
419
@backend = I18n::Backend::Simple.new
420
add_datetime_translations
421
@morning = Time.parse '2008-01-01 6:00 UTC'
422
@evening = Time.parse '2008-01-01 18:00 UTC'
426
@old_timezone ? ENV['TZ'] = @old_timezone : ENV.delete('TZ')
429
def test_translate_given_the_short_format_it_uses_it
430
assert_equal '01. Jan 06:00', @backend.localize('de', @morning, :short)
433
def test_translate_given_the_long_format_it_uses_it
434
assert_equal '01. Januar 2008 06:00', @backend.localize('de', @morning, :long)
437
# TODO Seems to break on Windows because ENV['TZ'] is ignored. What's a better way to do this?
438
# def test_translate_given_the_default_format_it_uses_it
439
# assert_equal 'Di, 01. Jan 2008 06:00:00 +0000', @backend.localize('de', @morning, :default)
442
def test_translate_given_a_day_name_format_it_returns_the_correct_day_name
443
assert_equal 'Dienstag', @backend.localize('de', @morning, '%A')
446
def test_translate_given_an_abbr_day_name_format_it_returns_the_correct_abbrevated_day_name
447
assert_equal 'Di', @backend.localize('de', @morning, '%a')
450
def test_translate_given_a_month_name_format_it_returns_the_correct_month_name
451
assert_equal 'Januar', @backend.localize('de', @morning, '%B')
454
def test_translate_given_an_abbr_month_name_format_it_returns_the_correct_abbrevated_month_name
455
assert_equal 'Jan', @backend.localize('de', @morning, '%b')
458
def test_translate_given_a_meridian_indicator_format_it_returns_the_correct_meridian_indicator
459
assert_equal 'am', @backend.localize('de', @morning, '%p')
460
assert_equal 'pm', @backend.localize('de', @evening, '%p')
463
def test_translate_given_no_format_it_does_not_fail
464
assert_nothing_raised{ @backend.localize 'de', @morning }
467
def test_translate_given_an_unknown_format_it_does_not_fail
468
assert_nothing_raised{ @backend.localize 'de', @morning, '%x' }
472
class I18nSimpleBackendHelperMethodsTest < Test::Unit::TestCase
474
@backend = I18n::Backend::Simple.new
477
def test_deep_symbolize_keys_works
478
result = @backend.send :deep_symbolize_keys, 'foo' => {'bar' => {'baz' => 'bar'}}
479
expected = {:foo => {:bar => {:baz => 'bar'}}}
480
assert_equal expected, result
484
class I18nSimpleBackendLoadTranslationsTest < Test::Unit::TestCase
485
include I18nSimpleBackendTestSetup
487
def test_load_translations_with_unknown_file_type_raises_exception
488
assert_raise(I18n::UnknownFileType) { @backend.load_translations "#{@locale_dir}/en.xml" }
491
def test_load_translations_with_ruby_file_type_does_not_raise_exception
492
assert_nothing_raised { @backend.load_translations "#{@locale_dir}/en.rb" }
495
def test_load_rb_loads_data_from_ruby_file
496
data = @backend.send :load_rb, "#{@locale_dir}/en.rb"
497
assert_equal({:'en-Ruby' => {:foo => {:bar => "baz"}}}, data)
500
def test_load_rb_loads_data_from_yaml_file
501
data = @backend.send :load_yml, "#{@locale_dir}/en.yml"
502
assert_equal({'en-Yaml' => {'foo' => {'bar' => 'baz'}}}, data)
505
def test_load_translations_loads_from_different_file_formats
506
@backend = I18n::Backend::Simple.new
507
@backend.load_translations "#{@locale_dir}/en.rb", "#{@locale_dir}/en.yml"
509
:'en-Ruby' => {:foo => {:bar => "baz"}},
510
:'en-Yaml' => {:foo => {:bar => "baz"}}
512
assert_equal expected, backend_get_translations
516
class I18nSimpleBackendLoadPathTest < Test::Unit::TestCase
517
include I18nSimpleBackendTestSetup
523
def test_nested_load_paths_do_not_break_locale_loading
524
@backend = I18n::Backend::Simple.new
525
I18n.load_path = [[File.dirname(__FILE__) + '/locale/en.yml']]
526
assert_nil backend_get_translations
527
assert_nothing_raised { @backend.send :init_translations }
528
assert_not_nil backend_get_translations
531
def test_adding_arrays_of_filenames_to_load_path_do_not_break_locale_loading
532
@backend = I18n::Backend::Simple.new
533
I18n.load_path << Dir[File.dirname(__FILE__) + '/locale/*.{rb,yml}']
534
assert_nil backend_get_translations
535
assert_nothing_raised { @backend.send :init_translations }
536
assert_not_nil backend_get_translations
540
class I18nSimpleBackendReloadTranslationsTest < Test::Unit::TestCase
541
include I18nSimpleBackendTestSetup
544
@backend = I18n::Backend::Simple.new
545
I18n.load_path = [File.dirname(__FILE__) + '/locale/en.yml']
546
assert_nil backend_get_translations
547
@backend.send :init_translations
555
assert_not_nil backend_get_translations
558
def test_reload_translations_unloads_translations
560
assert_nil backend_get_translations
563
def test_reload_translations_uninitializes_translations
565
assert_equal @backend.initialized?, false