-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Test default locale and update README Co-authored-by: Thiago Araujo <[email protected]>
- Loading branch information
Showing
3 changed files
with
118 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative 'test_helper' | ||
|
||
class TestDefaultLocale < Test::Unit::TestCase | ||
def test_setting_default_locale | ||
# if locale is not set, fallback is :en | ||
assert_equal :en, Faker::Config.locale | ||
|
||
# locale can be updated initially | ||
# and it becomes the default value | ||
# for new threads | ||
Faker::Config.default_locale = :pt | ||
|
||
assert_equal :pt, Faker::Config.locale | ||
|
||
t1 = Thread.new do | ||
# child thread has initial locale equal to | ||
# latest locale set on main thread | ||
# instead of the fallback value | ||
assert_equal :pt, Faker::Config.locale | ||
|
||
# child thread can set its own locale | ||
Faker::Config.locale = :es | ||
|
||
assert_equal :es, Faker::Config.locale | ||
end | ||
|
||
t1.join | ||
|
||
# child thread won't change locale of other threads | ||
assert_equal :pt, Faker::Config.locale | ||
|
||
t2 = Thread.new do | ||
# initial default locale is copied over to new thread | ||
assert_equal :pt, Faker::Config.locale | ||
|
||
Faker::Config.locale = :it | ||
|
||
assert_equal :it, Faker::Config.locale | ||
end | ||
|
||
t2.join | ||
|
||
assert_equal :pt, Faker::Config.locale | ||
|
||
# setting this to reset the default locale for all tests | ||
Faker::Config.default_locale = nil | ||
|
||
assert_equal :en, Faker::Config.locale | ||
end | ||
|
||
def test_setting_default_locale_on_child_thread | ||
# if locale is not set, fallback is :en | ||
assert_equal :en, Faker::Config.locale | ||
|
||
# locale can be updated initially | ||
# and it becomes the default value | ||
# for new threads | ||
Faker::Config.default_locale = :pt | ||
|
||
assert_equal :pt, Faker::Config.locale | ||
|
||
t1 = Thread.new do | ||
# child thread has initial locale equal to | ||
# latest locale set on main thread | ||
# instead of the fallback value | ||
assert_equal :pt, Faker::Config.locale | ||
|
||
# child thread can set the default locale | ||
Faker::Config.default_locale = :es | ||
|
||
assert_equal :es, Faker::Config.locale | ||
end | ||
|
||
t1.join | ||
|
||
# all threads now will have the same default | ||
assert_equal :es, Faker::Config.locale | ||
|
||
t2 = Thread.new do | ||
# initial default locale is copied over to new thread | ||
assert_equal :es, Faker::Config.locale | ||
|
||
Faker::Config.locale = :it | ||
|
||
assert_equal :it, Faker::Config.locale | ||
end | ||
|
||
t2.join | ||
|
||
assert_equal :es, Faker::Config.locale | ||
|
||
# setting this to reset the default locale for all tests | ||
Faker::Config.default_locale = nil | ||
|
||
assert_equal :en, Faker::Config.locale | ||
end | ||
end |