Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 1 | from test.support import verbose, TestSkipped |
Martin v. Löwis | 88ad12a | 2001-04-13 08:09:50 +0000 | [diff] [blame] | 2 | import locale |
Guido van Rossum | fc34986 | 2001-04-15 13:15:56 +0000 | [diff] [blame] | 3 | import sys |
Martin v. Löwis | 88ad12a | 2001-04-13 08:09:50 +0000 | [diff] [blame] | 4 | |
Jack Jansen | 06f0cef | 2002-12-30 23:02:55 +0000 | [diff] [blame] | 5 | if sys.platform == 'darwin': |
Collin Winter | 3add4d7 | 2007-08-29 23:37:32 +0000 | [diff] [blame] | 6 | raise TestSkipped( |
| 7 | "Locale support on MacOSX is minimal and cannot be tested") |
Martin v. Löwis | 88ad12a | 2001-04-13 08:09:50 +0000 | [diff] [blame] | 8 | oldlocale = locale.setlocale(locale.LC_NUMERIC) |
| 9 | |
Hye-Shik Chang | 8d2e08d | 2003-12-19 01:16:03 +0000 | [diff] [blame] | 10 | if sys.platform.startswith("win"): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 11 | tlocs = ("En", "English") |
Hye-Shik Chang | 8d2e08d | 2003-12-19 01:16:03 +0000 | [diff] [blame] | 12 | else: |
Anthony Baxter | 451ae18 | 2005-06-03 15:04:15 +0000 | [diff] [blame] | 13 | tlocs = ("en_US.UTF-8", "en_US.US-ASCII", "en_US") |
Guido van Rossum | fc34986 | 2001-04-15 13:15:56 +0000 | [diff] [blame] | 14 | |
Anthony Baxter | 451ae18 | 2005-06-03 15:04:15 +0000 | [diff] [blame] | 15 | for tloc in tlocs: |
| 16 | try: |
| 17 | locale.setlocale(locale.LC_NUMERIC, tloc) |
| 18 | break |
| 19 | except locale.Error: |
| 20 | continue |
| 21 | else: |
Collin Winter | 3add4d7 | 2007-08-29 23:37:32 +0000 | [diff] [blame] | 22 | raise ImportError( |
| 23 | "test locale not supported (tried %s)" % (', '.join(tlocs))) |
Martin v. Löwis | 88ad12a | 2001-04-13 08:09:50 +0000 | [diff] [blame] | 24 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 25 | def testformat(formatstr, value, grouping = 0, output=None, func=locale.format): |
Martin v. Löwis | 88ad12a | 2001-04-13 08:09:50 +0000 | [diff] [blame] | 26 | if verbose: |
| 27 | if output: |
Collin Winter | 3add4d7 | 2007-08-29 23:37:32 +0000 | [diff] [blame] | 28 | print("%s %% %s =? %s ..." % |
| 29 | (repr(formatstr), repr(value), repr(output)), end=' ') |
Martin v. Löwis | 88ad12a | 2001-04-13 08:09:50 +0000 | [diff] [blame] | 30 | else: |
Collin Winter | 3add4d7 | 2007-08-29 23:37:32 +0000 | [diff] [blame] | 31 | print("%s %% %s works? ..." % (repr(formatstr), repr(value)), |
| 32 | end=' ') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 33 | result = func(formatstr, value, grouping = grouping) |
Martin v. Löwis | 88ad12a | 2001-04-13 08:09:50 +0000 | [diff] [blame] | 34 | if output and result != output: |
| 35 | if verbose: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 36 | print('no') |
Collin Winter | 3add4d7 | 2007-08-29 23:37:32 +0000 | [diff] [blame] | 37 | print("%s %% %s == %s != %s" % |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 38 | (repr(formatstr), repr(value), repr(result), repr(output))) |
Martin v. Löwis | 88ad12a | 2001-04-13 08:09:50 +0000 | [diff] [blame] | 39 | else: |
| 40 | if verbose: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 41 | print("yes") |
Martin v. Löwis | 88ad12a | 2001-04-13 08:09:50 +0000 | [diff] [blame] | 42 | |
| 43 | try: |
Martin v. Löwis | 4cfa136 | 2005-12-30 12:51:45 +0000 | [diff] [blame] | 44 | # On Solaris 10, the thousands_sep is the empty string |
| 45 | sep = locale.localeconv()['thousands_sep'] |
| 46 | testformat("%f", 1024, grouping=1, output='1%s024.000000' % sep) |
Martin v. Löwis | 88ad12a | 2001-04-13 08:09:50 +0000 | [diff] [blame] | 47 | testformat("%f", 102, grouping=1, output='102.000000') |
| 48 | testformat("%f", -42, grouping=1, output='-42.000000') |
| 49 | testformat("%+f", -42, grouping=1, output='-42.000000') |
| 50 | testformat("%20.f", -42, grouping=1, output=' -42') |
Martin v. Löwis | 4cfa136 | 2005-12-30 12:51:45 +0000 | [diff] [blame] | 51 | testformat("%+10.f", -4200, grouping=1, output=' -4%s200' % sep) |
| 52 | testformat("%-10.f", 4200, grouping=1, output='4%s200 ' % sep) |
Martin v. Löwis | f0a4668 | 2002-11-03 17:20:12 +0000 | [diff] [blame] | 53 | # Invoke getpreferredencoding to make sure it does not cause exceptions, |
| 54 | locale.getpreferredencoding() |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 55 | |
| 56 | # === Test format() with more complex formatting strings |
| 57 | # test if grouping is independent from other characters in formatting string |
| 58 | testformat("One million is %i", 1000000, grouping=1, |
| 59 | output='One million is 1%s000%s000' % (sep, sep), |
| 60 | func=locale.format_string) |
| 61 | testformat("One million is %i", 1000000, grouping=1, |
| 62 | output='One million is 1%s000%s000' % (sep, sep), |
| 63 | func=locale.format_string) |
| 64 | # test dots in formatting string |
| 65 | testformat(".%f.", 1000.0, output='.1000.000000.', func=locale.format_string) |
| 66 | # test floats |
| 67 | testformat("--> %10.2f", 1000.0, grouping=1, output='--> 1%s000.00' % sep, |
| 68 | func=locale.format_string) |
| 69 | # test asterisk formats |
| 70 | testformat("%10.*f", (2, 1000.0), grouping=0, output=' 1000.00', |
| 71 | func=locale.format_string) |
| 72 | testformat("%*.*f", (10, 2, 1000.0), grouping=1, output=' 1%s000.00' % sep, |
| 73 | func=locale.format_string) |
| 74 | # test more-in-one |
| 75 | testformat("int %i float %.2f str %s", (1000, 1000.0, 'str'), grouping=1, |
| 76 | output='int 1%s000 float 1%s000.00 str str' % (sep, sep), |
| 77 | func=locale.format_string) |
| 78 | |
Martin v. Löwis | 88ad12a | 2001-04-13 08:09:50 +0000 | [diff] [blame] | 79 | finally: |
| 80 | locale.setlocale(locale.LC_NUMERIC, oldlocale) |