MCPcopy Index your code
hub / github.com/python/cpython / test_constructor

Method test_constructor

Lib/test/test_str.py:1713–1777  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

1711 self.assertEqual('1.0', '%.1f' % 1.0)
1712
1713 def test_constructor(self):
1714 # unicode(obj) tests (this maps to PyObject_Unicode() at C level)
1715
1716 self.assertEqual(
1717 str('unicode remains unicode'),
1718 'unicode remains unicode'
1719 )
1720
1721 for text in ('ascii', '\xe9', '\u20ac', '\U0010FFFF'):
1722 subclass = StrSubclass(text)
1723 self.assertEqual(str(subclass), text)
1724 self.assertEqual(len(subclass), len(text))
1725 if text == 'ascii':
1726 self.assertEqual(subclass.encode('ascii'), b'ascii')
1727 self.assertEqual(subclass.encode('utf-8'), b'ascii')
1728
1729 self.assertEqual(
1730 str('strings are converted to unicode'),
1731 'strings are converted to unicode'
1732 )
1733
1734 class StringCompat:
1735 def __init__(self, x):
1736 self.x = x
1737 def __str__(self):
1738 return self.x
1739
1740 self.assertEqual(
1741 str(StringCompat('__str__ compatible objects are recognized')),
1742 '__str__ compatible objects are recognized'
1743 )
1744
1745 # unicode(obj) is compatible to str():
1746
1747 o = StringCompat('unicode(obj) is compatible to str()')
1748 self.assertEqual(str(o), 'unicode(obj) is compatible to str()')
1749 self.assertEqual(str(o), 'unicode(obj) is compatible to str()')
1750
1751 for obj in (123, 123.45, 123):
1752 self.assertEqual(str(obj), str(str(obj)))
1753
1754 # unicode(obj, encoding, error) tests (this maps to
1755 # PyUnicode_FromEncodedObject() at C level)
1756
1757 self.assertRaises(
1758 TypeError,
1759 str,
1760 'decoding unicode is not supported',
1761 'utf-8',
1762 'strict'
1763 )
1764
1765 self.assertEqual(
1766 str(b'strings are decoded to unicode', 'utf-8', 'strict'),
1767 'strings are decoded to unicode'
1768 )
1769
1770 self.assertEqual(

Callers

nothing calls this directly

Calls 6

strFunction · 0.85
StringCompatClass · 0.85
StrSubclassClass · 0.70
assertEqualMethod · 0.45
encodeMethod · 0.45
assertRaisesMethod · 0.45

Tested by

no test coverage detected