blob: 9187c9e84b5a96ab5e69cb4a7f7b5a7a71861a12 [file] [log] [blame]
Jack Jansen06f0cef2002-12-30 23:02:55 +00001from test.test_support import verbose, TestSkipped
Martin v. Löwis88ad12a2001-04-13 08:09:50 +00002import locale
Guido van Rossumfc349862001-04-15 13:15:56 +00003import sys
Martin v. Löwis88ad12a2001-04-13 08:09:50 +00004
Jack Jansen06f0cef2002-12-30 23:02:55 +00005if sys.platform == 'darwin':
Tim Petersf2715e02003-02-19 02:35:07 +00006 raise TestSkipped("Locale support on MacOSX is minimal and cannot be tested")
Martin v. Löwis88ad12a2001-04-13 08:09:50 +00007oldlocale = locale.setlocale(locale.LC_NUMERIC)
8
Hye-Shik Chang8d2e08d2003-12-19 01:16:03 +00009if sys.platform.startswith("win"):
Guido van Rossumfc349862001-04-15 13:15:56 +000010 tloc = "en"
Hye-Shik Chang8d2e08d2003-12-19 01:16:03 +000011elif sys.platform.startswith("freebsd"):
12 tloc = "en_US.US-ASCII"
13else:
14 tloc = "en_US"
Guido van Rossumfc349862001-04-15 13:15:56 +000015
Martin v. Löwis88ad12a2001-04-13 08:09:50 +000016try:
Guido van Rossumfc349862001-04-15 13:15:56 +000017 locale.setlocale(locale.LC_NUMERIC, tloc)
Martin v. Löwis88ad12a2001-04-13 08:09:50 +000018except locale.Error:
Guido van Rossumfc349862001-04-15 13:15:56 +000019 raise ImportError, "test locale %s not supported" % tloc
Martin v. Löwis88ad12a2001-04-13 08:09:50 +000020
21def testformat(formatstr, value, grouping = 0, output=None):
22 if verbose:
23 if output:
24 print "%s %% %s =? %s ..." %\
25 (repr(formatstr), repr(value), repr(output)),
26 else:
27 print "%s %% %s works? ..." % (repr(formatstr), repr(value)),
28 result = locale.format(formatstr, value, grouping = grouping)
29 if output and result != output:
30 if verbose:
31 print 'no'
32 print "%s %% %s == %s != %s" %\
33 (repr(formatstr), repr(value), repr(result), repr(output))
34 else:
35 if verbose:
36 print "yes"
37
38try:
39 testformat("%f", 1024, grouping=1, output='1,024.000000')
40 testformat("%f", 102, grouping=1, output='102.000000')
41 testformat("%f", -42, grouping=1, output='-42.000000')
42 testformat("%+f", -42, grouping=1, output='-42.000000')
43 testformat("%20.f", -42, grouping=1, output=' -42')
Tim Peters8ae2df42001-05-02 05:54:44 +000044 testformat("%+10.f", -4200, grouping=1, output=' -4,200')
45 testformat("%-10.f", 4200, grouping=1, output='4,200 ')
Martin v. Löwisf0a46682002-11-03 17:20:12 +000046 # Invoke getpreferredencoding to make sure it does not cause exceptions,
47 locale.getpreferredencoding()
Martin v. Löwis88ad12a2001-04-13 08:09:50 +000048finally:
49 locale.setlocale(locale.LC_NUMERIC, oldlocale)