changeset 19:d8352fbaca65 trunk

Moved some datetime tests from doctest to unittest, to avoid breaking docutils/epydoc doctest block detection.
author cmlenz
date Thu, 31 May 2007 17:56:14 +0000
parents 990909fdf98b
children c4fc7b88206e
files babel/dates.py babel/tests/dates.py
diffstat 2 files changed, 42 insertions(+), 40 deletions(-) [+]
line wrap: on
line diff
--- a/babel/dates.py
+++ b/babel/dates.py
@@ -160,28 +160,16 @@
     >>> format_time(d, "EEE, MMM d, ''yy", locale='en')
     u"Sun, Apr 1, '07"
     
-    If the pattern contains time fields, an `AttributeError` will be raised
-    when trying to apply the formatting:
-    
-    >>> format_date(d, "yyyy-MM-dd HH:mm", locale='en_US')
-    Traceback (most recent call last):
-      ...
-    AttributeError: 'datetime.date' object has no attribute 'hour'
-    
-    This is also true if the value of ``date`` parameter is a ``datetime``
-    object, as this function automatically converts it to a ``date``::
-    
-    >>> dt = datetime(2007, 04, 01, 15, 30)
-    >>> format_date(dt, "yyyy-MM-dd HH:mm", locale='en_US')
-    Traceback (most recent call last):
-      ...
-    AttributeError: 'datetime.date' object has no attribute 'hour'
-    
-    :param date: the ``date`` object
+    :param date: the ``date`` or ``datetime`` object
     :param format: one of "full", "long", "medium", or "short", or a custom
                    date/time pattern
-    :param locale: a `Locale` object or a locale string
+    :param locale: a `Locale` object or a locale identifier
     :rtype: `unicode`
+    
+    :note: If the pattern contains time fields, an `AttributeError` will be
+           raised when trying to apply the formatting. This is also true if
+           the value of ``date`` parameter is actually a ``datetime`` object,
+           as this function automatically converts that to a ``date``.
     """
     if isinstance(date, datetime):
         date = date.date()
@@ -197,7 +185,7 @@
     :param datetime: the ``date`` object
     :param format: one of "full", "long", "medium", or "short", or a custom
                    date/time pattern
-    :param locale: a `Locale` object or a locale string
+    :param locale: a `Locale` object or a locale identifier
     :rtype: `unicode`
     """
     locale = Locale.parse(locale)
@@ -221,28 +209,16 @@
     >>> format_time(t, "hh 'o''clock' a", locale='en')
     u"03 o'clock PM"
     
-    If the pattern contains date fields, an `AttributeError` will be raised
-    when trying to apply the formatting:
-    
-    >>> format_time(t, "yyyy-MM-dd HH:mm", locale='en_US')
-    Traceback (most recent call last):
-      ...
-    AttributeError: 'datetime.time' object has no attribute 'year'
-    
-    This is also true if the value of ``time`` parameter is a ``datetime``
-    object, as this function automatically converts it to a ``time``::
-    
-    >>> dt = datetime(2007, 04, 01, 15, 30)
-    >>> format_time(dt, "yyyy-MM-dd HH:mm", locale='en_US')
-    Traceback (most recent call last):
-      ...
-    AttributeError: 'datetime.time' object has no attribute 'year'
-    
-    :param time: the ``time`` object
+    :param time: the ``time`` or ``datetime`` object
     :param format: one of "full", "long", "medium", or "short", or a custom
                    date/time pattern
-    :param locale: a `Locale` object or a locale string
+    :param locale: a `Locale` object or a locale identifier
     :rtype: `unicode`
+    
+    :note: If the pattern contains date fields, an `AttributeError` will be
+           raised when trying to apply the formatting. This is also true if
+           the value of ``time`` parameter is actually a ``datetime`` object,
+           as this function automatically converts that to a ``time``.
     """
     if isinstance(time, (int, long)):
         time = datetime.fromtimestamp(time).time()
--- a/babel/tests/dates.py
+++ b/babel/tests/dates.py
@@ -11,7 +11,7 @@
 # individuals. For the exact contribution history, see the revision
 # history and logs, available at http://babel.edgewall.org/log/.
 
-from datetime import date, datetime
+from datetime import date, datetime, time
 import doctest
 import unittest
 
@@ -55,10 +55,36 @@
         self.assertEqual('4', fmt['c']) # friday is first day of week
 
 
+class FormatDateTestCase(unittest.TestCase):
+
+    def test_with_time_fields_in_pattern(self):
+        self.assertRaises(AttributeError, dates.format_date, date(2007, 04, 01),
+                          "yyyy-MM-dd HH:mm", locale='en_US')
+
+    def test_with_time_fields_in_pattern_and_datetime_param(self):
+        self.assertRaises(AttributeError, dates.format_date,
+                          datetime(2007, 04, 01, 15, 30),
+                          "yyyy-MM-dd HH:mm", locale='en_US')
+
+
+class FormatTimeTestCase(unittest.TestCase):
+
+    def test_with_date_fields_in_pattern(self):
+        self.assertRaises(AttributeError, dates.format_time, date(2007, 04, 01),
+                          "yyyy-MM-dd HH:mm", locale='en_US')
+
+    def test_with_date_fields_in_pattern_and_datetime_param(self):
+        self.assertRaises(AttributeError, dates.format_time,
+                          datetime(2007, 04, 01, 15, 30),
+                          "yyyy-MM-dd HH:mm", locale='en_US')
+
+
 def suite():
     suite = unittest.TestSuite()
     suite.addTest(doctest.DocTestSuite(dates))
     suite.addTest(unittest.makeSuite(DateTimeFormatTestCase))
+    suite.addTest(unittest.makeSuite(FormatDateTestCase))
+    suite.addTest(unittest.makeSuite(FormatTimeTestCase))
     return suite
 
 if __name__ == '__main__':
Copyright (C) 2012-2017 Edgewall Software