| 1 |
# |
| 2 |
# Copyright (C) 2007-2011 Edgewall Software, 2013-2025 the Babel team |
| 3 |
# All rights reserved. |
| 4 |
# |
| 5 |
# This software is licensed as described in the file LICENSE, which |
| 6 |
# you should have received as part of this distribution. The terms |
| 7 |
# are also available at https://github.com/python-babel/babel/blob/master/LICENSE. |
| 8 |
# |
| 9 |
# This software consists of voluntary contributions made by many |
| 10 |
# individuals. For the exact contribution history, see the revision |
| 11 |
# history and logs, available at https://github.com/python-babel/babel/commits/master/. |
| 12 |
|
| 13 |
from datetime import datetime |
| 14 |
from io import BytesIO, StringIO |
| 15 |
|
| 16 |
import pytest |
| 17 |
|
| 18 |
from babel import Locale |
| 19 |
from babel.messages import Catalog, pofile |
| 20 |
from babel.util import FixedOffsetTimezone |
| 21 |
|
| 22 |
|
| 23 |
def test_preserve_locale(): |
| 24 |
buf = StringIO(r'''msgid "foo" |
| 25 |
msgstr "Voh"''') |
| 26 |
catalog = pofile.read_po(buf, locale='en_US') |
| 27 |
assert Locale('en', 'US') == catalog.locale |
| 28 |
|
| 29 |
|
| 30 |
def test_locale_gets_overridden_by_file(): |
| 31 |
buf = StringIO(r''' |
| 32 |
msgid "" |
| 33 |
msgstr "" |
| 34 |
"Language: en_US\n"''') |
| 35 |
catalog = pofile.read_po(buf, locale='de') |
| 36 |
assert Locale('en', 'US') == catalog.locale |
| 37 |
buf = StringIO(r''' |
| 38 |
msgid "" |
| 39 |
msgstr "" |
| 40 |
"Language: ko-KR\n"''') |
| 41 |
catalog = pofile.read_po(buf, locale='de') |
| 42 |
assert Locale('ko', 'KR') == catalog.locale |
| 43 |
|
| 44 |
|
| 45 |
def test_preserve_domain(): |
| 46 |
buf = StringIO(r'''msgid "foo" |
| 47 |
msgstr "Voh"''') |
| 48 |
catalog = pofile.read_po(buf, domain='mydomain') |
| 49 |
assert catalog.domain == 'mydomain' |
| 50 |
|
| 51 |
|
| 52 |
def test_applies_specified_encoding_during_read(): |
| 53 |
buf = BytesIO(''' |
| 54 |
msgid "" |
| 55 |
msgstr "" |
| 56 |
"Project-Id-Version: 3.15\\n" |
| 57 |
"Report-Msgid-Bugs-To: Fliegender Zirkus <[email protected]>\\n" |
| 58 |
"POT-Creation-Date: 2007-09-27 11:19+0700\\n" |
| 59 |
"PO-Revision-Date: 2007-09-27 21:42-0700\\n" |
| 60 |
"Last-Translator: John <[email protected]>\\n" |
| 61 |
"Language-Team: German Lang <[email protected]>\\n" |
| 62 |
"Plural-Forms: nplurals=2; plural=(n != 1);\\n" |
| 63 |
"MIME-Version: 1.0\\n" |
| 64 |
"Content-Type: text/plain; charset=iso-8859-1\\n" |
| 65 |
"Content-Transfer-Encoding: 8bit\\n" |
| 66 |
"Generated-By: Babel 1.0dev-r313\\n" |
| 67 |
|
| 68 |
msgid "foo" |
| 69 |
msgstr "bär"'''.encode('iso-8859-1')) |
| 70 |
catalog = pofile.read_po(buf, locale='de_DE') |
| 71 |
assert catalog.get('foo').string == 'bär' |
| 72 |
|
| 73 |
|
| 74 |
def test_encoding_header_read(): |
| 75 |
buf = BytesIO(b'msgid ""\nmsgstr ""\n"Content-Type: text/plain; charset=mac_roman\\n"\n') |
| 76 |
catalog = pofile.read_po(buf, locale='xx_XX') |
| 77 |
assert catalog.charset == 'mac_roman' |
| 78 |
|
| 79 |
|
| 80 |
def test_plural_forms_header_parsed(): |
| 81 |
buf = BytesIO(b'msgid ""\nmsgstr ""\n"Plural-Forms: nplurals=42; plural=(n % 11);\\n"\n') |
| 82 |
catalog = pofile.read_po(buf, locale='xx_XX') |
| 83 |
assert catalog.plural_expr == '(n % 11)' |
| 84 |
assert catalog.num_plurals == 42 |
| 85 |
|
| 86 |
|
| 87 |
def test_read_multiline(): |
| 88 |
buf = StringIO(r'''msgid "" |
| 89 |
"Here's some text that\n" |
| 90 |
"includesareallylongwordthatmightbutshouldnt" |
| 91 |
" throw us into an infinite " |
| 92 |
"loop\n" |
| 93 |
msgstr ""''') |
| 94 |
catalog = pofile.read_po(buf) |
| 95 |
assert len(catalog) == 1 |
| 96 |
message = list(catalog)[1] |
| 97 |
assert message.id == ( |
| 98 |
"Here's some text that\nincludesareallylongwordthat" |
| 99 |
"mightbutshouldnt throw us into an infinite loop\n" |
| 100 |
) |
| 101 |
|
| 102 |
|
| 103 |
def test_fuzzy_header(): |
| 104 |
buf = StringIO(r''' |
| 105 |
# Translations template for AReallyReallyLongNameForAProject. |
| 106 |
# Copyright (C) 2007 ORGANIZATION |
| 107 |
# This file is distributed under the same license as the |
| 108 |
# AReallyReallyLongNameForAProject project. |
| 109 |
# FIRST AUTHOR <EMAIL@ADDRESS>, 2007. |
| 110 |
# |
| 111 |
#, fuzzy |
| 112 |
''') |
| 113 |
catalog = pofile.read_po(buf) |
| 114 |
assert len(list(catalog)) == 1 |
| 115 |
assert list(catalog)[0].fuzzy |
| 116 |
|
| 117 |
|
| 118 |
def test_not_fuzzy_header(): |
| 119 |
buf = StringIO(r''' |
| 120 |
# Translations template for AReallyReallyLongNameForAProject. |
| 121 |
# Copyright (C) 2007 ORGANIZATION |
| 122 |
# This file is distributed under the same license as the |
| 123 |
# AReallyReallyLongNameForAProject project. |
| 124 |
# FIRST AUTHOR <EMAIL@ADDRESS>, 2007. |
| 125 |
# |
| 126 |
''') |
| 127 |
catalog = pofile.read_po(buf) |
| 128 |
assert len(list(catalog)) == 1 |
| 129 |
assert not list(catalog)[0].fuzzy |
| 130 |
|
| 131 |
|
| 132 |
def test_header_entry(): |
| 133 |
buf = StringIO(r''' |
| 134 |
# SOME DESCRIPTIVE TITLE. |
| 135 |
# Copyright (C) 2007 THE PACKAGE'S COPYRIGHT HOLDER |
| 136 |
# This file is distributed under the same license as the PACKAGE package. |
| 137 |
# FIRST AUTHOR <EMAIL@ADDRESS>, 2007. |
| 138 |
# |
| 139 |
#, fuzzy |
| 140 |
msgid "" |
| 141 |
msgstr "" |
| 142 |
"Project-Id-Version: 3.15\n" |
| 143 |
"Report-Msgid-Bugs-To: Fliegender Zirkus <[email protected]>\n" |
| 144 |
"POT-Creation-Date: 2007-09-27 11:19+0700\n" |
| 145 |
"PO-Revision-Date: 2007-09-27 21:42-0700\n" |
| 146 |
"Last-Translator: John <[email protected]>\n" |
| 147 |
"Language: de\n" |
| 148 |
"Language-Team: German Lang <[email protected]>\n" |
| 149 |
"Plural-Forms: nplurals=2; plural=(n != 1);\n" |
| 150 |
"MIME-Version: 1.0\n" |
| 151 |
"Content-Type: text/plain; charset=iso-8859-2\n" |
| 152 |
"Content-Transfer-Encoding: 8bit\n" |
| 153 |
"Generated-By: Babel 1.0dev-r313\n" |
| 154 |
''') |
| 155 |
catalog = pofile.read_po(buf) |
| 156 |
assert len(list(catalog)) == 1 |
| 157 |
assert catalog.version == '3.15' |
| 158 |
assert catalog.msgid_bugs_address == 'Fliegender Zirkus <[email protected]>' |
| 159 |
assert datetime(2007, 9, 27, 11, 19, tzinfo=FixedOffsetTimezone(7 * 60)) == catalog.creation_date |
| 160 |
assert catalog.last_translator == 'John <[email protected]>' |
| 161 |
assert Locale('de') == catalog.locale |
| 162 |
assert catalog.language_team == 'German Lang <[email protected]>' |
| 163 |
assert catalog.charset == 'iso-8859-2' |
| 164 |
assert list(catalog)[0].fuzzy |
| 165 |
|
| 166 |
|
| 167 |
def test_obsolete_message(): |
| 168 |
buf = StringIO(r'''# This is an obsolete message |
| 169 |
#~ msgid "foo" |
| 170 |
#~ msgstr "Voh" |
| 171 |
|
| 172 |
# This message is not obsolete |
| 173 |
#: main.py:1 |
| 174 |
msgid "bar" |
| 175 |
msgstr "Bahr" |
| 176 |
''') |
| 177 |
catalog = pofile.read_po(buf) |
| 178 |
assert len(catalog) == 1 |
| 179 |
assert len(catalog.obsolete) == 1 |
| 180 |
message = catalog.obsolete['foo'] |
| 181 |
assert message.id == 'foo' |
| 182 |
assert message.string == 'Voh' |
| 183 |
assert message.user_comments == ['This is an obsolete message'] |
| 184 |
|
| 185 |
|
| 186 |
def test_obsolete_message_ignored(): |
| 187 |
buf = StringIO(r'''# This is an obsolete message |
| 188 |
#~ msgid "foo" |
| 189 |
#~ msgstr "Voh" |
| 190 |
|
| 191 |
# This message is not obsolete |
| 192 |
#: main.py:1 |
| 193 |
msgid "bar" |
| 194 |
msgstr "Bahr" |
| 195 |
''') |
| 196 |
catalog = pofile.read_po(buf, ignore_obsolete=True) |
| 197 |
assert len(catalog) == 1 |
| 198 |
assert len(catalog.obsolete) == 0 |
| 199 |
|
| 200 |
|
| 201 |
def test_multi_line_obsolete_message(): |
| 202 |
buf = StringIO(r'''# This is an obsolete message |
| 203 |
#~ msgid "" |
| 204 |
#~ "foo" |
| 205 |
#~ "foo" |
| 206 |
#~ msgstr "" |
| 207 |
#~ "Voh" |
| 208 |
#~ "Vooooh" |
| 209 |
|
| 210 |
# This message is not obsolete |
| 211 |
#: main.py:1 |
| 212 |
msgid "bar" |
| 213 |
msgstr "Bahr" |
| 214 |
''') |
| 215 |
catalog = pofile.read_po(buf) |
| 216 |
assert len(catalog.obsolete) == 1 |
| 217 |
message = catalog.obsolete['foofoo'] |
| 218 |
assert message.id == 'foofoo' |
| 219 |
assert message.string == 'VohVooooh' |
| 220 |
assert message.user_comments == ['This is an obsolete message'] |
| 221 |
|
| 222 |
|
| 223 |
def test_unit_following_multi_line_obsolete_message(): |
| 224 |
buf = StringIO(r'''# This is an obsolete message |
| 225 |
#~ msgid "" |
| 226 |
#~ "foo" |
| 227 |
#~ "fooooooo" |
| 228 |
#~ msgstr "" |
| 229 |
#~ "Voh" |
| 230 |
#~ "Vooooh" |
| 231 |
|
| 232 |
# This message is not obsolete |
| 233 |
#: main.py:1 |
| 234 |
msgid "bar" |
| 235 |
msgstr "Bahr" |
| 236 |
''') |
| 237 |
catalog = pofile.read_po(buf) |
| 238 |
assert len(catalog) == 1 |
| 239 |
message = catalog['bar'] |
| 240 |
assert message.id == 'bar' |
| 241 |
assert message.string == 'Bahr' |
| 242 |
assert message.user_comments == ['This message is not obsolete'] |
| 243 |
|
| 244 |
|
| 245 |
def test_unit_before_obsolete_is_not_obsoleted(): |
| 246 |
buf = StringIO(r''' |
| 247 |
# This message is not obsolete |
| 248 |
#: main.py:1 |
| 249 |
msgid "bar" |
| 250 |
msgstr "Bahr" |
| 251 |
|
| 252 |
# This is an obsolete message |
| 253 |
#~ msgid "" |
| 254 |
#~ "foo" |
| 255 |
#~ "fooooooo" |
| 256 |
#~ msgstr "" |
| 257 |
#~ "Voh" |
| 258 |
#~ "Vooooh" |
| 259 |
''') |
| 260 |
catalog = pofile.read_po(buf) |
| 261 |
assert len(catalog) == 1 |
| 262 |
message = catalog['bar'] |
| 263 |
assert message.id == 'bar' |
| 264 |
assert message.string == 'Bahr' |
| 265 |
assert message.user_comments == ['This message is not obsolete'] |
| 266 |
|
| 267 |
|
| 268 |
def test_with_context(): |
| 269 |
buf = BytesIO(b'''# Some string in the menu |
| 270 |
#: main.py:1 |
| 271 |
msgctxt "Menu" |
| 272 |
msgid "foo" |
| 273 |
msgstr "Voh" |
| 274 |
|
| 275 |
# Another string in the menu |
| 276 |
#: main.py:2 |
| 277 |
msgctxt "Menu" |
| 278 |
msgid "bar" |
| 279 |
msgstr "Bahr" |
| 280 |
''') |
| 281 |
catalog = pofile.read_po(buf, ignore_obsolete=True) |
| 282 |
assert len(catalog) == 2 |
| 283 |
message = catalog.get('foo', context='Menu') |
| 284 |
assert message.context == 'Menu' |
| 285 |
message = catalog.get('bar', context='Menu') |
| 286 |
assert message.context == 'Menu' |
| 287 |
|
| 288 |
# And verify it pass through write_po |
| 289 |
out_buf = BytesIO() |
| 290 |
pofile.write_po(out_buf, catalog, omit_header=True) |
| 291 |
assert out_buf.getvalue().strip() == buf.getvalue().strip() |
| 292 |
|
| 293 |
|
| 294 |
def test_obsolete_message_with_context(): |
| 295 |
buf = StringIO(''' |
| 296 |
# This message is not obsolete |
| 297 |
msgid "baz" |
| 298 |
msgstr "Bazczch" |
| 299 |
|
| 300 |
# This is an obsolete message |
| 301 |
#~ msgctxt "other" |
| 302 |
#~ msgid "foo" |
| 303 |
#~ msgstr "Voh" |
| 304 |
|
| 305 |
# This message is not obsolete |
| 306 |
#: main.py:1 |
| 307 |
msgid "bar" |
| 308 |
msgstr "Bahr" |
| 309 |
''') |
| 310 |
catalog = pofile.read_po(buf) |
| 311 |
assert len(catalog) == 2 |
| 312 |
assert len(catalog.obsolete) == 1 |
| 313 |
message = catalog.obsolete[("foo", "other")] |
| 314 |
assert message.context == 'other' |
| 315 |
assert message.string == 'Voh' |
| 316 |
|
| 317 |
|
| 318 |
def test_obsolete_messages_with_context(): |
| 319 |
buf = StringIO(''' |
| 320 |
# This is an obsolete message |
| 321 |
#~ msgctxt "apple" |
| 322 |
#~ msgid "foo" |
| 323 |
#~ msgstr "Foo" |
| 324 |
|
| 325 |
# This is an obsolete message with the same id but different context |
| 326 |
#~ msgctxt "orange" |
| 327 |
#~ msgid "foo" |
| 328 |
#~ msgstr "Bar" |
| 329 |
''') |
| 330 |
catalog = pofile.read_po(buf) |
| 331 |
assert len(catalog) == 0 |
| 332 |
assert len(catalog.obsolete) == 2 |
| 333 |
assert 'foo' not in catalog.obsolete |
| 334 |
|
| 335 |
apple_msg = catalog.obsolete[('foo', 'apple')] |
| 336 |
assert apple_msg.id == 'foo' |
| 337 |
assert apple_msg.string == 'Foo' |
| 338 |
assert apple_msg.user_comments == ['This is an obsolete message'] |
| 339 |
|
| 340 |
orange_msg = catalog.obsolete[('foo', 'orange')] |
| 341 |
assert orange_msg.id == 'foo' |
| 342 |
assert orange_msg.string == 'Bar' |
| 343 |
assert orange_msg.user_comments == ['This is an obsolete message with the same id but different context'] |
| 344 |
|
| 345 |
|
| 346 |
def test_obsolete_messages_roundtrip(): |
| 347 |
buf = StringIO('''\ |
| 348 |
# This message is not obsolete |
| 349 |
#: main.py:1 |
| 350 |
msgid "bar" |
| 351 |
msgstr "Bahr" |
| 352 |
|
| 353 |
# This is an obsolete message |
| 354 |
#~ msgid "foo" |
| 355 |
#~ msgstr "Voh" |
| 356 |
|
| 357 |
# This is an obsolete message |
| 358 |
#~ msgctxt "apple" |
| 359 |
#~ msgid "foo" |
| 360 |
#~ msgstr "Foo" |
| 361 |
|
| 362 |
# This is an obsolete message with the same id but different context |
| 363 |
#~ msgctxt "orange" |
| 364 |
#~ msgid "foo" |
| 365 |
#~ msgstr "Bar" |
| 366 |
|
| 367 |
''') |
| 368 |
generated_po_file = ''.join(pofile.generate_po(pofile.read_po(buf), omit_header=True)) |
| 369 |
assert buf.getvalue() == generated_po_file |
| 370 |
|
| 371 |
|
| 372 |
def test_multiline_context(): |
| 373 |
buf = StringIO(''' |
| 374 |
msgctxt "a really long " |
| 375 |
"message context " |
| 376 |
"why?" |
| 377 |
msgid "mid" |
| 378 |
msgstr "mst" |
| 379 |
''') |
| 380 |
catalog = pofile.read_po(buf) |
| 381 |
assert len(catalog) == 1 |
| 382 |
message = catalog.get('mid', context="a really long message context why?") |
| 383 |
assert message is not None |
| 384 |
assert message.context == 'a really long message context why?' |
| 385 |
|
| 386 |
|
| 387 |
def test_with_context_two(): |
| 388 |
buf = BytesIO(b'''msgctxt "Menu" |
| 389 |
msgid "foo" |
| 390 |
msgstr "Voh" |
| 391 |
|
| 392 |
msgctxt "Mannu" |
| 393 |
msgid "bar" |
| 394 |
msgstr "Bahr" |
| 395 |
''') |
| 396 |
catalog = pofile.read_po(buf, ignore_obsolete=True) |
| 397 |
assert len(catalog) == 2 |
| 398 |
message = catalog.get('foo', context='Menu') |
| 399 |
assert message.context == 'Menu' |
| 400 |
message = catalog.get('bar', context='Mannu') |
| 401 |
assert message.context == 'Mannu' |
| 402 |
|
| 403 |
# And verify it pass through write_po |
| 404 |
out_buf = BytesIO() |
| 405 |
pofile.write_po(out_buf, catalog, omit_header=True) |
| 406 |
assert out_buf.getvalue().strip() == buf.getvalue().strip(), out_buf.getvalue() |
| 407 |
|
| 408 |
|
| 409 |
def test_single_plural_form(): |
| 410 |
buf = StringIO(r'''msgid "foo" |
| 411 |
msgid_plural "foos" |
| 412 |
msgstr[0] "Voh"''') |
| 413 |
catalog = pofile.read_po(buf, locale='ja_JP') |
| 414 |
assert len(catalog) == 1 |
| 415 |
assert catalog.num_plurals == 1 |
| 416 |
message = catalog['foo'] |
| 417 |
assert len(message.string) == 1 |
| 418 |
|
| 419 |
|
| 420 |
def test_singular_plural_form(): |
| 421 |
buf = StringIO(r'''msgid "foo" |
| 422 |
msgid_plural "foos" |
| 423 |
msgstr[0] "Voh" |
| 424 |
msgstr[1] "Vohs"''') |
| 425 |
catalog = pofile.read_po(buf, locale='nl_NL') |
| 426 |
assert len(catalog) == 1 |
| 427 |
assert catalog.num_plurals == 2 |
| 428 |
message = catalog['foo'] |
| 429 |
assert len(message.string) == 2 |
| 430 |
|
| 431 |
|
| 432 |
def test_more_than_two_plural_forms(): |
| 433 |
buf = StringIO(r'''msgid "foo" |
| 434 |
msgid_plural "foos" |
| 435 |
msgstr[0] "Voh" |
| 436 |
msgstr[1] "Vohs" |
| 437 |
msgstr[2] "Vohss"''') |
| 438 |
catalog = pofile.read_po(buf, locale='lv_LV') |
| 439 |
assert len(catalog) == 1 |
| 440 |
assert catalog.num_plurals == 3 |
| 441 |
message = catalog['foo'] |
| 442 |
assert len(message.string) == 3 |
| 443 |
assert message.string[2] == 'Vohss' |
| 444 |
|
| 445 |
|
| 446 |
def test_plural_with_square_brackets(): |
| 447 |
buf = StringIO(r'''msgid "foo" |
| 448 |
msgid_plural "foos" |
| 449 |
msgstr[0] "Voh [text]" |
| 450 |
msgstr[1] "Vohs [text]"''') |
| 451 |
catalog = pofile.read_po(buf, locale='nb_NO') |
| 452 |
assert len(catalog) == 1 |
| 453 |
assert catalog.num_plurals == 2 |
| 454 |
message = catalog['foo'] |
| 455 |
assert len(message.string) == 2 |
| 456 |
|
| 457 |
|
| 458 |
def test_obsolete_plural_with_square_brackets(): |
| 459 |
buf = StringIO('''\ |
| 460 |
#~ msgid "foo" |
| 461 |
#~ msgid_plural "foos" |
| 462 |
#~ msgstr[0] "Voh [text]" |
| 463 |
#~ msgstr[1] "Vohs [text]" |
| 464 |
''') |
| 465 |
catalog = pofile.read_po(buf, locale='nb_NO') |
| 466 |
assert len(catalog) == 0 |
| 467 |
assert len(catalog.obsolete) == 1 |
| 468 |
assert catalog.num_plurals == 2 |
| 469 |
message = catalog.obsolete['foo'] |
| 470 |
assert len(message.string) == 2 |
| 471 |
assert message.string[0] == 'Voh [text]' |
| 472 |
assert message.string[1] == 'Vohs [text]' |
| 473 |
|
| 474 |
|
| 475 |
def test_missing_plural(): |
| 476 |
buf = StringIO('''\ |
| 477 |
msgid "" |
| 478 |
msgstr "" |
| 479 |
"Plural-Forms: nplurals=3; plural=(n < 2) ? n : 2;\n" |
| 480 |
|
| 481 |
msgid "foo" |
| 482 |
msgid_plural "foos" |
| 483 |
msgstr[0] "Voh [text]" |
| 484 |
msgstr[1] "Vohs [text]" |
| 485 |
''') |
| 486 |
catalog = pofile.read_po(buf, locale='nb_NO') |
| 487 |
assert len(catalog) == 1 |
| 488 |
assert catalog.num_plurals == 3 |
| 489 |
message = catalog['foo'] |
| 490 |
assert len(message.string) == 3 |
| 491 |
assert message.string[0] == 'Voh [text]' |
| 492 |
assert message.string[1] == 'Vohs [text]' |
| 493 |
assert message.string[2] == '' |
| 494 |
|
| 495 |
|
| 496 |
def test_missing_plural_in_the_middle(): |
| 497 |
buf = StringIO('''\ |
| 498 |
msgid "" |
| 499 |
msgstr "" |
| 500 |
"Plural-Forms: nplurals=3; plural=(n < 2) ? n : 2;\n" |
| 501 |
|
| 502 |
msgid "foo" |
| 503 |
msgid_plural "foos" |
| 504 |
msgstr[0] "Voh [text]" |
| 505 |
msgstr[2] "Vohs [text]" |
| 506 |
''') |
| 507 |
catalog = pofile.read_po(buf, locale='nb_NO') |
| 508 |
assert len(catalog) == 1 |
| 509 |
assert catalog.num_plurals == 3 |
| 510 |
message = catalog['foo'] |
| 511 |
assert len(message.string) == 3 |
| 512 |
assert message.string[0] == 'Voh [text]' |
| 513 |
assert message.string[1] == '' |
| 514 |
assert message.string[2] == 'Vohs [text]' |
| 515 |
|
| 516 |
|
| 517 |
def test_with_location(): |
| 518 |
buf = StringIO('''\ |
| 519 |
#: main.py:1 \u2068filename with whitespace.py\u2069:123 |
| 520 |
msgid "foo" |
| 521 |
msgstr "bar" |
| 522 |
''') |
| 523 |
catalog = pofile.read_po(buf, locale='de_DE') |
| 524 |
assert len(catalog) == 1 |
| 525 |
message = catalog['foo'] |
| 526 |
assert message.string == 'bar' |
| 527 |
assert message.locations == [("main.py", 1), ("filename with whitespace.py", 123)] |
| 528 |
|
| 529 |
|
| 530 |
def test_abort_invalid_po_file(): |
| 531 |
invalid_po = ''' |
| 532 |
msgctxt "" |
| 533 |
"{\"checksum\": 2148532640, \"cxt\": \"collector_thankyou\", \"id\": " |
| 534 |
"270005359}" |
| 535 |
msgid "" |
| 536 |
"Thank you very much for your time.\n" |
| 537 |
"If you have any questions regarding this survey, please contact Fulano " |
| 538 |
"at [email protected]" |
| 539 |
msgstr "Merci de prendre le temps de remplir le sondage. |
| 540 |
Pour toute question, veuillez communiquer avec Fulano à [email protected] |
| 541 |
" |
| 542 |
''' |
| 543 |
invalid_po_2 = ''' |
| 544 |
msgctxt "" |
| 545 |
"{\"checksum\": 2148532640, \"cxt\": \"collector_thankyou\", \"id\": " |
| 546 |
"270005359}" |
| 547 |
msgid "" |
| 548 |
"Thank you very much for your time.\n" |
| 549 |
"If you have any questions regarding this survey, please contact Fulano " |
| 550 |
"at [email protected]." |
| 551 |
msgstr "Merci de prendre le temps de remplir le sondage. |
| 552 |
Pour toute question, veuillez communiquer avec Fulano a [email protected] |
| 553 |
" |
| 554 |
''' |
| 555 |
# Catalog not created, throws Unicode Error |
| 556 |
buf = StringIO(invalid_po) |
| 557 |
output = pofile.read_po(buf, locale='fr', abort_invalid=False) |
| 558 |
assert isinstance(output, Catalog) |
| 559 |
|
| 560 |
# Catalog not created, throws PoFileError |
| 561 |
buf = StringIO(invalid_po_2) |
| 562 |
with pytest.raises(pofile.PoFileError): |
| 563 |
pofile.read_po(buf, locale='fr', abort_invalid=True) |
| 564 |
|
| 565 |
# Catalog is created with warning, no abort |
| 566 |
buf = StringIO(invalid_po_2) |
| 567 |
output = pofile.read_po(buf, locale='fr', abort_invalid=False) |
| 568 |
assert isinstance(output, Catalog) |
| 569 |
|
| 570 |
# Catalog not created, aborted with PoFileError |
| 571 |
buf = StringIO(invalid_po_2) |
| 572 |
with pytest.raises(pofile.PoFileError): |
| 573 |
pofile.read_po(buf, locale='fr', abort_invalid=True) |
| 574 |
|
| 575 |
|
| 576 |
def test_invalid_pofile_with_abort_flag(): |
| 577 |
parser = pofile.PoFileParser(None, abort_invalid=True) |
| 578 |
lineno = 10 |
| 579 |
line = 'Algo esta mal' |
| 580 |
msg = 'invalid file' |
| 581 |
with pytest.raises(pofile.PoFileError): |
| 582 |
parser._invalid_pofile(line, lineno, msg) |