summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2016-02-08 18:28:03 (GMT)
committerSerhiy Storchaka <storchaka@gmail.com>2016-02-08 18:28:03 (GMT)
commit763f9f0d89548e02c2efcd843655944584d6d33f (patch)
treef9952d3aa87e472890f9b93df5d8894d12ad22c8 /Lib
parente27403b75f4cae15650292d6735b917fed3e9a8f (diff)
downloadcpython-763f9f0d89548e02c2efcd843655944584d6d33f.zip
cpython-763f9f0d89548e02c2efcd843655944584d6d33f.tar.gz
cpython-763f9f0d89548e02c2efcd843655944584d6d33f.tar.bz2
Issue #25983: Added tests for multi-argument type().
Diffstat (limited to 'Lib')
-rw-r--r--Lib/test/test_builtin.py155
1 files changed, 155 insertions, 0 deletions
diff --git a/Lib/test/test_builtin.py b/Lib/test/test_builtin.py
index 5deeb72..8cc1b00 100644
--- a/Lib/test/test_builtin.py
+++ b/Lib/test/test_builtin.py
@@ -1668,6 +1668,161 @@ class ShutdownTest(unittest.TestCase):
self.assertEqual(["before", "after"], out.decode().splitlines())
+class TestType(unittest.TestCase):
+ def test_new_type(self):
+ A = type('A', (), {})
+ self.assertEqual(A.__name__, 'A')
+ self.assertEqual(A.__qualname__, 'A')
+ self.assertEqual(A.__module__, __name__)
+ self.assertEqual(A.__bases__, (object,))
+ self.assertIs(A.__base__, object)
+ x = A()
+ self.assertIs(type(x), A)
+ self.assertIs(x.__class__, A)
+
+ class B:
+ def ham(self):
+ return 'ham%d' % self
+ C = type('C', (B, int), {'spam': lambda self: 'spam%s' % self})
+ self.assertEqual(C.__name__, 'C')
+ self.assertEqual(C.__qualname__, 'C')
+ self.assertEqual(C.__module__, __name__)
+ self.assertEqual(C.__bases__, (B, int))
+ self.assertIs(C.__base__, int)
+ self.assertIn('spam', C.__dict__)
+ self.assertNotIn('ham', C.__dict__)
+ x = C(42)
+ self.assertEqual(x, 42)
+ self.assertIs(type(x), C)
+ self.assertIs(x.__class__, C)
+ self.assertEqual(x.ham(), 'ham42')
+ self.assertEqual(x.spam(), 'spam42')
+ self.assertEqual(x.to_bytes(2, 'little'), b'\x2a\x00')
+
+ def test_type_new_keywords(self):
+ class B:
+ def ham(self):
+ return 'ham%d' % self
+ C = type.__new__(type,
+ name='C',
+ bases=(B, int),
+ dict={'spam': lambda self: 'spam%s' % self})
+ self.assertEqual(C.__name__, 'C')
+ self.assertEqual(C.__qualname__, 'C')
+ self.assertEqual(C.__module__, __name__)
+ self.assertEqual(C.__bases__, (B, int))
+ self.assertIs(C.__base__, int)
+ self.assertIn('spam', C.__dict__)
+ self.assertNotIn('ham', C.__dict__)
+
+ def test_type_name(self):
+ for name in 'A', '\xc4', '\U0001f40d', 'B.A', '42', '':
+ with self.subTest(name=name):
+ A = type(name, (), {})
+ self.assertEqual(A.__name__, name)
+ self.assertEqual(A.__qualname__, name)
+ self.assertEqual(A.__module__, __name__)
+ with self.assertRaises(ValueError):
+ type('A\x00B', (), {})
+ with self.assertRaises(ValueError):
+ type('A\udcdcB', (), {})
+ with self.assertRaises(TypeError):
+ type(b'A', (), {})
+
+ C = type('C', (), {})
+ for name in 'A', '\xc4', '\U0001f40d', 'B.A', '42', '':
+ with self.subTest(name=name):
+ C.__name__ = name
+ self.assertEqual(C.__name__, name)
+ self.assertEqual(C.__qualname__, 'C')
+ self.assertEqual(C.__module__, __name__)
+
+ A = type('C', (), {})
+ with self.assertRaises(ValueError):
+ A.__name__ = 'A\x00B'
+ self.assertEqual(A.__name__, 'C')
+ with self.assertRaises(ValueError):
+ A.__name__ = 'A\udcdcB'
+ self.assertEqual(A.__name__, 'C')
+ with self.assertRaises(TypeError):
+ A.__name__ = b'A'
+ self.assertEqual(A.__name__, 'C')
+
+ def test_type_qualname(self):
+ A = type('A', (), {'__qualname__': 'B.C'})
+ self.assertEqual(A.__name__, 'A')
+ self.assertEqual(A.__qualname__, 'B.C')
+ self.assertEqual(A.__module__, __name__)
+ with self.assertRaises(TypeError):
+ type('A', (), {'__qualname__': b'B'})
+ self.assertEqual(A.__qualname__, 'B.C')
+
+ A.__qualname__ = 'D.E'
+ self.assertEqual(A.__name__, 'A')
+ self.assertEqual(A.__qualname__, 'D.E')
+ with self.assertRaises(TypeError):
+ A.__qualname__ = b'B'
+ self.assertEqual(A.__qualname__, 'D.E')
+
+ def test_type_doc(self):
+ for doc in 'x', '\xc4', '\U0001f40d', 'x\x00y', b'x', 42, None:
+ A = type('A', (), {'__doc__': doc})
+ self.assertEqual(A.__doc__, doc)
+ with self.assertRaises(UnicodeEncodeError):
+ type('A', (), {'__doc__': 'x\udcdcy'})
+
+ A = type('A', (), {})
+ self.assertEqual(A.__doc__, None)
+ for doc in 'x', '\xc4', '\U0001f40d', 'x\x00y', 'x\udcdcy', b'x', 42, None:
+ A.__doc__ = doc
+ self.assertEqual(A.__doc__, doc)
+
+ def test_bad_args(self):
+ with self.assertRaises(TypeError):
+ type()
+ with self.assertRaises(TypeError):
+ type('A', ())
+ with self.assertRaises(TypeError):
+ type('A', (), {}, ())
+ with self.assertRaises(TypeError):
+ type('A', (), dict={})
+ with self.assertRaises(TypeError):
+ type('A', [], {})
+ with self.assertRaises(TypeError):
+ type('A', (), types.MappingProxyType({}))
+ with self.assertRaises(TypeError):
+ type('A', (None,), {})
+ with self.assertRaises(TypeError):
+ type('A', (bool,), {})
+ with self.assertRaises(TypeError):
+ type('A', (int, str), {})
+
+ def test_bad_slots(self):
+ with self.assertRaises(TypeError):
+ type('A', (), {'__slots__': b'x'})
+ with self.assertRaises(TypeError):
+ type('A', (int,), {'__slots__': 'x'})
+ with self.assertRaises(TypeError):
+ type('A', (), {'__slots__': ''})
+ with self.assertRaises(TypeError):
+ type('A', (), {'__slots__': '42'})
+ with self.assertRaises(TypeError):
+ type('A', (), {'__slots__': 'x\x00y'})
+ with self.assertRaises(ValueError):
+ type('A', (), {'__slots__': 'x', 'x': 0})
+ with self.assertRaises(TypeError):
+ type('A', (), {'__slots__': ('__dict__', '__dict__')})
+ with self.assertRaises(TypeError):
+ type('A', (), {'__slots__': ('__weakref__', '__weakref__')})
+
+ class B:
+ pass
+ with self.assertRaises(TypeError):
+ type('A', (B,), {'__slots__': '__dict__'})
+ with self.assertRaises(TypeError):
+ type('A', (B,), {'__slots__': '__weakref__'})
+
+
def load_tests(loader, tests, pattern):
from doctest import DocTestSuite
tests.addTest(DocTestSuite(builtins))