summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_float.py
diff options
context:
space:
mode:
authorEric Smith <eric@trueblade.com>2007-08-25 02:26:07 (GMT)
committerEric Smith <eric@trueblade.com>2007-08-25 02:26:07 (GMT)
commit8c6632636807c35bee40210ed8483c1eca82664f (patch)
tree50f386d98ce14116eaf9d83085b82ff11bdb3e69 /Lib/test/test_float.py
parente4dc32488446240942123cf4e9e7296ad97e20bf (diff)
downloadcpython-8c6632636807c35bee40210ed8483c1eca82664f.zip
cpython-8c6632636807c35bee40210ed8483c1eca82664f.tar.gz
cpython-8c6632636807c35bee40210ed8483c1eca82664f.tar.bz2
Implementation of PEP 3101, Advanced String Formatting.
Known issues: The string.Formatter class, as discussed in the PEP, is incomplete. Error handling needs to conform to the PEP. Need to fix this warning that I introduced in Python/formatter_unicode.c: Objects/stringlib/unicodedefs.h:26: warning: `STRINGLIB_CMP' defined but not used Need to make sure sign formatting is correct, more tests needed. Need to remove '()' sign formatting, left over from an earlier version of the PEP.
Diffstat (limited to 'Lib/test/test_float.py')
-rw-r--r--Lib/test/test_float.py34
1 files changed, 33 insertions, 1 deletions
diff --git a/Lib/test/test_float.py b/Lib/test/test_float.py
index 48abec9..e5a4537 100644
--- a/Lib/test/test_float.py
+++ b/Lib/test/test_float.py
@@ -114,12 +114,44 @@ class IEEEFormatTestCase(unittest.TestCase):
self.assertEquals(pos_pos(), neg_pos())
self.assertEquals(pos_neg(), neg_neg())
+class FormatTestCase(unittest.TestCase):
+ def testFormat(self):
+ # these should be rewritten to use both format(x, spec) and
+ # x.__format__(spec)
+
+ self.assertEqual(format(0.0, 'f'), '0.000000')
+
+ # the default is 'g', except for empty format spec
+ self.assertEqual(format(0.0, ''), '0.0')
+ self.assertEqual(format(0.01, ''), '0.01')
+ self.assertEqual(format(0.01, 'g'), '0.01')
+
+ self.assertEqual(format(0, 'f'), '0.000000')
+
+ self.assertEqual(format(1.0, 'f'), '1.000000')
+ self.assertEqual(format(1, 'f'), '1.000000')
+
+ self.assertEqual(format(-1.0, 'f'), '-1.000000')
+ self.assertEqual(format(-1, 'f'), '-1.000000')
+
+ self.assertEqual(format( 1.0, ' f'), ' 1.000000')
+ self.assertEqual(format(-1.0, ' f'), '-1.000000')
+ self.assertEqual(format( 1.0, '+f'), '+1.000000')
+ self.assertEqual(format(-1.0, '+f'), '-1.000000')
+
+ # % formatting
+ self.assertEqual(format(-1.0, '%'), '-100.000000%')
+
+ # conversion to string should fail
+ self.assertRaises(ValueError, format, 3.0, "s")
+
def test_main():
test_support.run_unittest(
FormatFunctionsTestCase,
UnknownFormatTestCase,
- IEEEFormatTestCase)
+ IEEEFormatTestCase,
+ FormatTestCase)
if __name__ == '__main__':
test_main()