Mercurial > babel > mirror
annotate babel/tests/core.py @ 592:e6b1efa9a255 trunk
fix Locale.default to return Locales with correct territory information
author | fschwarz |
---|---|
date | Thu, 09 Aug 2012 07:47:09 +0000 |
parents | d1618dfaf114 |
children |
rev | line source |
---|---|
1 | 1 # -*- coding: utf-8 -*- |
2 # | |
530 | 3 # Copyright (C) 2007-2011 Edgewall Software |
1 | 4 # All rights reserved. |
5 # | |
6 # This software is licensed as described in the file COPYING, which | |
7 # you should have received as part of this distribution. The terms | |
8 # are also available at http://babel.edgewall.org/wiki/License. | |
9 # | |
10 # This software consists of voluntary contributions made by many | |
11 # individuals. For the exact contribution history, see the revision | |
12 # history and logs, available at http://babel.edgewall.org/log/. | |
13 | |
14 import doctest | |
534
c938dbfb8944
catch exception if environment contains an invalid locale setting (fixes #200)
fschwarz
parents:
530
diff
changeset
|
15 import os |
1 | 16 import unittest |
17 | |
18 from babel import core | |
590
6f86d60dab56
change repr output for babel.Locale so all attributes (territory, script, variant) are shown by itself. This should help identifying bad usage of the Locale class (e.g. Locale('de_DE') instead of Locale('de', 'DE'), #279
fschwarz
parents:
534
diff
changeset
|
19 from babel.core import default_locale, Locale |
6f86d60dab56
change repr output for babel.Locale so all attributes (territory, script, variant) are shown by itself. This should help identifying bad usage of the Locale class (e.g. Locale('de_DE') instead of Locale('de', 'DE'), #279
fschwarz
parents:
534
diff
changeset
|
20 |
6f86d60dab56
change repr output for babel.Locale so all attributes (territory, script, variant) are shown by itself. This should help identifying bad usage of the Locale class (e.g. Locale('de_DE') instead of Locale('de', 'DE'), #279
fschwarz
parents:
534
diff
changeset
|
21 |
592
e6b1efa9a255
fix Locale.default to return Locales with correct territory information
fschwarz
parents:
591
diff
changeset
|
22 class LocaleEnvironmentTestMixin(object): |
e6b1efa9a255
fix Locale.default to return Locales with correct territory information
fschwarz
parents:
591
diff
changeset
|
23 |
e6b1efa9a255
fix Locale.default to return Locales with correct territory information
fschwarz
parents:
591
diff
changeset
|
24 def setUp(self): |
e6b1efa9a255
fix Locale.default to return Locales with correct territory information
fschwarz
parents:
591
diff
changeset
|
25 self._old_locale_settings = self.current_locale_settings() |
e6b1efa9a255
fix Locale.default to return Locales with correct territory information
fschwarz
parents:
591
diff
changeset
|
26 |
e6b1efa9a255
fix Locale.default to return Locales with correct territory information
fschwarz
parents:
591
diff
changeset
|
27 def tearDown(self): |
e6b1efa9a255
fix Locale.default to return Locales with correct territory information
fschwarz
parents:
591
diff
changeset
|
28 self.reset_locale_settings(self._old_locale_settings) |
e6b1efa9a255
fix Locale.default to return Locales with correct territory information
fschwarz
parents:
591
diff
changeset
|
29 |
e6b1efa9a255
fix Locale.default to return Locales with correct territory information
fschwarz
parents:
591
diff
changeset
|
30 def current_locale_settings(self): |
e6b1efa9a255
fix Locale.default to return Locales with correct territory information
fschwarz
parents:
591
diff
changeset
|
31 settings = {} |
e6b1efa9a255
fix Locale.default to return Locales with correct territory information
fschwarz
parents:
591
diff
changeset
|
32 for name in ('LC_MESSAGES', 'LANGUAGE', 'LC_ALL', 'LC_CTYPE', 'LANG'): |
e6b1efa9a255
fix Locale.default to return Locales with correct territory information
fschwarz
parents:
591
diff
changeset
|
33 settings[name] = os.environ.get(name) |
e6b1efa9a255
fix Locale.default to return Locales with correct territory information
fschwarz
parents:
591
diff
changeset
|
34 return settings |
e6b1efa9a255
fix Locale.default to return Locales with correct territory information
fschwarz
parents:
591
diff
changeset
|
35 |
e6b1efa9a255
fix Locale.default to return Locales with correct territory information
fschwarz
parents:
591
diff
changeset
|
36 def reset_locale_settings(self, settings): |
e6b1efa9a255
fix Locale.default to return Locales with correct territory information
fschwarz
parents:
591
diff
changeset
|
37 for name, value in settings.items(): |
e6b1efa9a255
fix Locale.default to return Locales with correct territory information
fschwarz
parents:
591
diff
changeset
|
38 if value is not None: |
e6b1efa9a255
fix Locale.default to return Locales with correct territory information
fschwarz
parents:
591
diff
changeset
|
39 os.environ[name] = value |
e6b1efa9a255
fix Locale.default to return Locales with correct territory information
fschwarz
parents:
591
diff
changeset
|
40 elif name in os.environ: |
e6b1efa9a255
fix Locale.default to return Locales with correct territory information
fschwarz
parents:
591
diff
changeset
|
41 del os.environ[name] |
e6b1efa9a255
fix Locale.default to return Locales with correct territory information
fschwarz
parents:
591
diff
changeset
|
42 |
e6b1efa9a255
fix Locale.default to return Locales with correct territory information
fschwarz
parents:
591
diff
changeset
|
43 |
e6b1efa9a255
fix Locale.default to return Locales with correct territory information
fschwarz
parents:
591
diff
changeset
|
44 class LocaleTest(LocaleEnvironmentTestMixin, unittest.TestCase): |
590
6f86d60dab56
change repr output for babel.Locale so all attributes (territory, script, variant) are shown by itself. This should help identifying bad usage of the Locale class (e.g. Locale('de_DE') instead of Locale('de', 'DE'), #279
fschwarz
parents:
534
diff
changeset
|
45 |
6f86d60dab56
change repr output for babel.Locale so all attributes (territory, script, variant) are shown by itself. This should help identifying bad usage of the Locale class (e.g. Locale('de_DE') instead of Locale('de', 'DE'), #279
fschwarz
parents:
534
diff
changeset
|
46 def test_locale_provides_access_to_cldr_locale_data(self): |
6f86d60dab56
change repr output for babel.Locale so all attributes (territory, script, variant) are shown by itself. This should help identifying bad usage of the Locale class (e.g. Locale('de_DE') instead of Locale('de', 'DE'), #279
fschwarz
parents:
534
diff
changeset
|
47 locale = Locale('en', 'US') |
6f86d60dab56
change repr output for babel.Locale so all attributes (territory, script, variant) are shown by itself. This should help identifying bad usage of the Locale class (e.g. Locale('de_DE') instead of Locale('de', 'DE'), #279
fschwarz
parents:
534
diff
changeset
|
48 self.assertEqual(u'English (United States)', locale.display_name) |
6f86d60dab56
change repr output for babel.Locale so all attributes (territory, script, variant) are shown by itself. This should help identifying bad usage of the Locale class (e.g. Locale('de_DE') instead of Locale('de', 'DE'), #279
fschwarz
parents:
534
diff
changeset
|
49 self.assertEqual(u'.', locale.number_symbols['decimal']) |
6f86d60dab56
change repr output for babel.Locale so all attributes (territory, script, variant) are shown by itself. This should help identifying bad usage of the Locale class (e.g. Locale('de_DE') instead of Locale('de', 'DE'), #279
fschwarz
parents:
534
diff
changeset
|
50 |
6f86d60dab56
change repr output for babel.Locale so all attributes (territory, script, variant) are shown by itself. This should help identifying bad usage of the Locale class (e.g. Locale('de_DE') instead of Locale('de', 'DE'), #279
fschwarz
parents:
534
diff
changeset
|
51 def test_repr(self): |
6f86d60dab56
change repr output for babel.Locale so all attributes (territory, script, variant) are shown by itself. This should help identifying bad usage of the Locale class (e.g. Locale('de_DE') instead of Locale('de', 'DE'), #279
fschwarz
parents:
534
diff
changeset
|
52 self.assertEqual("Locale('de', territory='DE')", |
6f86d60dab56
change repr output for babel.Locale so all attributes (territory, script, variant) are shown by itself. This should help identifying bad usage of the Locale class (e.g. Locale('de_DE') instead of Locale('de', 'DE'), #279
fschwarz
parents:
534
diff
changeset
|
53 repr(Locale('de', 'DE'))) |
6f86d60dab56
change repr output for babel.Locale so all attributes (territory, script, variant) are shown by itself. This should help identifying bad usage of the Locale class (e.g. Locale('de_DE') instead of Locale('de', 'DE'), #279
fschwarz
parents:
534
diff
changeset
|
54 self.assertEqual("Locale('zh', territory='CN', script='Hans')", |
6f86d60dab56
change repr output for babel.Locale so all attributes (territory, script, variant) are shown by itself. This should help identifying bad usage of the Locale class (e.g. Locale('de_DE') instead of Locale('de', 'DE'), #279
fschwarz
parents:
534
diff
changeset
|
55 repr(Locale('zh', 'CN', script='Hans'))) |
591
d1618dfaf114
change Locale comparison: Locales are now considered equal if all of their attributes (language, territory, script, variant) are equal. Before __eq__ used the simple string representation which hides errors in Locale instantiation (see #279 and #311 for more information).
fschwarz
parents:
590
diff
changeset
|
56 |
d1618dfaf114
change Locale comparison: Locales are now considered equal if all of their attributes (language, territory, script, variant) are equal. Before __eq__ used the simple string representation which hides errors in Locale instantiation (see #279 and #311 for more information).
fschwarz
parents:
590
diff
changeset
|
57 def test_locale_comparison(self): |
d1618dfaf114
change Locale comparison: Locales are now considered equal if all of their attributes (language, territory, script, variant) are equal. Before __eq__ used the simple string representation which hides errors in Locale instantiation (see #279 and #311 for more information).
fschwarz
parents:
590
diff
changeset
|
58 en_US = Locale('en', 'US') |
d1618dfaf114
change Locale comparison: Locales are now considered equal if all of their attributes (language, territory, script, variant) are equal. Before __eq__ used the simple string representation which hides errors in Locale instantiation (see #279 and #311 for more information).
fschwarz
parents:
590
diff
changeset
|
59 self.assertEqual(en_US, en_US) |
d1618dfaf114
change Locale comparison: Locales are now considered equal if all of their attributes (language, territory, script, variant) are equal. Before __eq__ used the simple string representation which hides errors in Locale instantiation (see #279 and #311 for more information).
fschwarz
parents:
590
diff
changeset
|
60 self.assertNotEqual(None, en_US) |
d1618dfaf114
change Locale comparison: Locales are now considered equal if all of their attributes (language, territory, script, variant) are equal. Before __eq__ used the simple string representation which hides errors in Locale instantiation (see #279 and #311 for more information).
fschwarz
parents:
590
diff
changeset
|
61 |
d1618dfaf114
change Locale comparison: Locales are now considered equal if all of their attributes (language, territory, script, variant) are equal. Before __eq__ used the simple string representation which hides errors in Locale instantiation (see #279 and #311 for more information).
fschwarz
parents:
590
diff
changeset
|
62 bad_en_US = Locale('en_US') |
d1618dfaf114
change Locale comparison: Locales are now considered equal if all of their attributes (language, territory, script, variant) are equal. Before __eq__ used the simple string representation which hides errors in Locale instantiation (see #279 and #311 for more information).
fschwarz
parents:
590
diff
changeset
|
63 self.assertNotEqual(en_US, bad_en_US) |
590
6f86d60dab56
change repr output for babel.Locale so all attributes (territory, script, variant) are shown by itself. This should help identifying bad usage of the Locale class (e.g. Locale('de_DE') instead of Locale('de', 'DE'), #279
fschwarz
parents:
534
diff
changeset
|
64 |
592
e6b1efa9a255
fix Locale.default to return Locales with correct territory information
fschwarz
parents:
591
diff
changeset
|
65 def test_can_return_default_locale(self): |
e6b1efa9a255
fix Locale.default to return Locales with correct territory information
fschwarz
parents:
591
diff
changeset
|
66 os.environ['LC_MESSAGES'] = 'fr_FR.UTF-8' |
e6b1efa9a255
fix Locale.default to return Locales with correct territory information
fschwarz
parents:
591
diff
changeset
|
67 self.assertEqual(Locale('fr', 'FR'), Locale.default('LC_MESSAGES')) |
534
c938dbfb8944
catch exception if environment contains an invalid locale setting (fixes #200)
fschwarz
parents:
530
diff
changeset
|
68 |
592
e6b1efa9a255
fix Locale.default to return Locales with correct territory information
fschwarz
parents:
591
diff
changeset
|
69 |
e6b1efa9a255
fix Locale.default to return Locales with correct territory information
fschwarz
parents:
591
diff
changeset
|
70 class DefaultLocaleTest(LocaleEnvironmentTestMixin, unittest.TestCase): |
534
c938dbfb8944
catch exception if environment contains an invalid locale setting (fixes #200)
fschwarz
parents:
530
diff
changeset
|
71 |
c938dbfb8944
catch exception if environment contains an invalid locale setting (fixes #200)
fschwarz
parents:
530
diff
changeset
|
72 def test_ignore_invalid_locales_in_lc_ctype(self): |
c938dbfb8944
catch exception if environment contains an invalid locale setting (fixes #200)
fschwarz
parents:
530
diff
changeset
|
73 # This is a regression test specifically for a bad LC_CTYPE setting on |
c938dbfb8944
catch exception if environment contains an invalid locale setting (fixes #200)
fschwarz
parents:
530
diff
changeset
|
74 # MacOS X 10.6 (#200) |
c938dbfb8944
catch exception if environment contains an invalid locale setting (fixes #200)
fschwarz
parents:
530
diff
changeset
|
75 os.environ['LC_CTYPE'] = 'UTF-8' |
c938dbfb8944
catch exception if environment contains an invalid locale setting (fixes #200)
fschwarz
parents:
530
diff
changeset
|
76 # must not throw an exception |
c938dbfb8944
catch exception if environment contains an invalid locale setting (fixes #200)
fschwarz
parents:
530
diff
changeset
|
77 default_locale('LC_CTYPE') |
1 | 78 |
590
6f86d60dab56
change repr output for babel.Locale so all attributes (territory, script, variant) are shown by itself. This should help identifying bad usage of the Locale class (e.g. Locale('de_DE') instead of Locale('de', 'DE'), #279
fschwarz
parents:
534
diff
changeset
|
79 |
1 | 80 def suite(): |
81 suite = unittest.TestSuite() | |
82 suite.addTest(doctest.DocTestSuite(core)) | |
590
6f86d60dab56
change repr output for babel.Locale so all attributes (territory, script, variant) are shown by itself. This should help identifying bad usage of the Locale class (e.g. Locale('de_DE') instead of Locale('de', 'DE'), #279
fschwarz
parents:
534
diff
changeset
|
83 suite.addTest(unittest.makeSuite(LocaleTest)) |
534
c938dbfb8944
catch exception if environment contains an invalid locale setting (fixes #200)
fschwarz
parents:
530
diff
changeset
|
84 suite.addTest(unittest.makeSuite(DefaultLocaleTest)) |
1 | 85 return suite |
86 | |
87 if __name__ == '__main__': | |
88 unittest.main(defaultTest='suite') |