44import sys
55import codecs
66
7- class BaseLocalizedTest (unittest .TestCase ):
8- #
9- # Base class for tests using a real locale
10- #
7+ enUS_locale = None
118
9+ def get_enUS_locale ():
10+ global enUS_locale
11+ if sys .platform == 'darwin' :
12+ raise TestSkipped ("Locale support on MacOSX is minimal" )
1213 if sys .platform .startswith ("win" ):
1314 tlocs = ("En" , "English" )
1415 else :
1516 tlocs = ("en_US.UTF-8" , "en_US.US-ASCII" , "en_US" )
17+ oldlocale = locale .setlocale (locale .LC_NUMERIC )
18+ for tloc in tlocs :
19+ try :
20+ locale .setlocale (locale .LC_NUMERIC , tloc )
21+ except locale .Error :
22+ continue
23+ break
24+ else :
25+ raise TestSkipped (
26+ "Test locale not supported (tried %s)" % (', ' .join (tlocs )))
27+ enUS_locale = tloc
28+ locale .setlocale (locale .LC_NUMERIC , oldlocale )
29+
30+
31+ class BaseLocalizedTest (unittest .TestCase ):
32+ #
33+ # Base class for tests using a real locale
34+ #
1635
1736 def setUp (self ):
18- if sys .platform == 'darwin' :
19- raise TestSkipped (
20- "Locale support on MacOSX is minimal and cannot be tested" )
2137 self .oldlocale = locale .setlocale (self .locale_type )
22- for tloc in self .tlocs :
23- try :
24- locale .setlocale (self .locale_type , tloc )
25- except locale .Error :
26- continue
27- break
28- else :
29- raise TestSkipped (
30- "Test locale not supported (tried %s)" % (', ' .join (self .tlocs )))
38+ locale .setlocale (self .locale_type , enUS_locale )
3139 if verbose :
32- print ("testing with \" %s\" ..." % tloc , end = ' ' )
40+ print ("testing with \" %s\" ..." % enUS_locale , end = ' ' )
3341
3442 def tearDown (self ):
3543 locale .setlocale (self .locale_type , self .oldlocale )
@@ -117,9 +125,10 @@ def _test_currency(self, value, out, **format_opts):
117125
118126
119127class EnUSNumberFormatting (BaseFormattingTest ):
128+ # XXX there is a grouping + padding bug when the thousands separator
129+ # is empty but the grouping array contains values (e.g. Solaris 10)
120130
121131 def setUp (self ):
122- # NOTE: On Solaris 10, the thousands_sep is the empty string
123132 self .sep = locale .localeconv ()['thousands_sep' ]
124133
125134 def test_grouping (self ):
@@ -130,10 +139,11 @@ def test_grouping(self):
130139
131140 def test_grouping_and_padding (self ):
132141 self ._test_format ("%20.f" , - 42 , grouping = 1 , out = '-42' .rjust (20 ))
133- self ._test_format ("%+10.f" , - 4200 , grouping = 1 ,
134- out = ('-4%s200' % self .sep ).rjust (10 ))
135- self ._test_format ("%-10.f" , - 4200 , grouping = 1 ,
136- out = ('-4%s200' % self .sep ).ljust (10 ))
142+ if self .sep :
143+ self ._test_format ("%+10.f" , - 4200 , grouping = 1 ,
144+ out = ('-4%s200' % self .sep ).rjust (10 ))
145+ self ._test_format ("%-10.f" , - 4200 , grouping = 1 ,
146+ out = ('-4%s200' % self .sep ).ljust (10 ))
137147
138148 def test_integer_grouping (self ):
139149 self ._test_format ("%d" , 4200 , grouping = True , out = '4%s200' % self .sep )
@@ -160,17 +170,21 @@ def test_complex_formatting(self):
160170 # Dots in formatting string
161171 self ._test_format_string (".%f." , 1000.0 , out = '.1000.000000.' )
162172 # Padding
163- self ._test_format_string ("--> %10.2f" , 4200 , grouping = 1 ,
164- out = '--> ' + ('4%s200.00' % self .sep ).rjust (10 ))
173+ if self .sep :
174+ self ._test_format_string ("--> %10.2f" , 4200 , grouping = 1 ,
175+ out = '--> ' + ('4%s200.00' % self .sep ).rjust (10 ))
165176 # Asterisk formats
166177 self ._test_format_string ("%10.*f" , (2 , 1000 ), grouping = 0 ,
167178 out = '1000.00' .rjust (10 ))
168- self ._test_format_string ("%*.*f" , (10 , 2 , 1000 ), grouping = 1 ,
169- out = ('1%s000.00' % self .sep ).rjust (10 ))
179+ if self .sep :
180+ self ._test_format_string ("%*.*f" , (10 , 2 , 1000 ), grouping = 1 ,
181+ out = ('1%s000.00' % self .sep ).rjust (10 ))
170182 # Test more-in-one
171- self ._test_format_string ("int %i float %.2f str %s" ,
172- (1000 , 1000.0 , 'str' ), grouping = 1 ,
173- out = 'int 1%s000 float 1%s000.00 str str' % (self .sep , self .sep ))
183+ if self .sep :
184+ self ._test_format_string ("int %i float %.2f str %s" ,
185+ (1000 , 1000.0 , 'str' ), grouping = 1 ,
186+ out = 'int 1%s000 float 1%s000.00 str str' %
187+ (self .sep , self .sep ))
174188
175189
176190class TestNumberFormatting (BaseLocalizedTest , EnUSNumberFormatting ):
@@ -223,7 +237,20 @@ def test_strcoll_3303(self):
223237
224238
225239def test_main ():
226- run_unittest (__name__ )
240+ tests = [
241+ TestMiscellaneous ,
242+ TestEnUSNumberFormatting ,
243+ TestCNumberFormatting
244+ ]
245+ # TestSkipped can't be raised inside unittests, handle it manually instead
246+ try :
247+ get_enUS_locale ()
248+ except TestSkipped as e :
249+ if verbose :
250+ print ("Some tests will be disabled: %s" % e )
251+ else :
252+ tests += [TestNumberFormatting ]
253+ run_unittest (* tests )
227254
228255if __name__ == '__main__' :
229256 test_main ()
0 commit comments