@@ -230,7 +230,9 @@ def test_plural_forms2(self):
230230 x = t .ngettext ('There is %s file' , 'There are %s files' , 2 )
231231 eq (x , 'Hay %s ficheros' )
232232
233- def test_hu (self ):
233+ # Examples from http://www.gnu.org/software/gettext/manual/gettext.html
234+
235+ def test_ja (self ):
234236 eq = self .assertEqual
235237 f = gettext .c2py ('0' )
236238 s = '' .join ([ str (f (x )) for x in range (200 ) ])
@@ -248,6 +250,12 @@ def test_fr(self):
248250 s = '' .join ([ str (f (x )) for x in range (200 ) ])
249251 eq (s , "00111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111" )
250252
253+ def test_lv (self ):
254+ eq = self .assertEqual
255+ f = gettext .c2py ('n%10==1 && n%100!=11 ? 0 : n != 0 ? 1 : 2' )
256+ s = '' .join ([ str (f (x )) for x in range (200 ) ])
257+ eq (s , "20111111111111111111101111111110111111111011111111101111111110111111111011111111101111111110111111111011111111111111111110111111111011111111101111111110111111111011111111101111111110111111111011111111" )
258+
251259 def test_gd (self ):
252260 eq = self .assertEqual
253261 f = gettext .c2py ('n==1 ? 0 : n==2 ? 1 : 2' )
@@ -261,6 +269,12 @@ def test_gd2(self):
261269 s = '' .join ([ str (f (x )) for x in range (200 ) ])
262270 eq (s , "20122222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222" )
263271
272+ def test_ro (self ):
273+ eq = self .assertEqual
274+ f = gettext .c2py ('n==1 ? 0 : (n==0 || (n%100 > 0 && n%100 < 20)) ? 1 : 2' )
275+ s = '' .join ([ str (f (x )) for x in range (200 ) ])
276+ eq (s , "10111111111111111111222222222222222222222222222222222222222222222222222222222222222222222222222222222111111111111111111122222222222222222222222222222222222222222222222222222222222222222222222222222222" )
277+
264278 def test_lt (self ):
265279 eq = self .assertEqual
266280 f = gettext .c2py ('n%10==1 && n%100!=11 ? 0 : n%10>=2 && (n%100<10 || n%100>=20) ? 1 : 2' )
@@ -273,6 +287,12 @@ def test_ru(self):
273287 s = '' .join ([ str (f (x )) for x in range (200 ) ])
274288 eq (s , "20111222222222222222201112222220111222222011122222201112222220111222222011122222201112222220111222222011122222222222222220111222222011122222201112222220111222222011122222201112222220111222222011122222" )
275289
290+ def test_cs (self ):
291+ eq = self .assertEqual
292+ f = gettext .c2py ('(n==1) ? 0 : (n>=2 && n<=4) ? 1 : 2' )
293+ s = '' .join ([ str (f (x )) for x in range (200 ) ])
294+ eq (s , "20111222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222" )
295+
276296 def test_pl (self ):
277297 eq = self .assertEqual
278298 f = gettext .c2py ('n==1 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2' )
@@ -285,10 +305,73 @@ def test_sl(self):
285305 s = '' .join ([ str (f (x )) for x in range (200 ) ])
286306 eq (s , "30122333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333012233333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333" )
287307
308+ def test_ar (self ):
309+ eq = self .assertEqual
310+ f = gettext .c2py ('n==0 ? 0 : n==1 ? 1 : n==2 ? 2 : n%100>=3 && n%100<=10 ? 3 : n%100>=11 ? 4 : 5' )
311+ s = '' .join ([ str (f (x )) for x in range (200 ) ])
312+ eq (s , "01233333333444444444444444444444444444444444444444444444444444444444444444444444444444444444444444445553333333344444444444444444444444444444444444444444444444444444444444444444444444444444444444444444" )
313+
288314 def test_security (self ):
289315 raises = self .assertRaises
290316 # Test for a dangerous expression
291317 raises (ValueError , gettext .c2py , "os.chmod('/etc/passwd',0777)" )
318+ # issue28563
319+ raises (ValueError , gettext .c2py , '"(eval(foo) && ""' )
320+ raises (ValueError , gettext .c2py , 'f"{os.system(\' sh\' )}"' )
321+ # Maximum recursion depth exceeded during compilation
322+ raises (ValueError , gettext .c2py , 'n+' * 10000 + 'n' )
323+ self .assertEqual (gettext .c2py ('n+' * 100 + 'n' )(1 ), 101 )
324+ # MemoryError during compilation
325+ raises (ValueError , gettext .c2py , '(' * 100 + 'n' + ')' * 100 )
326+ # Maximum recursion depth exceeded in C to Python translator
327+ raises (ValueError , gettext .c2py , '(' * 10000 + 'n' + ')' * 10000 )
328+ self .assertEqual (gettext .c2py ('(' * 20 + 'n' + ')' * 20 )(1 ), 1 )
329+
330+ def test_chained_comparison (self ):
331+ # C doesn't chain comparison as Python so 2 == 2 == 2 gets different results
332+ f = gettext .c2py ('n == n == n' )
333+ self .assertEqual ('' .join (str (f (x )) for x in range (3 )), '010' )
334+ f = gettext .c2py ('1 < n == n' )
335+ self .assertEqual ('' .join (str (f (x )) for x in range (3 )), '100' )
336+ f = gettext .c2py ('n == n < 2' )
337+ self .assertEqual ('' .join (str (f (x )) for x in range (3 )), '010' )
338+ f = gettext .c2py ('0 < n < 2' )
339+ self .assertEqual ('' .join (str (f (x )) for x in range (3 )), '111' )
340+
341+ def test_decimal_number (self ):
342+ self .assertEqual (gettext .c2py ('0123' )(1 ), 123 )
343+
344+ def test_invalid_syntax (self ):
345+ invalid_expressions = [
346+ 'x>1' , '(n>1' , 'n>1)' , '42**42**42' , '0xa' , '1.0' , '1e2' ,
347+ 'n>0x1' , '+n' , '-n' , 'n()' , 'n(1)' , '1+' , 'nn' , 'n n' ,
348+ ]
349+ for expr in invalid_expressions :
350+ with self .assertRaises (ValueError ):
351+ gettext .c2py (expr )
352+
353+ def test_nested_condition_operator (self ):
354+ self .assertEqual (gettext .c2py ('n?1?2:3:4' )(0 ), 4 )
355+ self .assertEqual (gettext .c2py ('n?1?2:3:4' )(1 ), 2 )
356+ self .assertEqual (gettext .c2py ('n?1:3?4:5' )(0 ), 4 )
357+ self .assertEqual (gettext .c2py ('n?1:3?4:5' )(1 ), 1 )
358+
359+ def test_division (self ):
360+ f = gettext .c2py ('2/n*3' )
361+ self .assertEqual (f (1 ), 6 )
362+ self .assertEqual (f (2 ), 3 )
363+ self .assertEqual (f (3 ), 0 )
364+ self .assertEqual (f (- 1 ), - 6 )
365+ self .assertRaises (ZeroDivisionError , f , 0 )
366+
367+ def test_plural_number (self ):
368+ f = gettext .c2py ('1' )
369+ self .assertEqual (f (1 ), 1 )
370+ self .assertRaises (ValueError , f , 1.0 )
371+ self .assertRaises (ValueError , f , '1' )
372+ self .assertRaises (ValueError , f , [])
373+ self .assertRaises (ValueError , f , object ())
374+
292375
293376
294377class UnicodeTranslationsTest (GettextBaseTest ):
0 commit comments