summaryrefslogtreecommitdiff
path: root/test/irb/test_locale.rb
blob: 930a38834c1ab6f9102d7a525e61be6950c11c20 (plain)
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
require "irb"
require "stringio"

require_relative "helper"

module TestIRB
  class LocaleTestCase < TestCase
    def test_initialize_with_en
      locale = IRB::Locale.new("en_US.UTF-8")

      assert_equal("en", locale.lang)
      assert_equal("US", locale.territory)
      assert_equal("UTF-8", locale.encoding.name)
      assert_equal(nil, locale.modifier)
    end

    def test_initialize_with_ja
      locale = IRB::Locale.new("ja_JP.UTF-8")

      assert_equal("ja", locale.lang)
      assert_equal("JP", locale.territory)
      assert_equal("UTF-8", locale.encoding.name)
      assert_equal(nil, locale.modifier)
    end

    def test_initialize_with_legacy_ja_encoding_ujis
      original_stderr = $stderr
      $stderr = StringIO.new

      locale = IRB::Locale.new("ja_JP.ujis")

      assert_equal("ja", locale.lang)
      assert_equal("JP", locale.territory)
      assert_equal(Encoding::EUC_JP, locale.encoding)
      assert_equal(nil, locale.modifier)

      assert_include $stderr.string, "ja_JP.ujis is obsolete. use ja_JP.EUC-JP"
    ensure
      $stderr = original_stderr
    end

    def test_initialize_with_legacy_ja_encoding_euc
      original_stderr = $stderr
      $stderr = StringIO.new

      locale = IRB::Locale.new("ja_JP.euc")

      assert_equal("ja", locale.lang)
      assert_equal("JP", locale.territory)
      assert_equal(Encoding::EUC_JP, locale.encoding)
      assert_equal(nil, locale.modifier)

      assert_include $stderr.string, "ja_JP.euc is obsolete. use ja_JP.EUC-JP"
    ensure
      $stderr = original_stderr
    end

    %w(IRB_LANG LC_MESSAGES LC_ALL LANG).each do |env_var|
      define_method "test_initialize_with_#{env_var.downcase}" do
        original_values = {
          "IRB_LANG" => ENV["IRB_LANG"],
          "LC_MESSAGES" => ENV["LC_MESSAGES"],
          "LC_ALL" => ENV["LC_ALL"],
          "LANG" => ENV["LANG"],
        }

        ENV["IRB_LANG"] = ENV["LC_MESSAGES"] = ENV["LC_ALL"] = ENV["LANG"] = nil
        ENV[env_var] = "zh_TW.UTF-8"

        locale = IRB::Locale.new

        assert_equal("zh", locale.lang)
        assert_equal("TW", locale.territory)
        assert_equal("UTF-8", locale.encoding.name)
        assert_equal(nil, locale.modifier)
      ensure
        original_values.each do |key, value|
          ENV[key] = value
        end
      end
    end

    def test_load
      # reset Locale's internal cache
      IRB::Locale.class_variable_set(:@@loaded, [])
      # Because error.rb files define the same class, loading them causes method redefinition warnings.
      original_verbose = $VERBOSE
      $VERBOSE = nil

      jp_local = IRB::Locale.new("ja_JP.UTF-8")
      jp_local.load("irb/error.rb")
      msg = IRB::CantReturnToNormalMode.new.message
      assert_equal("Normalモードに戻れません.", msg)

      # reset Locale's internal cache
      IRB::Locale.class_variable_set(:@@loaded, [])

      en_local = IRB::Locale.new("en_US.UTF-8")
      en_local.load("irb/error.rb")
      msg = IRB::CantReturnToNormalMode.new.message
      assert_equal("Can't return to normal mode.", msg)
    ensure
      # before turning warnings back on, load the error.rb file again to avoid warnings in other tests
      IRB::Locale.new.load("irb/error.rb")
      $VERBOSE = original_verbose
    end

    def test_find
      jp_local = IRB::Locale.new("ja_JP.UTF-8")
      path = jp_local.find("irb/error.rb")
      assert_include(path, "/lib/irb/lc/ja/error.rb")

      en_local = IRB::Locale.new("en_US.UTF-8")
      path = en_local.find("irb/error.rb")
      assert_include(path, "/lib/irb/lc/error.rb")
    end
  end
end