activesupport 2.2.3 → 2.3.2

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of activesupport might be problematic. Click here for more details.

Files changed (94) hide show
  1. data/CHANGELOG +128 -89
  2. data/lib/active_support.rb +31 -33
  3. data/lib/active_support/backtrace_cleaner.rb +72 -0
  4. data/lib/active_support/buffered_logger.rb +9 -7
  5. data/lib/active_support/cache.rb +13 -8
  6. data/lib/active_support/cache/drb_store.rb +2 -3
  7. data/lib/active_support/cache/mem_cache_store.rb +6 -1
  8. data/lib/active_support/cache/strategy/local_cache.rb +104 -0
  9. data/lib/active_support/callbacks.rb +20 -21
  10. data/lib/active_support/core_ext.rb +1 -1
  11. data/lib/active_support/core_ext/array.rb +2 -0
  12. data/lib/active_support/core_ext/array/conversions.rb +26 -13
  13. data/lib/active_support/core_ext/array/wrapper.rb +24 -0
  14. data/lib/active_support/core_ext/benchmark.rb +13 -6
  15. data/lib/active_support/core_ext/cgi/escape_skipping_slashes.rb +14 -5
  16. data/lib/active_support/core_ext/class/attribute_accessors.rb +24 -24
  17. data/lib/active_support/core_ext/class/delegating_attributes.rb +20 -19
  18. data/lib/active_support/core_ext/class/inheritable_attributes.rb +34 -34
  19. data/lib/active_support/core_ext/date/conversions.rb +3 -3
  20. data/lib/active_support/core_ext/date_time/conversions.rb +1 -1
  21. data/lib/active_support/core_ext/enumerable.rb +9 -0
  22. data/lib/active_support/core_ext/exception.rb +12 -8
  23. data/lib/active_support/core_ext/file/atomic.rb +2 -2
  24. data/lib/active_support/core_ext/hash/conversions.rb +32 -54
  25. data/lib/active_support/core_ext/hash/indifferent_access.rb +6 -0
  26. data/lib/active_support/core_ext/hash/keys.rb +1 -1
  27. data/lib/active_support/core_ext/hash/slice.rb +8 -1
  28. data/lib/active_support/core_ext/logger.rb +8 -6
  29. data/lib/active_support/core_ext/module/aliasing.rb +3 -3
  30. data/lib/active_support/core_ext/module/attr_accessor_with_default.rb +4 -4
  31. data/lib/active_support/core_ext/module/attribute_accessors.rb +24 -24
  32. data/lib/active_support/core_ext/module/delegation.rb +29 -3
  33. data/lib/active_support/core_ext/module/synchronization.rb +5 -5
  34. data/lib/active_support/core_ext/object/conversions.rb +2 -1
  35. data/lib/active_support/core_ext/object/misc.rb +16 -0
  36. data/lib/active_support/core_ext/range/conversions.rb +1 -1
  37. data/lib/active_support/core_ext/rexml.rb +29 -24
  38. data/lib/active_support/core_ext/string/inflections.rb +3 -3
  39. data/lib/active_support/core_ext/time/calculations.rb +1 -2
  40. data/lib/active_support/core_ext/time/conversions.rb +1 -1
  41. data/lib/active_support/core_ext/try.rb +36 -0
  42. data/lib/active_support/dependencies.rb +18 -14
  43. data/lib/active_support/deprecation.rb +10 -57
  44. data/lib/active_support/duration.rb +3 -1
  45. data/lib/active_support/inflections.rb +1 -0
  46. data/lib/active_support/inflector.rb +16 -7
  47. data/lib/active_support/json/decoding.rb +21 -3
  48. data/lib/active_support/json/encoders/date.rb +1 -1
  49. data/lib/active_support/json/encoders/date_time.rb +1 -1
  50. data/lib/active_support/json/encoders/hash.rb +10 -11
  51. data/lib/active_support/json/encoders/time.rb +1 -1
  52. data/lib/active_support/json/encoding.rb +23 -29
  53. data/lib/active_support/locale/en.yml +3 -2
  54. data/lib/active_support/memoizable.rb +61 -43
  55. data/lib/active_support/message_encryptor.rb +70 -0
  56. data/lib/active_support/message_verifier.rb +46 -0
  57. data/lib/active_support/multibyte.rb +6 -30
  58. data/lib/active_support/multibyte/chars.rb +30 -9
  59. data/lib/active_support/multibyte/unicode_database.rb +4 -4
  60. data/lib/active_support/option_merger.rb +7 -1
  61. data/lib/active_support/ordered_hash.rb +75 -27
  62. data/lib/active_support/secure_random.rb +8 -6
  63. data/lib/active_support/test_case.rb +32 -17
  64. data/lib/active_support/testing/{core_ext/test/unit/assertions.rb → assertions.rb} +13 -20
  65. data/lib/active_support/testing/declarative.rb +21 -0
  66. data/lib/active_support/testing/deprecation.rb +55 -0
  67. data/lib/active_support/testing/performance.rb +1 -1
  68. data/lib/active_support/testing/setup_and_teardown.rb +57 -86
  69. data/lib/active_support/time_with_zone.rb +8 -6
  70. data/lib/active_support/values/time_zone.rb +1 -0
  71. data/lib/active_support/vendor.rb +6 -11
  72. data/lib/active_support/vendor/i18n-0.1.3/MIT-LICENSE +20 -0
  73. data/lib/active_support/vendor/i18n-0.1.3/README.textile +20 -0
  74. data/lib/active_support/vendor/i18n-0.1.3/Rakefile +5 -0
  75. data/lib/active_support/vendor/i18n-0.1.3/i18n.gemspec +27 -0
  76. data/lib/active_support/vendor/{i18n-0.0.1 → i18n-0.1.3/lib}/i18n.rb +42 -37
  77. data/lib/active_support/vendor/{i18n-0.0.1 → i18n-0.1.3/lib}/i18n/backend/simple.rb +37 -39
  78. data/lib/active_support/vendor/{i18n-0.0.1 → i18n-0.1.3/lib}/i18n/exceptions.rb +3 -3
  79. data/lib/active_support/vendor/i18n-0.1.3/test/all.rb +5 -0
  80. data/lib/active_support/vendor/i18n-0.1.3/test/i18n_exceptions_test.rb +100 -0
  81. data/lib/active_support/vendor/i18n-0.1.3/test/i18n_test.rb +125 -0
  82. data/lib/active_support/vendor/i18n-0.1.3/test/locale/en.rb +1 -0
  83. data/lib/active_support/vendor/i18n-0.1.3/test/locale/en.yml +3 -0
  84. data/lib/active_support/vendor/i18n-0.1.3/test/simple_backend_test.rb +568 -0
  85. data/lib/active_support/vendor/{memcache-client-1.5.1 → memcache-client-1.6.5}/memcache.rb +381 -295
  86. data/lib/active_support/version.rb +2 -2
  87. data/lib/active_support/xml_mini.rb +31 -0
  88. data/lib/active_support/xml_mini/libxml.rb +133 -0
  89. data/lib/active_support/xml_mini/nokogiri.rb +77 -0
  90. data/lib/active_support/xml_mini/rexml.rb +108 -0
  91. metadata +85 -14
  92. data/lib/active_support/multibyte/utils.rb +0 -61
  93. data/lib/active_support/testing/core_ext/test.rb +0 -6
  94. data/lib/active_support/vendor/xml-simple-1.0.11/xmlsimple.rb +0 -1021
@@ -1,6 +1,6 @@
1
1
  module I18n
2
2
  class ArgumentError < ::ArgumentError; end
3
-
3
+
4
4
  class InvalidLocale < ArgumentError
5
5
  attr_reader :locale
6
6
  def initialize(locale)
@@ -42,7 +42,7 @@ module I18n
42
42
  super "reserved key #{key.inspect} used in #{string.inspect}"
43
43
  end
44
44
  end
45
-
45
+
46
46
  class UnknownFileType < ArgumentError
47
47
  attr_reader :type, :filename
48
48
  def initialize(type, filename)
@@ -50,4 +50,4 @@ module I18n
50
50
  super "can not load translations from #{filename}, the file type #{type} is not known"
51
51
  end
52
52
  end
53
- end
53
+ end
@@ -0,0 +1,5 @@
1
+ dir = File.dirname(__FILE__)
2
+ require dir + '/i18n_test.rb'
3
+ require dir + '/simple_backend_test.rb'
4
+ require dir + '/i18n_exceptions_test.rb'
5
+ # *require* dir + '/custom_backend_test.rb'
@@ -0,0 +1,100 @@
1
+ $:.unshift "lib"
2
+
3
+ require 'rubygems'
4
+ require 'test/unit'
5
+ require 'mocha'
6
+ require 'i18n'
7
+ require 'active_support'
8
+
9
+ class I18nExceptionsTest < Test::Unit::TestCase
10
+ def test_invalid_locale_stores_locale
11
+ force_invalid_locale
12
+ rescue I18n::ArgumentError => e
13
+ assert_nil e.locale
14
+ end
15
+
16
+ def test_invalid_locale_message
17
+ force_invalid_locale
18
+ rescue I18n::ArgumentError => e
19
+ assert_equal 'nil is not a valid locale', e.message
20
+ end
21
+
22
+ def test_missing_translation_data_stores_locale_key_and_options
23
+ force_missing_translation_data
24
+ rescue I18n::ArgumentError => e
25
+ options = {:scope => :bar}
26
+ assert_equal 'de', e.locale
27
+ assert_equal :foo, e.key
28
+ assert_equal options, e.options
29
+ end
30
+
31
+ def test_missing_translation_data_message
32
+ force_missing_translation_data
33
+ rescue I18n::ArgumentError => e
34
+ assert_equal 'translation missing: de, bar, foo', e.message
35
+ end
36
+
37
+ def test_invalid_pluralization_data_stores_entry_and_count
38
+ force_invalid_pluralization_data
39
+ rescue I18n::ArgumentError => e
40
+ assert_equal [:bar], e.entry
41
+ assert_equal 1, e.count
42
+ end
43
+
44
+ def test_invalid_pluralization_data_message
45
+ force_invalid_pluralization_data
46
+ rescue I18n::ArgumentError => e
47
+ assert_equal 'translation data [:bar] can not be used with :count => 1', e.message
48
+ end
49
+
50
+ def test_missing_interpolation_argument_stores_key_and_string
51
+ force_missing_interpolation_argument
52
+ rescue I18n::ArgumentError => e
53
+ assert_equal 'bar', e.key
54
+ assert_equal "{{bar}}", e.string
55
+ end
56
+
57
+ def test_missing_interpolation_argument_message
58
+ force_missing_interpolation_argument
59
+ rescue I18n::ArgumentError => e
60
+ assert_equal 'interpolation argument bar missing in "{{bar}}"', e.message
61
+ end
62
+
63
+ def test_reserved_interpolation_key_stores_key_and_string
64
+ force_reserved_interpolation_key
65
+ rescue I18n::ArgumentError => e
66
+ assert_equal 'scope', e.key
67
+ assert_equal "{{scope}}", e.string
68
+ end
69
+
70
+ def test_reserved_interpolation_key_message
71
+ force_reserved_interpolation_key
72
+ rescue I18n::ArgumentError => e
73
+ assert_equal 'reserved key "scope" used in "{{scope}}"', e.message
74
+ end
75
+
76
+ private
77
+ def force_invalid_locale
78
+ I18n.backend.translate nil, :foo
79
+ end
80
+
81
+ def force_missing_translation_data
82
+ I18n.backend.store_translations 'de', :bar => nil
83
+ I18n.backend.translate 'de', :foo, :scope => :bar
84
+ end
85
+
86
+ def force_invalid_pluralization_data
87
+ I18n.backend.store_translations 'de', :foo => [:bar]
88
+ I18n.backend.translate 'de', :foo, :count => 1
89
+ end
90
+
91
+ def force_missing_interpolation_argument
92
+ I18n.backend.store_translations 'de', :foo => "{{bar}}"
93
+ I18n.backend.translate 'de', :foo, :baz => 'baz'
94
+ end
95
+
96
+ def force_reserved_interpolation_key
97
+ I18n.backend.store_translations 'de', :foo => "{{scope}}"
98
+ I18n.backend.translate 'de', :foo, :baz => 'baz'
99
+ end
100
+ end
@@ -0,0 +1,125 @@
1
+ $:.unshift "lib"
2
+
3
+ require 'rubygems'
4
+ require 'test/unit'
5
+ require 'mocha'
6
+ require 'i18n'
7
+ require 'active_support'
8
+
9
+ class I18nTest < Test::Unit::TestCase
10
+ def setup
11
+ I18n.backend.store_translations :'en', {
12
+ :currency => {
13
+ :format => {
14
+ :separator => '.',
15
+ :delimiter => ',',
16
+ }
17
+ }
18
+ }
19
+ end
20
+
21
+ def test_uses_simple_backend_set_by_default
22
+ assert I18n.backend.is_a?(I18n::Backend::Simple)
23
+ end
24
+
25
+ def test_can_set_backend
26
+ assert_nothing_raised{ I18n.backend = self }
27
+ assert_equal self, I18n.backend
28
+ I18n.backend = I18n::Backend::Simple.new
29
+ end
30
+
31
+ def test_uses_en_us_as_default_locale_by_default
32
+ assert_equal 'en', I18n.default_locale
33
+ end
34
+
35
+ def test_can_set_default_locale
36
+ assert_nothing_raised{ I18n.default_locale = 'de' }
37
+ assert_equal 'de', I18n.default_locale
38
+ I18n.default_locale = 'en'
39
+ end
40
+
41
+ def test_uses_default_locale_as_locale_by_default
42
+ assert_equal I18n.default_locale, I18n.locale
43
+ end
44
+
45
+ def test_can_set_locale_to_thread_current
46
+ assert_nothing_raised{ I18n.locale = 'de' }
47
+ assert_equal 'de', I18n.locale
48
+ assert_equal 'de', Thread.current[:locale]
49
+ I18n.locale = 'en'
50
+ end
51
+
52
+ def test_can_set_exception_handler
53
+ assert_nothing_raised{ I18n.exception_handler = :custom_exception_handler }
54
+ I18n.exception_handler = :default_exception_handler # revert it
55
+ end
56
+
57
+ def test_uses_custom_exception_handler
58
+ I18n.exception_handler = :custom_exception_handler
59
+ I18n.expects(:custom_exception_handler)
60
+ I18n.translate :bogus
61
+ I18n.exception_handler = :default_exception_handler # revert it
62
+ end
63
+
64
+ def test_delegates_translate_to_backend
65
+ I18n.backend.expects(:translate).with 'de', :foo, {}
66
+ I18n.translate :foo, :locale => 'de'
67
+ end
68
+
69
+ def test_delegates_localize_to_backend
70
+ I18n.backend.expects(:localize).with 'de', :whatever, :default
71
+ I18n.localize :whatever, :locale => 'de'
72
+ end
73
+
74
+ def test_translate_given_no_locale_uses_i18n_locale
75
+ I18n.backend.expects(:translate).with 'en', :foo, {}
76
+ I18n.translate :foo
77
+ end
78
+
79
+ def test_translate_on_nested_symbol_keys_works
80
+ assert_equal ".", I18n.t(:'currency.format.separator')
81
+ end
82
+
83
+ def test_translate_with_nested_string_keys_works
84
+ assert_equal ".", I18n.t('currency.format.separator')
85
+ end
86
+
87
+ def test_translate_with_array_as_scope_works
88
+ assert_equal ".", I18n.t(:separator, :scope => ['currency.format'])
89
+ end
90
+
91
+ def test_translate_with_array_containing_dot_separated_strings_as_scope_works
92
+ assert_equal ".", I18n.t(:separator, :scope => ['currency.format'])
93
+ end
94
+
95
+ def test_translate_with_key_array_and_dot_separated_scope_works
96
+ assert_equal [".", ","], I18n.t(%w(separator delimiter), :scope => 'currency.format')
97
+ end
98
+
99
+ def test_translate_with_dot_separated_key_array_and_scope_works
100
+ assert_equal [".", ","], I18n.t(%w(format.separator format.delimiter), :scope => 'currency')
101
+ end
102
+
103
+ def test_translate_with_options_using_scope_works
104
+ I18n.backend.expects(:translate).with('de', :precision, :scope => :"currency.format")
105
+ I18n.with_options :locale => 'de', :scope => :'currency.format' do |locale|
106
+ locale.t :precision
107
+ end
108
+ end
109
+
110
+ # def test_translate_given_no_args_raises_missing_translation_data
111
+ # assert_equal "translation missing: en, no key", I18n.t
112
+ # end
113
+
114
+ def test_translate_given_a_bogus_key_raises_missing_translation_data
115
+ assert_equal "translation missing: en, bogus", I18n.t(:bogus)
116
+ end
117
+
118
+ def test_localize_nil_raises_argument_error
119
+ assert_raise(I18n::ArgumentError) { I18n.l nil }
120
+ end
121
+
122
+ def test_localize_object_raises_argument_error
123
+ assert_raise(I18n::ArgumentError) { I18n.l Object.new }
124
+ end
125
+ end
@@ -0,0 +1 @@
1
+ {:'en-Ruby' => {:foo => {:bar => "baz"}}}
@@ -0,0 +1,3 @@
1
+ en-Yaml:
2
+ foo:
3
+ bar: baz
@@ -0,0 +1,568 @@
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