Mercurial > babel > mirror
annotate babel/tests/core.py @ 591:d1618dfaf114 trunk
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).
author | fschwarz |
---|---|
date | Thu, 09 Aug 2012 07:36:36 +0000 |
parents | 6f86d60dab56 |
children | e6b1efa9a255 |
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 |
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
|
22 class LocaleTest(unittest.TestCase): |
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
|
23 |
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
|
24 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
|
25 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
|
26 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
|
27 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
|
28 |
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
|
29 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
|
30 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
|
31 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
|
32 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
|
33 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
|
34 |
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
|
35 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
|
36 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
|
37 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
|
38 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
|
39 |
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
|
40 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
|
41 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
|
42 |
534
c938dbfb8944
catch exception if environment contains an invalid locale setting (fixes #200)
fschwarz
parents:
530
diff
changeset
|
43 |
c938dbfb8944
catch exception if environment contains an invalid locale setting (fixes #200)
fschwarz
parents:
530
diff
changeset
|
44 class DefaultLocaleTest(unittest.TestCase): |
c938dbfb8944
catch exception if environment contains an invalid locale setting (fixes #200)
fschwarz
parents:
530
diff
changeset
|
45 |
c938dbfb8944
catch exception if environment contains an invalid locale setting (fixes #200)
fschwarz
parents:
530
diff
changeset
|
46 def setUp(self): |
c938dbfb8944
catch exception if environment contains an invalid locale setting (fixes #200)
fschwarz
parents:
530
diff
changeset
|
47 self._old_locale_settings = self._current_locale_settings() |
c938dbfb8944
catch exception if environment contains an invalid locale setting (fixes #200)
fschwarz
parents:
530
diff
changeset
|
48 |
c938dbfb8944
catch exception if environment contains an invalid locale setting (fixes #200)
fschwarz
parents:
530
diff
changeset
|
49 def tearDown(self): |
c938dbfb8944
catch exception if environment contains an invalid locale setting (fixes #200)
fschwarz
parents:
530
diff
changeset
|
50 self._set_locale_settings(self._old_locale_settings) |
c938dbfb8944
catch exception if environment contains an invalid locale setting (fixes #200)
fschwarz
parents:
530
diff
changeset
|
51 |
c938dbfb8944
catch exception if environment contains an invalid locale setting (fixes #200)
fschwarz
parents:
530
diff
changeset
|
52 def _current_locale_settings(self): |
c938dbfb8944
catch exception if environment contains an invalid locale setting (fixes #200)
fschwarz
parents:
530
diff
changeset
|
53 settings = {} |
c938dbfb8944
catch exception if environment contains an invalid locale setting (fixes #200)
fschwarz
parents:
530
diff
changeset
|
54 for name in ('LANGUAGE', 'LC_ALL', 'LC_CTYPE', 'LANG'): |
c938dbfb8944
catch exception if environment contains an invalid locale setting (fixes #200)
fschwarz
parents:
530
diff
changeset
|
55 settings[name] = os.environ[name] |
c938dbfb8944
catch exception if environment contains an invalid locale setting (fixes #200)
fschwarz
parents:
530
diff
changeset
|
56 return settings |
c938dbfb8944
catch exception if environment contains an invalid locale setting (fixes #200)
fschwarz
parents:
530
diff
changeset
|
57 |
c938dbfb8944
catch exception if environment contains an invalid locale setting (fixes #200)
fschwarz
parents:
530
diff
changeset
|
58 def _set_locale_settings(self, settings): |
c938dbfb8944
catch exception if environment contains an invalid locale setting (fixes #200)
fschwarz
parents:
530
diff
changeset
|
59 for name, value in settings.items(): |
c938dbfb8944
catch exception if environment contains an invalid locale setting (fixes #200)
fschwarz
parents:
530
diff
changeset
|
60 os.environ[name] = value |
c938dbfb8944
catch exception if environment contains an invalid locale setting (fixes #200)
fschwarz
parents:
530
diff
changeset
|
61 |
c938dbfb8944
catch exception if environment contains an invalid locale setting (fixes #200)
fschwarz
parents:
530
diff
changeset
|
62 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
|
63 # 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
|
64 # MacOS X 10.6 (#200) |
c938dbfb8944
catch exception if environment contains an invalid locale setting (fixes #200)
fschwarz
parents:
530
diff
changeset
|
65 os.environ['LC_CTYPE'] = 'UTF-8' |
c938dbfb8944
catch exception if environment contains an invalid locale setting (fixes #200)
fschwarz
parents:
530
diff
changeset
|
66 # must not throw an exception |
c938dbfb8944
catch exception if environment contains an invalid locale setting (fixes #200)
fschwarz
parents:
530
diff
changeset
|
67 default_locale('LC_CTYPE') |
1 | 68 |
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
|
69 |
1 | 70 def suite(): |
71 suite = unittest.TestSuite() | |
72 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
|
73 suite.addTest(unittest.makeSuite(LocaleTest)) |
534
c938dbfb8944
catch exception if environment contains an invalid locale setting (fixes #200)
fschwarz
parents:
530
diff
changeset
|
74 suite.addTest(unittest.makeSuite(DefaultLocaleTest)) |
1 | 75 return suite |
76 | |
77 if __name__ == '__main__': | |
78 unittest.main(defaultTest='suite') |