From 739e2ad64bc2a5a345723a23ca01fa6b54ba1c9f Mon Sep 17 00:00:00 2001 From: Eric Smith Date: Mon, 27 Aug 2007 19:07:22 +0000 Subject: Additional test for formatting code. --- Lib/test/test_builtin.py | 3 ++- Lib/test/test_unicode.py | 12 +++++++++--- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/Lib/test/test_builtin.py b/Lib/test/test_builtin.py index 0560045..2d3e1cc 100644 --- a/Lib/test/test_builtin.py +++ b/Lib/test/test_builtin.py @@ -541,7 +541,8 @@ class BuiltinTest(unittest.TestCase): self.assertEqual(format(-3.1415e-104, ""), str(-3.1415e-104)) self.assertEqual(format(object, ""), str(object)) - #self.assertRaises(TypeError, format, H(), "") + # TypeError because self.__format__ returns the wrong type + self.assertRaises(TypeError, format, H(), "") def test_getattr(self): import sys diff --git a/Lib/test/test_unicode.py b/Lib/test/test_unicode.py index 662acd3..47d7236 100644 --- a/Lib/test/test_unicode.py +++ b/Lib/test/test_unicode.py @@ -424,6 +424,10 @@ class UnicodeTest( def __format__(self, format_spec): return 1.0 + class I(datetime.date): + def __format__(self, format_spec): + return self.strftime(format_spec) + self.assertEqual(''.format(), '') self.assertEqual('abc'.format(), 'abc') @@ -504,9 +508,6 @@ class UnicodeTest( self.assertEqual('{0!r:}'.format('Hello'), "'Hello'") self.assertEqual('{0!r}'.format(F('Hello')), 'F(Hello)') - # XXX should pass, but currently don't - # format(object, "") - # test fallback to object.__format__ self.assertEqual('{0}'.format({}), '{}') self.assertEqual('{0}'.format([]), '[]') @@ -518,6 +519,11 @@ class UnicodeTest( self.assertEqual('{0:>15s}'.format(G('data')), ' string is data') self.assertEqual('{0!s}'.format(G('data')), 'string is data') + self.assertEqual("{0:date: %Y-%m-%d}".format(I(year=2007, + month=8, + day=27)), + "date: 2007-08-27") + # string format specifiers self.assertEqual('{0:}'.format('a'), 'a') -- cgit v0.12