@@ -236,7 +236,9 @@ def test_plural_forms2(self):
236236 x = t .ngettext ('There is %s file' , 'There are %s files' , 2 )
237237 eq (x , 'Hay %s ficheros' )
238238
239- def test_hu (self ):
239+ # Examples from http://www.gnu.org/software/gettext/manual/gettext.html
240+
241+ def test_ja (self ):
240242 eq = self .assertEqual
241243 f = gettext .c2py ('0' )
242244 s = '' .join ([ str (f (x )) for x in range (200 ) ])
@@ -254,6 +256,12 @@ def test_fr(self):
254256 s = '' .join ([ str (f (x )) for x in range (200 ) ])
255257 eq (s , "00111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111" )
256258
259+ def test_lv (self ):
260+ eq = self .assertEqual
261+ f = gettext .c2py ('n%10==1 && n%100!=11 ? 0 : n != 0 ? 1 : 2' )
262+ s = '' .join ([ str (f (x )) for x in range (200 ) ])
263+ eq (s , "20111111111111111111101111111110111111111011111111101111111110111111111011111111101111111110111111111011111111111111111110111111111011111111101111111110111111111011111111101111111110111111111011111111" )
264+
257265 def test_gd (self ):
258266 eq = self .assertEqual
259267 f = gettext .c2py ('n==1 ? 0 : n==2 ? 1 : 2' )
@@ -267,6 +275,12 @@ def test_gd2(self):
267275 s = '' .join ([ str (f (x )) for x in range (200 ) ])
268276 eq (s , "20122222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222" )
269277
278+ def test_ro (self ):
279+ eq = self .assertEqual
280+ f = gettext .c2py ('n==1 ? 0 : (n==0 || (n%100 > 0 && n%100 < 20)) ? 1 : 2' )
281+ s = '' .join ([ str (f (x )) for x in range (200 ) ])
282+ eq (s , "10111111111111111111222222222222222222222222222222222222222222222222222222222222222222222222222222222111111111111111111122222222222222222222222222222222222222222222222222222222222222222222222222222222" )
283+
270284 def test_lt (self ):
271285 eq = self .assertEqual
272286 f = gettext .c2py ('n%10==1 && n%100!=11 ? 0 : n%10>=2 && (n%100<10 || n%100>=20) ? 1 : 2' )
@@ -279,6 +293,12 @@ def test_ru(self):
279293 s = '' .join ([ str (f (x )) for x in range (200 ) ])
280294 eq (s , "20111222222222222222201112222220111222222011122222201112222220111222222011122222201112222220111222222011122222222222222220111222222011122222201112222220111222222011122222201112222220111222222011122222" )
281295
296+ def test_cs (self ):
297+ eq = self .assertEqual
298+ f = gettext .c2py ('(n==1) ? 0 : (n>=2 && n<=4) ? 1 : 2' )
299+ s = '' .join ([ str (f (x )) for x in range (200 ) ])
300+ eq (s , "20111222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222" )
301+
282302 def test_pl (self ):
283303 eq = self .assertEqual
284304 f = gettext .c2py ('n==1 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2' )
@@ -291,10 +311,73 @@ def test_sl(self):
291311 s = '' .join ([ str (f (x )) for x in range (200 ) ])
292312 eq (s , "30122333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333012233333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333" )
293313
314+ def test_ar (self ):
315+ eq = self .assertEqual
316+ f = gettext .c2py ('n==0 ? 0 : n==1 ? 1 : n==2 ? 2 : n%100>=3 && n%100<=10 ? 3 : n%100>=11 ? 4 : 5' )
317+ s = '' .join ([ str (f (x )) for x in range (200 ) ])
318+ eq (s , "01233333333444444444444444444444444444444444444444444444444444444444444444444444444444444444444444445553333333344444444444444444444444444444444444444444444444444444444444444444444444444444444444444444" )
319+
294320 def test_security (self ):
295321 raises = self .assertRaises
296322 # Test for a dangerous expression
297323 raises (ValueError , gettext .c2py , "os.chmod('/etc/passwd',0777)" )
324+ # issue28563
325+ raises (ValueError , gettext .c2py , '"(eval(foo) && ""' )
326+ raises (ValueError , gettext .c2py , 'f"{os.system(\' sh\' )}"' )
327+ # Maximum recursion depth exceeded during compilation
328+ raises (ValueError , gettext .c2py , 'n+' * 10000 + 'n' )
329+ self .assertEqual (gettext .c2py ('n+' * 100 + 'n' )(1 ), 101 )
330+ # MemoryError during compilation
331+ raises (ValueError , gettext .c2py , '(' * 100 + 'n' + ')' * 100 )
332+ # Maximum recursion depth exceeded in C to Python translator
333+ raises (ValueError , gettext .c2py , '(' * 10000 + 'n' + ')' * 10000 )
334+ self .assertEqual (gettext .c2py ('(' * 20 + 'n' + ')' * 20 )(1 ), 1 )
335+
336+ def test_chained_comparison (self ):
337+ # C doesn't chain comparison as Python so 2 == 2 == 2 gets different results
338+ f = gettext .c2py ('n == n == n' )
339+ self .assertEqual ('' .join (str (f (x )) for x in range (3 )), '010' )
340+ f = gettext .c2py ('1 < n == n' )
341+ self .assertEqual ('' .join (str (f (x )) for x in range (3 )), '100' )
342+ f = gettext .c2py ('n == n < 2' )
343+ self .assertEqual ('' .join (str (f (x )) for x in range (3 )), '010' )
344+ f = gettext .c2py ('0 < n < 2' )
345+ self .assertEqual ('' .join (str (f (x )) for x in range (3 )), '111' )
346+
347+ def test_decimal_number (self ):
348+ self .assertEqual (gettext .c2py ('0123' )(1 ), 123 )
349+
350+ def test_invalid_syntax (self ):
351+ invalid_expressions = [
352+ 'x>1' , '(n>1' , 'n>1)' , '42**42**42' , '0xa' , '1.0' , '1e2' ,
353+ 'n>0x1' , '+n' , '-n' , 'n()' , 'n(1)' , '1+' , 'nn' , 'n n' ,
354+ ]
355+ for expr in invalid_expressions :
356+ with self .assertRaises (ValueError ):
357+ gettext .c2py (expr )
358+
359+ def test_nested_condition_operator (self ):
360+ self .assertEqual (gettext .c2py ('n?1?2:3:4' )(0 ), 4 )
361+ self .assertEqual (gettext .c2py ('n?1?2:3:4' )(1 ), 2 )
362+ self .assertEqual (gettext .c2py ('n?1:3?4:5' )(0 ), 4 )
363+ self .assertEqual (gettext .c2py ('n?1:3?4:5' )(1 ), 1 )
364+
365+ def test_division (self ):
366+ f = gettext .c2py ('2/n*3' )
367+ self .assertEqual (f (1 ), 6 )
368+ self .assertEqual (f (2 ), 3 )
369+ self .assertEqual (f (3 ), 0 )
370+ self .assertEqual (f (- 1 ), - 6 )
371+ self .assertRaises (ZeroDivisionError , f , 0 )
372+
373+ def test_plural_number (self ):
374+ f = gettext .c2py ('1' )
375+ self .assertEqual (f (1 ), 1 )
376+ self .assertRaises (ValueError , f , 1.0 )
377+ self .assertRaises (ValueError , f , '1' )
378+ self .assertRaises (ValueError , f , [])
379+ self .assertRaises (ValueError , f , object ())
380+
298381
299382class GNUTranslationParsingTest (GettextBaseTest ):
300383 def test_plural_form_error_issue17898 (self ):
0 commit comments