strontium avatar strontium committed b52d809

Add unittests to replace deactivated doctests for Python 3 for dates.py
dates.py passes all tests for Py2.6, Py2.7 and Py3.2

Comments (0)

Files changed (2)

babel/tests/core.py

         
         if sys.version_info >=(3, 0):
             # This test should also work for Py2 as fare as I can tell, but fails because
-            # for some reason repr() on py2 is returning "<NumberPattern u'\\xa4#,##0.00'>
+            # for some reason repr() on py2 is returning "<NumberPattern u'\\xa4#,##0.00'>"
             self.assertEqual(repr(Locale('en', 'US').currency_formats[None]) ,'<NumberPattern %s\'\xa4#,##0.00\'>' % py2u)
         else:
             self.assertEqual(Locale('en', 'US').currency_formats[None].pattern, u'\xa4#,##0.00' )

babel/tests/dates.py

 
 from babel import dates
 from babel.util import FixedOffsetTimezone
+from babel.core import Locale
+from babel.dates import DateTimeFormat, format_date, format_datetime, \
+                        format_time, format_timedelta, get_date_format, \
+                        get_datetime_format, get_day_names, get_era_names, \
+                        get_month_names, get_period_names, \
+                        get_quarter_names, get_time_format, \
+                        get_timezone_gmt, get_timezone_location, \
+                        get_timezone_name, parse_date, parse_pattern, \
+                        parse_time
 
+if sys.version_info >= (3, 0):
+  #Python 3 no need for u''
+  py2u = '' 
+else:
+  py2u = 'u'
 
 class DateTimeFormatTestCase(unittest.TestCase):
 
         formatted_time = dates.format_time(t, 'long', tzinfo=utc, locale='en')
         self.assertEqual('3:30:00 PM +0000', formatted_time)
 
+class datesDocTest(unittest.TestCase):
+
+    def test_dates_DateTimeFormat_get_week_number(self):
+        # Return the number of the week of a day within a period. 
+        format = DateTimeFormat(date(2006, 1, 8), Locale.parse('de_DE'))
+        self.assertEqual(format.get_week_number(6),1)
+        format = DateTimeFormat(date(2006, 1, 8), Locale.parse('en_US'))
+        self.assertEqual(format.get_week_number(6),2)
+
+    def test_dates_format_date(self):
+        # Return a date formatted according to the given pattern.
+        d = date(2007, 04, 01)
+        self.assertEqual(format_date(d, locale='en_US'), u'Apr 1, 2007')
+        self.assertEqual(format_date(d, format='full', locale='de_DE'), u'Sonntag, 1. April 2007')
+        self.assertEqual(format_date(d, "EEE, MMM d, ''yy", locale='en'), u"Sun, Apr 1, '07")
+
+    def test_dates_format_datetime(self):
+        # Return a date formatted according to the given pattern.
+        dt = datetime(2007, 04, 01, 15, 30)
+        self.assertEqual(format_datetime(dt, locale='en_US'), u'Apr 1, 2007 3:30:00 PM')
+        self.assertEqual(format_datetime(dt, 'full', tzinfo=timezone('Europe/Paris'), locale='fr_FR'),
+                    u'dimanche 1 avril 2007 17:30:00 Heure avanc\xe9e de l\u2019Europe centrale')
+        self.assertEqual(format_datetime(dt, "yyyy.MM.dd G 'at' HH:mm:ss zzz",
+                             tzinfo=timezone('US/Eastern'), locale='en'), u'2007.04.01 AD at 11:30:00 EDT')
+    
+    def test_dates_format_time(self):
+        # Return a time formatted according to the given pattern.
+        t = time(15, 30)
+        self.assertEqual(format_time(t, locale='en_US'), u'3:30:00 PM')
+        self.assertEqual(format_time(t, format='short', locale='de_DE'), u'15:30')
+        self.assertEqual(format_time(t, "hh 'o''clock' a", locale='en'), u"03 o'clock PM")
+        t = datetime(2007, 4, 1, 15, 30)
+        tzinfo = timezone('Europe/Paris')
+        t = tzinfo.localize(t)
+        self.assertEqual(format_time(t, format='full', tzinfo=tzinfo, locale='fr_FR'),
+                         u'15:30:00 Heure avanc\xe9e de l\u2019Europe centrale')
+        self.assertEqual(format_time(t, "hh 'o''clock' a, zzzz", tzinfo=timezone('US/Eastern'),
+                         locale='en'), u"09 o'clock AM, Eastern Daylight Time")
+    
+        t = time(15, 30)
+        self.assertEqual(format_time(t, format='full', tzinfo=timezone('Europe/Paris'),
+                         locale='fr_FR'), u'15:30:00 Heure normale de l\u2019Europe centrale')
+        self.assertEqual(format_time(t, format='full', tzinfo=timezone('US/Eastern'),
+                         locale='en_US'), u'3:30:00 PM Eastern Standard Time')
+
+    def test_dates_format_timedelta(self):
+        # Return a time delta according to the rules of the given locale.
+        self.assertEqual(format_timedelta(timedelta(weeks=12), locale='en_US'), u'3 mths')
+        self.assertEqual(format_timedelta(timedelta(seconds=1), locale='es'), u'1 s')
+        self.assertEqual(format_timedelta(timedelta(hours=3), granularity='day', locale='en_US'), u'1 day')
+        self.assertEqual(format_timedelta(timedelta(hours=23), threshold=0.9, locale='en_US'), u'1 day')
+        self.assertEqual(format_timedelta(timedelta(hours=23), threshold=1.1, locale='en_US'), u'23 hrs')
+
+    def test_dates_get_date_format(self):
+        # Return the date formatting patterns used by the locale for the specified format.
+        self.assertEqual(repr(get_date_format(locale='en_US')),'<DateTimePattern %s\'MMM d, y\'>'% py2u)
+        self.assertEqual(repr(get_date_format('full', locale='de_DE')),'<DateTimePattern %s\'EEEE, d. MMMM y\'>'% py2u)
+
+    def test_dates_get_datetime_format(self):
+        # Return the datetime formatting patterns used by the locale for the specified format.
+        self.assertEqual(get_datetime_format(locale='en_US'), u'{1} {0}')
+
+    def test_dates_get_day_names(self):
+        # Return the day names used by the locale for the specified format.
+        self.assertEqual(get_day_names('wide', locale='en_US')[1], u'Tuesday')
+        self.assertEqual(get_day_names('abbreviated', locale='es')[1], u'mar')
+        self.assertEqual(get_day_names('narrow', context='stand-alone', locale='de_DE')[1], u'D')
+
+    def test_dates_get_era_names(self):
+        # Return the era names used by the locale for the specified format.
+        self.assertEqual(get_era_names('wide', locale='en_US')[1], u'Anno Domini')
+        self.assertEqual(get_era_names('abbreviated', locale='de_DE')[1], u'n. Chr.')
+
+    def test_dates_get_month_names(self):
+        # Return the month names used by the locale for the specified format.
+        self.assertEqual(get_month_names('wide', locale='en_US')[1], u'January')
+        self.assertEqual(get_month_names('abbreviated', locale='es')[1], u'ene')
+        self.assertEqual(get_month_names('narrow', context='stand-alone', locale='de_DE')[1], u'J')
+
+    def test_dates_get_period_names(self):
+        # Return the names for day periods (AM/PM) used by the locale.
+        self.assertEqual(get_period_names(locale='en_US')['am'], u'AM')
+
+    def test_dates_get_quarter_names(self):
+        # Return the quarter names used by the locale for the specified format.
+        self.assertEqual(get_quarter_names('wide', locale='en_US')[1], u'1st quarter')
+        self.assertEqual(get_quarter_names('abbreviated', locale='de_DE')[1], u'Q1')
+
+    def test_dates_get_time_format(self):
+        # Return the time formatting patterns used by the locale for the specified format.
+        self.assertEqual(repr(get_time_format(locale='en_US')), '<DateTimePattern %s\'h:mm:ss a\'>' % py2u)
+        self.assertEqual(repr(get_time_format('full', locale='de_DE')), '<DateTimePattern %s\'HH:mm:ss zzzz\'>' % py2u)
+
+    def test_dates_get_timezone_gmt(self):
+        # Return the timezone associated with the given `datetime` object formatted
+        # as string indicating the offset from GMT.
+        dt = datetime(2007, 4, 1, 15, 30)
+        self.assertEqual(get_timezone_gmt(dt, locale='en'), u'GMT+00:00')
+        tz = timezone('America/Los_Angeles')
+        dt = datetime(2007, 4, 1, 15, 30, tzinfo=tz)
+        self.assertEqual(get_timezone_gmt(dt, locale='en'), u'GMT-08:00')
+        self.assertEqual(get_timezone_gmt(dt, 'short', locale='en'), u'-0800')
+        self.assertEqual(get_timezone_gmt(dt, 'long', locale='fr_FR'), u'UTC-08:00')
+
+    def test_dates_get_timezone_location(self):
+        # Return a representation of the given timezone using "location format".
+        tz = timezone('America/St_Johns')
+        self.assertEqual(get_timezone_location(tz, locale='de_DE'), u"Kanada (St. John's)")
+        tz = timezone('America/Mexico_City')
+        self.assertEqual(get_timezone_location(tz, locale='de_DE'), u'Mexiko (Mexiko-Stadt)')
+        tz = timezone('Europe/Berlin')
+        self.assertEqual(get_timezone_location(tz, locale='de_DE'), u'Deutschland')
+
+    def test_dates_get_timezone_name(self):
+        # Return the localized display name for the given timezone. 
+        dt = time(15, 30, tzinfo=timezone('America/Los_Angeles'))
+        self.assertEqual(get_timezone_name(dt, locale='en_US'), u'Pacific Standard Time')
+        self.assertEqual(get_timezone_name(dt, width='short', locale='en_US'),u'PST')
+        tz = timezone('America/Los_Angeles')
+        self.assertEqual(get_timezone_name(tz, locale='en_US'), u'Pacific Time')
+        self.assertEqual(get_timezone_name(tz, 'short', locale='en_US'), u'PT')
+        tz = timezone('Europe/Berlin')
+        self.assertEqual(get_timezone_name(tz, locale='de_DE'), u'Deutschland')
+        self.assertEqual(get_timezone_name(tz, locale='pt_BR'), u'Hor\xe1rio Alemanha')
+        tz = timezone('America/St_Johns')
+        self.assertEqual(get_timezone_name(tz, locale='de_DE'), u"Kanada (St. John's)")
+        tz = timezone('Europe/Paris')
+        self.assertEqual(get_timezone_name(tz, 'short', locale='fr_CA'), u'France')
+        self.assertEqual(get_timezone_name(tz, 'short', uncommon=True, locale='fr_CA'), u'HEC')
+
+    def test_dates_parse_date(self):
+        # Parse a date from a string.
+        self.assertEqual(repr(parse_date('4/1/04', locale='en_US')), 'datetime.date(2004, 4, 1)')
+        self.assertEqual(repr(parse_date('01.04.2004', locale='de_DE')), 'datetime.date(2004, 4, 1)')
+
+    def test_dates_parse_pattern(self):
+        # Parse date, time, and datetime format patterns.
+        self.assertEqual(parse_pattern("MMMMd").format, u'%(MMMM)s%(d)s')
+        self.assertEqual(parse_pattern("MMM d, yyyy").format, u'%(MMM)s %(d)s, %(yyyy)s')
+        self.assertEqual(parse_pattern("H:mm' Uhr 'z").format, u'%(H)s:%(mm)s Uhr %(z)s')
+        self.assertEqual(parse_pattern("hh' o''clock'").format, u"%(hh)s o'clock")
+
+    def test_dates_parse_time(self):
+        # Parse a time from a string.
+        self.assertEqual(repr(parse_time('15:30:00', locale='en_US')), 'datetime.time(15, 30)')
 
 def suite():
     suite = unittest.TestSuite()
     if sys.version_info < (3, 0):
         #Doctests are not converted properly by 2to3, skip them for Python3.
         suite.addTest(doctest.DocTestSuite(dates))
+    suite.addTest(unittest.makeSuite(datesDocTest))
     suite.addTest(unittest.makeSuite(DateTimeFormatTestCase))
     suite.addTest(unittest.makeSuite(FormatDateTestCase))
     suite.addTest(unittest.makeSuite(FormatTimeTestCase))
 
 if __name__ == '__main__':
     unittest.main(defaultTest='suite')
+
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.