|
14 | 14 | from datetime import datetime
|
15 | 15 | from io import BytesIO
|
16 | 16 |
|
| 17 | +import pytest |
| 18 | + |
17 | 19 | from babel import __version__ as VERSION
|
18 | 20 | from babel.core import Locale, UnknownLocaleError
|
19 | 21 | from babel.dates import format_datetime
|
20 |
| -from babel.messages import checkers |
| 22 | +from babel.messages import Message, checkers |
| 23 | +from babel.messages.catalog import TranslationError |
| 24 | +from babel.messages.checkers import _validate_format, python_format |
21 | 25 | from babel.messages.plurals import PLURALS
|
22 | 26 | from babel.messages.pofile import read_po
|
23 | 27 | from babel.util import LOCALTZ
|
@@ -325,3 +329,59 @@ def test_6_num_plurals_checkers(self):
|
325 | 329 | catalog = read_po(BytesIO(po_file), _locale)
|
326 | 330 | message = catalog['foobar']
|
327 | 331 | checkers.num_plurals(catalog, message)
|
| 332 | + |
| 333 | + |
| 334 | +class TestPythonFormat: |
| 335 | + @pytest.mark.parametrize(('msgid', 'msgstr'), [ |
| 336 | + ('foo %s', 'foo'), |
| 337 | + (('foo %s', 'bar'), ('foo', 'bar')), |
| 338 | + (('foo', 'bar %s'), ('foo', 'bar')), |
| 339 | + (('foo %s', 'bar'), ('foo')), |
| 340 | + ]) |
| 341 | + def test_python_format_invalid(self, msgid, msgstr): |
| 342 | + msg = Message(msgid, msgstr) |
| 343 | + with pytest.raises(TranslationError): |
| 344 | + python_format(None, msg) |
| 345 | + |
| 346 | + @pytest.mark.parametrize(('msgid', 'msgstr'), [ |
| 347 | + ('foo', 'foo'), |
| 348 | + ('foo', 'foo %s'), |
| 349 | + (('foo %s', 'bar %d'), ('foo %s', 'bar %d')), |
| 350 | + (('foo %s', 'bar %d'), ('foo %s', 'bar %d', 'baz')), |
| 351 | + (('foo', 'bar %s'), ('foo')), |
| 352 | + ]) |
| 353 | + def test_python_format_valid(self, msgid, msgstr): |
| 354 | + msg = Message(msgid, msgstr) |
| 355 | + python_format(None, msg) |
| 356 | + |
| 357 | + @pytest.mark.parametrize(('msgid', 'msgstr', 'error'), [ |
| 358 | + ('%s %(foo)s', '%s %(foo)s', 'format string mixes positional and named placeholders'), |
| 359 | + ('foo %s', 'foo', 'placeholders are incompatible'), |
| 360 | + ('%s', '%(foo)s', 'the format strings are of different kinds'), |
| 361 | + ('%s', '%s %d', 'positional format placeholders are unbalanced'), |
| 362 | + ('%s', '%d', "incompatible format for placeholder 1: 's' and 'd' are not compatible"), |
| 363 | + ('%s %s %d', '%s %s %s', "incompatible format for placeholder 3: 'd' and 's' are not compatible"), |
| 364 | + ('%(foo)s', '%(bar)s', "unknown named placeholder 'bar'"), |
| 365 | + ('%(foo)s', '%(bar)d', "unknown named placeholder 'bar'"), |
| 366 | + ('%(foo)s', '%(foo)d', "incompatible format for placeholder 'foo': 'd' and 's' are not compatible"), |
| 367 | + ]) |
| 368 | + def test__validate_format_invalid(self, msgid, msgstr, error): |
| 369 | + with pytest.raises(TranslationError, match=error): |
| 370 | + _validate_format(msgid, msgstr) |
| 371 | + |
| 372 | + @pytest.mark.parametrize(('msgid', 'msgstr'), [ |
| 373 | + ('foo', 'foo'), |
| 374 | + ('foo', 'foo %s'), |
| 375 | + ('%s foo', 'foo %s'), |
| 376 | + ('%i', '%d'), |
| 377 | + ('%d', '%u'), |
| 378 | + ('%x', '%X'), |
| 379 | + ('%f', '%F'), |
| 380 | + ('%F', '%g'), |
| 381 | + ('%g', '%G'), |
| 382 | + ('%(foo)s', 'foo'), |
| 383 | + ('%(foo)s', '%(foo)s %(foo)s'), |
| 384 | + ('%(bar)s foo %(n)d', '%(n)d foo %(bar)s'), |
| 385 | + ]) |
| 386 | + def test__validate_format_valid(self, msgid, msgstr): |
| 387 | + _validate_format(msgid, msgstr) |
0 commit comments