diff options
author | Eric Smith <eric@trueblade.com> | 2010-11-25 16:08:06 (GMT) |
---|---|---|
committer | Eric Smith <eric@trueblade.com> | 2010-11-25 16:08:06 (GMT) |
commit | 984bb58000df9cdba438c7ecb0bae5ad67878696 (patch) | |
tree | 87077ab2bbe949b5241ed9db5f2073572094ffde /Lib/test/test_complex.py | |
parent | c1d98d685032dd831ced32463b3f88cce6af4067 (diff) | |
download | cpython-984bb58000df9cdba438c7ecb0bae5ad67878696.zip cpython-984bb58000df9cdba438c7ecb0bae5ad67878696.tar.gz cpython-984bb58000df9cdba438c7ecb0bae5ad67878696.tar.bz2 |
Issue #7094: Add alternate ('#') flag to __format__ methods for float, complex and Decimal. Allows greater control over when decimal points appear. Added to make transitioning from %-formatting easier. '#g' still has a problem with Decimal which I'll fix soon.
Diffstat (limited to 'Lib/test/test_complex.py')
-rw-r--r-- | Lib/test/test_complex.py | 24 |
1 files changed, 22 insertions, 2 deletions
diff --git a/Lib/test/test_complex.py b/Lib/test/test_complex.py index 2810b36..cc21aa7 100644 --- a/Lib/test/test_complex.py +++ b/Lib/test/test_complex.py @@ -555,8 +555,28 @@ class ComplexTest(unittest.TestCase): self.assertEqual(format(1.5e21+3j, '^40,.2f'), ' 1,500,000,000,000,000,000,000.00+3.00j ') self.assertEqual(format(1.5e21+3000j, ',.2f'), '1,500,000,000,000,000,000,000.00+3,000.00j') - # alternate is invalid - self.assertRaises(ValueError, (1.5+0.5j).__format__, '#f') + # Issue 7094: Alternate formatting (specified by #) + self.assertEqual(format(1+1j, '.0e'), '1e+00+1e+00j') + self.assertEqual(format(1+1j, '#.0e'), '1.e+00+1.e+00j') + self.assertEqual(format(1+1j, '.0f'), '1+1j') + self.assertEqual(format(1+1j, '#.0f'), '1.+1.j') + self.assertEqual(format(1.1+1.1j, 'g'), '1.1+1.1j') + self.assertEqual(format(1.1+1.1j, '#g'), '1.10000+1.10000j') + + # Alternate doesn't make a difference for these, they format the same with or without it + self.assertEqual(format(1+1j, '.1e'), '1.0e+00+1.0e+00j') + self.assertEqual(format(1+1j, '#.1e'), '1.0e+00+1.0e+00j') + self.assertEqual(format(1+1j, '.1f'), '1.0+1.0j') + self.assertEqual(format(1+1j, '#.1f'), '1.0+1.0j') + + # Misc. other alternate tests + self.assertEqual(format((-1.5+0.5j), '#f'), '-1.500000+0.500000j') + self.assertEqual(format((-1.5+0.5j), '#.0f'), '-2.+0.j') + self.assertEqual(format((-1.5+0.5j), '#e'), '-1.500000e+00+5.000000e-01j') + self.assertEqual(format((-1.5+0.5j), '#.0e'), '-2.e+00+5.e-01j') + self.assertEqual(format((-1.5+0.5j), '#g'), '-1.50000+0.500000j') + self.assertEqual(format((-1.5+0.5j), '.0g'), '-2+0.5j') + self.assertEqual(format((-1.5+0.5j), '#.0g'), '-2.+0.5j') # zero padding is invalid self.assertRaises(ValueError, (1.5+0.5j).__format__, '010f') |