/
test_locale.rb
96 lines (80 loc) · 3.06 KB
/
test_locale.rb
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
# frozen_string_literal: true
require_relative 'test_helper'
LoadedYaml = %w[en en-BORK].each_with_object({}) do |locale, h|
h[locale] = YAML.load_file(File.expand_path(File.dirname(__FILE__) + "/../lib/locales/#{locale}.yml"))[locale]['faker']
end
class TestLocale < Test::Unit::TestCase
def teardown
Faker::Config.locale = nil
end
def test_locale_separate_from_i18n
I18n.locale = :en
Faker::Config.locale = :de
assert Faker::PhoneNumber.phone_number.match(/\(0\d+\) \d+|\d+-\d+/)
assert Faker::Address.street_name.match(//)
Faker::Config.locale = :ru
assert Faker::Internet.domain_name.match(/([\da-z\.-]+)\.([a-z\.]{2,6})/)
end
def test_configured_locale_translation
Faker::Config.locale = 'en-BORK'
assert_equal Faker::Base.translate('faker.lorem.words').first, LoadedYaml['en-BORK']['lorem']['words'].first
end
def test_locale_override_when_calling_translate
Faker::Config.locale = 'en-BORK'
assert_equal Faker::Base.translate('faker.separator', locale: :en), LoadedYaml['en']['separator']
end
def test_translation_fallback
Faker::Config.locale = 'en-BORK'
assert_nil LoadedYaml['en-BORK']['name']
assert_equal Faker::Base.translate('faker.separator'), LoadedYaml['en']['separator']
end
def test_translation_fallback_without_en_in_available_locales
I18n.available_locales -= [:en]
Faker::Config.locale = 'en-BORK'
assert_nil LoadedYaml['en-BORK']['name']
assert_equal Faker::Base.translate('faker.separator'), LoadedYaml['en']['separator']
ensure
I18n.available_locales += [:en]
end
def test_with_locale_does_not_fail_without_the_locale_in_available_locales
I18n.available_locales -= [:en]
Faker::Base.with_locale(:en) do
assert_equal Faker::Base.translate('faker.separator'), LoadedYaml['en']['separator']
end
ensure
I18n.available_locales += [:en]
end
def test_no_en_in_available_locales
I18n.available_locales -= [:en]
assert_kind_of String, Faker::Address.country
ensure
I18n.available_locales += [:en]
end
def test_with_locale_changes_locale_temporarily
Faker::Config.locale = 'en-BORK'
I18n.with_locale(:en) do
assert_equal Faker::Base.translate('faker.separator'), LoadedYaml['en']['separator']
end
assert_equal 'en-BORK', Faker::Config.locale
end
def test_regex
Faker::Config.locale = 'en-GB'
re = /[A-PR-UWYZ]([A-HK-Y][0-9][ABEHMNPRVWXY0-9]?|[0-9][ABCDEFGHJKPSTUW0-9]?) [0-9][ABD-HJLNP-UW-Z]{2}/
assert re.match(result = Faker::Address.postcode), "#{result} didn't match #{re}"
end
def test_available_locales
assert I18n.locale_available?('en-GB')
end
def test_load_i18n
Faker.instance_variable_set :@i18n_loaded, nil
available_locales_was = I18n.available_locales
I18n.available_locales = %i(en ja id)
Faker.load_i18n
I18n.eager_load!
assert Faker.instance_variable_get(:@i18n_loaded)
assert_equal %i(en id ja), I18n.backend.translations.keys.sort
ensure
I18n.available_locales = available_locales_was
Faker.instance_variable_set :@i18n_loaded, nil
end
end