summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_long.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_long.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_long.py')
-rw-r--r--Lib/test/test_long.py44
1 files changed, 44 insertions, 0 deletions
diff --git a/Lib/test/test_long.py b/Lib/test/test_long.py
index 0b67c3e..4e15340 100644
--- a/Lib/test/test_long.py
+++ b/Lib/test/test_long.py
@@ -493,6 +493,50 @@ class LongTest(unittest.TestCase):
eq(x > y, Rcmp > 0, Frm("%r > %r %d", x, y, Rcmp))
eq(x >= y, Rcmp >= 0, Frm("%r >= %r %d", x, y, Rcmp))
+ def test_format(self):
+ self.assertEqual(format(123456789, 'd'), '123456789')
+ self.assertEqual(format(123456789, 'd'), '123456789')
+
+ # 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, "x"), "4d2")
+ self.assertEqual(format(-1234, "x"), "-4d2")
+ self.assertEqual(format(-3, "x"), "-3")
+ self.assertEqual(format(-3, "X"), "-3")
+ self.assertEqual(format(int('be', 16), "x"), "be")
+ self.assertEqual(format(int('be', 16), "X"), "BE")
+ self.assertEqual(format(-int('be', 16), "x"), "-be")
+ self.assertEqual(format(-int('be', 16), "X"), "-BE")
+
+ # octal
+ self.assertEqual(format(3, "b"), "11")
+ self.assertEqual(format(-3, "b"), "-11")
+ self.assertEqual(format(1234, "b"), "10011010010")
+ self.assertEqual(format(-1234, "b"), "-10011010010")
+ self.assertEqual(format(1234, "-b"), "10011010010")
+ self.assertEqual(format(-1234, "-b"), "-10011010010")
+ self.assertEqual(format(1234, " b"), " 10011010010")
+ self.assertEqual(format(-1234, " b"), "-10011010010")
+ self.assertEqual(format(1234, "+b"), "+10011010010")
+ self.assertEqual(format(-1234, "+b"), "-10011010010")
+
+ # conversion to float
+ self.assertEqual(format(0, 'f'), '0.000000')
+
+ # 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
+ # conversion to string should fail
+ self.assertRaises(ValueError, format, 3, "s")
+
def test_main():
test_support.run_unittest(LongTest)