65f3ac11a6b260a7e173d6cd6fd857a12fb4ae57
[feedcatcher.git] / vendor / rails / activesupport / lib / active_support / vendor / i18n-0.1.3 / test / simple_backend_test.rb
1 # encoding: utf-8
2 $:.unshift "lib"
3
4 require 'rubygems'
5 require 'test/unit'
6 require 'mocha'
7 require 'i18n'
8 require 'time'
9 require 'yaml'
10
11 module I18nSimpleBackendTestSetup
12 def setup_backend
13 # backend_reset_translations!
14 @backend = I18n::Backend::Simple.new
15 @backend.store_translations 'en', :foo => {:bar => 'bar', :baz => 'baz'}
16 @locale_dir = File.dirname(__FILE__) + '/locale'
17 end
18 alias :setup :setup_backend
19
20 # def backend_reset_translations!
21 # I18n::Backend::Simple::ClassMethods.send :class_variable_set, :@@translations, {}
22 # end
23
24 def backend_get_translations
25 # I18n::Backend::Simple::ClassMethods.send :class_variable_get, :@@translations
26 @backend.instance_variable_get :@translations
27 end
28
29 def add_datetime_translations
30 @backend.store_translations :'de', {
31 :date => {
32 :formats => {
33 :default => "%d.%m.%Y",
34 :short => "%d. %b",
35 :long => "%d. %B %Y",
36 },
37 :day_names => %w(Sonntag Montag Dienstag Mittwoch Donnerstag Freitag Samstag),
38 :abbr_day_names => %w(So Mo Di Mi Do Fr Sa),
39 :month_names => %w(Januar Februar März April Mai Juni Juli August September Oktober November Dezember).unshift(nil),
40 :abbr_month_names => %w(Jan Feb Mar Apr Mai Jun Jul Aug Sep Okt Nov Dez).unshift(nil),
41 :order => [:day, :month, :year]
42 },
43 :time => {
44 :formats => {
45 :default => "%a, %d. %b %Y %H:%M:%S %z",
46 :short => "%d. %b %H:%M",
47 :long => "%d. %B %Y %H:%M",
48 },
49 :am => 'am',
50 :pm => 'pm'
51 },
52 :datetime => {
53 :distance_in_words => {
54 :half_a_minute => 'half a minute',
55 :less_than_x_seconds => {
56 :one => 'less than 1 second',
57 :other => 'less than {{count}} seconds'
58 },
59 :x_seconds => {
60 :one => '1 second',
61 :other => '{{count}} seconds'
62 },
63 :less_than_x_minutes => {
64 :one => 'less than a minute',
65 :other => 'less than {{count}} minutes'
66 },
67 :x_minutes => {
68 :one => '1 minute',
69 :other => '{{count}} minutes'
70 },
71 :about_x_hours => {
72 :one => 'about 1 hour',
73 :other => 'about {{count}} hours'
74 },
75 :x_days => {
76 :one => '1 day',
77 :other => '{{count}} days'
78 },
79 :about_x_months => {
80 :one => 'about 1 month',
81 :other => 'about {{count}} months'
82 },
83 :x_months => {
84 :one => '1 month',
85 :other => '{{count}} months'
86 },
87 :about_x_years => {
88 :one => 'about 1 year',
89 :other => 'about {{count}} year'
90 },
91 :over_x_years => {
92 :one => 'over 1 year',
93 :other => 'over {{count}} years'
94 }
95 }
96 }
97 }
98 end
99 end
100
101 class I18nSimpleBackendTranslationsTest < Test::Unit::TestCase
102 include I18nSimpleBackendTestSetup
103
104 def test_store_translations_adds_translations # no, really :-)
105 @backend.store_translations :'en', :foo => 'bar'
106 assert_equal Hash[:'en', {:foo => 'bar'}], backend_get_translations
107 end
108
109 def test_store_translations_deep_merges_translations
110 @backend.store_translations :'en', :foo => {:bar => 'bar'}
111 @backend.store_translations :'en', :foo => {:baz => 'baz'}
112 assert_equal Hash[:'en', {:foo => {:bar => 'bar', :baz => 'baz'}}], backend_get_translations
113 end
114
115 def test_store_translations_forces_locale_to_sym
116 @backend.store_translations 'en', :foo => 'bar'
117 assert_equal Hash[:'en', {:foo => 'bar'}], backend_get_translations
118 end
119
120 def test_store_translations_converts_keys_to_symbols
121 # backend_reset_translations!
122 @backend.store_translations 'en', 'foo' => {'bar' => 'bar', 'baz' => 'baz'}
123 assert_equal Hash[:'en', {:foo => {:bar => 'bar', :baz => 'baz'}}], backend_get_translations
124 end
125 end
126
127 class I18nSimpleBackendAvailableLocalesTest < Test::Unit::TestCase
128 def test_available_locales
129 @backend = I18n::Backend::Simple.new
130 @backend.store_translations 'de', :foo => 'bar'
131 @backend.store_translations 'en', :foo => 'foo'
132
133 assert_equal ['de', 'en'], @backend.available_locales.map{|locale| locale.to_s }.sort
134 end
135 end
136
137 class I18nSimpleBackendTranslateTest < Test::Unit::TestCase
138 include I18nSimpleBackendTestSetup
139
140 def test_translate_calls_lookup_with_locale_given
141 @backend.expects(:lookup).with('de', :bar, [:foo]).returns 'bar'
142 @backend.translate 'de', :bar, :scope => [:foo]
143 end
144
145 def test_given_no_keys_it_returns_the_default
146 assert_equal 'default', @backend.translate('en', nil, :default => 'default')
147 end
148
149 def test_translate_given_a_symbol_as_a_default_translates_the_symbol
150 assert_equal 'bar', @backend.translate('en', nil, :scope => [:foo], :default => :bar)
151 end
152
153 def test_translate_given_an_array_as_default_uses_the_first_match
154 assert_equal 'bar', @backend.translate('en', :does_not_exist, :scope => [:foo], :default => [:does_not_exist_2, :bar])
155 end
156
157 def test_translate_given_an_array_of_inexistent_keys_it_raises_missing_translation_data
158 assert_raise I18n::MissingTranslationData do
159 @backend.translate('en', :does_not_exist, :scope => [:foo], :default => [:does_not_exist_2, :does_not_exist_3])
160 end
161 end
162
163 def test_translate_an_array_of_keys_translates_all_of_them
164 assert_equal %w(bar baz), @backend.translate('en', [:bar, :baz], :scope => [:foo])
165 end
166
167 def test_translate_calls_pluralize
168 @backend.expects(:pluralize).with 'en', 'bar', 1
169 @backend.translate 'en', :bar, :scope => [:foo], :count => 1
170 end
171
172 def test_translate_calls_interpolate
173 @backend.expects(:interpolate).with 'en', 'bar', {}
174 @backend.translate 'en', :bar, :scope => [:foo]
175 end
176
177 def test_translate_calls_interpolate_including_count_as_a_value
178 @backend.expects(:interpolate).with 'en', 'bar', {:count => 1}
179 @backend.translate 'en', :bar, :scope => [:foo], :count => 1
180 end
181
182 def test_translate_given_nil_as_a_locale_raises_an_argument_error
183 assert_raise(I18n::InvalidLocale){ @backend.translate nil, :bar }
184 end
185
186 def test_translate_with_a_bogus_key_and_no_default_raises_missing_translation_data
187 assert_raise(I18n::MissingTranslationData){ @backend.translate 'de', :bogus }
188 end
189 end
190
191 class I18nSimpleBackendLookupTest < Test::Unit::TestCase
192 include I18nSimpleBackendTestSetup
193
194 # useful because this way we can use the backend with no key for interpolation/pluralization
195 def test_lookup_given_nil_as_a_key_returns_nil
196 assert_nil @backend.send(:lookup, 'en', nil)
197 end
198
199 def test_lookup_given_nested_keys_looks_up_a_nested_hash_value
200 assert_equal 'bar', @backend.send(:lookup, 'en', :bar, [:foo])
201 end
202 end
203
204 class I18nSimpleBackendPluralizeTest < Test::Unit::TestCase
205 include I18nSimpleBackendTestSetup
206
207 def test_pluralize_given_nil_returns_the_given_entry
208 entry = {:one => 'bar', :other => 'bars'}
209 assert_equal entry, @backend.send(:pluralize, nil, entry, nil)
210 end
211
212 def test_pluralize_given_0_returns_zero_string_if_zero_key_given
213 assert_equal 'zero', @backend.send(:pluralize, nil, {:zero => 'zero', :one => 'bar', :other => 'bars'}, 0)
214 end
215
216 def test_pluralize_given_0_returns_plural_string_if_no_zero_key_given
217 assert_equal 'bars', @backend.send(:pluralize, nil, {:one => 'bar', :other => 'bars'}, 0)
218 end
219
220 def test_pluralize_given_1_returns_singular_string
221 assert_equal 'bar', @backend.send(:pluralize, nil, {:one => 'bar', :other => 'bars'}, 1)
222 end
223
224 def test_pluralize_given_2_returns_plural_string
225 assert_equal 'bars', @backend.send(:pluralize, nil, {:one => 'bar', :other => 'bars'}, 2)
226 end
227
228 def test_pluralize_given_3_returns_plural_string
229 assert_equal 'bars', @backend.send(:pluralize, nil, {:one => 'bar', :other => 'bars'}, 3)
230 end
231
232 def test_interpolate_given_incomplete_pluralization_data_raises_invalid_pluralization_data
233 assert_raise(I18n::InvalidPluralizationData){ @backend.send(:pluralize, nil, {:one => 'bar'}, 2) }
234 end
235
236 # def test_interpolate_given_a_string_raises_invalid_pluralization_data
237 # assert_raise(I18n::InvalidPluralizationData){ @backend.send(:pluralize, nil, 'bar', 2) }
238 # end
239 #
240 # def test_interpolate_given_an_array_raises_invalid_pluralization_data
241 # assert_raise(I18n::InvalidPluralizationData){ @backend.send(:pluralize, nil, ['bar'], 2) }
242 # end
243 end
244
245 class I18nSimpleBackendInterpolateTest < Test::Unit::TestCase
246 include I18nSimpleBackendTestSetup
247
248 def test_interpolate_given_a_value_hash_interpolates_the_values_to_the_string
249 assert_equal 'Hi David!', @backend.send(:interpolate, nil, 'Hi {{name}}!', :name => 'David')
250 end
251
252 def test_interpolate_given_a_value_hash_interpolates_into_unicode_string
253 assert_equal 'Häi David!', @backend.send(:interpolate, nil, 'Häi {{name}}!', :name => 'David')
254 end
255
256 def test_interpolate_given_an_unicode_value_hash_interpolates_to_the_string
257 assert_equal 'Hi ゆきひろ!', @backend.send(:interpolate, nil, 'Hi {{name}}!', :name => 'ゆきひろ')
258 end
259
260 def test_interpolate_given_an_unicode_value_hash_interpolates_into_unicode_string
261 assert_equal 'こんにちは、ゆきひろさん!', @backend.send(:interpolate, nil, 'こんにちは、{{name}}さん!', :name => 'ゆきひろ')
262 end
263
264 if Kernel.const_defined?(:Encoding)
265 def test_interpolate_given_a_non_unicode_multibyte_value_hash_interpolates_into_a_string_with_the_same_encoding
266 assert_equal euc_jp('Hi ゆきひろ!'), @backend.send(:interpolate, nil, 'Hi {{name}}!', :name => euc_jp('ゆきひろ'))
267 end
268
269 def test_interpolate_given_an_unicode_value_hash_into_a_non_unicode_multibyte_string_raises_encoding_compatibility_error
270 assert_raise(Encoding::CompatibilityError) do
271 @backend.send(:interpolate, nil, euc_jp('こんにちは、{{name}}さん!'), :name => 'ゆきひろ')
272 end
273 end
274
275 def test_interpolate_given_a_non_unicode_multibyte_value_hash_into_an_unicode_string_raises_encoding_compatibility_error
276 assert_raise(Encoding::CompatibilityError) do
277 @backend.send(:interpolate, nil, 'こんにちは、{{name}}さん!', :name => euc_jp('ゆきひろ'))
278 end
279 end
280 end
281
282 def test_interpolate_given_nil_as_a_string_returns_nil
283 assert_nil @backend.send(:interpolate, nil, nil, :name => 'David')
284 end
285
286 def test_interpolate_given_an_non_string_as_a_string_returns_nil
287 assert_equal [], @backend.send(:interpolate, nil, [], :name => 'David')
288 end
289
290 def test_interpolate_given_a_values_hash_with_nil_values_interpolates_the_string
291 assert_equal 'Hi !', @backend.send(:interpolate, nil, 'Hi {{name}}!', {:name => nil})
292 end
293
294 def test_interpolate_given_an_empty_values_hash_raises_missing_interpolation_argument
295 assert_raise(I18n::MissingInterpolationArgument) { @backend.send(:interpolate, nil, 'Hi {{name}}!', {}) }
296 end
297
298 def test_interpolate_given_a_string_containing_a_reserved_key_raises_reserved_interpolation_key
299 assert_raise(I18n::ReservedInterpolationKey) { @backend.send(:interpolate, nil, '{{default}}', {:default => nil}) }
300 end
301
302 private
303
304 def euc_jp(string)
305 string.encode!(Encoding::EUC_JP)
306 end
307 end
308
309 class I18nSimpleBackendLocalizeDateTest < Test::Unit::TestCase
310 include I18nSimpleBackendTestSetup
311
312 def setup
313 @backend = I18n::Backend::Simple.new
314 add_datetime_translations
315 @date = Date.new 2008, 1, 1
316 end
317
318 def test_translate_given_the_short_format_it_uses_it
319 assert_equal '01. Jan', @backend.localize('de', @date, :short)
320 end
321
322 def test_translate_given_the_long_format_it_uses_it
323 assert_equal '01. Januar 2008', @backend.localize('de', @date, :long)
324 end
325
326 def test_translate_given_the_default_format_it_uses_it
327 assert_equal '01.01.2008', @backend.localize('de', @date, :default)
328 end
329
330 def test_translate_given_a_day_name_format_it_returns_a_day_name
331 assert_equal 'Dienstag', @backend.localize('de', @date, '%A')
332 end
333
334 def test_translate_given_an_abbr_day_name_format_it_returns_an_abbrevated_day_name
335 assert_equal 'Di', @backend.localize('de', @date, '%a')
336 end
337
338 def test_translate_given_a_month_name_format_it_returns_a_month_name
339 assert_equal 'Januar', @backend.localize('de', @date, '%B')
340 end
341
342 def test_translate_given_an_abbr_month_name_format_it_returns_an_abbrevated_month_name
343 assert_equal 'Jan', @backend.localize('de', @date, '%b')
344 end
345
346 def test_translate_given_no_format_it_does_not_fail
347 assert_nothing_raised{ @backend.localize 'de', @date }
348 end
349
350 def test_translate_given_an_unknown_format_it_does_not_fail
351 assert_nothing_raised{ @backend.localize 'de', @date, '%x' }
352 end
353
354 def test_localize_nil_raises_argument_error
355 assert_raise(I18n::ArgumentError) { @backend.localize 'de', nil }
356 end
357
358 def test_localize_object_raises_argument_error
359 assert_raise(I18n::ArgumentError) { @backend.localize 'de', Object.new }
360 end
361 end
362
363 class I18nSimpleBackendLocalizeDateTimeTest < Test::Unit::TestCase
364 include I18nSimpleBackendTestSetup
365
366 def setup
367 @backend = I18n::Backend::Simple.new
368 add_datetime_translations
369 @morning = DateTime.new 2008, 1, 1, 6
370 @evening = DateTime.new 2008, 1, 1, 18
371 end
372
373 def test_translate_given_the_short_format_it_uses_it
374 assert_equal '01. Jan 06:00', @backend.localize('de', @morning, :short)
375 end
376
377 def test_translate_given_the_long_format_it_uses_it
378 assert_equal '01. Januar 2008 06:00', @backend.localize('de', @morning, :long)
379 end
380
381 def test_translate_given_the_default_format_it_uses_it
382 assert_equal 'Di, 01. Jan 2008 06:00:00 +0000', @backend.localize('de', @morning, :default)
383 end
384
385 def test_translate_given_a_day_name_format_it_returns_the_correct_day_name
386 assert_equal 'Dienstag', @backend.localize('de', @morning, '%A')
387 end
388
389 def test_translate_given_an_abbr_day_name_format_it_returns_the_correct_abbrevated_day_name
390 assert_equal 'Di', @backend.localize('de', @morning, '%a')
391 end
392
393 def test_translate_given_a_month_name_format_it_returns_the_correct_month_name
394 assert_equal 'Januar', @backend.localize('de', @morning, '%B')
395 end
396
397 def test_translate_given_an_abbr_month_name_format_it_returns_the_correct_abbrevated_month_name
398 assert_equal 'Jan', @backend.localize('de', @morning, '%b')
399 end
400
401 def test_translate_given_a_meridian_indicator_format_it_returns_the_correct_meridian_indicator
402 assert_equal 'am', @backend.localize('de', @morning, '%p')
403 assert_equal 'pm', @backend.localize('de', @evening, '%p')
404 end
405
406 def test_translate_given_no_format_it_does_not_fail
407 assert_nothing_raised{ @backend.localize 'de', @morning }
408 end
409
410 def test_translate_given_an_unknown_format_it_does_not_fail
411 assert_nothing_raised{ @backend.localize 'de', @morning, '%x' }
412 end
413 end
414
415 class I18nSimpleBackendLocalizeTimeTest < Test::Unit::TestCase
416 include I18nSimpleBackendTestSetup
417
418 def setup
419 @old_timezone, ENV['TZ'] = ENV['TZ'], 'UTC'
420 @backend = I18n::Backend::Simple.new
421 add_datetime_translations
422 @morning = Time.parse '2008-01-01 6:00 UTC'
423 @evening = Time.parse '2008-01-01 18:00 UTC'
424 end
425
426 def teardown
427 @old_timezone ? ENV['TZ'] = @old_timezone : ENV.delete('TZ')
428 end
429
430 def test_translate_given_the_short_format_it_uses_it
431 assert_equal '01. Jan 06:00', @backend.localize('de', @morning, :short)
432 end
433
434 def test_translate_given_the_long_format_it_uses_it
435 assert_equal '01. Januar 2008 06:00', @backend.localize('de', @morning, :long)
436 end
437
438 # TODO Seems to break on Windows because ENV['TZ'] is ignored. What's a better way to do this?
439 # def test_translate_given_the_default_format_it_uses_it
440 # assert_equal 'Di, 01. Jan 2008 06:00:00 +0000', @backend.localize('de', @morning, :default)
441 # end
442
443 def test_translate_given_a_day_name_format_it_returns_the_correct_day_name
444 assert_equal 'Dienstag', @backend.localize('de', @morning, '%A')
445 end
446
447 def test_translate_given_an_abbr_day_name_format_it_returns_the_correct_abbrevated_day_name
448 assert_equal 'Di', @backend.localize('de', @morning, '%a')
449 end
450
451 def test_translate_given_a_month_name_format_it_returns_the_correct_month_name
452 assert_equal 'Januar', @backend.localize('de', @morning, '%B')
453 end
454
455 def test_translate_given_an_abbr_month_name_format_it_returns_the_correct_abbrevated_month_name
456 assert_equal 'Jan', @backend.localize('de', @morning, '%b')
457 end
458
459 def test_translate_given_a_meridian_indicator_format_it_returns_the_correct_meridian_indicator
460 assert_equal 'am', @backend.localize('de', @morning, '%p')
461 assert_equal 'pm', @backend.localize('de', @evening, '%p')
462 end
463
464 def test_translate_given_no_format_it_does_not_fail
465 assert_nothing_raised{ @backend.localize 'de', @morning }
466 end
467
468 def test_translate_given_an_unknown_format_it_does_not_fail
469 assert_nothing_raised{ @backend.localize 'de', @morning, '%x' }
470 end
471 end
472
473 class I18nSimpleBackendHelperMethodsTest < Test::Unit::TestCase
474 def setup
475 @backend = I18n::Backend::Simple.new
476 end
477
478 def test_deep_symbolize_keys_works
479 result = @backend.send :deep_symbolize_keys, 'foo' => {'bar' => {'baz' => 'bar'}}
480 expected = {:foo => {:bar => {:baz => 'bar'}}}
481 assert_equal expected, result
482 end
483 end
484
485 class I18nSimpleBackendLoadTranslationsTest < Test::Unit::TestCase
486 include I18nSimpleBackendTestSetup
487
488 def test_load_translations_with_unknown_file_type_raises_exception
489 assert_raise(I18n::UnknownFileType) { @backend.load_translations "#{@locale_dir}/en.xml" }
490 end
491
492 def test_load_translations_with_ruby_file_type_does_not_raise_exception
493 assert_nothing_raised { @backend.load_translations "#{@locale_dir}/en.rb" }
494 end
495
496 def test_load_rb_loads_data_from_ruby_file
497 data = @backend.send :load_rb, "#{@locale_dir}/en.rb"
498 assert_equal({:'en-Ruby' => {:foo => {:bar => "baz"}}}, data)
499 end
500
501 def test_load_rb_loads_data_from_yaml_file
502 data = @backend.send :load_yml, "#{@locale_dir}/en.yml"
503 assert_equal({'en-Yaml' => {'foo' => {'bar' => 'baz'}}}, data)
504 end
505
506 def test_load_translations_loads_from_different_file_formats
507 @backend = I18n::Backend::Simple.new
508 @backend.load_translations "#{@locale_dir}/en.rb", "#{@locale_dir}/en.yml"
509 expected = {
510 :'en-Ruby' => {:foo => {:bar => "baz"}},
511 :'en-Yaml' => {:foo => {:bar => "baz"}}
512 }
513 assert_equal expected, backend_get_translations
514 end
515 end
516
517 class I18nSimpleBackendLoadPathTest < Test::Unit::TestCase
518 include I18nSimpleBackendTestSetup
519
520 def teardown
521 I18n.load_path = []
522 end
523
524 def test_nested_load_paths_do_not_break_locale_loading
525 @backend = I18n::Backend::Simple.new
526 I18n.load_path = [[File.dirname(__FILE__) + '/locale/en.yml']]
527 assert_nil backend_get_translations
528 assert_nothing_raised { @backend.send :init_translations }
529 assert_not_nil backend_get_translations
530 end
531
532 def test_adding_arrays_of_filenames_to_load_path_do_not_break_locale_loading
533 @backend = I18n::Backend::Simple.new
534 I18n.load_path << Dir[File.dirname(__FILE__) + '/locale/*.{rb,yml}']
535 assert_nil backend_get_translations
536 assert_nothing_raised { @backend.send :init_translations }
537 assert_not_nil backend_get_translations
538 end
539 end
540
541 class I18nSimpleBackendReloadTranslationsTest < Test::Unit::TestCase
542 include I18nSimpleBackendTestSetup
543
544 def setup
545 @backend = I18n::Backend::Simple.new
546 I18n.load_path = [File.dirname(__FILE__) + '/locale/en.yml']
547 assert_nil backend_get_translations
548 @backend.send :init_translations
549 end
550
551 def teardown
552 I18n.load_path = []
553 end
554
555 def test_setup
556 assert_not_nil backend_get_translations
557 end
558
559 def test_reload_translations_unloads_translations
560 @backend.reload!
561 assert_nil backend_get_translations
562 end
563
564 def test_reload_translations_uninitializes_translations
565 @backend.reload!
566 assert_equal @backend.initialized?, false
567 end
568 end