Back to index

python3.2  3.2.2
Public Member Functions
test.test_float.FormatTestCase Class Reference

List of all members.

Public Member Functions

def test_format
def test_format_testfile
def test_issue5864

Detailed Description

Definition at line 518 of file test_float.py.


Member Function Documentation

Definition at line 520 of file test_float.py.

00520 
00521     def test_format(self):
00522         # these should be rewritten to use both format(x, spec) and
00523         # x.__format__(spec)
00524 
00525         self.assertEqual(format(0.0, 'f'), '0.000000')
00526 
00527         # the default is 'g', except for empty format spec
00528         self.assertEqual(format(0.0, ''), '0.0')
00529         self.assertEqual(format(0.01, ''), '0.01')
00530         self.assertEqual(format(0.01, 'g'), '0.01')
00531 
00532         # empty presentation type should format in the same way as str
00533         # (issue 5920)
00534         x = 100/7.
00535         self.assertEqual(format(x, ''), str(x))
00536         self.assertEqual(format(x, '-'), str(x))
00537         self.assertEqual(format(x, '>'), str(x))
00538         self.assertEqual(format(x, '2'), str(x))
00539 
00540         self.assertEqual(format(1.0, 'f'), '1.000000')
00541 
00542         self.assertEqual(format(-1.0, 'f'), '-1.000000')
00543 
00544         self.assertEqual(format( 1.0, ' f'), ' 1.000000')
00545         self.assertEqual(format(-1.0, ' f'), '-1.000000')
00546         self.assertEqual(format( 1.0, '+f'), '+1.000000')
00547         self.assertEqual(format(-1.0, '+f'), '-1.000000')
00548 
00549         # % formatting
00550         self.assertEqual(format(-1.0, '%'), '-100.000000%')
00551 
00552         # conversion to string should fail
00553         self.assertRaises(ValueError, format, 3.0, "s")
00554 
00555         # other format specifiers shouldn't work on floats,
00556         #  in particular int specifiers
00557         for format_spec in ([chr(x) for x in range(ord('a'), ord('z')+1)] +
00558                             [chr(x) for x in range(ord('A'), ord('Z')+1)]):
00559             if not format_spec in 'eEfFgGn%':
00560                 self.assertRaises(ValueError, format, 0.0, format_spec)
00561                 self.assertRaises(ValueError, format, 1.0, format_spec)
00562                 self.assertRaises(ValueError, format, -1.0, format_spec)
00563                 self.assertRaises(ValueError, format, 1e100, format_spec)
00564                 self.assertRaises(ValueError, format, -1e100, format_spec)
00565                 self.assertRaises(ValueError, format, 1e-100, format_spec)
00566                 self.assertRaises(ValueError, format, -1e-100, format_spec)
00567 
00568         # issue 3382
00569         self.assertEqual(format(NAN, 'f'), 'nan')
00570         self.assertEqual(format(NAN, 'F'), 'NAN')
00571         self.assertEqual(format(INF, 'f'), 'inf')
00572         self.assertEqual(format(INF, 'F'), 'INF')

Here is the call graph for this function:

Definition at line 574 of file test_float.py.

00574 
00575     def test_format_testfile(self):
00576         with open(format_testfile) as testfile:
00577             for line in testfile:
00578                 if line.startswith('--'):
00579                     continue
00580                 line = line.strip()
00581                 if not line:
00582                     continue
00583 
00584                 lhs, rhs = map(str.strip, line.split('->'))
00585                 fmt, arg = lhs.split()
00586                 self.assertEqual(fmt % float(arg), rhs)
00587                 self.assertEqual(fmt % -float(arg), '-' + rhs)

Here is the call graph for this function:

Definition at line 588 of file test_float.py.

00588 
00589     def test_issue5864(self):
00590         self.assertEqual(format(123.456, '.4'), '123.5')
00591         self.assertEqual(format(1234.56, '.4'), '1.235e+03')
00592         self.assertEqual(format(12345.6, '.4'), '1.235e+04')

Here is the call graph for this function:


The documentation for this class was generated from the following file: