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
|
# -*- coding: utf-8 -*-
#
# Copyright (C) 2012 Haruka Yoshihara <yoshihara@clear-code.com>
# Copyright (C) 2012-2021 Sutou Kouhei <kou@clear-code.com>
# Copyright (C) 2010 masone (Christian Felder) <ema@rh-productions.ch>
# Copyright (C) 2009 Vladimir Dobriakov <vladimir@geekq.net>
# 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 "tempfile"
require "gettext/tools/parser/ruby"
require "gettext/tools/parser/erb"
require "gettext/tools/parser/erubi"
require "gettext/tools/xgettext"
class TestGetTextParser < Test::Unit::TestCase
def setup
@xgettext = GetText::Tools::XGetText.new
end
class TestRuby < self
def test__
@xgettext.parse_options[:comment_tag] = "TRANSLATORS:"
@ary = @xgettext.parse(['fixtures/_.rb'])
assert_target 'jjj', ['fixtures/_.rb:71']
assert_target 'kkk', ['fixtures/_.rb:72']
assert_target 'lllmmm', ['fixtures/_.rb:76']
assert_target "nnn\nooo", ['fixtures/_.rb:84']
assert_target "\#", ['fixtures/_.rb:88', 'fixtures/_.rb:92']
assert_target "\\taaa'bbb\\ccc", ['fixtures/_.rb:96']
assert_target "Here document1\nHere document2\n", ['fixtures/_.rb:100']
assert_target "Francois Pinard", ['fixtures/_.rb:120'] do |t|
assert_match(/proper name/, t.extracted_comment)
assert_match(/Pronunciation/, t.extracted_comment)
end
assert_target("No TRANSLATORS comment", ["fixtures/_.rb:123"]) do |t|
assert_nil(t.comment)
end
assert_target "self explaining", ['fixtures/_.rb:128'] do |t|
assert_nil t.comment
end
assert_target "This is a # including string.", ["fixtures/_.rb:132"]
# TODO: assert_target "in_quote", ['fixtures/_.rb:118']
end
def test_N_
@ary = @xgettext.parse(['fixtures/upper_n_.rb'])
assert_target('aaa',
['fixtures/upper_n_.rb:10'])
assert_target("aaa\n",
['fixtures/upper_n_.rb:14'])
assert_target("bbb\nccc",
['fixtures/upper_n_.rb:18'])
assert_target("bbb\nccc\nddd\n",
['fixtures/upper_n_.rb:22'])
assert_target('eee',
[
'fixtures/upper_n_.rb:29',
'fixtures/upper_n_.rb:33',
])
assert_target('fff',
['fixtures/upper_n_.rb:33'])
assert_target('ggghhhiii',
['fixtures/upper_n_.rb:37'])
assert_target('a"b"c"',
['fixtures/upper_n_.rb:43'])
assert_target('d"e"f"',
['fixtures/upper_n_.rb:47'])
assert_target('jjj',
['fixtures/upper_n_.rb:51'])
assert_target('kkk',
['fixtures/upper_n_.rb:52'])
assert_target('lllmmm',
['fixtures/upper_n_.rb:56'])
assert_target("nnn\nooo",
['fixtures/upper_n_.rb:64'])
end
def test_Nn_
@ary = @xgettext.parse(['fixtures/upper_nn_.rb'])
assert_plural_target('aaa', 'aaas',
['fixtures/upper_nn_.rb:10'])
assert_plural_target("aaa\n", "aaas\n",
['fixtures/upper_nn_.rb:14'])
assert_plural_target("bbb\nccc", "bbbs\ncccs",
['fixtures/upper_nn_.rb:18'])
assert_plural_target("bbb\nccc\nddd\n", "bbbs\ncccs\nddds\n",
['fixtures/upper_nn_.rb:22'])
assert_plural_target('eee', 'eees',
[
'fixtures/upper_nn_.rb:33',
'fixtures/upper_nn_.rb:37',
])
assert_plural_target('fff', 'fffs',
['fixtures/upper_nn_.rb:37'])
assert_plural_target('ggghhhiii', 'gggshhhsiiis',
['fixtures/upper_nn_.rb:41'])
assert_plural_target('a"b"c"', 'as"bs"cs"',
['fixtures/upper_nn_.rb:50'])
assert_plural_target('d"e"f"', 'ds"es"fs"',
['fixtures/upper_nn_.rb:54'])
assert_plural_target('jjj', 'jjjs',
['fixtures/upper_nn_.rb:58'])
assert_plural_target('kkk', 'kkks',
['fixtures/upper_nn_.rb:59'])
assert_plural_target('lllmmm', 'lllsmmms',
['fixtures/upper_nn_.rb:63'])
assert_plural_target("nnn\nooo", "nnns\nooos",
['fixtures/upper_nn_.rb:71'])
end
def test_n_
@xgettext.parse_options[:comment_tag] = "TRANSLATORS:"
@ary = @xgettext.parse(['fixtures/lower_n_.rb'])
assert_plural_target("aaa", "aaa2",
['fixtures/lower_n_.rb:29'])
assert_plural_target("bbb\n", "ccc2\nccc2",
['fixtures/lower_n_.rb:33'])
assert_plural_target("ddd\nddd", "ddd2\nddd2",
['fixtures/lower_n_.rb:37'])
assert_plural_target("eee\neee\n", "eee2\neee2\n",
['fixtures/lower_n_.rb:42'])
assert_plural_target("ddd\neee\n", "ddd\neee2",
['fixtures/lower_n_.rb:48'])
assert_plural_target("fff", "fff2",
[
'fixtures/lower_n_.rb:55',
'fixtures/lower_n_.rb:59',
])
assert_plural_target("ggg", "ggg2",
['fixtures/lower_n_.rb:59'])
assert_plural_target("ggghhhiii", "jjjkkklll",
['fixtures/lower_n_.rb:63'])
assert_plural_target("a\"b\"c\"", "a\"b\"c\"2",
['fixtures/lower_n_.rb:72'])
assert_plural_target("mmmmmm", "mmm2mmm2",
['fixtures/lower_n_.rb:80'])
assert_plural_target("nnn", "nnn2", ['fixtures/lower_n_.rb:81'])
assert_plural_target("comment", "comments",
['fixtures/lower_n_.rb:97']) do |t|
assert_equal "TRANSLATORS:please provide translations for all\n the plural forms!",
t.extracted_comment
end
end
def test_p_
@xgettext.parse_options[:comment_tag] = "TRANSLATORS:"
@ary = @xgettext.parse(['fixtures/p_.rb'])
assert_target_in_context "AAA", "BBB", ["fixtures/p_.rb:29", "fixtures/p_.rb:33"]
assert_target_in_context "AAA|BBB", "CCC", ["fixtures/p_.rb:37"]
assert_target_in_context "AAA", "CCC", ["fixtures/p_.rb:41"]
assert_target_in_context "CCC", "BBB", ["fixtures/p_.rb:45"]
assert_target_in_context "program", "name", ['fixtures/p_.rb:55'] do |t|
assert_equal "TRANSLATORS:please translate 'name' in the context of 'program'.\n Hint: the translation should NOT contain the translation of 'program'.", t.extracted_comment
end
end
end
class TestErbParser < self
include Helper::Path
def test_detect_encoding
euc_file = Tempfile.new("euc-jp.rhtml")
euc_file.open
euc_file.puts("<%#-*- coding: euc-jp -*-%>")
euc_file.close
erb_source = ERB.new(File.read(euc_file.path)).src
encoding = GetText::ErbParser.new(euc_file.path).detect_encoding(erb_source)
assert_equal("EUC-JP", encoding)
end
def test_ascii
path = fixture_path("erb", "ascii.rhtml")
@ary = GetText::ErbParser.parse(path)
assert_target 'aaa', ["#{path}:8"]
assert_target "aaa\n", ["#{path}:11"]
assert_target 'bbb', ["#{path}:12"]
assert_plural_target "ccc1", "ccc2", ["#{path}:13"]
end
def test_non_ascii
path = fixture_path("erb", "non_ascii.rhtml")
@ary = GetText::ErbParser.parse(path)
assert_target('わたし', ["#{path}:12"])
end
def test_minus
path = fixture_path("erb", "minus.rhtml")
@ary = GetText::ErbParser.parse(path)
assert_target("Hello", ["#{path}:8"])
end
def test_case
path = fixture_path("erb", "case.rhtml")
@ary = GetText::ErbParser.parse(path)
if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('3.0.0')
assert_target("Hello", ["#{path}:11"])
assert_target("World", ["#{path}:14"])
else
assert_target("Hello", ["#{path}:11"])
assert_nil(@ary.detect {|elem| elem.msgid == 'World'}) # Not detected. see PR #91
end
end
end
class TestErubiParser < self
include Helper::Path
def test_detect_encoding
euc_file = Tempfile.new("euc-jp.rhtml")
euc_file.open
euc_file.puts("<%#-*- coding: euc-jp -*-%>")
euc_file.close
euc_file_content = File.read(euc_file.path)
encoding = GetText::ErubiParser.new(euc_file.path).detect_encoding(euc_file_content)
assert_equal("EUC-JP", encoding)
end
def test_ascii
path = fixture_path("erb", "ascii.rhtml")
@ary = GetText::ErubiParser.parse(path)
assert_target 'aaa', ["#{path}:8"]
assert_target "aaa\n", ["#{path}:11"]
assert_target 'bbb', ["#{path}:12"]
assert_plural_target "ccc1", "ccc2", ["#{path}:13"]
end
def test_non_ascii
path = fixture_path("erb", "non_ascii.rhtml")
@ary = GetText::ErubiParser.parse(path)
assert_target('わたし', ["#{path}:12"])
end
def test_minus
path = fixture_path("erb", "minus.rhtml")
@ary = GetText::ErubiParser.parse(path)
assert_target("Hello", ["#{path}:8"])
end
def test_case
path = fixture_path("erb", "case.rhtml")
@ary = GetText::ErubiParser.parse(path)
assert_target("Hello", ["#{path}:11"])
assert_target("World", ["#{path}:14"]) # Detected with Erubi
end
end
def test_xgettext_parse
GetText::ErbParser.init(:extnames => ['.rhtml', '.rxml'])
@ary = @xgettext.parse(['fixtures/erb/ascii.rhtml'])
assert_target 'aaa', ['fixtures/erb/ascii.rhtml:8']
assert_target "aaa\n", ['fixtures/erb/ascii.rhtml:11']
assert_target 'bbb', ['fixtures/erb/ascii.rhtml:12']
assert_plural_target "ccc1", "ccc2", ['fixtures/erb/ascii.rhtml:13']
@ary = @xgettext.parse(['fixtures/erb/ascii.rxml'])
assert_target 'aaa', ['fixtures/erb/ascii.rxml:9']
assert_target "aaa\n", ['fixtures/erb/ascii.rxml:12']
assert_target 'bbb', ['fixtures/erb/ascii.rxml:13']
assert_plural_target "ccc1", "ccc2", ['fixtures/erb/ascii.rxml:14']
@ary = @xgettext.parse(['fixtures/lower_n_.rb'])
assert_plural_target("ooo", "ppp",
[
'fixtures/lower_n_.rb:85',
'fixtures/lower_n_.rb:86',
])
assert_plural_target("qqq", "rrr",
[
'fixtures/lower_n_.rb:90',
'fixtures/lower_n_.rb:91',
])
end
private
def assert_target(msgid, references = nil)
t = @ary.detect {|elem| elem.msgid == msgid}
if t
if references
assert_equal references.sort, t.references.sort, 'Translation target references do not match.'
end
yield t if block_given?
else
flunk "Expected a translation target with id '#{msgid}'. Not found."
end
end
def assert_plural_target(msgid, plural, references = nil)
assert_target msgid, references do |t|
assert_equal plural, t.msgid_plural, 'Expected plural form'
yield t if block_given?
end
end
def assert_target_in_context(msgctxt, msgid, references = nil)
t = @ary.detect {|elem| elem.msgid == msgid && elem.msgctxt == msgctxt}
if t
if references
assert_equal references.sort, t.references.sort, 'Translation target references do not match.'
end
yield t if block_given?
else
flunk "Expected a translation target with id '#{msgid}' and context '#{msgctxt}'. Not found."
end
end
end
|