@@ -305,7 +305,9 @@ def test_plural_forms2(self):
305305 x = t .ngettext ('There is %s file' , 'There are %s files' , 2 )
306306 eq (x , 'Hay %s ficheros' )
307307
308- def test_hu (self ):
308+ # Examples from http://www.gnu.org/software/gettext/manual/gettext.html
309+
310+ def test_ja (self ):
309311 eq = self .assertEqual
310312 f = gettext .c2py ('0' )
311313 s = '' .join ([ str (f (x )) for x in range (200 ) ])
@@ -323,6 +325,12 @@ def test_fr(self):
323325 s = '' .join ([ str (f (x )) for x in range (200 ) ])
324326 eq (s , "00111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111" )
325327
328+ def test_lv (self ):
329+ eq = self .assertEqual
330+ f = gettext .c2py ('n%10==1 && n%100!=11 ? 0 : n != 0 ? 1 : 2' )
331+ s = '' .join ([ str (f (x )) for x in range (200 ) ])
332+ eq (s , "20111111111111111111101111111110111111111011111111101111111110111111111011111111101111111110111111111011111111111111111110111111111011111111101111111110111111111011111111101111111110111111111011111111" )
333+
326334 def test_gd (self ):
327335 eq = self .assertEqual
328336 f = gettext .c2py ('n==1 ? 0 : n==2 ? 1 : 2' )
@@ -336,6 +344,12 @@ def test_gd2(self):
336344 s = '' .join ([ str (f (x )) for x in range (200 ) ])
337345 eq (s , "20122222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222" )
338346
347+ def test_ro (self ):
348+ eq = self .assertEqual
349+ f = gettext .c2py ('n==1 ? 0 : (n==0 || (n%100 > 0 && n%100 < 20)) ? 1 : 2' )
350+ s = '' .join ([ str (f (x )) for x in range (200 ) ])
351+ eq (s , "10111111111111111111222222222222222222222222222222222222222222222222222222222222222222222222222222222111111111111111111122222222222222222222222222222222222222222222222222222222222222222222222222222222" )
352+
339353 def test_lt (self ):
340354 eq = self .assertEqual
341355 f = gettext .c2py ('n%10==1 && n%100!=11 ? 0 : n%10>=2 && (n%100<10 || n%100>=20) ? 1 : 2' )
@@ -348,6 +362,12 @@ def test_ru(self):
348362 s = '' .join ([ str (f (x )) for x in range (200 ) ])
349363 eq (s , "20111222222222222222201112222220111222222011122222201112222220111222222011122222201112222220111222222011122222222222222220111222222011122222201112222220111222222011122222201112222220111222222011122222" )
350364
365+ def test_cs (self ):
366+ eq = self .assertEqual
367+ f = gettext .c2py ('(n==1) ? 0 : (n>=2 && n<=4) ? 1 : 2' )
368+ s = '' .join ([ str (f (x )) for x in range (200 ) ])
369+ eq (s , "20111222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222" )
370+
351371 def test_pl (self ):
352372 eq = self .assertEqual
353373 f = gettext .c2py ('n==1 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2' )
@@ -360,10 +380,73 @@ def test_sl(self):
360380 s = '' .join ([ str (f (x )) for x in range (200 ) ])
361381 eq (s , "30122333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333012233333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333" )
362382
383+ def test_ar (self ):
384+ eq = self .assertEqual
385+ f = gettext .c2py ('n==0 ? 0 : n==1 ? 1 : n==2 ? 2 : n%100>=3 && n%100<=10 ? 3 : n%100>=11 ? 4 : 5' )
386+ s = '' .join ([ str (f (x )) for x in range (200 ) ])
387+ eq (s , "01233333333444444444444444444444444444444444444444444444444444444444444444444444444444444444444444445553333333344444444444444444444444444444444444444444444444444444444444444444444444444444444444444444" )
388+
363389 def test_security (self ):
364390 raises = self .assertRaises
365391 # Test for a dangerous expression
366392 raises (ValueError , gettext .c2py , "os.chmod('/etc/passwd',0777)" )
393+ # issue28563
394+ raises (ValueError , gettext .c2py , '"(eval(foo) && ""' )
395+ raises (ValueError , gettext .c2py , 'f"{os.system(\' sh\' )}"' )
396+ # Maximum recursion depth exceeded during compilation
397+ raises (ValueError , gettext .c2py , 'n+' * 10000 + 'n' )
398+ self .assertEqual (gettext .c2py ('n+' * 100 + 'n' )(1 ), 101 )
399+ # MemoryError during compilation
400+ raises (ValueError , gettext .c2py , '(' * 100 + 'n' + ')' * 100 )
401+ # Maximum recursion depth exceeded in C to Python translator
402+ raises (ValueError , gettext .c2py , '(' * 10000 + 'n' + ')' * 10000 )
403+ self .assertEqual (gettext .c2py ('(' * 20 + 'n' + ')' * 20 )(1 ), 1 )
404+
405+ def test_chained_comparison (self ):
406+ # C doesn't chain comparison as Python so 2 == 2 == 2 gets different results
407+ f = gettext .c2py ('n == n == n' )
408+ self .assertEqual ('' .join (str (f (x )) for x in range (3 )), '010' )
409+ f = gettext .c2py ('1 < n == n' )
410+ self .assertEqual ('' .join (str (f (x )) for x in range (3 )), '100' )
411+ f = gettext .c2py ('n == n < 2' )
412+ self .assertEqual ('' .join (str (f (x )) for x in range (3 )), '010' )
413+ f = gettext .c2py ('0 < n < 2' )
414+ self .assertEqual ('' .join (str (f (x )) for x in range (3 )), '111' )
415+
416+ def test_decimal_number (self ):
417+ self .assertEqual (gettext .c2py ('0123' )(1 ), 123 )
418+
419+ def test_invalid_syntax (self ):
420+ invalid_expressions = [
421+ 'x>1' , '(n>1' , 'n>1)' , '42**42**42' , '0xa' , '1.0' , '1e2' ,
422+ 'n>0x1' , '+n' , '-n' , 'n()' , 'n(1)' , '1+' , 'nn' , 'n n' ,
423+ ]
424+ for expr in invalid_expressions :
425+ with self .assertRaises (ValueError ):
426+ gettext .c2py (expr )
427+
428+ def test_nested_condition_operator (self ):
429+ self .assertEqual (gettext .c2py ('n?1?2:3:4' )(0 ), 4 )
430+ self .assertEqual (gettext .c2py ('n?1?2:3:4' )(1 ), 2 )
431+ self .assertEqual (gettext .c2py ('n?1:3?4:5' )(0 ), 4 )
432+ self .assertEqual (gettext .c2py ('n?1:3?4:5' )(1 ), 1 )
433+
434+ def test_division (self ):
435+ f = gettext .c2py ('2/n*3' )
436+ self .assertEqual (f (1 ), 6 )
437+ self .assertEqual (f (2 ), 3 )
438+ self .assertEqual (f (3 ), 0 )
439+ self .assertEqual (f (- 1 ), - 6 )
440+ self .assertRaises (ZeroDivisionError , f , 0 )
441+
442+ def test_plural_number (self ):
443+ f = gettext .c2py ('1' )
444+ self .assertEqual (f (1 ), 1 )
445+ self .assertRaises (ValueError , f , 1.0 )
446+ self .assertRaises (ValueError , f , '1' )
447+ self .assertRaises (ValueError , f , [])
448+ self .assertRaises (ValueError , f , object ())
449+
367450
368451class GNUTranslationParsingTest (GettextBaseTest ):
369452 def test_plural_form_error_issue17898 (self ):
0 commit comments