File: test_default_locale.rb

package info (click to toggle)
ruby-faker 3.6.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 11,596 kB
  • sloc: ruby: 20,656; sh: 6; makefile: 6
file content (99 lines) | stat: -rw-r--r-- 2,478 bytes parent folder | download
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
# 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