locale_test.py
Go to the documentation of this file.
00001 from __future__ import absolute_import, division, print_function, with_statement
00002 
00003 import datetime
00004 import os
00005 import tornado.locale
00006 from tornado.escape import utf8
00007 from tornado.test.util import unittest
00008 from tornado.util import u, unicode_type
00009 
00010 
00011 class TranslationLoaderTest(unittest.TestCase):
00012     # TODO: less hacky way to get isolated tests
00013     SAVE_VARS = ['_translations', '_supported_locales', '_use_gettext']
00014 
00015     def clear_locale_cache(self):
00016         if hasattr(tornado.locale.Locale, '_cache'):
00017             del tornado.locale.Locale._cache
00018 
00019     def setUp(self):
00020         self.saved = {}
00021         for var in TranslationLoaderTest.SAVE_VARS:
00022             self.saved[var] = getattr(tornado.locale, var)
00023         self.clear_locale_cache()
00024 
00025     def tearDown(self):
00026         for k, v in self.saved.items():
00027             setattr(tornado.locale, k, v)
00028         self.clear_locale_cache()
00029 
00030     def test_csv(self):
00031         tornado.locale.load_translations(
00032             os.path.join(os.path.dirname(__file__), 'csv_translations'))
00033         locale = tornado.locale.get("fr_FR")
00034         self.assertTrue(isinstance(locale, tornado.locale.CSVLocale))
00035         self.assertEqual(locale.translate("school"), u("\u00e9cole"))
00036 
00037     def test_gettext(self):
00038         tornado.locale.load_gettext_translations(
00039             os.path.join(os.path.dirname(__file__), 'gettext_translations'),
00040             "tornado_test")
00041         locale = tornado.locale.get("fr_FR")
00042         self.assertTrue(isinstance(locale, tornado.locale.GettextLocale))
00043         self.assertEqual(locale.translate("school"), u("\u00e9cole"))
00044 
00045 
00046 class LocaleDataTest(unittest.TestCase):
00047     def test_non_ascii_name(self):
00048         name = tornado.locale.LOCALE_NAMES['es_LA']['name']
00049         self.assertTrue(isinstance(name, unicode_type))
00050         self.assertEqual(name, u('Espa\u00f1ol'))
00051         self.assertEqual(utf8(name), b'Espa\xc3\xb1ol')
00052 
00053 
00054 class EnglishTest(unittest.TestCase):
00055     def test_format_date(self):
00056         locale = tornado.locale.get('en_US')
00057         date = datetime.datetime(2013, 4, 28, 18, 35)
00058         self.assertEqual(locale.format_date(date, full_format=True),
00059                          'April 28, 2013 at 6:35 pm')


rosbridge_tools
Author(s): Jonathan Mace
autogenerated on Sat Dec 27 2014 11:25:59