activesupport 2.3.5 → 2.3.6.pre

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 (121) hide show
  1. data/CHANGELOG +23 -0
  2. data/lib/active_support.rb +1 -0
  3. data/lib/active_support/core_ext/date_time/conversions.rb +14 -3
  4. data/lib/active_support/core_ext/enumerable.rb +6 -0
  5. data/lib/active_support/core_ext/file/atomic.rb +2 -1
  6. data/lib/active_support/core_ext/numeric/conversions.rb +2 -2
  7. data/lib/active_support/core_ext/object.rb +1 -0
  8. data/lib/active_support/core_ext/object/metaclass.rb +6 -5
  9. data/lib/active_support/core_ext/object/singleton_class.rb +13 -0
  10. data/lib/active_support/core_ext/string.rb +0 -1
  11. data/lib/active_support/core_ext/string/output_safety.rb +148 -44
  12. data/lib/active_support/core_ext/time/calculations.rb +1 -1
  13. data/lib/active_support/inflector.rb +1 -1
  14. data/lib/active_support/json/backends/yajl.rb +40 -0
  15. data/lib/active_support/json/decoding.rb +16 -1
  16. data/lib/active_support/json/encoding.rb +11 -2
  17. data/lib/active_support/ordered_hash.rb +24 -1
  18. data/lib/active_support/vendor.rb +10 -2
  19. data/lib/active_support/vendor/i18n-0.3.3/CHANGELOG.textile +76 -0
  20. data/lib/active_support/vendor/{i18n-0.1.3 → i18n-0.3.3}/MIT-LICENSE +0 -0
  21. data/lib/active_support/vendor/i18n-0.3.3/README.textile +81 -0
  22. data/lib/active_support/vendor/i18n-0.3.3/Rakefile +24 -0
  23. data/lib/active_support/vendor/i18n-0.3.3/benchmark/example.yml +144 -0
  24. data/lib/active_support/vendor/i18n-0.3.3/benchmark/run.rb +71 -0
  25. data/lib/active_support/vendor/i18n-0.3.3/contributors.txt +17 -0
  26. data/lib/active_support/vendor/i18n-0.3.3/i18n.gemspec +165 -0
  27. data/lib/active_support/vendor/i18n-0.3.3/init.rb +1 -0
  28. data/lib/active_support/vendor/{i18n-0.1.3 → i18n-0.3.3}/lib/i18n.rb +99 -21
  29. data/lib/active_support/vendor/i18n-0.3.3/lib/i18n/backend.rb +17 -0
  30. data/lib/active_support/vendor/i18n-0.3.3/lib/i18n/backend/active_record.rb +70 -0
  31. data/lib/active_support/vendor/i18n-0.3.3/lib/i18n/backend/active_record/missing.rb +67 -0
  32. data/lib/active_support/vendor/i18n-0.3.3/lib/i18n/backend/active_record/store_procs.rb +38 -0
  33. data/lib/active_support/vendor/i18n-0.3.3/lib/i18n/backend/active_record/translation.rb +83 -0
  34. data/lib/active_support/vendor/i18n-0.3.3/lib/i18n/backend/base.rb +259 -0
  35. data/lib/active_support/vendor/i18n-0.3.3/lib/i18n/backend/cache.rb +75 -0
  36. data/lib/active_support/vendor/i18n-0.3.3/lib/i18n/backend/cascade.rb +44 -0
  37. data/lib/active_support/vendor/i18n-0.3.3/lib/i18n/backend/chain.rb +74 -0
  38. data/lib/active_support/vendor/i18n-0.3.3/lib/i18n/backend/fallbacks.rb +52 -0
  39. data/lib/active_support/vendor/i18n-0.3.3/lib/i18n/backend/fast.rb +68 -0
  40. data/lib/active_support/vendor/i18n-0.3.3/lib/i18n/backend/gettext.rb +75 -0
  41. data/lib/active_support/vendor/i18n-0.3.3/lib/i18n/backend/helpers.rb +80 -0
  42. data/lib/active_support/vendor/i18n-0.3.3/lib/i18n/backend/interpolation_compiler.rb +119 -0
  43. data/lib/active_support/vendor/i18n-0.3.3/lib/i18n/backend/metadata.rb +73 -0
  44. data/lib/active_support/vendor/i18n-0.3.3/lib/i18n/backend/pluralization.rb +57 -0
  45. data/lib/active_support/vendor/i18n-0.3.3/lib/i18n/backend/simple.rb +22 -0
  46. data/lib/active_support/vendor/i18n-0.3.3/lib/i18n/core_ext/object/meta_class.rb +5 -0
  47. data/lib/active_support/vendor/i18n-0.3.3/lib/i18n/core_ext/string/interpolate.rb +99 -0
  48. data/lib/active_support/vendor/{i18n-0.1.3 → i18n-0.3.3}/lib/i18n/exceptions.rb +14 -6
  49. data/lib/active_support/vendor/i18n-0.3.3/lib/i18n/gettext.rb +25 -0
  50. data/lib/active_support/vendor/i18n-0.3.3/lib/i18n/helpers.rb +5 -0
  51. data/lib/active_support/vendor/i18n-0.3.3/lib/i18n/helpers/gettext.rb +64 -0
  52. data/lib/active_support/vendor/i18n-0.3.3/lib/i18n/locale.rb +6 -0
  53. data/lib/active_support/vendor/i18n-0.3.3/lib/i18n/locale/fallbacks.rb +98 -0
  54. data/lib/active_support/vendor/i18n-0.3.3/lib/i18n/locale/tag.rb +28 -0
  55. data/lib/active_support/vendor/i18n-0.3.3/lib/i18n/locale/tag/parents.rb +24 -0
  56. data/lib/active_support/vendor/i18n-0.3.3/lib/i18n/locale/tag/rfc4646.rb +76 -0
  57. data/lib/active_support/vendor/i18n-0.3.3/lib/i18n/locale/tag/simple.rb +41 -0
  58. data/lib/active_support/vendor/i18n-0.3.3/lib/i18n/version.rb +3 -0
  59. data/lib/active_support/vendor/i18n-0.3.3/test/all.rb +8 -0
  60. data/lib/active_support/vendor/i18n-0.3.3/test/api/basics.rb +15 -0
  61. data/lib/active_support/vendor/i18n-0.3.3/test/api/defaults.rb +40 -0
  62. data/lib/active_support/vendor/i18n-0.3.3/test/api/interpolation.rb +92 -0
  63. data/lib/active_support/vendor/i18n-0.3.3/test/api/link.rb +55 -0
  64. data/lib/active_support/vendor/i18n-0.3.3/test/api/localization/date.rb +91 -0
  65. data/lib/active_support/vendor/i18n-0.3.3/test/api/localization/date_time.rb +90 -0
  66. data/lib/active_support/vendor/i18n-0.3.3/test/api/localization/procs.rb +54 -0
  67. data/lib/active_support/vendor/i18n-0.3.3/test/api/localization/time.rb +84 -0
  68. data/lib/active_support/vendor/i18n-0.3.3/test/api/lookup.rb +45 -0
  69. data/lib/active_support/vendor/i18n-0.3.3/test/api/pluralization.rb +35 -0
  70. data/lib/active_support/vendor/i18n-0.3.3/test/api/procs.rb +40 -0
  71. data/lib/active_support/vendor/i18n-0.3.3/test/cases/api/active_record_test.rb +29 -0
  72. data/lib/active_support/vendor/i18n-0.3.3/test/cases/api/all_features_test.rb +40 -0
  73. data/lib/active_support/vendor/i18n-0.3.3/test/cases/api/cascade_test.rb +31 -0
  74. data/lib/active_support/vendor/i18n-0.3.3/test/cases/api/chain_test.rb +26 -0
  75. data/lib/active_support/vendor/i18n-0.3.3/test/cases/api/fallbacks_test.rb +33 -0
  76. data/lib/active_support/vendor/i18n-0.3.3/test/cases/api/fast_test.rb +31 -0
  77. data/lib/active_support/vendor/i18n-0.3.3/test/cases/api/pluralization_test.rb +33 -0
  78. data/lib/active_support/vendor/i18n-0.3.3/test/cases/api/simple_test.rb +21 -0
  79. data/lib/active_support/vendor/i18n-0.3.3/test/cases/backend/active_record/missing_test.rb +60 -0
  80. data/lib/active_support/vendor/i18n-0.3.3/test/cases/backend/active_record_test.rb +52 -0
  81. data/lib/active_support/vendor/i18n-0.3.3/test/cases/backend/cache_test.rb +72 -0
  82. data/lib/active_support/vendor/i18n-0.3.3/test/cases/backend/cascade_test.rb +66 -0
  83. data/lib/active_support/vendor/i18n-0.3.3/test/cases/backend/chain_test.rb +64 -0
  84. data/lib/active_support/vendor/i18n-0.3.3/test/cases/backend/fallbacks_test.rb +57 -0
  85. data/lib/active_support/vendor/i18n-0.3.3/test/cases/backend/fast_test.rb +50 -0
  86. data/lib/active_support/vendor/i18n-0.3.3/test/cases/backend/helpers_test.rb +26 -0
  87. data/lib/active_support/vendor/i18n-0.3.3/test/cases/backend/interpolation_compiler_test.rb +107 -0
  88. data/lib/active_support/vendor/i18n-0.3.3/test/cases/backend/metadata_test.rb +67 -0
  89. data/lib/active_support/vendor/i18n-0.3.3/test/cases/backend/pluralization_test.rb +43 -0
  90. data/lib/active_support/vendor/i18n-0.3.3/test/cases/backend/simple_test.rb +77 -0
  91. data/lib/active_support/vendor/i18n-0.3.3/test/cases/core_ext/string/interpolate_test.rb +94 -0
  92. data/lib/active_support/vendor/i18n-0.3.3/test/cases/gettext/api_test.rb +201 -0
  93. data/lib/active_support/vendor/i18n-0.3.3/test/cases/gettext/backend_test.rb +91 -0
  94. data/lib/active_support/vendor/{i18n-0.1.3/test → i18n-0.3.3/test/cases}/i18n_exceptions_test.rb +8 -10
  95. data/lib/active_support/vendor/i18n-0.3.3/test/cases/i18n_load_path_test.rb +23 -0
  96. data/lib/active_support/vendor/i18n-0.3.3/test/cases/i18n_test.rb +172 -0
  97. data/lib/active_support/vendor/i18n-0.3.3/test/cases/locale/fallbacks_test.rb +126 -0
  98. data/lib/active_support/vendor/i18n-0.3.3/test/cases/locale/tag/rfc4646_test.rb +143 -0
  99. data/lib/active_support/vendor/i18n-0.3.3/test/cases/locale/tag/simple_test.rb +33 -0
  100. data/lib/active_support/vendor/i18n-0.3.3/test/fixtures/locales/de.po +72 -0
  101. data/lib/active_support/vendor/i18n-0.3.3/test/fixtures/locales/en.rb +3 -0
  102. data/lib/active_support/vendor/i18n-0.3.3/test/fixtures/locales/en.yml +3 -0
  103. data/lib/active_support/vendor/i18n-0.3.3/test/fixtures/locales/plurals.rb +113 -0
  104. data/lib/active_support/vendor/i18n-0.3.3/test/test_helper.rb +100 -0
  105. data/lib/active_support/vendor/i18n-0.3.3/vendor/po_parser.rb +329 -0
  106. data/lib/active_support/version.rb +1 -1
  107. data/lib/active_support/whiny_nil.rb +1 -1
  108. data/lib/active_support/xml_mini/libxml.rb +23 -83
  109. data/lib/active_support/xml_mini/libxmlsax.rb +74 -0
  110. data/lib/active_support/xml_mini/nokogiri.rb +25 -22
  111. data/lib/active_support/xml_mini/nokogirisax.rb +73 -0
  112. metadata +108 -20
  113. data/lib/active_support/vendor/i18n-0.1.3/README.textile +0 -20
  114. data/lib/active_support/vendor/i18n-0.1.3/Rakefile +0 -5
  115. data/lib/active_support/vendor/i18n-0.1.3/i18n.gemspec +0 -27
  116. data/lib/active_support/vendor/i18n-0.1.3/lib/i18n/backend/simple.rb +0 -214
  117. data/lib/active_support/vendor/i18n-0.1.3/test/all.rb +0 -5
  118. data/lib/active_support/vendor/i18n-0.1.3/test/i18n_test.rb +0 -124
  119. data/lib/active_support/vendor/i18n-0.1.3/test/locale/en.rb +0 -1
  120. data/lib/active_support/vendor/i18n-0.1.3/test/locale/en.yml +0 -3
  121. data/lib/active_support/vendor/i18n-0.1.3/test/simple_backend_test.rb +0 -567
@@ -1,9 +1,6 @@
1
- $:.unshift "lib"
1
+ # encoding: utf-8
2
2
 
3
- require 'rubygems'
4
- require 'test/unit'
5
- require 'i18n'
6
- require 'active_support'
3
+ require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
7
4
 
8
5
  class I18nExceptionsTest < Test::Unit::TestCase
9
6
  def test_invalid_locale_stores_locale
@@ -47,29 +44,30 @@ class I18nExceptionsTest < Test::Unit::TestCase
47
44
  end
48
45
 
49
46
  def test_missing_interpolation_argument_stores_key_and_string
47
+ assert_raises(I18n::MissingInterpolationArgument) { force_missing_interpolation_argument }
50
48
  force_missing_interpolation_argument
51
49
  rescue I18n::ArgumentError => e
52
- assert_equal 'bar', e.key
50
+ # assert_equal :bar, e.key
53
51
  assert_equal "{{bar}}", e.string
54
52
  end
55
53
 
56
54
  def test_missing_interpolation_argument_message
57
55
  force_missing_interpolation_argument
58
56
  rescue I18n::ArgumentError => e
59
- assert_equal 'interpolation argument bar missing in "{{bar}}"', e.message
57
+ assert_equal 'missing interpolation argument in "{{bar}}" ({:baz=>"baz"} given)', e.message
60
58
  end
61
59
 
62
60
  def test_reserved_interpolation_key_stores_key_and_string
63
61
  force_reserved_interpolation_key
64
62
  rescue I18n::ArgumentError => e
65
- assert_equal 'scope', e.key
63
+ assert_equal :scope, e.key
66
64
  assert_equal "{{scope}}", e.string
67
65
  end
68
66
 
69
67
  def test_reserved_interpolation_key_message
70
68
  force_reserved_interpolation_key
71
69
  rescue I18n::ArgumentError => e
72
- assert_equal 'reserved key "scope" used in "{{scope}}"', e.message
70
+ assert_equal 'reserved key :scope used in "{{scope}}"', e.message
73
71
  end
74
72
 
75
73
  private
@@ -96,4 +94,4 @@ class I18nExceptionsTest < Test::Unit::TestCase
96
94
  I18n.backend.store_translations 'de', :foo => "{{scope}}"
97
95
  I18n.backend.translate 'de', :foo, :baz => 'baz'
98
96
  end
99
- end
97
+ end
@@ -0,0 +1,23 @@
1
+ # encoding: utf-8
2
+
3
+ require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
4
+
5
+ class I18nLoadPathTest < Test::Unit::TestCase
6
+ # include Tests::Backend::Simple::Setup::Base
7
+
8
+ def setup
9
+ I18n.locale = :en
10
+ I18n.backend = I18n::Backend::Simple.new
11
+ store_translations(:en, :foo => {:bar => 'bar', :baz => 'baz'})
12
+ end
13
+
14
+ def test_nested_load_paths_do_not_break_locale_loading
15
+ I18n.load_path = [[locales_dir + '/en.yml']]
16
+ assert_equal "baz", I18n.t(:'foo.bar')
17
+ end
18
+
19
+ def test_adding_arrays_of_filenames_to_load_path_do_not_break_locale_loading
20
+ I18n.load_path << Dir[locales_dir + '/*.{rb,yml}']
21
+ assert_equal "baz", I18n.t(:'foo.bar')
22
+ end
23
+ end
@@ -0,0 +1,172 @@
1
+ # encoding: utf-8
2
+
3
+ require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
4
+
5
+ class I18nTest < Test::Unit::TestCase
6
+ def setup
7
+ I18n.backend.store_translations :'en', {
8
+ :currency => {
9
+ :format => {
10
+ :separator => '.',
11
+ :delimiter => ',',
12
+ }
13
+ }
14
+ }
15
+ end
16
+
17
+ def test_uses_simple_backend_set_by_default
18
+ assert I18n.backend.is_a?(I18n::Backend::Simple)
19
+ end
20
+
21
+ def test_can_set_backend
22
+ assert_nothing_raised { I18n.backend = self }
23
+ assert_equal self, I18n.backend
24
+ I18n.backend = I18n::Backend::Simple.new
25
+ end
26
+
27
+ def test_uses_en_us_as_default_locale_by_default
28
+ assert_equal :en, I18n.default_locale
29
+ end
30
+
31
+ def test_can_set_default_locale
32
+ assert_nothing_raised { I18n.default_locale = 'de' }
33
+ assert_equal :de, I18n.default_locale
34
+ I18n.default_locale = :en
35
+ end
36
+
37
+ def test_uses_default_locale_as_locale_by_default
38
+ assert_equal I18n.default_locale, I18n.locale
39
+ end
40
+
41
+ def test_can_set_locale_to_thread_current
42
+ assert_nothing_raised { I18n.locale = 'de' }
43
+ assert_equal :de, I18n.locale
44
+ assert_equal :de, Thread.current[:locale]
45
+ I18n.locale = :en
46
+ end
47
+
48
+ def test_defaults_to_dot_as_separator
49
+ assert_equal '.', I18n.default_separator
50
+ end
51
+
52
+ def test_can_set_default_separator
53
+ assert_nothing_raised { I18n.default_separator = "\001" }
54
+ I18n.default_separator = '.' # revert it
55
+ end
56
+
57
+ def test_normalize_keys
58
+ assert_equal [:en, :foo, :bar], I18n.send(:normalize_translation_keys, :en, :bar, :foo)
59
+ assert_equal [:en, :foo, :bar, :baz, :buz], I18n.send(:normalize_translation_keys, :en, :'baz.buz', :'foo.bar')
60
+ assert_equal [:en, :foo, :bar, :baz, :buz], I18n.send(:normalize_translation_keys, :en, 'baz.buz', 'foo.bar')
61
+ assert_equal [:en, :foo, :bar, :baz, :buz], I18n.send(:normalize_translation_keys, :en, %w(baz buz), %w(foo bar))
62
+ assert_equal [:en, :foo, :bar, :baz, :buz], I18n.send(:normalize_translation_keys, :en, [:baz, :buz], [:foo, :bar])
63
+ end
64
+
65
+ def test_normalize_keys_should_not_attempt_to_sym_on_empty_string
66
+ assert_equal [:en, :foo, :bar, :baz, :buz], I18n.send(:normalize_translation_keys, :en, :'baz.buz', :'foo..bar')
67
+ assert_equal [:en, :foo, :bar, :baz, :buz], I18n.send(:normalize_translation_keys, :en, :'baz.buz', :'foo......bar')
68
+ end
69
+
70
+ def test_uses_passed_separator_to_normalize_keys
71
+ assert_equal [:en, :foo, :bar, :baz, :buz], I18n.send(:normalize_translation_keys, :en, :'baz|buz', :'foo|bar', '|')
72
+ end
73
+
74
+ def test_can_set_exception_handler
75
+ assert_nothing_raised { I18n.exception_handler = :custom_exception_handler }
76
+ I18n.exception_handler = :default_exception_handler # revert it
77
+ end
78
+
79
+ with_mocha do
80
+ def test_uses_custom_exception_handler
81
+ I18n.exception_handler = :custom_exception_handler
82
+ I18n.expects(:custom_exception_handler)
83
+ I18n.translate :bogus
84
+ I18n.exception_handler = :default_exception_handler # revert it
85
+ end
86
+
87
+ def test_delegates_translate_to_backend
88
+ I18n.backend.expects(:translate).with 'de', :foo, {}
89
+ I18n.translate :foo, :locale => 'de'
90
+ end
91
+
92
+ def test_delegates_localize_to_backend
93
+ I18n.backend.expects(:localize).with 'de', :whatever, :default
94
+ I18n.localize :whatever, :locale => 'de'
95
+ end
96
+
97
+ def test_translate_given_no_locale_uses_i18n_locale
98
+ I18n.backend.expects(:translate).with :en, :foo, {}
99
+ I18n.translate :foo
100
+ end
101
+ end
102
+
103
+ def test_translate_on_nested_symbol_keys_works
104
+ assert_equal ".", I18n.t(:'currency.format.separator')
105
+ end
106
+
107
+ def test_translate_with_nested_string_keys_works
108
+ assert_equal ".", I18n.t('currency.format.separator')
109
+ end
110
+
111
+ def test_translate_with_array_as_scope_works
112
+ assert_equal ".", I18n.t(:separator, :scope => %w(currency format))
113
+ end
114
+
115
+ def test_translate_with_array_containing_dot_separated_strings_as_scope_works
116
+ assert_equal ".", I18n.t(:separator, :scope => ['currency.format'])
117
+ end
118
+
119
+ def test_translate_with_key_array_and_dot_separated_scope_works
120
+ assert_equal [".", ","], I18n.t(%w(separator delimiter), :scope => 'currency.format')
121
+ end
122
+
123
+ def test_translate_with_dot_separated_key_array_and_scope_works
124
+ assert_equal [".", ","], I18n.t(%w(format.separator format.delimiter), :scope => 'currency')
125
+ end
126
+
127
+ with_mocha do
128
+ def test_translate_with_options_using_scope_works
129
+ I18n.backend.expects(:translate).with('de', :precision, :scope => :"currency.format")
130
+ I18n.with_options :locale => 'de', :scope => :'currency.format' do |locale|
131
+ locale.t :precision
132
+ end
133
+ end
134
+ end
135
+
136
+ # def test_translate_given_no_args_raises_missing_translation_data
137
+ # assert_equal "translation missing: en, no key", I18n.t
138
+ # end
139
+
140
+ def test_translate_given_a_bogus_key_raises_missing_translation_data
141
+ assert_equal "translation missing: en, bogus", I18n.t(:bogus)
142
+ end
143
+
144
+ def test_localize_nil_raises_argument_error
145
+ assert_raises(I18n::ArgumentError) { I18n.l nil }
146
+ end
147
+
148
+ def test_localize_object_raises_argument_error
149
+ assert_raises(I18n::ArgumentError) { I18n.l Object.new }
150
+ end
151
+
152
+ def test_proc_exception_handler
153
+ I18n.exception_handler = Proc.new { |exception, locale, key, options|
154
+ "No exception here! [Proc handler]"
155
+ }
156
+ assert_equal "No exception here! [Proc handler]", I18n.translate(:test_proc_handler)
157
+ ensure
158
+ I18n.exception_handler = :default_exception_handler
159
+ end
160
+
161
+ def test_class_exception_handler
162
+ I18n.exception_handler = Class.new do
163
+ def call(exception, locale, key, options)
164
+ "No exception here! [Class handler]"
165
+ end
166
+ end.new
167
+ assert_equal "No exception here! [Class handler]", I18n.translate(:test_class_handler)
168
+ ensure
169
+ I18n.exception_handler = :default_exception_handler
170
+ end
171
+
172
+ end
@@ -0,0 +1,126 @@
1
+ # encoding: utf-8
2
+
3
+ require File.expand_path(File.dirname(__FILE__) + '/../../test_helper')
4
+
5
+ include I18n::Locale
6
+
7
+ class I18nFallbacksDefaultsTest < Test::Unit::TestCase
8
+ def teardown
9
+ I18n.default_locale = :en
10
+ end
11
+
12
+ test "defaults reflect the I18n.default_locale if no default has been set manually" do
13
+ I18n.default_locale = :'en-US'
14
+ fallbacks = Fallbacks.new
15
+ assert_equal [:'en-US', :en], fallbacks.defaults
16
+ end
17
+
18
+ test "defaults reflect a manually passed default locale if any" do
19
+ fallbacks = Fallbacks.new(:'fi-FI')
20
+ assert_equal [:'fi-FI', :fi], fallbacks.defaults
21
+ I18n.default_locale = :'de-DE'
22
+ assert_equal [:'fi-FI', :fi], fallbacks.defaults
23
+ end
24
+
25
+ test "defaults allows to set multiple defaults" do
26
+ fallbacks = Fallbacks.new(:'fi-FI', :'se-FI')
27
+ assert_equal [:'fi-FI', :fi, :'se-FI', :se], fallbacks.defaults
28
+ end
29
+ end
30
+
31
+ class I18nFallbacksComputationTest < Test::Unit::TestCase
32
+ def setup
33
+ @fallbacks = Fallbacks.new(:'en-US')
34
+ end
35
+
36
+ test "with no mappings defined it returns [:es, :en-US] for :es" do
37
+ assert_equal [:es, :"en-US", :en], @fallbacks[:es]
38
+ end
39
+
40
+ test "with no mappings defined it returns [:es-ES, :es, :en-US] for :es-ES" do
41
+ assert_equal [:"es-ES", :es, :"en-US", :en], @fallbacks[:"es-ES"]
42
+ end
43
+
44
+ test "with no mappings defined it returns [:es-MX, :es, :en-US] for :es-MX" do
45
+ assert_equal [:"es-MX", :es, :"en-US", :en], @fallbacks[:"es-MX"]
46
+ end
47
+
48
+ test "with no mappings defined it returns [:es-Latn-ES, :es-Latn, :es, :en-US] for :es-Latn-ES" do
49
+ assert_equal [:"es-Latn-ES", :"es-Latn", :es, :"en-US", :en], @fallbacks[:'es-Latn-ES']
50
+ end
51
+
52
+ test "with no mappings defined it returns [:en, :en-US] for :en" do
53
+ assert_equal [:en, :"en-US"], @fallbacks[:en]
54
+ end
55
+
56
+ test "with no mappings defined it returns [:en-US, :en] for :en-US (special case: locale == default)" do
57
+ assert_equal [:"en-US", :en], @fallbacks[:"en-US"]
58
+ end
59
+
60
+ # Most people who speak Catalan also live in Spain, so it is safe to assume
61
+ # that they also speak Spanish as spoken in Spain.
62
+ test "with a Catalan mapping defined it returns [:ca, :es-ES, :es, :en-US] for :ca" do
63
+ @fallbacks.map(:ca => :"es-ES")
64
+ assert_equal [:ca, :"es-ES", :es, :"en-US", :en], @fallbacks[:ca]
65
+ end
66
+
67
+ test "with a Catalan mapping defined it returns [:ca-ES, :ca, :es-ES, :es, :en-US] for :ca-ES" do
68
+ @fallbacks.map(:ca => :"es-ES")
69
+ assert_equal [:"ca-ES", :ca, :"es-ES", :es, :"en-US", :en], @fallbacks[:"ca-ES"]
70
+ end
71
+
72
+ # People who speak Arabic as spoken in Palestine often times also speak
73
+ # Hebrew as spoken in Israel. However it is in no way safe to assume that
74
+ # everybody who speaks Arabic also speaks Hebrew.
75
+
76
+ test "with a Hebrew mapping defined it returns [:ar, :en-US] for :ar" do
77
+ @fallbacks.map(:"ar-PS" => :"he-IL")
78
+ assert_equal [:ar, :"en-US", :en], @fallbacks[:ar]
79
+ end
80
+
81
+ test "with a Hebrew mapping defined it returns [:ar-EG, :ar, :en-US] for :ar-EG" do
82
+ @fallbacks.map(:"ar-PS" => :"he-IL")
83
+ assert_equal [:"ar-EG", :ar, :"en-US", :en], @fallbacks[:"ar-EG"]
84
+ end
85
+
86
+ test "with a Hebrew mapping defined it returns [:ar-PS, :ar, :he-IL, :he, :en-US] for :ar-PS" do
87
+ @fallbacks.map(:"ar-PS" => :"he-IL")
88
+ assert_equal [:"ar-PS", :ar, :"he-IL", :he, :"en-US", :en], @fallbacks[:"ar-PS"]
89
+ end
90
+
91
+ # Sami people live in several scandinavian countries. In Finnland many people
92
+ # know Swedish and Finnish. Thus, it can be assumed that Sami living in
93
+ # Finnland also speak Swedish and Finnish.
94
+
95
+ test "with a Sami mapping defined it returns [:sms-FI, :sms, :se-FI, :se, :fi-FI, :fi, :en-US] for :sms-FI" do
96
+ @fallbacks.map(:sms => [:"se-FI", :"fi-FI"])
97
+ assert_equal [:"sms-FI", :sms, :"se-FI", :se, :"fi-FI", :fi, :"en-US", :en], @fallbacks[:"sms-FI"]
98
+ end
99
+
100
+ # Austrian people understand German as spoken in Germany
101
+
102
+ test "with a German mapping defined it returns [:de, :en-US] for de" do
103
+ @fallbacks.map(:"de-AT" => :"de-DE")
104
+ assert_equal [:de, :"en-US", :en], @fallbacks[:"de"]
105
+ end
106
+
107
+ test "with a German mapping defined it returns [:de-DE, :de, :en-US] for de-DE" do
108
+ @fallbacks.map(:"de-AT" => :"de-DE")
109
+ assert_equal [:"de-DE", :de, :"en-US", :en], @fallbacks[:"de-DE"]
110
+ end
111
+
112
+ test "with a German mapping defined it returns [:de-AT, :de, :de-DE, :en-US] for de-AT" do
113
+ @fallbacks.map(:"de-AT" => :"de-DE")
114
+ assert_equal [:"de-AT", :de, :"de-DE", :"en-US", :en], @fallbacks[:"de-AT"]
115
+ end
116
+
117
+ # Mapping :de => :en, :he => :en
118
+
119
+ test "with a mapping :de => :en, :he => :en defined it returns [:de, :en] for :de" do
120
+ assert_equal [:de, :"en-US", :en], @fallbacks[:de]
121
+ end
122
+
123
+ test "with a mapping :de => :en, :he => :en defined it [:he, :en] for :de" do
124
+ assert_equal [:he, :"en-US", :en], @fallbacks[:he]
125
+ end
126
+ end
@@ -0,0 +1,143 @@
1
+ # encoding: utf-8
2
+
3
+ require File.expand_path(File.dirname(__FILE__) + '/../../../test_helper')
4
+
5
+ class I18nLocaleTagRfc4646ParserTest < Test::Unit::TestCase
6
+ include I18n::Locale
7
+
8
+ test "Rfc4646::Parser given a valid tag 'de' returns an array of subtags" do
9
+ assert_equal ['de', nil, nil, nil, nil, nil, nil], Tag::Rfc4646::Parser.match('de')
10
+ end
11
+
12
+ test "Rfc4646::Parser given a valid tag 'de' returns an array of subtags" do
13
+ assert_equal ['de', nil, 'DE', nil, nil, nil, nil], Tag::Rfc4646::Parser.match('de-DE')
14
+ end
15
+
16
+ test "Rfc4646::Parser given a valid lowercase tag 'de-latn-de-variant-x-phonebk' returns an array of subtags" do
17
+ assert_equal ['de', 'latn', 'de', 'variant', nil, 'x-phonebk', nil], Tag::Rfc4646::Parser.match('de-latn-de-variant-x-phonebk')
18
+ end
19
+
20
+ test "Rfc4646::Parser given a valid uppercase tag 'DE-LATN-DE-VARIANT-X-PHONEBK' returns an array of subtags" do
21
+ assert_equal ['DE', 'LATN', 'DE', 'VARIANT', nil, 'X-PHONEBK', nil], Tag::Rfc4646::Parser.match('DE-LATN-DE-VARIANT-X-PHONEBK')
22
+ end
23
+
24
+ test "Rfc4646::Parser given an invalid tag 'a-DE' it returns false" do
25
+ assert_equal false, Tag::Rfc4646::Parser.match('a-DE')
26
+ end
27
+
28
+ test "Rfc4646::Parser given an invalid tag 'de-419-DE' it returns false" do
29
+ assert_equal false, Tag::Rfc4646::Parser.match('de-419-DE')
30
+ end
31
+ end
32
+
33
+ # Tag for the locale 'de-Latn-DE-Variant-a-ext-x-phonebk-i-klingon'
34
+
35
+ class I18nLocaleTagSubtagsTest < Test::Unit::TestCase
36
+ include I18n::Locale
37
+
38
+ def setup
39
+ subtags = %w(de Latn DE variant a-ext x-phonebk i-klingon)
40
+ @tag = Tag::Rfc4646.new *subtags
41
+ end
42
+
43
+ test "returns 'de' as the language subtag in lowercase" do
44
+ assert_equal 'de', @tag.language
45
+ end
46
+
47
+ test "returns 'Latn' as the script subtag in titlecase" do
48
+ assert_equal 'Latn', @tag.script
49
+ end
50
+
51
+ test "returns 'DE' as the region subtag in uppercase" do
52
+ assert_equal 'DE', @tag.region
53
+ end
54
+
55
+ test "returns 'variant' as the variant subtag in lowercase" do
56
+ assert_equal 'variant', @tag.variant
57
+ end
58
+
59
+ test "returns 'a-ext' as the extension subtag" do
60
+ assert_equal 'a-ext', @tag.extension
61
+ end
62
+
63
+ test "returns 'x-phonebk' as the privateuse subtag" do
64
+ assert_equal 'x-phonebk', @tag.privateuse
65
+ end
66
+
67
+ test "returns 'i-klingon' as the grandfathered subtag" do
68
+ assert_equal 'i-klingon', @tag.grandfathered
69
+ end
70
+
71
+ test "returns a formatted tag string from #to_s" do
72
+ assert_equal 'de-Latn-DE-variant-a-ext-x-phonebk-i-klingon', @tag.to_s
73
+ end
74
+
75
+ test "returns an array containing the formatted subtags from #to_a" do
76
+ assert_equal %w(de Latn DE variant a-ext x-phonebk i-klingon), @tag.to_a
77
+ end
78
+ end
79
+
80
+ # Tag inheritance
81
+
82
+ class I18nLocaleTagSubtagsTest < Test::Unit::TestCase
83
+ test "#parent returns 'de-Latn-DE-variant-a-ext-x-phonebk' as the parent of 'de-Latn-DE-variant-a-ext-x-phonebk-i-klingon'" do
84
+ tag = Tag::Rfc4646.new *%w(de Latn DE variant a-ext x-phonebk i-klingon)
85
+ assert_equal 'de-Latn-DE-variant-a-ext-x-phonebk', tag.parent.to_s
86
+ end
87
+
88
+ test "#parent returns 'de-Latn-DE-variant-a-ext' as the parent of 'de-Latn-DE-variant-a-ext-x-phonebk'" do
89
+ tag = Tag::Rfc4646.new *%w(de Latn DE variant a-ext x-phonebk)
90
+ assert_equal 'de-Latn-DE-variant-a-ext', tag.parent.to_s
91
+ end
92
+
93
+ test "#parent returns 'de-Latn-DE-variant' as the parent of 'de-Latn-DE-variant-a-ext'" do
94
+ tag = Tag::Rfc4646.new *%w(de Latn DE variant a-ext)
95
+ assert_equal 'de-Latn-DE-variant', tag.parent.to_s
96
+ end
97
+
98
+ test "#parent returns 'de-Latn-DE' as the parent of 'de-Latn-DE-variant'" do
99
+ tag = Tag::Rfc4646.new *%w(de Latn DE variant)
100
+ assert_equal 'de-Latn-DE', tag.parent.to_s
101
+ end
102
+
103
+ test "#parent returns 'de-Latn' as the parent of 'de-Latn-DE'" do
104
+ tag = Tag::Rfc4646.new *%w(de Latn DE)
105
+ assert_equal 'de-Latn', tag.parent.to_s
106
+ end
107
+
108
+ test "#parent returns 'de' as the parent of 'de-Latn'" do
109
+ tag = Tag::Rfc4646.new *%w(de Latn)
110
+ assert_equal 'de', tag.parent.to_s
111
+ end
112
+
113
+ # TODO RFC4647 says: "If no language tag matches the request, the "default" value is returned."
114
+ # where should we set the default language?
115
+ # test "#parent returns '' as the parent of 'de'" do
116
+ # tag = Tag::Rfc4646.new *%w(de)
117
+ # assert_equal '', tag.parent.to_s
118
+ # end
119
+
120
+ test "#parent returns an array of 5 parents for 'de-Latn-DE-variant-a-ext-x-phonebk-i-klingon'" do
121
+ parents = %w(de-Latn-DE-variant-a-ext-x-phonebk-i-klingon
122
+ de-Latn-DE-variant-a-ext-x-phonebk
123
+ de-Latn-DE-variant-a-ext
124
+ de-Latn-DE-variant
125
+ de-Latn-DE
126
+ de-Latn
127
+ de)
128
+ tag = Tag::Rfc4646.new *%w(de Latn DE variant a-ext x-phonebk i-klingon)
129
+ assert_equal parents, tag.self_and_parents.map{|tag| tag.to_s}
130
+ end
131
+
132
+ test "returns an array of 5 parents for 'de-Latn-DE-variant-a-ext-x-phonebk-i-klingon'" do
133
+ parents = %w(de-Latn-DE-variant-a-ext-x-phonebk-i-klingon
134
+ de-Latn-DE-variant-a-ext-x-phonebk
135
+ de-Latn-DE-variant-a-ext
136
+ de-Latn-DE-variant
137
+ de-Latn-DE
138
+ de-Latn
139
+ de)
140
+ tag = Tag::Rfc4646.new *%w(de Latn DE variant a-ext x-phonebk i-klingon)
141
+ assert_equal parents, tag.self_and_parents.map{|tag| tag.to_s}
142
+ end
143
+ end