1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551
|
# encoding: utf-8
require 'test_helper'
class I18nTest < I18n::TestCase
def setup
super
store_translations(:en, :currency => { :format => { :separator => '.', :delimiter => ',', } })
store_translations(:nl, :currency => { :format => { :separator => ',', :delimiter => '.', } })
store_translations(:en, "true" => "Yes", "false" => "No")
end
test "exposes its VERSION constant" do
assert I18n::VERSION
end
test "uses the simple backend by default" do
assert I18n.backend.is_a?(I18n::Backend::Simple)
end
test "can set the backend" do
begin
assert_nothing_raised { I18n.backend = self }
assert_equal self, I18n.backend
ensure
I18n.backend = I18n::Backend::Simple.new
end
end
test "uses :en as a default_locale by default" do
assert_equal :en, I18n.default_locale
end
test "can set the default locale" do
begin
assert_nothing_raised { I18n.default_locale = 'de' }
assert_equal :de, I18n.default_locale
ensure
I18n.default_locale = :en
end
end
test "default_locale= doesn't ignore junk" do
assert_raises(NoMethodError) { I18n.default_locale = Class }
end
test "raises an I18n::InvalidLocale exception when setting an unavailable default locale" do
begin
I18n.config.enforce_available_locales = true
assert_raises(I18n::InvalidLocale) { I18n.default_locale = :klingon }
ensure
I18n.config.enforce_available_locales = false
end
end
test "uses the default locale as a locale by default" do
assert_equal I18n.default_locale, I18n.locale
end
test "sets the current locale to Thread.current" do
assert_nothing_raised { I18n.locale = 'de' }
assert_equal :de, I18n.locale
assert_equal :de, Thread.current[:i18n_config].locale
I18n.locale = :en
end
test "locale= doesn't ignore junk" do
assert_raises(NoMethodError) { I18n.locale = Class }
end
test "raises an I18n::InvalidLocale exception when setting an unavailable locale" do
begin
I18n.config.enforce_available_locales = true
assert_raises(I18n::InvalidLocale) { I18n.locale = :klingon }
ensure
I18n.config.enforce_available_locales = false
end
end
test "can set the configuration object" do
begin
I18n.config = self
assert_equal self, I18n.config
assert_equal self, Thread.current[:i18n_config]
ensure
I18n.config = ::I18n::Config.new
end
end
test "locale is not shared between configurations" do
a = I18n::Config.new
b = I18n::Config.new
a.locale = :fr
b.locale = :es
assert_equal :fr, a.locale
assert_equal :es, b.locale
assert_equal :en, I18n.locale
end
test "other options are shared between configurations" do
begin
a = I18n::Config.new
b = I18n::Config.new
a.default_locale = :fr
b.default_locale = :es
assert_equal :es, a.default_locale
assert_equal :es, b.default_locale
assert_equal :es, I18n.default_locale
ensure
I18n.default_locale = :en
end
end
test "uses a dot as a default_separator by default" do
assert_equal '.', I18n.default_separator
end
test "can set the default_separator" do
begin
assert_nothing_raised { I18n.default_separator = "\001" }
ensure
I18n.default_separator = '.'
end
end
test "normalize_keys normalizes given locale, keys and scope to an array of single-key symbols" do
assert_equal [:en, :foo, :bar], I18n.normalize_keys(:en, :bar, :foo)
assert_equal [:en, :foo, :bar, :baz, :buz], I18n.normalize_keys(:en, :'baz.buz', :'foo.bar')
assert_equal [:en, :foo, :bar, :baz, :buz], I18n.normalize_keys(:en, 'baz.buz', 'foo.bar')
assert_equal [:en, :foo, :bar, :baz, :buz], I18n.normalize_keys(:en, %w(baz buz), %w(foo bar))
assert_equal [:en, :foo, :bar, :baz, :buz], I18n.normalize_keys(:en, [:baz, :buz], [:foo, :bar])
end
test "normalize_keys discards empty keys" do
assert_equal [:en, :foo, :bar, :baz, :buz], I18n.normalize_keys(:en, :'baz..buz', :'foo..bar')
assert_equal [:en, :foo, :bar, :baz, :buz], I18n.normalize_keys(:en, :'baz......buz', :'foo......bar')
assert_equal [:en, :foo, :bar, :baz, :buz], I18n.normalize_keys(:en, ['baz', nil, '', 'buz'], ['foo', nil, '', 'bar'])
end
test "normalize_keys uses a given separator" do
assert_equal [:en, :foo, :bar, :baz, :buz], I18n.normalize_keys(:en, :'baz|buz', :'foo|bar', '|')
end
test "normalize_keys normalizes given locale with separator" do
assert_equal [:en, :foo, :bar, :baz], I18n.normalize_keys(:"en.foo", :baz, :bar)
end
test "can set the exception_handler" do
begin
previous_exception_handler = I18n.exception_handler
assert_nothing_raised { I18n.exception_handler = :custom_exception_handler }
ensure
I18n.exception_handler = previous_exception_handler
end
end
test "uses a custom exception handler set to I18n.exception_handler" do
begin
previous_exception_handler = I18n.exception_handler
I18n.exception_handler = :custom_exception_handler
I18n.expects(:custom_exception_handler)
I18n.translate :bogus
ensure
I18n.exception_handler = previous_exception_handler
end
end
test "uses a custom exception handler passed as an option" do
I18n.expects(:custom_exception_handler)
I18n.translate(:bogus, :exception_handler => :custom_exception_handler)
end
test "delegates translate calls to the backend" do
I18n.backend.expects(:translate).with('de', :foo, {})
I18n.translate :foo, :locale => 'de'
end
test "delegates localize calls to the backend" do
I18n.backend.expects(:localize).with('de', :whatever, :default, {})
I18n.localize :whatever, :locale => 'de'
end
test "translate given no locale uses the current locale" do
I18n.backend.expects(:translate).with(:en, :foo, {})
I18n.translate :foo
end
test "translate works with nested symbol keys" do
assert_equal ".", I18n.t(:'currency.format.separator')
end
test "translate works with nested string keys" do
assert_equal ".", I18n.t('currency.format.separator')
end
test "translate with an array as a scope works" do
assert_equal ".", I18n.t(:separator, :scope => %w(currency format))
end
test "translate with an array containing dot separated strings as a scope works" do
assert_equal ".", I18n.t(:separator, :scope => ['currency.format'])
end
test "translate with an array of keys and a dot separated string as a scope works" do
assert_equal [".", ","], I18n.t(%w(separator delimiter), :scope => 'currency.format')
end
test "translate with an array of dot separated keys and a scope works" do
assert_equal [".", ","], I18n.t(%w(format.separator format.delimiter), :scope => 'currency')
end
# def test_translate_given_no_args_raises_missing_translation_data
# assert_equal "Translation missing: en, no key", I18n.t
# end
test "translate given a bogus key returns an error message" do
assert_equal "Translation missing: en.bogus", I18n.t(:bogus)
end
test "translate given multiple bogus keys returns an array of error messages" do
assert_equal(
["Translation missing: en.bogus", "Translation missing: en.also_bogus"],
I18n.t([:bogus, :also_bogus]),
)
end
test "translate given an empty string as a key raises an I18n::ArgumentError" do
assert_raises(I18n::ArgumentError) { I18n.t("") }
end
test "translate given an empty symbol as a key raises an I18n::ArgumentError" do
assert_raises(I18n::ArgumentError) { I18n.t(:"") }
end
test "translate given an array with empty string as a key raises an I18n::ArgumentError" do
assert_raises(I18n::ArgumentError) { I18n.t(["", :foo]) }
end
test "translate given an empty array as a key returns empty array" do
assert_equal [], I18n.t([])
end
test "translate given nil returns nil" do
assert_nil I18n.t(nil)
end
test "translate given an unavailable locale rases an I18n::InvalidLocale" do
begin
I18n.config.enforce_available_locales = true
assert_raises(I18n::InvalidLocale) { I18n.t(:foo, :locale => 'klingon') }
ensure
I18n.config.enforce_available_locales = false
end
end
test "translate given true as a key works" do
assert_equal "Yes", I18n.t(true)
end
test "translate given false as a key works" do
assert_equal "No", I18n.t(false)
end
test "translate raises Disabled if locale is false" do
I18n.with_locale(false) do
assert_raises I18n::Disabled do
I18n.t('foo')
end
assert_equal 'Translation missing: en.foo', I18n.t('foo', locale: :en)
end
end
test "available_locales can be replaced at runtime" do
begin
I18n.config.enforce_available_locales = true
assert_raises(I18n::InvalidLocale) { I18n.t(:foo, :locale => 'klingon') }
old_locales, I18n.config.available_locales = I18n.config.available_locales, [:klingon]
I18n.t(:foo, :locale => 'klingon')
ensure
I18n.config.enforce_available_locales = false
I18n.config.available_locales = old_locales
end
end
test "available_locales_set should return a set" do
assert_equal Set, I18n.config.available_locales_set.class
assert_equal I18n.config.available_locales.size * 2, I18n.config.available_locales_set.size
end
test "interpolation_keys returns an array of keys" do
store_translations(:en, "example_two" => "Two interpolations %{foo} %{bar}")
assert_equal ["foo", "bar"], I18n.interpolation_keys("example_two")
end
test "interpolation_keys returns an empty array when no interpolations " do
store_translations(:en, "example_zero" => "Zero interpolations")
assert_equal [], I18n.interpolation_keys("example_zero")
end
test "interpolation_keys returns an empty array when missing translation " do
assert_equal [], I18n.interpolation_keys("does-not-exist")
end
test "interpolation_keys returns an empty array when nested translation" do
store_translations(:en, "example_nested" => { "one" => "One %{foo}", "two" => "Two %{bar}" })
assert_equal [], I18n.interpolation_keys("example_nested")
end
test "interpolation_keys returns an array of keys when translation is an Array" do
store_translations(:en, "example_array" => ["One %{foo}", ["Two %{bar}", ["Three %{baz}"]]])
assert_equal ["foo", "bar", "baz"], I18n.interpolation_keys("example_array")
end
test "interpolation_keys raises I18n::ArgumentError when non-string argument" do
assert_raises(I18n::ArgumentError) { I18n.interpolation_keys(["bad-argument"]) }
end
test "exists? given nil raises I18n::ArgumentError" do
assert_raises(I18n::ArgumentError) { I18n.exists?(nil) }
end
test "exists? given an existing key will return true" do
assert_equal true, I18n.exists?(:currency)
end
test "exists? given a non-existing key will return false" do
assert_equal false, I18n.exists?(:bogus)
end
test "exists? given an existing dot-separated key will return true" do
assert_equal true, I18n.exists?('currency.format.delimiter')
end
test "exists? given a non-existing dot-separated key will return false" do
assert_equal false, I18n.exists?('currency.format.bogus')
end
test "exists? given an existing key and an existing locale will return true" do
assert_equal true, I18n.exists?(:currency, :nl)
end
test "exists? given an existing key and a scope will return true" do
assert_equal true, I18n.exists?(:delimiter, scope: [:currency, :format])
end
test "exists? given a non-existing key and an existing locale will return false" do
assert_equal false, I18n.exists?(:bogus, :nl)
end
test "exists? raises Disabled if locale is false" do
I18n.with_locale(false) do
assert_raises I18n::Disabled do
I18n.exists?(:bogus)
end
assert_equal false, I18n.exists?(:bogus, :nl)
end
end
test "localize given nil raises an I18n::ArgumentError" do
assert_raises(I18n::ArgumentError) { I18n.l nil }
end
test "localize given nil and default returns default" do
assert_nil I18n.l(nil, :default => nil)
end
test "localize given an Object raises an I18n::ArgumentError" do
assert_raises(I18n::ArgumentError) { I18n.l Object.new }
end
test "localize given an unavailable locale rases an I18n::InvalidLocale" do
begin
I18n.config.enforce_available_locales = true
assert_raises(I18n::InvalidLocale) { I18n.l(Time.now, :locale => 'klingon') }
ensure
I18n.config.enforce_available_locales = false
end
end
test "localize raises Disabled if locale is false" do
I18n.with_locale(false) do
assert_raises I18n::Disabled do
I18n.l(Time.now)
end
end
end
test "can use a lambda as an exception handler" do
begin
previous_exception_handler = I18n.exception_handler
I18n.exception_handler = Proc.new { |exception, locale, key, options| key }
assert_equal :test_proc_handler, I18n.translate(:test_proc_handler)
ensure
I18n.exception_handler = previous_exception_handler
end
end
test "can use an object responding to #call as an exception handler" do
begin
previous_exception_handler = I18n.exception_handler
I18n.exception_handler = Class.new do
def call(exception, locale, key, options); key; end
end.new
assert_equal :test_proc_handler, I18n.translate(:test_proc_handler)
ensure
I18n.exception_handler = previous_exception_handler
end
end
test "I18n.with_locale temporarily sets the given locale" do
store_translations(:en, :foo => 'Foo in :en')
store_translations(:de, :foo => 'Foo in :de')
store_translations(:pl, :foo => 'Foo in :pl')
I18n.with_locale { assert_equal [:en, 'Foo in :en'], [I18n.locale, I18n.t(:foo)] }
I18n.with_locale(:de) { assert_equal [:de, 'Foo in :de'], [I18n.locale, I18n.t(:foo)] }
I18n.with_locale(:pl) { assert_equal [:pl, 'Foo in :pl'], [I18n.locale, I18n.t(:foo)] }
I18n.with_locale(:en) { assert_equal [:en, 'Foo in :en'], [I18n.locale, I18n.t(:foo)] }
assert_equal I18n.default_locale, I18n.locale
end
test "I18n.with_locale resets the locale in case of errors" do
assert_raises(I18n::ArgumentError) { I18n.with_locale(:pl) { raise I18n::ArgumentError } }
assert_equal I18n.default_locale, I18n.locale
end
test "I18n.transliterate handles I18n::ArgumentError exception" do
I18n::Backend::Transliterator.stubs(:get).raises(I18n::ArgumentError)
I18n.exception_handler.expects(:call).raises(I18n::ArgumentError)
assert_raises(I18n::ArgumentError) {
I18n.transliterate("ąćó")
}
end
test "I18n.transliterate raises I18n::ArgumentError exception" do
I18n::Backend::Transliterator.stubs(:get).raises(I18n::ArgumentError)
I18n.exception_handler.expects(:call).never
assert_raises(I18n::ArgumentError) {
I18n.transliterate("ąćó", :raise => true)
}
end
test "transliterate given an unavailable locale rases an I18n::InvalidLocale" do
begin
I18n.config.enforce_available_locales = true
assert_raises(I18n::InvalidLocale) { I18n.transliterate('string', :locale => 'klingon') }
ensure
I18n.config.enforce_available_locales = false
end
end
test "transliterate non-ASCII chars not in map with default replacement char" do
assert_equal "???", I18n.transliterate("日本語")
end
test "I18n.locale_available? returns true when the passed locale is available" do
I18n.available_locales = [:en, :de]
assert_equal true, I18n.locale_available?(:de)
end
test "I18n.locale_available? returns true when the passed locale is a string and is available" do
I18n.available_locales = [:en, :de]
assert_equal true, I18n.locale_available?('de')
end
test "I18n.locale_available? returns false when the passed locale is unavailable" do
assert_equal false, I18n.locale_available?(:klingon)
end
test "I18n.enforce_available_locales! raises an I18n::InvalidLocale when the passed locale is unavailable" do
begin
I18n.config.enforce_available_locales = true
assert_raises(I18n::InvalidLocale) { I18n.enforce_available_locales!(:klingon) }
ensure
I18n.config.enforce_available_locales = false
end
end
test "I18n.enforce_available_locales! does nothing when the passed locale is available" do
I18n.available_locales = [:en, :de]
begin
I18n.config.enforce_available_locales = true
assert_nothing_raised { I18n.enforce_available_locales!(:en) }
ensure
I18n.config.enforce_available_locales = false
end
end
test "I18n.enforce_available_locales config can be set to false" do
begin
I18n.config.enforce_available_locales = false
assert_equal false, I18n.config.enforce_available_locales
ensure
I18n.config.enforce_available_locales = false
end
end
test 'I18n.reload! reloads the set of locales that are enforced' do
begin
# Clear the backend that affects the available locales and somehow can remain
# set from the last running test.
# For instance, it contains enough translations to cause a false positive with
# this test when ran with --seed=50992
I18n.backend = I18n::Backend::Simple.new
assert !I18n.available_locales.include?(:de), "Available locales should not include :de at this point"
I18n.enforce_available_locales = true
assert_raises(I18n::InvalidLocale) { I18n.default_locale = :de }
assert_raises(I18n::InvalidLocale) { I18n.locale = :de }
store_translations(:de, :foo => 'Foo in :de')
assert_raises(I18n::InvalidLocale) { I18n.default_locale = :de }
assert_raises(I18n::InvalidLocale) { I18n.locale = :de }
I18n.reload!
store_translations(:en, :foo => 'Foo in :en')
store_translations(:de, :foo => 'Foo in :de')
store_translations(:pl, :foo => 'Foo in :pl')
assert I18n.available_locales.include?(:de), ":de should now be allowed"
assert I18n.available_locales.include?(:en), ":en should now be allowed"
assert I18n.available_locales.include?(:pl), ":pl should now be allowed"
assert_nothing_raised { I18n.default_locale = I18n.locale = :en }
assert_nothing_raised { I18n.default_locale = I18n.locale = :de }
assert_nothing_raised { I18n.default_locale = I18n.locale = :pl }
ensure
I18n.enforce_available_locales = false
end
end
test "can reserve a key" do
begin
stub_const(I18n, :RESERVED_KEYS, []) do
I18n.reserve_key(:foo)
I18n.reserve_key("bar")
assert I18n::RESERVED_KEYS.include?(:foo)
assert I18n::RESERVED_KEYS.include?(:bar)
end
ensure
I18n.instance_variable_set(:@reserved_keys_pattern, nil)
end
end
end
|