88
99def get_enUS_locale ():
1010 global enUS_locale
11- if sys .platform == 'darwin' :
12- raise unittest .SkipTest ("Locale support on MacOSX is minimal" )
1311 if sys .platform .startswith ("win" ):
1412 tlocs = ("En" , "English" )
1513 else :
16- tlocs = ("en_US.UTF-8" , "en_US.US-ASCII" , "en_US" )
14+ tlocs = ("en_US.UTF-8" , "en_US.ISO8859-1" , "en_US. US-ASCII" , "en_US" )
1715 oldlocale = locale .setlocale (locale .LC_NUMERIC )
1816 for tloc in tlocs :
1917 try :
@@ -309,6 +307,39 @@ def test_currency(self):
309307 grouping = True , international = True )
310308
311309
310+ class TestCollation (unittest .TestCase ):
311+ # Test string collation functions
312+
313+ def test_strcoll (self ):
314+ self .assertLess (locale .strcoll ('a' , 'b' ), 0 )
315+ self .assertEqual (locale .strcoll ('a' , 'a' ), 0 )
316+ self .assertGreater (locale .strcoll ('b' , 'a' ), 0 )
317+
318+ def test_strxfrm (self ):
319+ self .assertLess (locale .strxfrm ('a' ), locale .strxfrm ('b' ))
320+
321+
322+ class TestEnUSCollation (BaseLocalizedTest , TestCollation ):
323+ # Test string collation functions with a real English locale
324+
325+ locale_type = locale .LC_ALL
326+
327+ def setUp (self ):
328+ BaseLocalizedTest .setUp (self )
329+ enc = codecs .lookup (locale .getpreferredencoding (False ) or 'ascii' ).name
330+ if enc not in ('utf-8' , 'iso8859-1' , 'cp1252' ):
331+ raise unittest .SkipTest ('encoding not suitable' )
332+ if enc != 'iso8859-1' and (sys .platform == 'darwin' or
333+ sys .platform .startswith ('freebsd' )):
334+ raise unittest .SkipTest ('wcscoll/wcsxfrm have known bugs' )
335+
336+ def test_strcoll_with_diacritic (self ):
337+ self .assertLess (locale .strcoll ('à' , 'b' ), 0 )
338+
339+ def test_strxfrm_with_diacritic (self ):
340+ self .assertLess (locale .strxfrm ('à' ), locale .strxfrm ('b' ))
341+
342+
312343class TestMiscellaneous (unittest .TestCase ):
313344 def test_getpreferredencoding (self ):
314345 # Invoke getpreferredencoding to make sure it does not cause exceptions.
@@ -317,11 +348,10 @@ def test_getpreferredencoding(self):
317348 # If encoding non-empty, make sure it is valid
318349 codecs .lookup (enc )
319350
320- if hasattr (locale , "strcoll" ):
321- def test_strcoll_3303 (self ):
322- # test crasher from bug #3303
323- self .assertRaises (TypeError , locale .strcoll , "a" , None )
324- self .assertRaises (TypeError , locale .strcoll , b"a" , None )
351+ def test_strcoll_3303 (self ):
352+ # test crasher from bug #3303
353+ self .assertRaises (TypeError , locale .strcoll , "a" , None )
354+ self .assertRaises (TypeError , locale .strcoll , b"a" , None )
325355
326356
327357def test_main ():
@@ -331,6 +361,7 @@ def test_main():
331361 TestEnUSNumberFormatting ,
332362 TestCNumberFormatting ,
333363 TestFrFRNumberFormatting ,
364+ TestCollation
334365 ]
335366 # SkipTest can't be raised inside unittests, handle it manually instead
336367 try :
@@ -339,7 +370,7 @@ def test_main():
339370 if verbose :
340371 print ("Some tests will be disabled: %s" % e )
341372 else :
342- tests += [TestNumberFormatting ]
373+ tests += [TestNumberFormatting , TestEnUSCollation ]
343374 run_unittest (* tests )
344375
345376if __name__ == '__main__' :
0 commit comments