Added format tests.

Fixed bug in alignment of negative numbers.
Whitespace normalization.
This commit is contained in:
Eric Smith 2007-08-30 22:23:08 +00:00
parent bc3b345f82
commit 185e30cdf3
4 changed files with 36 additions and 24 deletions

View File

@ -540,6 +540,7 @@ class BuiltinTest(unittest.TestCase):
self.assertEqual(format(3.1415e-104, ""), str(3.1415e-104))
self.assertEqual(format(-3.1415e-104, ""), str(-3.1415e-104))
self.assertEqual(format(object, ""), str(object))
self.assertEqual(format(None, ""), str(None))
# TypeError because self.__format__ returns the wrong type
self.assertRaises(TypeError, format, H(), "")

View File

@ -497,13 +497,25 @@ class LongTest(unittest.TestCase):
self.assertEqual(format(123456789, 'd'), '123456789')
self.assertEqual(format(123456789, 'd'), '123456789')
# sign and aligning are interdependent
self.assertEqual(format(1, "-"), '1')
self.assertEqual(format(-1, "-"), '-1')
self.assertEqual(format(1, "-3"), ' 1')
self.assertEqual(format(-1, "-3"), ' -1')
self.assertEqual(format(1, "+3"), ' +1')
self.assertEqual(format(-1, "+3"), ' -1')
self.assertEqual(format(1, " 3"), ' 1')
self.assertEqual(format(-1, " 3"), ' -1')
self.assertEqual(format(1, " "), ' 1')
self.assertEqual(format(-1, " "), '-1')
# hex
self.assertEqual(format(3, "x"), "3")
self.assertEqual(format(3, "X"), "3")
self.assertEqual(format(1234, "x"), "4d2")
self.assertEqual(format(-1234, "x"), "-4d2")
self.assertEqual(format(1234, "8x"), " 4d2")
# XXX fix self.assertEqual(format(-1234, "8x"), " -4d2")
self.assertEqual(format(-1234, "8x"), " -4d2")
self.assertEqual(format(1234, "x"), "4d2")
self.assertEqual(format(-1234, "x"), "-4d2")
self.assertEqual(format(-3, "x"), "-3")
@ -530,7 +542,6 @@ class LongTest(unittest.TestCase):
# make sure these are errors
self.assertRaises(ValueError, format, 3, "1.3") # precision disallowed
return
self.assertRaises(ValueError, format, 3, "+c") # sign not allowed
# with 'c'
self.assertRaises(ValueError, format, 3, "R") # bogus format type

View File

@ -379,8 +379,6 @@ class UnicodeTest(
self.assertEqual("The year is {0.year}".format(d),
"The year is 2007")
#"{0!r:20}".format("Hello")
# classes we'll use for testing
class C:
def __init__(self, x=100):
@ -428,6 +426,10 @@ class UnicodeTest(
def __format__(self, format_spec):
return self.strftime(format_spec)
class J(int):
def __format__(self, format_spec):
return int.__format__(self * 2, format_spec)
self.assertEqual(''.format(), '')
self.assertEqual('abc'.format(), 'abc')
@ -465,12 +467,6 @@ class UnicodeTest(
self.assertEqual('{0[1][0]}'.format(['abc', ['def']]), 'def')
self.assertEqual('{0[1][0].x}'.format(['abc', [D('def')]]), 'def')
# I'm not sure if this should work, or if it's a problem if it does work
#'{0[_{foo}]}'.format({'_FOO': 'abc'}, foo='FOO')
#('{0[{foo}{bar}]}'.format({'FOOBAR': 'abc'}, foo='FOO', bar='BAR')
# format specifiers for built in types
# strings
self.assertEqual('{0:.3s}'.format('abc'), 'abc')
self.assertEqual('{0:.3s}'.format('ab'), 'ab')
@ -524,6 +520,10 @@ class UnicodeTest(
day=27)),
"date: 2007-08-27")
# test deriving from a builtin type and overriding __format__
self.assertEqual("{0}".format(J(10)), "20")
# string format specifiers
self.assertEqual('{0:}'.format('a'), 'a')

View File

@ -145,8 +145,7 @@ parse_internal_render_format_spec(PyObject *format_spec,
}
/* The special case for 0-padding (backwards compat) */
if (format->fill_char == '\0' &&
end-ptr >= 1 && ptr[0] == '0') {
if (format->fill_char == '\0' && end-ptr >= 1 && ptr[0] == '0') {
format->fill_char = '0';
if (format->align == '\0') {
format->align = '=';
@ -298,9 +297,10 @@ calc_number_widths(NumberFieldWidths *r, STRINGLIB_CHAR actual_sign,
r->n_lpadding = padding / 2;
r->n_rpadding = padding - r->n_lpadding;
}
else
/* must be '=' */
else if (format->align == '=')
r->n_spadding = padding;
else
r->n_lpadding = padding;
}
}
r->n_total = r->n_lpadding + r->n_lsign + r->n_spadding +