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 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
|
# Copyright (C) 2012-2023 Sutou Kouhei <kou@clear-code.com>
# Copyright (C) 2012 Haruka Yoshihara <yoshihara@clear-code.com>
#
# 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 "gettext/po_parser"
class TestPOParser < Test::Unit::TestCase
private
def create_po_file(content)
po_file = Tempfile.new("hello.po")
po_file.print(content)
po_file.close
po_file
end
def parse_po_file(po_file, parsed_entries)
parser = GetText::POParser.new
parser.parse_file(po_file.path, parsed_entries)
end
class TestMsgStr < self
class TestEmpty < self
def test_normal
po_file = create_po_file(<<-EOP)
msgid "Hello"
msgstr ""
EOP
messages = parse_po_file(po_file, MO.new)
assert_equal(nil, messages["Hello"])
end
def test_plural
po_file = create_po_file(<<-EOP)
msgid "He"
msgid_plural "They"
msgstr[0] ""
msgstr[1] ""
EOP
messages = parse_po_file(po_file, MO.new)
assert_true(messages.has_key?("He\000They"))
assert_equal(nil, messages["He\000They"])
end
end
end
class TestPO < self
include Helper::Warning
def test_msgid_escape
po_file = create_po_file(<<-'EOP')
msgid "\\\\r \\r \t \r \n"
msgstr "\\\\r \\r \t \r \n"
EOP
entries = parse_po_file(po_file)
assert_equal("\\\\r \\r \t \r \n",
entries["\\\\r \\r \t \r \n"].msgstr)
end
def test_msgstr
po_file = create_po_file(<<-EOP)
# This is the comment.
#: file.rb:10
msgid "hello"
msgstr "bonjour"
EOP
entries = parse_po_file(po_file)
assert_true(entries.has_key?(nil, "hello"))
assert_equal("bonjour", entries["hello"].msgstr)
end
class TestReferences < self
def test_single
po_file = create_po_file(<<-EOP)
# This is the comment.
#: file.rb:10
msgid "hello"
msgstr "bonjour"
EOP
entries = parse_po_file(po_file)
assert_true(entries.has_key?(nil, "hello"))
assert_equal(["file.rb:10"], entries["hello"].references)
end
def test_per_line
po_file = create_po_file(<<-PO)
# This is the comment.
#: file.rb:10
#: file.rb:20
msgid "hello"
msgstr "bonjour"
PO
entries = parse_po_file(po_file)
assert_true(entries.has_key?(nil, "hello"))
assert_equal(["file.rb:10", "file.rb:20"],
entries["hello"].references)
end
def test_same_line
po_file = create_po_file(<<-PO)
# This is the comment.
#: file.rb:10 file.rb:20
msgid "hello"
msgstr "bonjour"
PO
entries = parse_po_file(po_file)
assert_true(entries.has_key?(nil, "hello"))
assert_equal(["file.rb:10", "file.rb:20"],
entries["hello"].references)
end
end
def test_translator_comment
po_file = create_po_file(<<-EOP)
# This is the translator comment.
msgid "hello"
msgstr "bonjour"
EOP
entries = parse_po_file(po_file)
assert_true(entries.has_key?(nil, "hello"))
entry = entries["hello"]
assert_equal("This is the translator comment.", entry.translator_comment)
end
def test_extracted_comment
po_file = create_po_file(<<-EOP)
#. This is the extracted comment.
msgid "hello"
msgstr "bonjour"
EOP
entries = parse_po_file(po_file)
assert_true(entries.has_key?(nil, "hello"))
entry = entries["hello"]
assert_equal("This is the extracted comment.", entry.extracted_comment)
end
def test_flag
po_file = create_po_file(<<-EOP)
#, flag
msgid "hello"
msgstr "bonjour"
EOP
entries = parse_po_file(po_file)
assert_true(entries.has_key?(nil, "hello"))
assert_equal("flag", entries["hello"].flag)
end
def test_previous
po_file = create_po_file(<<-EOP)
#| msgctxt Normal
#| msgid He
#| msgid_plural Them
msgid "he"
msgid_plural "them"
msgstr[0] "il"
msgstr[1] "ils"
EOP
expected_previous = "msgctxt Normal\n" +
"msgid He\n" +
"msgid_plural Them"
entries = parse_po_file(po_file)
assert_true(entries.has_key?(nil, "he"))
assert_equal(expected_previous, entries["he"].previous)
end
def test_msgid_plural
po_file = create_po_file(<<-EOP)
# This is the comment.
#: file.rb:10
msgid "he"
msgid_plural "them"
msgstr[0] "il"
msgstr[1] "ils"
EOP
entries = parse_po_file(po_file)
assert_true(entries.has_key?(nil, "he"))
assert_equal("them", entries["he"].msgid_plural)
assert_equal("il\000ils", entries["he"].msgstr)
end
def test_msgctxt
po_file = create_po_file(<<-EOP)
# This is the comment.
#: file.rb:10
msgctxt "pronoun"
msgid "he"
msgstr "il"
EOP
entries = parse_po_file(po_file)
assert_true(entries.has_key?("pronoun", "he"))
assert_equal("pronoun", entries["pronoun", "he"].msgctxt)
end
def test_msgctxt_with_msgid_plural
po_file = create_po_file(<<-EOP)
# This is the comment.
#: file.rb:10
msgctxt "pronoun"
msgid "he"
msgid_plural "them"
msgstr[0] "il"
msgstr[1] "ils"
EOP
entries = parse_po_file(po_file)
assert_true(entries.has_key?("pronoun", "he"))
assert_equal("pronoun", entries["pronoun", "he"].msgctxt)
assert_equal("them", entries["pronoun", "he"].msgid_plural)
assert_equal("il\000ils", entries["pronoun", "he"].msgstr)
end
def test_fuzzy
po_file = create_po_file(<<-EOP)
#, fuzzy
#: file.rb:10
msgid "hello"
msgstr "bonjour"
EOP
entries = suppress_warning do
parse_po_file(po_file, :ignore_fuzzy => false)
end
assert_true(entries.has_key?("hello"))
assert_equal("fuzzy", entries["hello"].flag)
end
def test_fuzzy_ignore
po_file = create_po_file(<<-PO)
#, fuzzy
#: file.rb:10
msgid "hello"
msgstr "bonjour"
#, c-format, fuzzy , no-sh-format
#: file.rb:11
msgid "world"
msgstr "monde"
# This is not fuzzy
msgid "non-fuzzy"
msgstr "non-fuzzy string"
PO
entries = suppress_warning do
parse_po_file(po_file)
end
actual = entries.collect do |entry|
[
entry.msgid,
entry.msgstr,
entry.flags,
]
end
assert_equal([
["non-fuzzy", "non-fuzzy string", []],
],
actual)
end
private
def parse_po_file(po_file, options={:ignore_fuzzy => true})
ignore_fuzzy = options[:ignore_fuzzy]
parser = GetText::POParser.new
parser.ignore_fuzzy = ignore_fuzzy
parser.parse_file(po_file.path, PO.new)
end
end
class TestFuzzy < self
def setup
@po = <<-EOP
#, fuzzy
msgid "Hello"
msgstr "Bonjour"
EOP
@po_file = Tempfile.new("hello.po")
@po_file.print(@po)
@po_file.close
end
private
def parse
parser = GetText::POParser.new
class << parser
def _(message_id)
message_id
end
end
messages = MO.new
yield parser
parser.parse_file(@po_file.path, messages)
messages
end
class TestIgnore < self
def test_report_warning
mock($stderr).print("Warning: fuzzy message was ignored.\n")
mock($stderr).print(" #{@po_file.path}: msgid 'Hello'\n")
messages = parse do |parser|
parser.ignore_fuzzy = true
parser.report_warning = true
end
assert_nil(messages["Hello"])
end
def test_not_report_warning
dont_allow($stderr).print("Warning: fuzzy message was ignored.\n")
dont_allow($stderr).print(" #{@po_file.path}: msgid 'Hello'\n")
messages = parse do |parser|
parser.ignore_fuzzy = true
parser.report_warning = false
end
assert_nil(messages["Hello"])
end
end
class TestNotIgnore < self
def test_report_warning
mock($stderr).print("Warning: fuzzy message was used.\n")
mock($stderr).print(" #{@po_file.path}: msgid 'Hello'\n")
messages = parse do |parser|
parser.ignore_fuzzy = false
parser.report_warning = true
end
assert_equal("Bonjour", messages["Hello"])
end
def test_not_report_warning
dont_allow($stderr).print("Warning: fuzzy message was used.\n")
dont_allow($stderr).print(" #{@po_file.path}: msgid 'Hello'\n")
messages = parse do |parser|
parser.ignore_fuzzy = false
parser.report_warning = false
end
assert_equal("Bonjour", messages["Hello"])
end
end
end
class TestHeader < self
class TestEncoding < self
def test_known
assert_equal(Encoding::EUC_JP, detect_encoding("EUC-JP"))
end
def test_unknown
assert_equal(Encoding.default_external, detect_encoding("CHARSET"))
end
def test_fuzzy
assert_equal(Encoding::EUC_JP,
detect_encoding("EUC-JP", :fuzzy => true))
end
private
def detect_encoding(encoding, options={})
comments = []
comments << "#, fuzzy" if options[:fuzzy]
po_file = create_po_file(<<-PO)
#{comments.join("\n")}
msgid ""
msgstr ""
"Content-Type: text/plain; charset=#{encoding}\\n"
PO
parser = GetText::POParser.new
parser.send(:detect_file_encoding, po_file.path)
end
end
end
end
|