summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_int.py
diff options
context:
space:
mode:
authorAndrew Svetlov <andrew.svetlov@gmail.com>2012-12-23 10:49:33 (GMT)
committerAndrew Svetlov <andrew.svetlov@gmail.com>2012-12-23 10:49:33 (GMT)
commit8e42e8a0c6d202e12a918494ed506252ff361187 (patch)
tree1d9fc10495661e388386ecb460ab72400bb1443d /Lib/test/test_int.py
parent1f415cf2c2b5f3eec5489ec1276c0629b32b28c0 (diff)
downloadcpython-8e42e8a0c6d202e12a918494ed506252ff361187.zip
cpython-8e42e8a0c6d202e12a918494ed506252ff361187.tar.gz
cpython-8e42e8a0c6d202e12a918494ed506252ff361187.tar.bz2
Issue #16045: add more unit tests for built-in int()
Patch by Chris Jerdonek.
Diffstat (limited to 'Lib/test/test_int.py')
-rw-r--r--Lib/test/test_int.py44
1 files changed, 42 insertions, 2 deletions
diff --git a/Lib/test/test_int.py b/Lib/test/test_int.py
index 227759f..e0406dd 100644
--- a/Lib/test/test_int.py
+++ b/Lib/test/test_int.py
@@ -1,7 +1,7 @@
import sys
import unittest
-from test.support import run_unittest
+from test import support
L = [
('0', 0),
@@ -221,6 +221,46 @@ class IntTestCases(unittest.TestCase):
self.assertEqual(int('2br45qc', 35), 4294967297)
self.assertEqual(int('1z141z5', 36), 4294967297)
+ def test_no_args(self):
+ self.assertEquals(int(), 0)
+
+ def test_keyword_args(self):
+ # Test invoking int() using keyword arguments.
+ self.assertEquals(int(x=1.2), 1)
+ self.assertEquals(int('100', base=2), 4)
+ self.assertEquals(int(x='100', base=2), 4)
+
+ # For example, PyPy 1.9.0 raised TypeError for these cases because it
+ # expects x to be a string if base is given.
+ @support.cpython_only
+ def test_base_arg_with_no_x_arg(self):
+ self.assertEquals(int(base=6), 0)
+ # Even invalid bases don't raise an exception.
+ self.assertEquals(int(base=1), 0)
+ self.assertEquals(int(base=1000), 0)
+ self.assertEquals(int(base='foo'), 0)
+
+ def test_non_numeric_input_types(self):
+ # Test possible non-numeric types for the argument x, including
+ # subclasses of the explicitly documented accepted types.
+ class CustomStr(str): pass
+ class CustomBytes(bytes): pass
+ class CustomByteArray(bytearray): pass
+
+ values = [b'100',
+ bytearray(b'100'),
+ CustomStr('100'),
+ CustomBytes(b'100'),
+ CustomByteArray(b'100')]
+
+ for x in values:
+ msg = 'x has type %s' % type(x).__name__
+ self.assertEquals(int(x), 100, msg=msg)
+ self.assertEquals(int(x, 2), 4, msg=msg)
+
+ def test_string_float(self):
+ self.assertRaises(ValueError, int, '1.2')
+
def test_intconversion(self):
# Test __int__()
class ClassicMissingMethods:
@@ -328,7 +368,7 @@ class IntTestCases(unittest.TestCase):
self.fail("Expected int(%r) to raise a ValueError", s)
def test_main():
- run_unittest(IntTestCases)
+ support.run_unittest(IntTestCases)
if __name__ == "__main__":
test_main()