summaryrefslogtreecommitdiff
path: root/Lib/test/test_datetime.py
diff options
context:
space:
mode:
authorEric Smith <eric@trueblade.com>2007-09-11 18:06:02 +0000
committerEric Smith <eric@trueblade.com>2007-09-11 18:06:02 +0000
commit5d538e59ca533aa0f6c6be2ee91105e8ab43ecdc (patch)
tree922903f2d3db41cd435338f5866041031835482c /Lib/test/test_datetime.py
parent20385d20761b0f575849c44071a2683ba492f77f (diff)
downloadcpython-5d538e59ca533aa0f6c6be2ee91105e8ab43ecdc.tar.gz
Added __format__ method to datetime.datetime, datetime.date, and datetime.time.
If format_spec is empty, __format__ calls str(self), else it calls self.strftime(format_spec).
Diffstat (limited to 'Lib/test/test_datetime.py')
-rw-r--r--Lib/test/test_datetime.py78
1 files changed, 78 insertions, 0 deletions
diff --git a/Lib/test/test_datetime.py b/Lib/test/test_datetime.py
index dce9fc348d..b1a1b38802 100644
--- a/Lib/test/test_datetime.py
+++ b/Lib/test/test_datetime.py
@@ -849,6 +849,32 @@ class TestDate(HarmlessMixedComparison, unittest.TestCase):
# A naive object replaces %z and %Z w/ empty strings.
self.assertEqual(t.strftime("'%z' '%Z'"), "'' ''")
+ def test_format(self):
+ dt = self.theclass(2007, 9, 10)
+ self.assertEqual(format(dt, ''), str(dt))
+
+ # check that a derived class's __str__() gets called
+ class A(self.theclass):
+ def __str__(self):
+ return 'A'
+ a = A(2007, 9, 10)
+ self.assertEqual(format(a, ''), 'A')
+
+ # check that a derived class's strftime gets called
+ class B(self.theclass):
+ def strftime(self, format_spec):
+ return 'B'
+ b = B(2007, 9, 10)
+ self.assertEqual(format(b, ''), str(dt))
+
+ for fmt in ["m:%m d:%d y:%y",
+ "m:%m d:%d y:%y H:%H M:%M S:%S",
+ "%z %Z",
+ ]:
+ self.assertEqual(format(dt, fmt), dt.strftime(fmt))
+ self.assertEqual(format(a, fmt), dt.strftime(fmt))
+ self.assertEqual(format(b, fmt), 'B')
+
def test_resolution_info(self):
self.assert_(isinstance(self.theclass.min, self.theclass))
self.assert_(isinstance(self.theclass.max, self.theclass))
@@ -1150,6 +1176,34 @@ class TestDateTime(TestDate):
# str is ISO format with the separator forced to a blank.
self.assertEqual(str(t), "0002-03-02 00:00:00")
+ def test_format(self):
+ dt = self.theclass(2007, 9, 10, 4, 5, 1, 123)
+ self.assertEqual(format(dt, ''), str(dt))
+
+ # check that a derived class's __str__() gets called
+ class A(self.theclass):
+ def __str__(self):
+ return 'A'
+ a = A(2007, 9, 10, 4, 5, 1, 123)
+ self.assertEqual(format(a, ''), 'A')
+
+ # check that a derived class's strftime gets called
+ class B(self.theclass):
+ def strftime(self, format_spec):
+ return 'B'
+ b = B(2007, 9, 10, 4, 5, 1, 123)
+ self.assertEqual(format(b, ''), str(dt))
+
+ for fmt in ["m:%m d:%d y:%y",
+ "m:%m d:%d y:%y H:%H M:%M S:%S",
+ "%z %Z",
+ ]:
+ self.assertEqual(format(dt, fmt), dt.strftime(fmt))
+ self.assertEqual(format(a, fmt), dt.strftime(fmt))
+ self.assertEqual(format(b, fmt), 'B')
+
+
+
def test_more_ctime(self):
# Test fields that TestDate doesn't touch.
import time
@@ -1781,6 +1835,30 @@ class TestTime(HarmlessMixedComparison, unittest.TestCase):
# A naive object replaces %z and %Z with empty strings.
self.assertEqual(t.strftime("'%z' '%Z'"), "'' ''")
+ def test_format(self):
+ t = self.theclass(1, 2, 3, 4)
+ self.assertEqual(format(t, ''), str(t))
+
+ # check that a derived class's __str__() gets called
+ class A(self.theclass):
+ def __str__(self):
+ return 'A'
+ a = A(1, 2, 3, 4)
+ self.assertEqual(format(a, ''), 'A')
+
+ # check that a derived class's strftime gets called
+ class B(self.theclass):
+ def strftime(self, format_spec):
+ return 'B'
+ b = B(1, 2, 3, 4)
+ self.assertEqual(format(b, ''), str(t))
+
+ for fmt in ['%H %M %S',
+ ]:
+ self.assertEqual(format(t, fmt), t.strftime(fmt))
+ self.assertEqual(format(a, fmt), t.strftime(fmt))
+ self.assertEqual(format(b, fmt), 'B')
+
def test_str(self):
self.assertEqual(str(self.theclass(1, 2, 3, 4)), "01:02:03.000004")
self.assertEqual(str(self.theclass(10, 2, 3, 4000)), "10:02:03.004000")