blob: ec5a533aaf4b6316d74cfde3f6b24daee252a566 [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"):
Anthony Baxter451ae182005-06-03 15:04:15 +000010 tlocs = ("en",)
Hye-Shik Chang8d2e08d2003-12-19 01:16:03 +000011else:
Anthony Baxter451ae182005-06-03 15:04:15 +000012 tlocs = ("en_US.UTF-8", "en_US.US-ASCII", "en_US")
Guido van Rossumfc349862001-04-15 13:15:56 +000013
Anthony Baxter451ae182005-06-03 15:04:15 +000014for tloc in tlocs:
15 try:
16 locale.setlocale(locale.LC_NUMERIC, tloc)
17 break
18 except locale.Error:
19 continue
20else:
21 raise ImportError, "test locale not supported (tried %s)"%(', '.join(tlocs))
Martin v. Löwis88ad12a2001-04-13 08:09:50 +000022
Georg Brandlb89316f2006-05-17 15:51:16 +000023def testformat(formatstr, value, grouping = 0, output=None, func=locale.format):
Martin v. Löwis88ad12a2001-04-13 08:09:50 +000024 if verbose:
25 if output:
26 print "%s %% %s =? %s ..." %\
27 (repr(formatstr), repr(value), repr(output)),
28 else:
29 print "%s %% %s works? ..." % (repr(formatstr), repr(value)),
Georg Brandlb89316f2006-05-17 15:51:16 +000030 result = func(formatstr, value, grouping = grouping)
Martin v. Löwis88ad12a2001-04-13 08:09:50 +000031 if output and result != output:
32 if verbose:
33 print 'no'
34 print "%s %% %s == %s != %s" %\
35 (repr(formatstr), repr(value), repr(result), repr(output))
36 else:
37 if verbose:
38 print "yes"
39
40try:
Martin v. Löwis4cfa1362005-12-30 12:51:45 +000041 # On Solaris 10, the thousands_sep is the empty string
42 sep = locale.localeconv()['thousands_sep']
43 testformat("%f", 1024, grouping=1, output='1%s024.000000' % sep)
Martin v. Löwis88ad12a2001-04-13 08:09:50 +000044 testformat("%f", 102, grouping=1, output='102.000000')
45 testformat("%f", -42, grouping=1, output='-42.000000')
46 testformat("%+f", -42, grouping=1, output='-42.000000')
47 testformat("%20.f", -42, grouping=1, output=' -42')
Martin v. Löwis4cfa1362005-12-30 12:51:45 +000048 testformat("%+10.f", -4200, grouping=1, output=' -4%s200' % sep)
49 testformat("%-10.f", 4200, grouping=1, output='4%s200 ' % sep)
Martin v. Löwisf0a46682002-11-03 17:20:12 +000050 # Invoke getpreferredencoding to make sure it does not cause exceptions,
51 locale.getpreferredencoding()
Georg Brandlb89316f2006-05-17 15:51:16 +000052
53 # === Test format() with more complex formatting strings
54 # test if grouping is independent from other characters in formatting string
55 testformat("One million is %i", 1000000, grouping=1, output='One million is 1,000,000',
56 func=locale.format_string)
57 testformat("One million is %i", 1000000, grouping=1, output='One million is 1,000,000',
58 func=locale.format_string)
59 # test dots in formatting string
60 testformat(".%f.", 1000.0, output='.1000.000000.', func=locale.format_string)
61 # test floats
62 testformat("--> %10.2f", 1000.0, grouping=1, output='--> 1,000.00',
63 func=locale.format_string)
64 # test asterisk formats
65 testformat("%10.*f", (2, 1000.0), grouping=0, output=' 1000.00',
66 func=locale.format_string)
67 testformat("%*.*f", (10, 2, 1000.0), grouping=1, output=' 1,000.00',
68 func=locale.format_string)
69 # test more-in-one
70 testformat("int %i float %.2f str %s", (1000, 1000.0, 'str'), grouping=1,
71 output='int 1,000 float 1,000.00 str str', func=locale.format_string)
72
Martin v. Löwis88ad12a2001-04-13 08:09:50 +000073finally:
74 locale.setlocale(locale.LC_NUMERIC, oldlocale)
Hye-Shik Changb5047fd2004-08-04 06:33:51 +000075
76
77# Test BSD Rune locale's bug for isctype functions.
78def teststrop(s, method, output):
79 if verbose:
80 print "%s.%s() =? %s ..." % (repr(s), method, repr(output)),
81 result = getattr(s, method)()
82 if result != output:
83 if verbose:
84 print "no"
85 print "%s.%s() == %s != %s" % (repr(s), method, repr(result),
86 repr(output))
87 elif verbose:
88 print "yes"
89
90try:
Martin v. Löwis4cfa1362005-12-30 12:51:45 +000091 if sys.platform == 'sunos5':
92 # On Solaris, in en_US.UTF-8, \xa0 is a space
93 raise locale.Error
Hye-Shik Changb5047fd2004-08-04 06:33:51 +000094 oldlocale = locale.setlocale(locale.LC_CTYPE)
95 locale.setlocale(locale.LC_CTYPE, 'en_US.UTF-8')
96except locale.Error:
97 pass
98else:
99 try:
100 teststrop('\x20', 'isspace', True)
101 teststrop('\xa0', 'isspace', False)
102 teststrop('\xa1', 'isspace', False)
103 teststrop('\xc0', 'isalpha', False)
104 teststrop('\xc0', 'isalnum', False)
105 teststrop('\xc0', 'isupper', False)
106 teststrop('\xc0', 'islower', False)
107 teststrop('\xec\xa0\xbc', 'split', ['\xec\xa0\xbc'])
108 teststrop('\xed\x95\xa0', 'strip', '\xed\x95\xa0')
109 teststrop('\xcc\x85', 'lower', '\xcc\x85')
110 teststrop('\xed\x95\xa0', 'upper', '\xed\x95\xa0')
111 finally:
112 locale.setlocale(locale.LC_CTYPE, oldlocale)