2
2
import base64
3
3
import gettext
4
4
import unittest
5
+ from functools import partial
5
6
6
7
from test import support
7
8
from test .support import os_helper
@@ -122,8 +123,9 @@ def reset_gettext():
122
123
123
124
124
125
class GettextBaseTest (unittest .TestCase ):
125
- def setUp (self ):
126
- self .addCleanup (os_helper .rmtree , os .path .split (LOCALEDIR )[0 ])
126
+ @classmethod
127
+ def setUpClass (cls ):
128
+ cls .addClassCleanup (os_helper .rmtree , os .path .split (LOCALEDIR )[0 ])
127
129
if not os .path .isdir (LOCALEDIR ):
128
130
os .makedirs (LOCALEDIR )
129
131
with open (MOFILE , 'wb' ) as fp :
@@ -136,6 +138,8 @@ def setUp(self):
136
138
fp .write (base64 .decodebytes (UMO_DATA ))
137
139
with open (MMOFILE , 'wb' ) as fp :
138
140
fp .write (base64 .decodebytes (MMO_DATA ))
141
+
142
+ def setUp (self ):
139
143
self .env = self .enterContext (os_helper .EnvironmentVarGuard ())
140
144
self .env ['LANGUAGE' ] = 'xx'
141
145
reset_gettext ()
@@ -316,59 +320,137 @@ def test_multiline_strings(self):
316
320
trggrkg zrffntr pngnybt yvoenel.''' )
317
321
318
322
319
- class PluralFormsTestCase (GettextBaseTest ):
323
+ class PluralFormsTests :
324
+
325
+ def _test_plural_forms (self , ngettext , gettext ,
326
+ singular , plural , tsingular , tplural ,
327
+ numbers_only = True ):
328
+ x = ngettext (singular , plural , 1 )
329
+ self .assertEqual (x , tsingular )
330
+ x = ngettext (singular , plural , 2 )
331
+ self .assertEqual (x , tplural )
332
+ x = gettext (singular )
333
+ self .assertEqual (x , tsingular )
334
+
335
+ if numbers_only :
336
+ lineno = self ._test_plural_forms .__code__ .co_firstlineno + 9
337
+ with self .assertWarns (DeprecationWarning ) as cm :
338
+ x = ngettext (singular , plural , 1.0 )
339
+ self .assertEqual (cm .filename , __file__ )
340
+ self .assertEqual (cm .lineno , lineno + 4 )
341
+ self .assertEqual (x , tsingular )
342
+ with self .assertWarns (DeprecationWarning ) as cm :
343
+ x = ngettext (singular , plural , 1.1 )
344
+ self .assertEqual (cm .filename , __file__ )
345
+ self .assertEqual (cm .lineno , lineno + 9 )
346
+ self .assertEqual (x , tplural )
347
+ with self .assertRaises (TypeError ):
348
+ ngettext (singular , plural , None )
349
+ else :
350
+ x = ngettext (singular , plural , None )
351
+ self .assertEqual (x , tplural )
352
+
353
+ def test_plural_forms (self ):
354
+ self ._test_plural_forms (
355
+ self .ngettext , self .gettext ,
356
+ 'There is %s file' , 'There are %s files' ,
357
+ 'Hay %s fichero' , 'Hay %s ficheros' )
358
+ self ._test_plural_forms (
359
+ self .ngettext , self .gettext ,
360
+ '%d file deleted' , '%d files deleted' ,
361
+ '%d file deleted' , '%d files deleted' )
362
+
363
+ def test_plural_context_forms (self ):
364
+ ngettext = partial (self .npgettext , 'With context' )
365
+ gettext = partial (self .pgettext , 'With context' )
366
+ self ._test_plural_forms (
367
+ ngettext , gettext ,
368
+ 'There is %s file' , 'There are %s files' ,
369
+ 'Hay %s fichero (context)' , 'Hay %s ficheros (context)' )
370
+ self ._test_plural_forms (
371
+ ngettext , gettext ,
372
+ '%d file deleted' , '%d files deleted' ,
373
+ '%d file deleted' , '%d files deleted' )
374
+
375
+ def test_plural_wrong_context_forms (self ):
376
+ self ._test_plural_forms (
377
+ partial (self .npgettext , 'Unknown context' ),
378
+ partial (self .pgettext , 'Unknown context' ),
379
+ 'There is %s file' , 'There are %s files' ,
380
+ 'There is %s file' , 'There are %s files' )
381
+
382
+
383
+ class GNUTranslationsPluralFormsTestCase (PluralFormsTests , GettextBaseTest ):
320
384
def setUp (self ):
321
385
GettextBaseTest .setUp (self )
322
- self .localedir = os .curdir
323
386
# Set up the bindings
324
- gettext .bindtextdomain ('gettext' , self . localedir )
387
+ gettext .bindtextdomain ('gettext' , os . curdir )
325
388
gettext .textdomain ('gettext' )
326
- self .mofile = MOFILE
327
389
328
- def test_plural_forms1 (self ):
329
- eq = self .assertEqual
330
- x = gettext .ngettext ('There is %s file' , 'There are %s files' , 1 )
331
- eq (x , 'Hay %s fichero' )
332
- x = gettext .ngettext ('There is %s file' , 'There are %s files' , 2 )
333
- eq (x , 'Hay %s ficheros' )
334
- x = gettext .gettext ('There is %s file' )
335
- eq (x , 'Hay %s fichero' )
336
-
337
- def test_plural_context_forms1 (self ):
338
- eq = self .assertEqual
339
- x = gettext .npgettext ('With context' ,
340
- 'There is %s file' , 'There are %s files' , 1 )
341
- eq (x , 'Hay %s fichero (context)' )
342
- x = gettext .npgettext ('With context' ,
343
- 'There is %s file' , 'There are %s files' , 2 )
344
- eq (x , 'Hay %s ficheros (context)' )
345
- x = gettext .pgettext ('With context' , 'There is %s file' )
346
- eq (x , 'Hay %s fichero (context)' )
347
-
348
- def test_plural_forms2 (self ):
349
- eq = self .assertEqual
350
- with open (self .mofile , 'rb' ) as fp :
351
- t = gettext .GNUTranslations (fp )
352
- x = t .ngettext ('There is %s file' , 'There are %s files' , 1 )
353
- eq (x , 'Hay %s fichero' )
354
- x = t .ngettext ('There is %s file' , 'There are %s files' , 2 )
355
- eq (x , 'Hay %s ficheros' )
356
- x = t .gettext ('There is %s file' )
357
- eq (x , 'Hay %s fichero' )
358
-
359
- def test_plural_context_forms2 (self ):
360
- eq = self .assertEqual
361
- with open (self .mofile , 'rb' ) as fp :
390
+ self .gettext = gettext .gettext
391
+ self .ngettext = gettext .ngettext
392
+ self .pgettext = gettext .pgettext
393
+ self .npgettext = gettext .npgettext
394
+
395
+
396
+ class GNUTranslationsWithDomainPluralFormsTestCase (PluralFormsTests , GettextBaseTest ):
397
+ def setUp (self ):
398
+ GettextBaseTest .setUp (self )
399
+ # Set up the bindings
400
+ gettext .bindtextdomain ('gettext' , os .curdir )
401
+
402
+ self .gettext = partial (gettext .dgettext , 'gettext' )
403
+ self .ngettext = partial (gettext .dngettext , 'gettext' )
404
+ self .pgettext = partial (gettext .dpgettext , 'gettext' )
405
+ self .npgettext = partial (gettext .dnpgettext , 'gettext' )
406
+
407
+ def test_plural_forms_wrong_domain (self ):
408
+ self ._test_plural_forms (
409
+ partial (gettext .dngettext , 'unknown' ),
410
+ partial (gettext .dgettext , 'unknown' ),
411
+ 'There is %s file' , 'There are %s files' ,
412
+ 'There is %s file' , 'There are %s files' ,
413
+ numbers_only = False )
414
+
415
+ def test_plural_context_forms_wrong_domain (self ):
416
+ self ._test_plural_forms (
417
+ partial (gettext .dnpgettext , 'unknown' , 'With context' ),
418
+ partial (gettext .dpgettext , 'unknown' , 'With context' ),
419
+ 'There is %s file' , 'There are %s files' ,
420
+ 'There is %s file' , 'There are %s files' ,
421
+ numbers_only = False )
422
+
423
+
424
+ class GNUTranslationsClassPluralFormsTestCase (PluralFormsTests , GettextBaseTest ):
425
+ def setUp (self ):
426
+ GettextBaseTest .setUp (self )
427
+ with open (MOFILE , 'rb' ) as fp :
362
428
t = gettext .GNUTranslations (fp )
363
- x = t .npgettext ('With context' ,
364
- 'There is %s file' , 'There are %s files' , 1 )
365
- eq (x , 'Hay %s fichero (context)' )
366
- x = t .npgettext ('With context' ,
367
- 'There is %s file' , 'There are %s files' , 2 )
368
- eq (x , 'Hay %s ficheros (context)' )
369
- x = t .pgettext ('With context' , 'There is %s file' )
370
- eq (x , 'Hay %s fichero (context)' )
371
429
430
+ self .gettext = t .gettext
431
+ self .ngettext = t .ngettext
432
+ self .pgettext = t .pgettext
433
+ self .npgettext = t .npgettext
434
+
435
+ def test_plural_forms_null_translations (self ):
436
+ t = gettext .NullTranslations ()
437
+ self ._test_plural_forms (
438
+ t .ngettext , t .gettext ,
439
+ 'There is %s file' , 'There are %s files' ,
440
+ 'There is %s file' , 'There are %s files' ,
441
+ numbers_only = False )
442
+
443
+ def test_plural_context_forms_null_translations (self ):
444
+ t = gettext .NullTranslations ()
445
+ self ._test_plural_forms (
446
+ partial (t .npgettext , 'With context' ),
447
+ partial (t .pgettext , 'With context' ),
448
+ 'There is %s file' , 'There are %s files' ,
449
+ 'There is %s file' , 'There are %s files' ,
450
+ numbers_only = False )
451
+
452
+
453
+ class PluralFormsInternalTestCase :
372
454
# Examples from http://www.gnu.org/software/gettext/manual/gettext.html
373
455
374
456
def test_ja (self ):
0 commit comments