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 313 314 315 316 317 318 319 320 321 322 323 324
|
# -*- coding: utf-8 -*-
require 'testlib/helper.rb'
require 'testlib/simple.rb'
require 'testlib/gettext.rb'
require 'testlib/sgettext.rb'
require 'testlib/nsgettext.rb'
require 'testlib/pgettext.rb'
require 'testlib/npgettext.rb'
class TestGetText < Test::Unit::TestCase
def setup
ENV["LC_ALL"] = "ja_JP.UTF-8"
ENV["LANG"] = "ja_JP.UTF-8"
GetText.locale = nil
GetText::TextDomainManager.clear_caches
end
def teardown
GetText.locale = nil
end
def test_change_locale
GetText.locale = nil
bindtextdomain("test2", "locale")
test = Simple.new
assert_equal("japanese", test.test)
set_locale("fr")
assert_equal("french", test.test) # influence of previous line
assert_equal("FRENCH:ONE IS 1.", test.test_formatted_string)
set_locale("ja")
assert_equal("JAPANESE", _("LANGUAGE")) # influence of previous line
assert_equal("japanese", test.test)
end
def test_no_msgstr
bindtextdomain("test1", :path => "locale")
assert_equal("nomsgstr", _("nomsgstr"))
end
def test_empty
bindtextdomain("test1", "locale")
assert_equal("japanese", gettext("language"))
assert_equal("", gettext(""))
assert_equal("", gettext(nil))
end
def test_gettext
test = TestRubyParser.new
assert_equal("AAA", test.test_1)
assert_equal("AAA\n", test.test_2)
assert_equal("BBB\nCCC", test.test_3)
assert_equal("BBB
CCC
DDD
", test.test_4)
assert_equal("EEE", test.test_5)
assert_equal("EEEfooFFF", test.test_6)
assert_equal("GGGHHHIII", test.test_7)
end
def test_N_
assert_equal("test", N_("test"))
end
def test_sgettext
test = TestSGetText.new
assert_equal("MATCHED", test.test_1)
assert_equal("MATCHED", test.test_2)
assert_equal("AAA", test.test_3)
assert_equal("CCC", test.test_4)
assert_equal("CCC", test.test_5)
assert_equal("BBB", test.test_6)
assert_equal("B|BB", test.test_7)
assert_equal("MATCHED", test.test_8)
assert_equal("BBB", test.test_9)
end
def test_s_uses_no_seperator_when_nil_is_given
assert_equal "AAA|BBB", s_("AAA|BBB", nil)
end
def test_pgettext
GetText.locale = nil
test = TestPGetText.new
assert_equal("えーびー", test.test_1)
assert_equal("えーびー", test.test_2)
assert_equal("えーびーしー", test.test_3)
assert_equal("CCC", test.test_4)
assert_equal("しーびー", test.test_5)
assert_equal("びー", test.test_6)
GetText.locale = "en"
test = TestPGetText.new
assert_equal("BBB", test.test_1)
assert_equal("BBB", test.test_2)
assert_equal("CCC", test.test_3)
assert_equal("CCC", test.test_4)
assert_equal("BBB", test.test_5)
assert_equal("BBB", test.test_6)
end
def test_npgettext
GetText.locale = nil
test = TestNPGetText.new
assert_equal(["一つの本", "%{num}の本たち"], test.test_1)
assert_equal(["一つの本", "%{num}の本たち"], test.test_2)
assert_equal(["一つのハードカバー本", "%{num}のハードカバー本たち"], test.test_3)
assert_equal(["マガジンを1冊持ってます。", "マガジンたちを%{num}冊持ってます。"], test.test_4)
assert_equal(["a picture", "%{num} pictures"], test.test_5)
end
def test_ngettext_defaults_to_1_when_number_is_missing
assert_equal n_("aaa","aaa2",1), "aaa"
end
def test_ngettext_format_error
assert_raise(ArgumentError) { n_("aaa", "aaa2") }
end
def test_nsgettext
GetText.locale = nil
test = TestNSGetText.new
assert_equal(["single", "plural"], test.test_1)
assert_equal(["single", "plural"], test.test_2)
assert_equal(["AAA", "BBB"], test.test_3)
assert_equal(["CCC", "DDD"], test.test_4)
assert_equal(["CCC", "DDD"], test.test_5)
assert_equal(["BBB", "CCC"], test.test_6)
assert_equal(["B|BB", "CCC"], test.test_7)
assert_equal(["single", "plural"], test.test_8)
assert_equal(["BBB", "DDD"], test.test_9)
end
def test_plural
GetText.locale = nil
bindtextdomain("plural", :path => "locale")
assert_equal("all", n_("one", "two", 0))
assert_equal("all", n_("one", "two", 1))
assert_equal("all", n_("one", "two", 2))
set_locale("da")
assert_equal("da_plural", n_("one", "two", 0))
assert_equal("da_one", n_("one", "two", 1))
assert_equal("da_plural", n_("one", "two", 2))
set_locale("fr")
assert_equal("fr_one", ngettext("one", "two", 0))
assert_equal("fr_one", ngettext("one", "two", 1))
assert_equal("fr_plural", ngettext("one", "two", 2))
set_locale("la")
assert_equal("la_one", ngettext("one", "two", 21))
assert_equal("la_one", ngettext("one", "two", 1))
assert_equal("la_plural", ngettext("one", "two", 2))
assert_equal("la_zero", ngettext("one", "two", 0))
set_locale("ir")
assert_equal("ir_one", ngettext("one", "two", 1))
assert_equal("ir_two", ngettext("one", "two", 2))
assert_equal("ir_plural", ngettext("one", "two", 3))
assert_equal("ir_plural", ngettext("one", "two", 0))
set_locale("li")
assert_equal("li_one", ngettext("one", "two", 1))
assert_equal("li_two", ngettext("one", "two", 22))
assert_equal("li_three", ngettext("one", "two", 11))
set_locale("cr")
assert_equal("cr_one", ngettext("one", "two", 1))
assert_equal("cr_two", ngettext("one", "two", 2))
assert_equal("cr_three", ngettext("one", "two", 5))
set_locale("po")
assert_equal("po_one", ngettext("one", "two", 1))
assert_equal("po_two", ngettext("one", "two", 2))
assert_equal("po_three", ngettext("one", "two", 5))
set_locale("sl")
assert_equal("sl_one", ngettext("one", "two", 1))
assert_equal("sl_two", ngettext("one", "two", 2))
assert_equal("sl_three", ngettext("one", "two", 3))
assert_equal("sl_three", ngettext("one", "two", 4))
assert_equal("sl_four", ngettext("one", "two", 5))
end
def test_plural_format_invalid
bindtextdomain("plural_error", :path => "locale")
#If it defines msgstr[0] only, msgstr[0] is used everytime.
assert_equal("a", n_("first", "second", 0))
assert_equal("a", n_("first", "second", 1))
assert_equal("a", n_("first", "second", 2))
# Use default(plural = 0)
set_locale("fr")
assert_equal("fr_first", n_("first", "second", 0))
assert_equal("fr_first", n_("first", "second", 1))
assert_equal("fr_first", n_("first", "second", 2))
setlocale("da") # Invalid Plural-Forms.
assert_equal("da_first", n_("first", "second", 0))
assert_equal("da_first", n_("first", "second", 1))
assert_equal("da_first", n_("first", "second", 2))
setlocale("la") # wrong number of msgstr.
assert_equal("la_first", n_("first", "second", 0))
assert_equal("la_first", n_("first", "second", 1))
assert_equal("la_first", n_("first", "second", 2))
setlocale("li") # Invalid Plural-Forms: nplurals is set, but wrong plural=.
assert_equal("li_first", n_("first", "second", 0))
assert_equal("li_first", n_("first", "second", 1))
assert_equal("li_first", n_("first", "second", 2))
assert_equal("li_one", n_("one", "two", 0))
assert_equal("li_one", n_("one", "two", 1))
assert_equal("li_one", n_("one", "two", 2))
end
def test_plural_array
bindtextdomain("plural", :path => "locale")
set_locale "da"
assert_equal("da_plural", n_(["one", "two"], 0))
assert_equal("da_one", n_(["one", "two"], 1))
assert_equal("da_plural", n_(["one", "two"], 2))
end
def test_plural_with_single
bindtextdomain("plural", :path => "locale")
assert_equal("hitotsu", _("single"))
assert_equal("hitotsu", n_("single", "plural", 1))
assert_equal("hitotsu", n_("single", "plural", 2))
assert_equal("all", n_("one", "two", 1))
assert_equal("all", n_("one", "two", 2))
assert_equal("all", _("one"))
bindtextdomain("plural", :path => "locale")
set_locale "fr"
assert_equal("fr_hitotsu", _("single"))
assert_equal("fr_hitotsu", n_("single", "plural", 1))
assert_equal("fr_fukusu", n_("single", "plural", 2))
assert_equal("fr_one", n_("one", "two", 1))
assert_equal("fr_plural", n_("one", "two", 2))
assert_equal("fr_one", _("one"))
# assert_equal("fr_hitotsu", n_("single", "not match", 1))
# assert_equal("fr_fukusu", n_("single", "not match", 2))
end
def test_Nn_
GetText.locale = "da"
bindtextdomain("plural", :path => "locale")
assert_equal(["one", "two"], Nn_("one", "two"))
end
def test_setlocale
bindtextdomain("test1", :path => "locale")
assert_equal("japanese", _("language"))
set_locale("en")
assert_equal("language", _("language"))
set_locale("fr")
assert_equal("french", _("language"))
set_locale(nil)
Locale.set "en"
assert_equal("language", _("language"))
Locale.set "ja"
assert_equal("japanese", _("language"))
# Confirm to set Locale::Object.
loc = Locale::Tag::Posix.parse("ja_JP.UTF-8")
assert_equal(loc, GetText.locale = loc)
assert_equal(Locale::Tag::Posix, GetText.locale.class)
end
def test_restrict_locale
bindtextdomain("test1", :path => "locale")
Locale.set_app_language_tags("ja", "en")
Locale.set_current "fr"
assert_equal("language", _("language"))
Locale.set_current "en"
assert_equal("language", _("language"))
Locale.set_current "ja"
assert_equal("japanese", _("language"))
Locale.set_app_language_tags(nil)
end
@anonymous_module = Module.new
class @anonymous_module::I
bindtextdomain("test1", :path => "locale")
def self.test
_("language")
end
def test2
_("language")
end
end
def test_anonymous_module
anonymous_module = self.class.instance_variable_get(:@anonymous_module)
assert_equal("japanese", anonymous_module::I.test)
assert_equal("japanese", anonymous_module::I.new.test2)
end
def test_frozen
GetText.bindtextdomain("test1", :path => "locale")
assert(GetText._("language").frozen?)
end
def test_safe_mode
Thread.start{
$SAFE = 1
GetText.bindtextdomain("test1", :path => "locale")
_("language")
}.join
end
end
|