»Core Development>Code coverage>Lib/test/test_locale.py

Python code coverage for Lib/test/test_locale.py

#countcontent
1n/afrom test.support import verbose, is_android
2n/aimport unittest
3n/aimport locale
4n/aimport sys
5n/aimport codecs
6n/a
7n/aclass BaseLocalizedTest(unittest.TestCase):
8n/a #
9n/a # Base class for tests using a real locale
10n/a #
11n/a
12n/a @classmethod
13n/a def setUpClass(cls):
14n/a if sys.platform == 'darwin':
15n/a import os
16n/a tlocs = ("en_US.UTF-8", "en_US.ISO8859-1", "en_US")
17n/a if int(os.uname().release.split('.')[0]) < 10:
18n/a # The locale test work fine on OSX 10.6, I (ronaldoussoren)
19n/a # haven't had time yet to verify if tests work on OSX 10.5
20n/a # (10.4 is known to be bad)
21n/a raise unittest.SkipTest("Locale support on MacOSX is minimal")
22n/a elif sys.platform.startswith("win"):
23n/a tlocs = ("En", "English")
24n/a else:
25n/a tlocs = ("en_US.UTF-8", "en_US.ISO8859-1",
26n/a "en_US.US-ASCII", "en_US")
27n/a try:
28n/a oldlocale = locale.setlocale(locale.LC_NUMERIC)
29n/a for tloc in tlocs:
30n/a try:
31n/a locale.setlocale(locale.LC_NUMERIC, tloc)
32n/a except locale.Error:
33n/a continue
34n/a break
35n/a else:
36n/a raise unittest.SkipTest("Test locale not supported "
37n/a "(tried %s)" % (', '.join(tlocs)))
38n/a cls.enUS_locale = tloc
39n/a finally:
40n/a locale.setlocale(locale.LC_NUMERIC, oldlocale)
41n/a
42n/a def setUp(self):
43n/a oldlocale = locale.setlocale(self.locale_type)
44n/a self.addCleanup(locale.setlocale, self.locale_type, oldlocale)
45n/a locale.setlocale(self.locale_type, self.enUS_locale)
46n/a if verbose:
47n/a print("testing with %r..." % self.enUS_locale, end=' ', flush=True)
48n/a
49n/a
50n/aclass BaseCookedTest(unittest.TestCase):
51n/a #
52n/a # Base class for tests using cooked localeconv() values
53n/a #
54n/a
55n/a def setUp(self):
56n/a locale._override_localeconv = self.cooked_values
57n/a
58n/a def tearDown(self):
59n/a locale._override_localeconv = {}
60n/a
61n/aclass CCookedTest(BaseCookedTest):
62n/a # A cooked "C" locale
63n/a
64n/a cooked_values = {
65n/a 'currency_symbol': '',
66n/a 'decimal_point': '.',
67n/a 'frac_digits': 127,
68n/a 'grouping': [],
69n/a 'int_curr_symbol': '',
70n/a 'int_frac_digits': 127,
71n/a 'mon_decimal_point': '',
72n/a 'mon_grouping': [],
73n/a 'mon_thousands_sep': '',
74n/a 'n_cs_precedes': 127,
75n/a 'n_sep_by_space': 127,
76n/a 'n_sign_posn': 127,
77n/a 'negative_sign': '',
78n/a 'p_cs_precedes': 127,
79n/a 'p_sep_by_space': 127,
80n/a 'p_sign_posn': 127,
81n/a 'positive_sign': '',
82n/a 'thousands_sep': ''
83n/a }
84n/a
85n/aclass EnUSCookedTest(BaseCookedTest):
86n/a # A cooked "en_US" locale
87n/a
88n/a cooked_values = {
89n/a 'currency_symbol': '$',
90n/a 'decimal_point': '.',
91n/a 'frac_digits': 2,
92n/a 'grouping': [3, 3, 0],
93n/a 'int_curr_symbol': 'USD ',
94n/a 'int_frac_digits': 2,
95n/a 'mon_decimal_point': '.',
96n/a 'mon_grouping': [3, 3, 0],
97n/a 'mon_thousands_sep': ',',
98n/a 'n_cs_precedes': 1,
99n/a 'n_sep_by_space': 0,
100n/a 'n_sign_posn': 1,
101n/a 'negative_sign': '-',
102n/a 'p_cs_precedes': 1,
103n/a 'p_sep_by_space': 0,
104n/a 'p_sign_posn': 1,
105n/a 'positive_sign': '',
106n/a 'thousands_sep': ','
107n/a }
108n/a
109n/a
110n/aclass FrFRCookedTest(BaseCookedTest):
111n/a # A cooked "fr_FR" locale with a space character as decimal separator
112n/a # and a non-ASCII currency symbol.
113n/a
114n/a cooked_values = {
115n/a 'currency_symbol': '\u20ac',
116n/a 'decimal_point': ',',
117n/a 'frac_digits': 2,
118n/a 'grouping': [3, 3, 0],
119n/a 'int_curr_symbol': 'EUR ',
120n/a 'int_frac_digits': 2,
121n/a 'mon_decimal_point': ',',
122n/a 'mon_grouping': [3, 3, 0],
123n/a 'mon_thousands_sep': ' ',
124n/a 'n_cs_precedes': 0,
125n/a 'n_sep_by_space': 1,
126n/a 'n_sign_posn': 1,
127n/a 'negative_sign': '-',
128n/a 'p_cs_precedes': 0,
129n/a 'p_sep_by_space': 1,
130n/a 'p_sign_posn': 1,
131n/a 'positive_sign': '',
132n/a 'thousands_sep': ' '
133n/a }
134n/a
135n/a
136n/aclass BaseFormattingTest(object):
137n/a #
138n/a # Utility functions for formatting tests
139n/a #
140n/a
141n/a def _test_formatfunc(self, format, value, out, func, **format_opts):
142n/a self.assertEqual(
143n/a func(format, value, **format_opts), out)
144n/a
145n/a def _test_format(self, format, value, out, **format_opts):
146n/a self._test_formatfunc(format, value, out,
147n/a func=locale.format, **format_opts)
148n/a
149n/a def _test_format_string(self, format, value, out, **format_opts):
150n/a self._test_formatfunc(format, value, out,
151n/a func=locale.format_string, **format_opts)
152n/a
153n/a def _test_currency(self, value, out, **format_opts):
154n/a self.assertEqual(locale.currency(value, **format_opts), out)
155n/a
156n/a
157n/aclass EnUSNumberFormatting(BaseFormattingTest):
158n/a # XXX there is a grouping + padding bug when the thousands separator
159n/a # is empty but the grouping array contains values (e.g. Solaris 10)
160n/a
161n/a def setUp(self):
162n/a self.sep = locale.localeconv()['thousands_sep']
163n/a
164n/a def test_grouping(self):
165n/a self._test_format("%f", 1024, grouping=1, out='1%s024.000000' % self.sep)
166n/a self._test_format("%f", 102, grouping=1, out='102.000000')
167n/a self._test_format("%f", -42, grouping=1, out='-42.000000')
168n/a self._test_format("%+f", -42, grouping=1, out='-42.000000')
169n/a
170n/a def test_grouping_and_padding(self):
171n/a self._test_format("%20.f", -42, grouping=1, out='-42'.rjust(20))
172n/a if self.sep:
173n/a self._test_format("%+10.f", -4200, grouping=1,
174n/a out=('-4%s200' % self.sep).rjust(10))
175n/a self._test_format("%-10.f", -4200, grouping=1,
176n/a out=('-4%s200' % self.sep).ljust(10))
177n/a
178n/a def test_integer_grouping(self):
179n/a self._test_format("%d", 4200, grouping=True, out='4%s200' % self.sep)
180n/a self._test_format("%+d", 4200, grouping=True, out='+4%s200' % self.sep)
181n/a self._test_format("%+d", -4200, grouping=True, out='-4%s200' % self.sep)
182n/a
183n/a def test_integer_grouping_and_padding(self):
184n/a self._test_format("%10d", 4200, grouping=True,
185n/a out=('4%s200' % self.sep).rjust(10))
186n/a self._test_format("%-10d", -4200, grouping=True,
187n/a out=('-4%s200' % self.sep).ljust(10))
188n/a
189n/a def test_simple(self):
190n/a self._test_format("%f", 1024, grouping=0, out='1024.000000')
191n/a self._test_format("%f", 102, grouping=0, out='102.000000')
192n/a self._test_format("%f", -42, grouping=0, out='-42.000000')
193n/a self._test_format("%+f", -42, grouping=0, out='-42.000000')
194n/a
195n/a def test_padding(self):
196n/a self._test_format("%20.f", -42, grouping=0, out='-42'.rjust(20))
197n/a self._test_format("%+10.f", -4200, grouping=0, out='-4200'.rjust(10))
198n/a self._test_format("%-10.f", 4200, grouping=0, out='4200'.ljust(10))
199n/a
200n/a def test_complex_formatting(self):
201n/a # Spaces in formatting string
202n/a self._test_format_string("One million is %i", 1000000, grouping=1,
203n/a out='One million is 1%s000%s000' % (self.sep, self.sep))
204n/a self._test_format_string("One million is %i", 1000000, grouping=1,
205n/a out='One million is 1%s000%s000' % (self.sep, self.sep))
206n/a # Dots in formatting string
207n/a self._test_format_string(".%f.", 1000.0, out='.1000.000000.')
208n/a # Padding
209n/a if self.sep:
210n/a self._test_format_string("--> %10.2f", 4200, grouping=1,
211n/a out='--> ' + ('4%s200.00' % self.sep).rjust(10))
212n/a # Asterisk formats
213n/a self._test_format_string("%10.*f", (2, 1000), grouping=0,
214n/a out='1000.00'.rjust(10))
215n/a if self.sep:
216n/a self._test_format_string("%*.*f", (10, 2, 1000), grouping=1,
217n/a out=('1%s000.00' % self.sep).rjust(10))
218n/a # Test more-in-one
219n/a if self.sep:
220n/a self._test_format_string("int %i float %.2f str %s",
221n/a (1000, 1000.0, 'str'), grouping=1,
222n/a out='int 1%s000 float 1%s000.00 str str' %
223n/a (self.sep, self.sep))
224n/a
225n/a
226n/aclass TestFormatPatternArg(unittest.TestCase):
227n/a # Test handling of pattern argument of format
228n/a
229n/a def test_onlyOnePattern(self):
230n/a # Issue 2522: accept exactly one % pattern, and no extra chars.
231n/a self.assertRaises(ValueError, locale.format, "%f\n", 'foo')
232n/a self.assertRaises(ValueError, locale.format, "%f\r", 'foo')
233n/a self.assertRaises(ValueError, locale.format, "%f\r\n", 'foo')
234n/a self.assertRaises(ValueError, locale.format, " %f", 'foo')
235n/a self.assertRaises(ValueError, locale.format, "%fg", 'foo')
236n/a self.assertRaises(ValueError, locale.format, "%^g", 'foo')
237n/a self.assertRaises(ValueError, locale.format, "%f%%", 'foo')
238n/a
239n/a
240n/aclass TestLocaleFormatString(unittest.TestCase):
241n/a """General tests on locale.format_string"""
242n/a
243n/a def test_percent_escape(self):
244n/a self.assertEqual(locale.format_string('%f%%', 1.0), '%f%%' % 1.0)
245n/a self.assertEqual(locale.format_string('%d %f%%d', (1, 1.0)),
246n/a '%d %f%%d' % (1, 1.0))
247n/a self.assertEqual(locale.format_string('%(foo)s %%d', {'foo': 'bar'}),
248n/a ('%(foo)s %%d' % {'foo': 'bar'}))
249n/a
250n/a def test_mapping(self):
251n/a self.assertEqual(locale.format_string('%(foo)s bing.', {'foo': 'bar'}),
252n/a ('%(foo)s bing.' % {'foo': 'bar'}))
253n/a self.assertEqual(locale.format_string('%(foo)s', {'foo': 'bar'}),
254n/a ('%(foo)s' % {'foo': 'bar'}))
255n/a
256n/a
257n/a
258n/aclass TestNumberFormatting(BaseLocalizedTest, EnUSNumberFormatting):
259n/a # Test number formatting with a real English locale.
260n/a
261n/a locale_type = locale.LC_NUMERIC
262n/a
263n/a def setUp(self):
264n/a BaseLocalizedTest.setUp(self)
265n/a EnUSNumberFormatting.setUp(self)
266n/a
267n/a
268n/aclass TestEnUSNumberFormatting(EnUSCookedTest, EnUSNumberFormatting):
269n/a # Test number formatting with a cooked "en_US" locale.
270n/a
271n/a def setUp(self):
272n/a EnUSCookedTest.setUp(self)
273n/a EnUSNumberFormatting.setUp(self)
274n/a
275n/a def test_currency(self):
276n/a self._test_currency(50000, "$50000.00")
277n/a self._test_currency(50000, "$50,000.00", grouping=True)
278n/a self._test_currency(50000, "USD 50,000.00",
279n/a grouping=True, international=True)
280n/a
281n/a
282n/aclass TestCNumberFormatting(CCookedTest, BaseFormattingTest):
283n/a # Test number formatting with a cooked "C" locale.
284n/a
285n/a def test_grouping(self):
286n/a self._test_format("%.2f", 12345.67, grouping=True, out='12345.67')
287n/a
288n/a def test_grouping_and_padding(self):
289n/a self._test_format("%9.2f", 12345.67, grouping=True, out=' 12345.67')
290n/a
291n/a
292n/aclass TestFrFRNumberFormatting(FrFRCookedTest, BaseFormattingTest):
293n/a # Test number formatting with a cooked "fr_FR" locale.
294n/a
295n/a def test_decimal_point(self):
296n/a self._test_format("%.2f", 12345.67, out='12345,67')
297n/a
298n/a def test_grouping(self):
299n/a self._test_format("%.2f", 345.67, grouping=True, out='345,67')
300n/a self._test_format("%.2f", 12345.67, grouping=True, out='12 345,67')
301n/a
302n/a def test_grouping_and_padding(self):
303n/a self._test_format("%6.2f", 345.67, grouping=True, out='345,67')
304n/a self._test_format("%7.2f", 345.67, grouping=True, out=' 345,67')
305n/a self._test_format("%8.2f", 12345.67, grouping=True, out='12 345,67')
306n/a self._test_format("%9.2f", 12345.67, grouping=True, out='12 345,67')
307n/a self._test_format("%10.2f", 12345.67, grouping=True, out=' 12 345,67')
308n/a self._test_format("%-6.2f", 345.67, grouping=True, out='345,67')
309n/a self._test_format("%-7.2f", 345.67, grouping=True, out='345,67 ')
310n/a self._test_format("%-8.2f", 12345.67, grouping=True, out='12 345,67')
311n/a self._test_format("%-9.2f", 12345.67, grouping=True, out='12 345,67')
312n/a self._test_format("%-10.2f", 12345.67, grouping=True, out='12 345,67 ')
313n/a
314n/a def test_integer_grouping(self):
315n/a self._test_format("%d", 200, grouping=True, out='200')
316n/a self._test_format("%d", 4200, grouping=True, out='4 200')
317n/a
318n/a def test_integer_grouping_and_padding(self):
319n/a self._test_format("%4d", 4200, grouping=True, out='4 200')
320n/a self._test_format("%5d", 4200, grouping=True, out='4 200')
321n/a self._test_format("%10d", 4200, grouping=True, out='4 200'.rjust(10))
322n/a self._test_format("%-4d", 4200, grouping=True, out='4 200')
323n/a self._test_format("%-5d", 4200, grouping=True, out='4 200')
324n/a self._test_format("%-10d", 4200, grouping=True, out='4 200'.ljust(10))
325n/a
326n/a def test_currency(self):
327n/a euro = '\u20ac'
328n/a self._test_currency(50000, "50000,00 " + euro)
329n/a self._test_currency(50000, "50 000,00 " + euro, grouping=True)
330n/a # XXX is the trailing space a bug?
331n/a self._test_currency(50000, "50 000,00 EUR ",
332n/a grouping=True, international=True)
333n/a
334n/a
335n/aclass TestCollation(unittest.TestCase):
336n/a # Test string collation functions
337n/a
338n/a def test_strcoll(self):
339n/a self.assertLess(locale.strcoll('a', 'b'), 0)
340n/a self.assertEqual(locale.strcoll('a', 'a'), 0)
341n/a self.assertGreater(locale.strcoll('b', 'a'), 0)
342n/a
343n/a def test_strxfrm(self):
344n/a self.assertLess(locale.strxfrm('a'), locale.strxfrm('b'))
345n/a
346n/a
347n/aclass TestEnUSCollation(BaseLocalizedTest, TestCollation):
348n/a # Test string collation functions with a real English locale
349n/a
350n/a locale_type = locale.LC_ALL
351n/a
352n/a def setUp(self):
353n/a enc = codecs.lookup(locale.getpreferredencoding(False) or 'ascii').name
354n/a if enc not in ('utf-8', 'iso8859-1', 'cp1252'):
355n/a raise unittest.SkipTest('encoding not suitable')
356n/a if enc != 'iso8859-1' and (sys.platform == 'darwin' or is_android or
357n/a sys.platform.startswith('freebsd')):
358n/a raise unittest.SkipTest('wcscoll/wcsxfrm have known bugs')
359n/a BaseLocalizedTest.setUp(self)
360n/a
361n/a def test_strcoll_with_diacritic(self):
362n/a self.assertLess(locale.strcoll('à', 'b'), 0)
363n/a
364n/a def test_strxfrm_with_diacritic(self):
365n/a self.assertLess(locale.strxfrm('à'), locale.strxfrm('b'))
366n/a
367n/a
368n/aclass NormalizeTest(unittest.TestCase):
369n/a def check(self, localename, expected):
370n/a self.assertEqual(locale.normalize(localename), expected, msg=localename)
371n/a
372n/a def test_locale_alias(self):
373n/a for localename, alias in locale.locale_alias.items():
374n/a with self.subTest(locale=(localename, alias)):
375n/a self.check(localename, alias)
376n/a
377n/a def test_empty(self):
378n/a self.check('', '')
379n/a
380n/a def test_c(self):
381n/a self.check('c', 'C')
382n/a self.check('posix', 'C')
383n/a
384n/a def test_english(self):
385n/a self.check('en', 'en_US.ISO8859-1')
386n/a self.check('EN', 'en_US.ISO8859-1')
387n/a self.check('en.iso88591', 'en_US.ISO8859-1')
388n/a self.check('en_US', 'en_US.ISO8859-1')
389n/a self.check('en_us', 'en_US.ISO8859-1')
390n/a self.check('en_GB', 'en_GB.ISO8859-1')
391n/a self.check('en_US.UTF-8', 'en_US.UTF-8')
392n/a self.check('en_US.utf8', 'en_US.UTF-8')
393n/a self.check('en_US:UTF-8', 'en_US.UTF-8')
394n/a self.check('en_US.ISO8859-1', 'en_US.ISO8859-1')
395n/a self.check('en_US.US-ASCII', 'en_US.ISO8859-1')
396n/a self.check('en_US.88591', 'en_US.ISO8859-1')
397n/a self.check('en_US.885915', 'en_US.ISO8859-15')
398n/a self.check('english', 'en_EN.ISO8859-1')
399n/a self.check('english_uk.ascii', 'en_GB.ISO8859-1')
400n/a
401n/a def test_hyphenated_encoding(self):
402n/a self.check('az_AZ.iso88599e', 'az_AZ.ISO8859-9E')
403n/a self.check('az_AZ.ISO8859-9E', 'az_AZ.ISO8859-9E')
404n/a self.check('tt_RU.koi8c', 'tt_RU.KOI8-C')
405n/a self.check('tt_RU.KOI8-C', 'tt_RU.KOI8-C')
406n/a self.check('lo_LA.cp1133', 'lo_LA.IBM-CP1133')
407n/a self.check('lo_LA.ibmcp1133', 'lo_LA.IBM-CP1133')
408n/a self.check('lo_LA.IBM-CP1133', 'lo_LA.IBM-CP1133')
409n/a self.check('uk_ua.microsoftcp1251', 'uk_UA.CP1251')
410n/a self.check('uk_ua.microsoft-cp1251', 'uk_UA.CP1251')
411n/a self.check('ka_ge.georgianacademy', 'ka_GE.GEORGIAN-ACADEMY')
412n/a self.check('ka_GE.GEORGIAN-ACADEMY', 'ka_GE.GEORGIAN-ACADEMY')
413n/a self.check('cs_CZ.iso88592', 'cs_CZ.ISO8859-2')
414n/a self.check('cs_CZ.ISO8859-2', 'cs_CZ.ISO8859-2')
415n/a
416n/a def test_euro_modifier(self):
417n/a self.check('de_DE@euro', 'de_DE.ISO8859-15')
418n/a self.check('en_US.ISO8859-15@euro', 'en_US.ISO8859-15')
419n/a self.check('de_DE.utf8@euro', 'de_DE.UTF-8')
420n/a
421n/a def test_latin_modifier(self):
422n/a self.check('be_BY.UTF-8@latin', 'be_BY.UTF-8@latin')
423n/a self.check('sr_RS.UTF-8@latin', 'sr_RS.UTF-8@latin')
424n/a self.check('sr_RS.UTF-8@latn', 'sr_RS.UTF-8@latin')
425n/a
426n/a def test_valencia_modifier(self):
427n/a self.check('ca_ES.UTF-8@valencia', 'ca_ES.UTF-8@valencia')
428n/a self.check('ca_ES@valencia', 'ca_ES.ISO8859-15@valencia')
429n/a self.check('ca@valencia', 'ca_ES.ISO8859-1@valencia')
430n/a
431n/a def test_devanagari_modifier(self):
432n/a self.check('ks_IN.UTF-8@devanagari', 'ks_IN.UTF-8@devanagari')
433n/a self.check('ks_IN@devanagari', 'ks_IN.UTF-8@devanagari')
434n/a self.check('ks@devanagari', 'ks_IN.UTF-8@devanagari')
435n/a self.check('ks_IN.UTF-8', 'ks_IN.UTF-8')
436n/a self.check('ks_IN', 'ks_IN.UTF-8')
437n/a self.check('ks', 'ks_IN.UTF-8')
438n/a self.check('sd_IN.UTF-8@devanagari', 'sd_IN.UTF-8@devanagari')
439n/a self.check('sd_IN@devanagari', 'sd_IN.UTF-8@devanagari')
440n/a self.check('sd@devanagari', 'sd_IN.UTF-8@devanagari')
441n/a self.check('sd_IN.UTF-8', 'sd_IN.UTF-8')
442n/a self.check('sd_IN', 'sd_IN.UTF-8')
443n/a self.check('sd', 'sd_IN.UTF-8')
444n/a
445n/a def test_euc_encoding(self):
446n/a self.check('ja_jp.euc', 'ja_JP.eucJP')
447n/a self.check('ja_jp.eucjp', 'ja_JP.eucJP')
448n/a self.check('ko_kr.euc', 'ko_KR.eucKR')
449n/a self.check('ko_kr.euckr', 'ko_KR.eucKR')
450n/a self.check('zh_cn.euc', 'zh_CN.eucCN')
451n/a self.check('zh_tw.euc', 'zh_TW.eucTW')
452n/a self.check('zh_tw.euctw', 'zh_TW.eucTW')
453n/a
454n/a def test_japanese(self):
455n/a self.check('ja', 'ja_JP.eucJP')
456n/a self.check('ja.jis', 'ja_JP.JIS7')
457n/a self.check('ja.sjis', 'ja_JP.SJIS')
458n/a self.check('ja_jp', 'ja_JP.eucJP')
459n/a self.check('ja_jp.ajec', 'ja_JP.eucJP')
460n/a self.check('ja_jp.euc', 'ja_JP.eucJP')
461n/a self.check('ja_jp.eucjp', 'ja_JP.eucJP')
462n/a self.check('ja_jp.iso-2022-jp', 'ja_JP.JIS7')
463n/a self.check('ja_jp.iso2022jp', 'ja_JP.JIS7')
464n/a self.check('ja_jp.jis', 'ja_JP.JIS7')
465n/a self.check('ja_jp.jis7', 'ja_JP.JIS7')
466n/a self.check('ja_jp.mscode', 'ja_JP.SJIS')
467n/a self.check('ja_jp.pck', 'ja_JP.SJIS')
468n/a self.check('ja_jp.sjis', 'ja_JP.SJIS')
469n/a self.check('ja_jp.ujis', 'ja_JP.eucJP')
470n/a self.check('ja_jp.utf8', 'ja_JP.UTF-8')
471n/a self.check('japan', 'ja_JP.eucJP')
472n/a self.check('japanese', 'ja_JP.eucJP')
473n/a self.check('japanese-euc', 'ja_JP.eucJP')
474n/a self.check('japanese.euc', 'ja_JP.eucJP')
475n/a self.check('japanese.sjis', 'ja_JP.SJIS')
476n/a self.check('jp_jp', 'ja_JP.eucJP')
477n/a
478n/a
479n/aclass TestMiscellaneous(unittest.TestCase):
480n/a def test_getpreferredencoding(self):
481n/a # Invoke getpreferredencoding to make sure it does not cause exceptions.
482n/a enc = locale.getpreferredencoding()
483n/a if enc:
484n/a # If encoding non-empty, make sure it is valid
485n/a codecs.lookup(enc)
486n/a
487n/a def test_strcoll_3303(self):
488n/a # test crasher from bug #3303
489n/a self.assertRaises(TypeError, locale.strcoll, "a", None)
490n/a self.assertRaises(TypeError, locale.strcoll, b"a", None)
491n/a
492n/a def test_setlocale_category(self):
493n/a locale.setlocale(locale.LC_ALL)
494n/a locale.setlocale(locale.LC_TIME)
495n/a locale.setlocale(locale.LC_CTYPE)
496n/a locale.setlocale(locale.LC_COLLATE)
497n/a locale.setlocale(locale.LC_MONETARY)
498n/a locale.setlocale(locale.LC_NUMERIC)
499n/a
500n/a # crasher from bug #7419
501n/a self.assertRaises(locale.Error, locale.setlocale, 12345)
502n/a
503n/a def test_getsetlocale_issue1813(self):
504n/a # Issue #1813: setting and getting the locale under a Turkish locale
505n/a oldlocale = locale.setlocale(locale.LC_CTYPE)
506n/a self.addCleanup(locale.setlocale, locale.LC_CTYPE, oldlocale)
507n/a try:
508n/a locale.setlocale(locale.LC_CTYPE, 'tr_TR')
509n/a except locale.Error:
510n/a # Unsupported locale on this system
511n/a self.skipTest('test needs Turkish locale')
512n/a loc = locale.getlocale(locale.LC_CTYPE)
513n/a if verbose:
514n/a print('testing with %a' % (loc,), end=' ', flush=True)
515n/a locale.setlocale(locale.LC_CTYPE, loc)
516n/a self.assertEqual(loc, locale.getlocale(locale.LC_CTYPE))
517n/a
518n/a def test_invalid_locale_format_in_localetuple(self):
519n/a with self.assertRaises(TypeError):
520n/a locale.setlocale(locale.LC_ALL, b'fi_FI')
521n/a
522n/a def test_invalid_iterable_in_localetuple(self):
523n/a with self.assertRaises(TypeError):
524n/a locale.setlocale(locale.LC_ALL, (b'not', b'valid'))
525n/a
526n/a
527n/aclass BaseDelocalizeTest(BaseLocalizedTest):
528n/a
529n/a def _test_delocalize(self, value, out):
530n/a self.assertEqual(locale.delocalize(value), out)
531n/a
532n/a def _test_atof(self, value, out):
533n/a self.assertEqual(locale.atof(value), out)
534n/a
535n/a def _test_atoi(self, value, out):
536n/a self.assertEqual(locale.atoi(value), out)
537n/a
538n/a
539n/aclass TestEnUSDelocalize(EnUSCookedTest, BaseDelocalizeTest):
540n/a
541n/a def test_delocalize(self):
542n/a self._test_delocalize('50000.00', '50000.00')
543n/a self._test_delocalize('50,000.00', '50000.00')
544n/a
545n/a def test_atof(self):
546n/a self._test_atof('50000.00', 50000.)
547n/a self._test_atof('50,000.00', 50000.)
548n/a
549n/a def test_atoi(self):
550n/a self._test_atoi('50000', 50000)
551n/a self._test_atoi('50,000', 50000)
552n/a
553n/a
554n/aclass TestCDelocalizeTest(CCookedTest, BaseDelocalizeTest):
555n/a
556n/a def test_delocalize(self):
557n/a self._test_delocalize('50000.00', '50000.00')
558n/a
559n/a def test_atof(self):
560n/a self._test_atof('50000.00', 50000.)
561n/a
562n/a def test_atoi(self):
563n/a self._test_atoi('50000', 50000)
564n/a
565n/a
566n/aclass TestfrFRDelocalizeTest(FrFRCookedTest, BaseDelocalizeTest):
567n/a
568n/a def test_delocalize(self):
569n/a self._test_delocalize('50000,00', '50000.00')
570n/a self._test_delocalize('50 000,00', '50000.00')
571n/a
572n/a def test_atof(self):
573n/a self._test_atof('50000,00', 50000.)
574n/a self._test_atof('50 000,00', 50000.)
575n/a
576n/a def test_atoi(self):
577n/a self._test_atoi('50000', 50000)
578n/a self._test_atoi('50 000', 50000)
579n/a
580n/a
581n/aif __name__ == '__main__':
582n/a unittest.main()