summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_long.py
diff options
context:
space:
mode:
authorEric V. Smith <eric@trueblade.com>2016-09-10 03:06:47 (GMT)
committerEric V. Smith <eric@trueblade.com>2016-09-10 03:06:47 (GMT)
commit89e1b1aae0775341735de6bc5e97b3c1e9cea0fa (patch)
treecadcc5860ce497d17783b2af9901d9bb71ee1820 /Lib/test/test_long.py
parent37d398ebd1b177001df231b9bba17429ac61ff12 (diff)
downloadcpython-89e1b1aae0775341735de6bc5e97b3c1e9cea0fa.zip
cpython-89e1b1aae0775341735de6bc5e97b3c1e9cea0fa.tar.gz
cpython-89e1b1aae0775341735de6bc5e97b3c1e9cea0fa.tar.bz2
Issue 27080: PEP 515: add '_' formatting option.
Diffstat (limited to 'Lib/test/test_long.py')
-rw-r--r--Lib/test/test_long.py28
1 files changed, 28 insertions, 0 deletions
diff --git a/Lib/test/test_long.py b/Lib/test/test_long.py
index 4cc4b05..fd15f04 100644
--- a/Lib/test/test_long.py
+++ b/Lib/test/test_long.py
@@ -621,6 +621,8 @@ class LongTest(unittest.TestCase):
def test__format__(self):
self.assertEqual(format(123456789, 'd'), '123456789')
self.assertEqual(format(123456789, 'd'), '123456789')
+ self.assertEqual(format(123456789, ','), '123,456,789')
+ self.assertEqual(format(123456789, '_'), '123_456_789')
# sign and aligning are interdependent
self.assertEqual(format(1, "-"), '1')
@@ -649,8 +651,25 @@ class LongTest(unittest.TestCase):
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.assertRaises(ValueError, format, 1234567890, ',x')
+ self.assertEqual(format(1234567890, '_x'), '4996_02d2')
+ self.assertEqual(format(1234567890, '_X'), '4996_02D2')
# octal
+ self.assertEqual(format(3, "o"), "3")
+ self.assertEqual(format(-3, "o"), "-3")
+ self.assertEqual(format(1234, "o"), "2322")
+ self.assertEqual(format(-1234, "o"), "-2322")
+ self.assertEqual(format(1234, "-o"), "2322")
+ self.assertEqual(format(-1234, "-o"), "-2322")
+ self.assertEqual(format(1234, " o"), " 2322")
+ self.assertEqual(format(-1234, " o"), "-2322")
+ self.assertEqual(format(1234, "+o"), "+2322")
+ self.assertEqual(format(-1234, "+o"), "-2322")
+ self.assertRaises(ValueError, format, 1234567890, ',o')
+ self.assertEqual(format(1234567890, '_o'), '111_4540_1322')
+
+ # binary
self.assertEqual(format(3, "b"), "11")
self.assertEqual(format(-3, "b"), "-11")
self.assertEqual(format(1234, "b"), "10011010010")
@@ -661,12 +680,21 @@ class LongTest(unittest.TestCase):
self.assertEqual(format(-1234, " b"), "-10011010010")
self.assertEqual(format(1234, "+b"), "+10011010010")
self.assertEqual(format(-1234, "+b"), "-10011010010")
+ self.assertRaises(ValueError, format, 1234567890, ',b')
+ self.assertEqual(format(12345, '_b'), '11_0000_0011_1001')
# make sure these are errors
self.assertRaises(ValueError, format, 3, "1.3") # precision disallowed
+ self.assertRaises(ValueError, format, 3, "_c") # underscore,
+ self.assertRaises(ValueError, format, 3, ",c") # comma, and
self.assertRaises(ValueError, format, 3, "+c") # sign not allowed
# with 'c'
+ self.assertRaisesRegex(ValueError, 'Cannot specify both', format, 3, '_,')
+ self.assertRaisesRegex(ValueError, 'Cannot specify both', format, 3, ',_')
+ self.assertRaisesRegex(ValueError, 'Cannot specify both', format, 3, '_,d')
+ self.assertRaisesRegex(ValueError, 'Cannot specify both', format, 3, ',_d')
+
# ensure that only int and float type specifiers work
for format_spec in ([chr(x) for x in range(ord('a'), ord('z')+1)] +
[chr(x) for x in range(ord('A'), ord('Z')+1)]):