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
|
# -*- coding: utf-8 -*-
#
# Copyright (C) 2012 Kouhei Sutou <kou@clear-code.com>
# Copyright (C) 2012 Hleb Valoshka
# Copyright (C) 2009-2010 Masao Mutoh
#
# License: Ruby's or LGPL
#
# This library is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
require 'locale'
require 'test/unit'
class TestDetectGeneral < Test::Unit::TestCase
def setup
Locale.init
Locale.clear_all
ENV["LC_ALL"] = nil
ENV["LC_CTYPE"] = nil
ENV["LC_MESSAGES"] = nil
ENV["LANG"] = nil
ENV["LANGUAGE"] = nil
end
def test_lc_all
ENV["LC_ALL"] = "ja_JP.eucJP"
ENV["LC_CTYPE"] = "fr_FR.ISO-8859-1" #Ignored.
ENV["LC_MESSAGES"] = "zh_CN.UTF-8" #Ignored.
ENV["LANG"] = "ko_KR.UTF-8" #Ignored.
ENV["LANGUAGE"] = nil
lang = Locale.current[0]
assert_equal Locale::Tag::Posix, lang.class
assert_equal "ja", lang.language
assert_equal "JP", lang.region
assert_equal "eucJP", lang.charset
assert_equal Locale::Tag::Posix.new("ja", "JP", "eucJP"), lang
assert_equal "eucJP", Locale.charset
end
def test_lc_messages
ENV["LC_ALL"] = nil
ENV["LC_CTYPE"] = nil
ENV["LC_MESSAGES"] = "ja_JP.eucJP"
ENV["LANG"] = "ko_KR.UTF-8" #Ignored except charset.
ENV["LANGUAGE"] = nil
lang = Locale.current[0]
assert_equal Locale::Tag::Posix, lang.class
assert_equal "ja", lang.language
assert_equal "JP", lang.region
assert_equal "eucJP", lang.charset
assert_equal Locale::Tag::Posix.new("ja", "JP", "eucJP"), lang
assert_equal "UTF-8", Locale.charset
end
def test_lc_messages_with_lc_ctype
ENV["LC_ALL"] = nil
ENV["LC_CTYPE"] = "fr_FR.ISO-8859-1"
ENV["LC_MESSAGES"] = "ja_JP.eucJP"
ENV["LANG"] = "ko_KR.UTF-8" #Ignored.
ENV["LANGUAGE"] = nil
lang = Locale.current[0]
assert_equal Locale::Tag::Posix, lang.class
assert_equal "ja", lang.language
assert_equal "JP", lang.region
assert_equal "ISO-8859-1", lang.charset
assert_equal Locale::Tag::Posix.new("ja", "JP", "ISO-8859-1"), lang
assert_equal "ISO-8859-1", Locale.charset
end
def test_lang
ENV["LC_ALL"] = nil
ENV["LC_CTYPE"] = nil
ENV["LC_MESSAGES"] = nil
ENV["LANG"] = "ja_JP.eucJP"
ENV["LANGUAGE"] = nil
lang = Locale.current[0]
assert_equal Locale::Tag::Posix, lang.class
assert_equal "ja", lang.language
assert_equal "JP", lang.region
assert_equal "eucJP", lang.charset
assert_equal Locale::Tag::Posix.new("ja", "JP", "eucJP"), lang
assert_equal "eucJP", Locale.charset
end
def test_lang_with_ctype
ENV["LC_ALL"] = nil
ENV["LC_CTYPE"] = "fr_FR.ISO-8859-1"
ENV["LC_MESSAGES"] = nil
ENV["LANG"] = "ja_JP.eucJP"
ENV["LANGUAGE"] = nil
lang = Locale.current[0]
assert_equal Locale::Tag::Posix, lang.class
assert_equal "ja", lang.language
assert_equal "JP", lang.region
assert_equal "ISO-8859-1", lang.charset
assert_equal Locale::Tag::Posix.new("ja", "JP", "ISO-8859-1"), lang
assert_equal "ISO-8859-1", Locale.charset
end
def test_lang_complex
ENV["LC_ALL"] = "zh_CN.UTF-8" # Ignored.
ENV["LC_CTYPE"] = "fr_FR.ISO-8859-1" #Ingored.
ENV["LC_MESSAGES"] = "ko_KR.UTF-8" #Ingored.
ENV["LANG"] = "en_US.UTF-8" # Ignored.
ENV["LANGUAGE"] ="ja_JP.eucJP:zh_CN.UTF-8"
lang = Locale.current[0]
assert_equal Locale::Tag::Posix, lang.class
assert_equal "ja", lang.language
assert_equal "JP", lang.region
assert_equal "eucJP", lang.charset
assert_equal Locale::Tag::Posix.new("ja", "JP", "eucJP"), lang
# Use the LANG value (locale charmap don't use LANGUAGE)
assert_equal "UTF-8", Locale.charset
end
def test_language
ENV["LC_ALL"] = "ja_JP.Shift_JIS"
ENV["LANGUAGE"] = "zh_CN.UTF-8:ja_JP"
tags = Locale.current
assert_equal Locale::Tag::Posix, tags[0].class
assert_equal Locale::Tag::Posix, tags[1].class
assert_equal "zh", tags.language
assert_equal "CN", tags.region
assert_equal "UTF-8", tags.charset
assert_equal "zh", tags[0].language
assert_equal "CN", tags[0].region
assert_equal "UTF-8", tags[0].charset
assert_equal "ja", tags[1].language
assert_equal "JP", tags[1].region
assert_equal nil, tags[1].charset
assert_equal Locale::TagList.new([Locale::Tag::Posix.new("zh", "CN", "UTF-8"),
Locale::Tag::Posix.new("ja", "JP")]), tags
assert_equal "Shift_JIS", Locale.charset
end
def test_language_strip
ENV["LC_ALL"] = "ja_JP.Shift_JIS"
ENV["LANGUAGE"] = nil
tags = Locale.current
assert_equal 1, tags.size
assert_equal Locale::Tag::Posix, tags[0].class
assert_equal "ja", tags.language
assert_equal "ja", tags[0].language
Locale.clear
ENV["LANGUAGE"] = ""
tags = Locale.current
assert_equal 1, tags.size
assert_equal Locale::Tag::Posix, tags[0].class
assert_equal "ja", tags.language
assert_equal "ja", tags[0].language
Locale.clear
ENV["LANGUAGE"] = "zh_CN.UTF-8:ja_JP"
tags = Locale.current
assert_equal 2, tags.size
assert_equal Locale::Tag::Posix, tags[0].class
assert_equal Locale::Tag::Posix, tags[1].class
assert_equal "zh", tags.language
assert_equal "zh", tags[0].language
assert_equal "ja", tags[1].language
end
def test_no_charset
ENV["LC_ALL"] = "cs_CZ"
lang = Locale.current[0]
assert_equal Locale::Tag::Posix, lang.class
assert_equal "cs", lang.language
assert_equal "CZ", lang.region
assert_equal nil, lang.charset
assert_equal Locale::Tag::Posix.new("cs", "CZ"), lang
=begin
This test doesn't work any environment such as Ubuntu.
Because this method gets the system locale via "locale -a" command.
if /linux|bsd/ =~ RUBY_PLATFORM
assert_equal "ISO-8859-2", Locale.charset
end
=end
end
def test_default
return unless /linux|bsd/ =~ RUBY_PLATFORM
Locale.set_default("yo_NG")
assert_equal Locale::Tag.parse("yo_NG"), Locale.default
assert_equal Locale::Tag.parse("yo_NG"), Locale.current[0]
Locale.set_default(nil)
Locale.default = "fr"
assert_equal Locale::Tag.parse("fr"), Locale.default
assert_equal Locale::Tag.parse("fr"), Locale.current[0]
Locale.default = nil
end
def test_current
Locale.set_current("yo_NG")
assert_equal Locale::Tag.parse("yo_NG"), Locale.current[0]
Locale.current = "fr"
assert_equal Locale::Tag.parse("fr"), Locale.current[0]
Locale.set_default("yo_NG")
Locale.current = "fr"
assert_equal Locale::Tag.parse("yo_NG"), Locale.default
assert_equal Locale::Tag.parse("fr"), Locale.current[0]
Locale.set_default(nil)
end
def test_wrong_envs
omit("JRuby never use default") if jruby?
ENV["LC_ALL"] = nil
ENV["LANGUAGE"] = "g"
Locale.default = "de"
assert_equal Locale::Tag.parse("de"), Locale.current[0]
ENV["LC_ALL"] = "f"
ENV["LANGUAGE"] = nil
Locale.default = "fr"
assert_equal Locale::Tag.parse("fr"), Locale.current[0]
ENV["LC_ALL"] = "j"
ENV["LANGUAGE"] = nil
Locale.default = nil
assert_equal Locale::Tag.parse("en"), Locale.current[0]
end
def test_clear
ENV["LC_ALL"] = "ja_JP.Shift_JIS"
ENV["LANGUAGE"] = nil
assert_equal Locale::Tag.parse("ja_JP.Shift_JIS"), Locale.current[0]
Locale.clear
ENV["LANGUAGE"] = "zh_CN.UTF-8:ja_JP"
assert_equal Locale::Tag::Posix.parse("zh_CN.UTF-8"), Locale.current[0]
assert_equal Locale::Tag::Posix.parse("ja_JP"), Locale.current[1]
end
class TestCharset < self
def test_lc_all
ENV["LC_ALL"] = "ja_JP.eucJP"
ENV["LC_CTYPE"] = "ko_KR.eucKR" # Ignored.
ENV["LANG"] = "fr_FR.ISO-8859-1" # Ignored.
assert_equal("eucJP", Locale.charset)
end
def test_lc_ctype
ENV["LC_ALL"] = nil
ENV["LC_CTYPE"] = "ko_KR.eucKR"
ENV["LANG"] = "fr_FR.ISO-8859-1" # Ignored.
assert_equal("eucKR", Locale.charset)
end
def test_lc_messages
ENV["LC_ALL"] = nil
ENV["LC_MESSAGES"] = "ko_KR.eucKR" # Ignored.
ENV["LANG"] = "fr_FR.ISO-8859-1"
assert_equal("ISO-8859-1", Locale.charset)
end
def test_lang
ENV["LC_ALL"] = nil
ENV["LC_CTYPE"] = nil
ENV["LANG"] = "fr_FR.ISO-8859-1"
assert_equal("ISO-8859-1", Locale.charset)
end
end
private
def jruby?
RUBY_PLATFORM == "java"
end
end
|