thedarkone-i18n 0.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/test/all.rb ADDED
@@ -0,0 +1,7 @@
1
+ dir = File.dirname(__FILE__)
2
+ require dir + '/i18n_test.rb'
3
+ require dir + '/backend_test.rb'
4
+ require dir + '/i18n_exceptions_test.rb'
5
+ require dir + '/fast_backend_test.rb'
6
+ require dir + '/pluralization_compiler_test.rb'
7
+ # *require* dir + '/custom_backend_test.rb'
@@ -0,0 +1,633 @@
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 I18nBackendTestSetup
12
+ def setup_backend
13
+ # backend_reset_translations!
14
+ @backend = new_backend
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
+ module I18nBackendTranslationsTest
102
+ def test_store_translations_adds_translations # no, really :-)
103
+ @backend.store_translations :'en', :foo => 'bar'
104
+ assert_equal Hash[:'en', {:foo => 'bar'}], backend_get_translations
105
+ end
106
+
107
+ def test_store_translations_deep_merges_translations
108
+ @backend.store_translations :'en', :foo => {:bar => 'bar'}
109
+ @backend.store_translations :'en', :foo => {:baz => 'baz'}
110
+ assert_equal Hash[:'en', {:foo => {:bar => 'bar', :baz => 'baz'}}], backend_get_translations
111
+ end
112
+
113
+ def test_store_translations_forces_locale_to_sym
114
+ @backend.store_translations 'en', :foo => 'bar'
115
+ assert_equal Hash[:'en', {:foo => 'bar'}], backend_get_translations
116
+ end
117
+
118
+ def test_store_translations_converts_keys_to_symbols
119
+ # backend_reset_translations!
120
+ @backend.store_translations 'en', 'foo' => {'bar' => 'bar', 'baz' => 'baz'}
121
+ assert_equal Hash[:'en', {:foo => {:bar => 'bar', :baz => 'baz'}}], backend_get_translations
122
+ end
123
+ end
124
+
125
+ module I18nBackendAvailableLocalesTest
126
+ def test_available_locales
127
+ @backend = new_backend
128
+ @backend.store_translations 'de', :foo => 'bar'
129
+ @backend.store_translations 'en', :foo => 'foo'
130
+
131
+ assert_equal ['de', 'en'], @backend.available_locales.map{|locale| locale.to_s }.sort
132
+ end
133
+ end
134
+
135
+ module I18nBackendTranslateTest
136
+ def test_translate_calls_lookup_with_locale_given
137
+ @backend.expects(:lookup).with('de', :bar, [:foo]).returns 'bar'
138
+ @backend.translate 'de', :bar, :scope => [:foo]
139
+ end
140
+
141
+ def test_given_no_keys_it_returns_the_default
142
+ assert_equal 'default', @backend.translate('en', nil, :default => 'default')
143
+ end
144
+
145
+ def test_translate_given_a_symbol_as_a_default_translates_the_symbol
146
+ assert_equal 'bar', @backend.translate('en', nil, :scope => [:foo], :default => :bar)
147
+ end
148
+
149
+ def test_translate_given_an_array_as_default_uses_the_first_match
150
+ assert_equal 'bar', @backend.translate('en', :does_not_exist, :scope => [:foo], :default => [:does_not_exist_2, :bar])
151
+ end
152
+
153
+ def test_translate_given_an_array_of_inexistent_keys_it_raises_missing_translation_data
154
+ assert_raise I18n::MissingTranslationData do
155
+ @backend.translate('en', :does_not_exist, :scope => [:foo], :default => [:does_not_exist_2, :does_not_exist_3])
156
+ end
157
+ end
158
+
159
+ def test_translate_an_array_of_keys_translates_all_of_them
160
+ assert_equal %w(bar baz), @backend.translate('en', [:bar, :baz], :scope => [:foo])
161
+ end
162
+
163
+ def test_translate_calls_pluralize
164
+ @backend.expects(:pluralize).with('en', 'bar', 1).returns('pluralized')
165
+ @backend.translate 'en', :bar, :scope => [:foo], :count => 1
166
+ end
167
+
168
+ def test_translate_calls_interpolate
169
+ @backend.expects(:interpolate).returns('interpolated')
170
+ @backend.translate 'en', :bar, :scope => [:foo]
171
+ end
172
+
173
+ def test_translate_calls_interpolate_including_count_as_a_value
174
+ @backend.expects(:interpolate).returns('interpolated')
175
+ @backend.translate 'en', :bar, :scope => [:foo], :count => 1
176
+ end
177
+
178
+ def test_translate_given_nil_as_a_locale_raises_an_argument_error
179
+ assert_raise(I18n::InvalidLocale){ @backend.translate nil, :bar }
180
+ end
181
+
182
+ def test_translate_with_a_bogus_key_and_no_default_raises_missing_translation_data
183
+ assert_raise(I18n::MissingTranslationData){ @backend.translate 'de', :bogus }
184
+ end
185
+ end
186
+
187
+ module I18nBackendLookupTest
188
+ # useful because this way we can use the backend with no key for interpolation/pluralization
189
+ def test_lookup_given_nil_as_a_key_returns_nil
190
+ assert_nil @backend.send(:lookup, 'en', nil)
191
+ end
192
+
193
+ def test_lookup_given_nested_keys_looks_up_a_nested_hash_value
194
+ assert_equal 'bar', @backend.send(:lookup, 'en', :bar, [:foo])
195
+ end
196
+ end
197
+
198
+ module I18nBackendPluralizeTest
199
+ def test_pluralize_given_nil_returns_the_given_entry
200
+ entry = {:one => 'bar', :other => 'bars'}
201
+ assert_equal entry, @backend.send(:pluralize, nil, entry, nil)
202
+ end
203
+
204
+ def test_pluralize_given_0_returns_zero_string_if_zero_key_given
205
+ assert_equal 'zero', @backend.send(:pluralize, nil, {:zero => 'zero', :one => 'bar', :other => 'bars'}, 0)
206
+ end
207
+
208
+ def test_pluralize_given_0_returns_plural_string_if_no_zero_key_given
209
+ assert_equal 'bars', @backend.send(:pluralize, nil, {:one => 'bar', :other => 'bars'}, 0)
210
+ end
211
+
212
+ def test_pluralize_given_1_returns_singular_string
213
+ assert_equal 'bar', @backend.send(:pluralize, nil, {:one => 'bar', :other => 'bars'}, 1)
214
+ end
215
+
216
+ def test_pluralize_given_2_returns_plural_string
217
+ assert_equal 'bars', @backend.send(:pluralize, nil, {:one => 'bar', :other => 'bars'}, 2)
218
+ end
219
+
220
+ def test_pluralize_given_3_returns_plural_string
221
+ assert_equal 'bars', @backend.send(:pluralize, nil, {:one => 'bar', :other => 'bars'}, 3)
222
+ end
223
+
224
+ def test_interpolate_given_incomplete_pluralization_data_raises_invalid_pluralization_data
225
+ assert_raise(I18n::InvalidPluralizationData){ @backend.send(:pluralize, nil, {:one => 'bar'}, 2) }
226
+ end
227
+
228
+ # def test_interpolate_given_a_string_raises_invalid_pluralization_data
229
+ # assert_raise(I18n::InvalidPluralizationData){ @backend.send(:pluralize, nil, 'bar', 2) }
230
+ # end
231
+ #
232
+ # def test_interpolate_given_an_array_raises_invalid_pluralization_data
233
+ # assert_raise(I18n::InvalidPluralizationData){ @backend.send(:pluralize, nil, ['bar'], 2) }
234
+ # end
235
+ end
236
+
237
+ module I18nBackendInterpolateTest
238
+ def interpolate_on_backend(str, values)
239
+ I18n::Backend::Fast::PluralizationCompiler.compile_if_an_interpolation(str) if @backend.kind_of?(I18n::Backend::Fast)
240
+ @backend.send(:interpolate, str, values)
241
+ end
242
+
243
+ def test_interpolate_given_a_value_hash_interpolates_the_values_to_the_string
244
+ assert_equal 'Hi David!', interpolate_on_backend('Hi {{name}}!', :name => 'David')
245
+ end
246
+
247
+ def test_interpolate_given_a_value_hash_interpolates_into_unicode_string
248
+ assert_equal 'Häi David!', interpolate_on_backend('Häi {{name}}!', :name => 'David')
249
+ end
250
+
251
+ def test_interpolate_given_an_unicode_value_hash_interpolates_to_the_string
252
+ assert_equal 'Hi ゆきひろ!', interpolate_on_backend('Hi {{name}}!', :name => 'ゆきひろ')
253
+ end
254
+
255
+ def test_interpolate_given_an_unicode_value_hash_interpolates_into_unicode_string
256
+ assert_equal 'こんにちは、ゆきひろさん!', interpolate_on_backend('こんにちは、{{name}}さん!', :name => 'ゆきひろ')
257
+ end
258
+
259
+ if Kernel.const_defined?(:Encoding)
260
+ def test_interpolate_given_a_non_unicode_multibyte_value_hash_interpolates_into_a_string_with_the_same_encoding
261
+ assert_equal euc_jp('Hi ゆきひろ!'), interpolate_on_backend('Hi {{name}}!', :name => euc_jp('ゆきひろ'))
262
+ end
263
+
264
+ def test_interpolate_given_an_unicode_value_hash_into_a_non_unicode_multibyte_string_raises_encoding_compatibility_error
265
+ assert_raise(Encoding::CompatibilityError) do
266
+ interpolate_on_backend(euc_jp('こんにちは、{{name}}さん!'), :name => 'ゆきひろ')
267
+ end
268
+ end
269
+
270
+ def test_interpolate_given_a_non_unicode_multibyte_value_hash_into_an_unicode_string_raises_encoding_compatibility_error
271
+ assert_raise(Encoding::CompatibilityError) do
272
+ interpolate_on_backend('こんにちは、{{name}}さん!', :name => euc_jp('ゆきひろ'))
273
+ end
274
+ end
275
+ end
276
+
277
+ def test_interpolate_given_nil_as_a_string_returns_nil
278
+ assert_nil interpolate_on_backend(nil, :name => 'David')
279
+ end
280
+
281
+ def test_interpolate_given_an_non_string_as_a_string_returns_nil
282
+ assert_equal [], interpolate_on_backend([], :name => 'David')
283
+ end
284
+
285
+ def test_interpolate_given_a_values_hash_with_nil_values_interpolates_the_string
286
+ assert_equal 'Hi !', interpolate_on_backend('Hi {{name}}!', {:name => nil})
287
+ end
288
+
289
+ def test_interpolate_given_an_empty_values_hash_raises_missing_interpolation_argument
290
+ assert_raise(I18n::MissingInterpolationArgument) { interpolate_on_backend('Hi {{name}}!', {}) }
291
+ end
292
+
293
+ def test_interpolate_given_a_string_containing_a_reserved_key_raises_reserved_interpolation_key
294
+ assert_raise(I18n::ReservedInterpolationKey) { interpolate_on_backend('{{default}}', {:default => nil}) }
295
+ end
296
+
297
+ private
298
+
299
+ def euc_jp(string)
300
+ string.encode!(Encoding::EUC_JP)
301
+ end
302
+ end
303
+
304
+ module I18nBackendLocalizeDateTest
305
+ def setup
306
+ @backend = new_backend
307
+ add_datetime_translations
308
+ @date = Date.new 2008, 1, 1
309
+ end
310
+
311
+ def test_translate_given_the_short_format_it_uses_it
312
+ assert_equal '01. Jan', @backend.localize('de', @date, :short)
313
+ end
314
+
315
+ def test_translate_given_the_long_format_it_uses_it
316
+ assert_equal '01. Januar 2008', @backend.localize('de', @date, :long)
317
+ end
318
+
319
+ def test_translate_given_the_default_format_it_uses_it
320
+ assert_equal '01.01.2008', @backend.localize('de', @date, :default)
321
+ end
322
+
323
+ def test_translate_given_a_day_name_format_it_returns_a_day_name
324
+ assert_equal 'Dienstag', @backend.localize('de', @date, '%A')
325
+ end
326
+
327
+ def test_translate_given_an_abbr_day_name_format_it_returns_an_abbrevated_day_name
328
+ assert_equal 'Di', @backend.localize('de', @date, '%a')
329
+ end
330
+
331
+ def test_translate_given_a_month_name_format_it_returns_a_month_name
332
+ assert_equal 'Januar', @backend.localize('de', @date, '%B')
333
+ end
334
+
335
+ def test_translate_given_an_abbr_month_name_format_it_returns_an_abbrevated_month_name
336
+ assert_equal 'Jan', @backend.localize('de', @date, '%b')
337
+ end
338
+
339
+ def test_translate_given_no_format_it_does_not_fail
340
+ assert_nothing_raised{ @backend.localize 'de', @date }
341
+ end
342
+
343
+ def test_translate_given_an_unknown_format_it_does_not_fail
344
+ assert_nothing_raised{ @backend.localize 'de', @date, '%x' }
345
+ end
346
+
347
+ def test_localize_nil_raises_argument_error
348
+ assert_raise(I18n::ArgumentError) { @backend.localize 'de', nil }
349
+ end
350
+
351
+ def test_localize_object_raises_argument_error
352
+ assert_raise(I18n::ArgumentError) { @backend.localize 'de', Object.new }
353
+ end
354
+ end
355
+
356
+ module I18nBackendLocalizeDateTimeTest
357
+ def setup
358
+ @backend = new_backend
359
+ add_datetime_translations
360
+ @morning = DateTime.new 2008, 1, 1, 6
361
+ @evening = DateTime.new 2008, 1, 1, 18
362
+ end
363
+
364
+ def test_translate_given_the_short_format_it_uses_it
365
+ assert_equal '01. Jan 06:00', @backend.localize('de', @morning, :short)
366
+ end
367
+
368
+ def test_translate_given_the_long_format_it_uses_it
369
+ assert_equal '01. Januar 2008 06:00', @backend.localize('de', @morning, :long)
370
+ end
371
+
372
+ def test_translate_given_the_default_format_it_uses_it
373
+ assert_equal 'Di, 01. Jan 2008 06:00:00 +0000', @backend.localize('de', @morning, :default)
374
+ end
375
+
376
+ def test_translate_given_a_day_name_format_it_returns_the_correct_day_name
377
+ assert_equal 'Dienstag', @backend.localize('de', @morning, '%A')
378
+ end
379
+
380
+ def test_translate_given_an_abbr_day_name_format_it_returns_the_correct_abbrevated_day_name
381
+ assert_equal 'Di', @backend.localize('de', @morning, '%a')
382
+ end
383
+
384
+ def test_translate_given_a_month_name_format_it_returns_the_correct_month_name
385
+ assert_equal 'Januar', @backend.localize('de', @morning, '%B')
386
+ end
387
+
388
+ def test_translate_given_an_abbr_month_name_format_it_returns_the_correct_abbrevated_month_name
389
+ assert_equal 'Jan', @backend.localize('de', @morning, '%b')
390
+ end
391
+
392
+ def test_translate_given_a_meridian_indicator_format_it_returns_the_correct_meridian_indicator
393
+ assert_equal 'am', @backend.localize('de', @morning, '%p')
394
+ assert_equal 'pm', @backend.localize('de', @evening, '%p')
395
+ end
396
+
397
+ def test_translate_given_no_format_it_does_not_fail
398
+ assert_nothing_raised{ @backend.localize 'de', @morning }
399
+ end
400
+
401
+ def test_translate_given_an_unknown_format_it_does_not_fail
402
+ assert_nothing_raised{ @backend.localize 'de', @morning, '%x' }
403
+ end
404
+ end
405
+
406
+ module I18nBackendLocalizeTimeTest
407
+ def setup
408
+ @old_timezone, ENV['TZ'] = ENV['TZ'], 'UTC'
409
+ @backend = new_backend
410
+ add_datetime_translations
411
+ @morning = Time.parse '2008-01-01 6:00 UTC'
412
+ @evening = Time.parse '2008-01-01 18:00 UTC'
413
+ end
414
+
415
+ def teardown
416
+ @old_timezone ? ENV['TZ'] = @old_timezone : ENV.delete('TZ')
417
+ end
418
+
419
+ def test_translate_given_the_short_format_it_uses_it
420
+ assert_equal '01. Jan 06:00', @backend.localize('de', @morning, :short)
421
+ end
422
+
423
+ def test_translate_given_the_long_format_it_uses_it
424
+ assert_equal '01. Januar 2008 06:00', @backend.localize('de', @morning, :long)
425
+ end
426
+
427
+ # TODO Seems to break on Windows because ENV['TZ'] is ignored. What's a better way to do this?
428
+ # def test_translate_given_the_default_format_it_uses_it
429
+ # assert_equal 'Di, 01. Jan 2008 06:00:00 +0000', @backend.localize('de', @morning, :default)
430
+ # end
431
+
432
+ def test_translate_given_a_day_name_format_it_returns_the_correct_day_name
433
+ assert_equal 'Dienstag', @backend.localize('de', @morning, '%A')
434
+ end
435
+
436
+ def test_translate_given_an_abbr_day_name_format_it_returns_the_correct_abbrevated_day_name
437
+ assert_equal 'Di', @backend.localize('de', @morning, '%a')
438
+ end
439
+
440
+ def test_translate_given_a_month_name_format_it_returns_the_correct_month_name
441
+ assert_equal 'Januar', @backend.localize('de', @morning, '%B')
442
+ end
443
+
444
+ def test_translate_given_an_abbr_month_name_format_it_returns_the_correct_abbrevated_month_name
445
+ assert_equal 'Jan', @backend.localize('de', @morning, '%b')
446
+ end
447
+
448
+ def test_translate_given_a_meridian_indicator_format_it_returns_the_correct_meridian_indicator
449
+ assert_equal 'am', @backend.localize('de', @morning, '%p')
450
+ assert_equal 'pm', @backend.localize('de', @evening, '%p')
451
+ end
452
+
453
+ def test_translate_given_no_format_it_does_not_fail
454
+ assert_nothing_raised{ @backend.localize 'de', @morning }
455
+ end
456
+
457
+ def test_translate_given_an_unknown_format_it_does_not_fail
458
+ assert_nothing_raised{ @backend.localize 'de', @morning, '%x' }
459
+ end
460
+ end
461
+
462
+ module I18nBackendHelperMethodsTest
463
+ def test_deep_symbolize_keys_works
464
+ result = @backend.send :deep_symbolize_keys, 'foo' => {'bar' => {'baz' => 'bar'}}
465
+ expected = {:foo => {:bar => {:baz => 'bar'}}}
466
+ assert_equal expected, result
467
+ end
468
+ end
469
+
470
+ module I18nBackendLoadTranslationsTest
471
+ def test_load_translations_with_unknown_file_type_raises_exception
472
+ assert_raise(I18n::UnknownFileType) { @backend.load_translations "#{@locale_dir}/en.xml" }
473
+ end
474
+
475
+ def test_load_translations_with_ruby_file_type_does_not_raise_exception
476
+ assert_nothing_raised { @backend.load_translations "#{@locale_dir}/en.rb" }
477
+ end
478
+
479
+ def test_load_rb_loads_data_from_ruby_file
480
+ data = @backend.send :load_rb, "#{@locale_dir}/en.rb"
481
+ assert_equal({:'en-Ruby' => {:foo => {:bar => "baz"}}}, data)
482
+ end
483
+
484
+ def test_load_rb_loads_data_from_yaml_file
485
+ data = @backend.send :load_yml, "#{@locale_dir}/en.yml"
486
+ assert_equal({'en-Yaml' => {'foo' => {'bar' => 'baz'}}}, data)
487
+ end
488
+
489
+ def test_load_translations_loads_from_different_file_formats
490
+ @backend = new_backend
491
+ @backend.load_translations "#{@locale_dir}/en.rb", "#{@locale_dir}/en.yml"
492
+ expected = {
493
+ :'en-Ruby' => {:foo => {:bar => "baz"}},
494
+ :'en-Yaml' => {:foo => {:bar => "baz"}}
495
+ }
496
+ assert_equal expected, backend_get_translations
497
+ end
498
+ end
499
+
500
+ module I18nBackendLoadPathTest
501
+ def teardown
502
+ I18n.load_path = []
503
+ end
504
+
505
+ def test_nested_load_paths_do_not_break_locale_loading
506
+ @backend = new_backend
507
+ I18n.load_path = [[File.dirname(__FILE__) + '/locale/en.yml']]
508
+ assert_nil backend_get_translations
509
+ assert_nothing_raised { @backend.send :init_translations }
510
+ assert_not_nil backend_get_translations
511
+ end
512
+
513
+ def test_adding_arrays_of_filenames_to_load_path_do_not_break_locale_loading
514
+ @backend = new_backend
515
+ I18n.load_path << Dir[File.dirname(__FILE__) + '/locale/*.{rb,yml}']
516
+ assert_nil backend_get_translations
517
+ assert_nothing_raised { @backend.send :init_translations }
518
+ assert_not_nil backend_get_translations
519
+ end
520
+ end
521
+
522
+ module I18nBackendReloadTranslationsTest
523
+ def setup
524
+ @backend = new_backend
525
+ I18n.load_path = [File.dirname(__FILE__) + '/locale/en.yml']
526
+ assert_nil backend_get_translations
527
+ @backend.send :init_translations
528
+ end
529
+
530
+ def teardown
531
+ I18n.load_path = []
532
+ end
533
+
534
+ def test_setup
535
+ assert_not_nil backend_get_translations
536
+ end
537
+
538
+ def test_reload_translations_unloads_translations
539
+ @backend.expects(:stale?).returns(true)
540
+ @backend.reload!
541
+ assert_nil backend_get_translations
542
+ end
543
+
544
+ def test_reload_translations_uninitializes_translations
545
+ @backend.expects(:stale?).returns(true)
546
+ @backend.reload!
547
+ assert_equal @backend.initialized?, false
548
+ end
549
+ end
550
+
551
+ module I18nBackendLazyReloadingTest
552
+ def locale_fixture_path(file)
553
+ File.join(File.dirname(__FILE__), 'locale', file)
554
+ end
555
+
556
+ def trigger_reload
557
+ @backend.reload!
558
+ @backend.available_locales
559
+ end
560
+
561
+ def assert_triggers_translations_reload
562
+ yield
563
+ @backend.expects(:init_translations)
564
+ trigger_reload
565
+ end
566
+
567
+ def assert_does_not_trigger_translations_reload
568
+ yield
569
+ @backend.expects(:init_translations).never
570
+ trigger_reload
571
+ end
572
+
573
+ def setup
574
+ @backend = new_backend
575
+ I18n.load_path = [locale_fixture_path('en.yml')]
576
+ @backend.send(:init_translations)
577
+ end
578
+
579
+ def test_does_not_perform_reload_if_translation_files_are_not_updated
580
+ assert_does_not_trigger_translations_reload do
581
+ @backend.reload!
582
+ end
583
+ end
584
+
585
+ def test_performs_reload_if_new_translation_is_added
586
+ assert_triggers_translations_reload do
587
+ I18n.load_path << locale_fixture_path('en.rb')
588
+ end
589
+ end
590
+
591
+ def test_performs_reload_if_translation_is_removed
592
+ assert_triggers_translations_reload do
593
+ I18n.load_path.clear
594
+ end
595
+ end
596
+
597
+ def test_performs_reload_if_translation_file_is_updated
598
+ assert_triggers_translations_reload do
599
+ File.expects(:mtime).with(I18n.load_path.first).returns(Time.now - 10)
600
+ end
601
+ end
602
+ end
603
+
604
+ test_modules = %w(I18nBackendTranslationsTest
605
+ I18nBackendAvailableLocalesTest
606
+ I18nBackendTranslateTest
607
+ I18nBackendLookupTest
608
+ I18nBackendPluralizeTest
609
+ I18nBackendInterpolateTest
610
+ I18nBackendLocalizeDateTest
611
+ I18nBackendLocalizeDateTimeTest
612
+ I18nBackendLocalizeTimeTest
613
+ I18nBackendHelperMethodsTest
614
+ I18nBackendLoadTranslationsTest
615
+ I18nBackendLoadPathTest
616
+ I18nBackendReloadTranslationsTest
617
+ I18nBackendLazyReloadingTest)
618
+
619
+ test_cases = test_modules.map do |test_module|
620
+ %w(Simple Fast).map do |backend_type|
621
+ Class.new(Test::Unit::TestCase) do
622
+
623
+ class_eval <<-RUBY_EVAL, __FILE__, __LINE__
624
+ def new_backend
625
+ I18n::Backend::#{backend_type}.new
626
+ end
627
+ RUBY_EVAL
628
+
629
+ include I18nBackendTestSetup
630
+ include Object.const_get(test_module)
631
+ end
632
+ end
633
+ end
@@ -0,0 +1,34 @@
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
+ class FastBackendTest < Test::Unit::TestCase
12
+ def setup
13
+ @backend = I18n::Backend::Fast.new
14
+ end
15
+
16
+ def assert_flattens(expected, nested)
17
+ assert_equal expected, @backend.send(:flatten_hash, nested)
18
+ end
19
+
20
+ def test_hash_flattening_works
21
+ assert_flattens( {:"b.c"=>"c", :"b.f.x"=>"x", :"b.d"=>"d", :a=>"a"}, {:a=>'a', :b=>{:c=>'c', :d=>'d', :f=>{:x=>'x'}}} )
22
+ assert_flattens( {:"a.b"=>['a', 'b']}, {:a=>{:b =>['a', 'b']}} )
23
+ end
24
+
25
+ def test_hash_flattening_preserves_pluralization_hashes
26
+ assert_flattens({:'a.b.one'=>'one', :'a.b'=>{:one => 'one'}}, {:a=>{:b=>{:one => 'one'}}})
27
+ end
28
+
29
+ def test_pluralization_logic_and_lookup_works
30
+ counts_hash = {:zero => 'zero', :one => 'one', :other => 'other'}
31
+ @backend.store_translations :en, {:a => counts_hash}
32
+ assert_equal 'one', @backend.translate(:en, :a, :count => 1)
33
+ end
34
+ end