blob: 1bfb3b33b9b5708d27c7ea576fd45c1c47276a66 [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':
Collin Winter3add4d72007-08-29 23:37:32 +00006 raise TestSkipped(
7 "Locale support on MacOSX is minimal and cannot be tested")
Martin v. Löwis88ad12a2001-04-13 08:09:50 +00008oldlocale = locale.setlocale(locale.LC_NUMERIC)
9
Hye-Shik Chang8d2e08d2003-12-19 01:16:03 +000010if sys.platform.startswith("win"):
Guido van Rossumd8faa362007-04-27 19:54:29 +000011 tlocs = ("En", "English")
Hye-Shik Chang8d2e08d2003-12-19 01:16:03 +000012else:
Anthony Baxter451ae182005-06-03 15:04:15 +000013 tlocs = ("en_US.UTF-8", "en_US.US-ASCII", "en_US")
Guido van Rossumfc349862001-04-15 13:15:56 +000014
Anthony Baxter451ae182005-06-03 15:04:15 +000015for tloc in tlocs:
16 try:
17 locale.setlocale(locale.LC_NUMERIC, tloc)
18 break
19 except locale.Error:
20 continue
21else:
Collin Winter3add4d72007-08-29 23:37:32 +000022 raise ImportError(
23 "test locale not supported (tried %s)" % (', '.join(tlocs)))
Martin v. Löwis88ad12a2001-04-13 08:09:50 +000024
Thomas Wouters477c8d52006-05-27 19:21:47 +000025def testformat(formatstr, value, grouping = 0, output=None, func=locale.format):
Martin v. Löwis88ad12a2001-04-13 08:09:50 +000026 if verbose:
27 if output:
Collin Winter3add4d72007-08-29 23:37:32 +000028 print("%s %% %s =? %s ..." %
29 (repr(formatstr), repr(value), repr(output)), end=' ')
Martin v. Löwis88ad12a2001-04-13 08:09:50 +000030 else:
Collin Winter3add4d72007-08-29 23:37:32 +000031 print("%s %% %s works? ..." % (repr(formatstr), repr(value)),
32 end=' ')
Thomas Wouters477c8d52006-05-27 19:21:47 +000033 result = func(formatstr, value, grouping = grouping)
Martin v. Löwis88ad12a2001-04-13 08:09:50 +000034 if output and result != output:
35 if verbose:
Guido van Rossumbe19ed72007-02-09 05:37:30 +000036 print('no')
Collin Winter3add4d72007-08-29 23:37:32 +000037 print("%s %% %s == %s != %s" %
Guido van Rossumbe19ed72007-02-09 05:37:30 +000038 (repr(formatstr), repr(value), repr(result), repr(output)))
Martin v. Löwis88ad12a2001-04-13 08:09:50 +000039 else:
40 if verbose:
Guido van Rossumbe19ed72007-02-09 05:37:30 +000041 print("yes")
Martin v. Löwis88ad12a2001-04-13 08:09:50 +000042
43try:
Martin v. Löwis4cfa1362005-12-30 12:51:45 +000044 # 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öwis88ad12a2001-04-13 08:09:50 +000047 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öwis4cfa1362005-12-30 12:51:45 +000051 testformat("%+10.f", -4200, grouping=1, output=' -4%s200' % sep)
52 testformat("%-10.f", 4200, grouping=1, output='4%s200 ' % sep)
Martin v. Löwisf0a46682002-11-03 17:20:12 +000053 # Invoke getpreferredencoding to make sure it does not cause exceptions,
54 locale.getpreferredencoding()
Thomas Wouters477c8d52006-05-27 19:21:47 +000055
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öwis88ad12a2001-04-13 08:09:50 +000079finally:
80 locale.setlocale(locale.LC_NUMERIC, oldlocale)
Hye-Shik Changb5047fd2004-08-04 06:33:51 +000081
82
83# Test BSD Rune locale's bug for isctype functions.
84def teststrop(s, method, output):
Guido van Rossum1a1f61b2007-05-17 21:11:47 +000085 s = str8(s)
Hye-Shik Changb5047fd2004-08-04 06:33:51 +000086 if verbose:
Guido van Rossumbe19ed72007-02-09 05:37:30 +000087 print("%s.%s() =? %s ..." % (repr(s), method, repr(output)), end=' ')
Hye-Shik Changb5047fd2004-08-04 06:33:51 +000088 result = getattr(s, method)()
89 if result != output:
90 if verbose:
Guido van Rossumbe19ed72007-02-09 05:37:30 +000091 print("no")
92 print("%s.%s() == %s != %s" % (repr(s), method, repr(result),
93 repr(output)))
Hye-Shik Changb5047fd2004-08-04 06:33:51 +000094 elif verbose:
Guido van Rossumbe19ed72007-02-09 05:37:30 +000095 print("yes")
Hye-Shik Changb5047fd2004-08-04 06:33:51 +000096
97try:
Martin v. Löwis4cfa1362005-12-30 12:51:45 +000098 if sys.platform == 'sunos5':
99 # On Solaris, in en_US.UTF-8, \xa0 is a space
100 raise locale.Error
Hye-Shik Changb5047fd2004-08-04 06:33:51 +0000101 oldlocale = locale.setlocale(locale.LC_CTYPE)
102 locale.setlocale(locale.LC_CTYPE, 'en_US.UTF-8')
103except locale.Error:
104 pass
105else:
106 try:
107 teststrop('\x20', 'isspace', True)
108 teststrop('\xa0', 'isspace', False)
109 teststrop('\xa1', 'isspace', False)
110 teststrop('\xc0', 'isalpha', False)
111 teststrop('\xc0', 'isalnum', False)
112 teststrop('\xc0', 'isupper', False)
113 teststrop('\xc0', 'islower', False)
114 teststrop('\xec\xa0\xbc', 'split', ['\xec\xa0\xbc'])
115 teststrop('\xed\x95\xa0', 'strip', '\xed\x95\xa0')
116 teststrop('\xcc\x85', 'lower', '\xcc\x85')
117 teststrop('\xed\x95\xa0', 'upper', '\xed\x95\xa0')
118 finally:
119 locale.setlocale(locale.LC_CTYPE, oldlocale)