MCPcopy Index your code
hub / github.com/RustPython/RustPython / test_constructor

Method test_constructor

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

Source from the content-addressed store, hash-verified

1689 self.assertEqual('1.0', '%.1f' % 1.0)
1690
1691 def test_constructor(self):
1692 # unicode(obj) tests (this maps to PyObject_Unicode() at C level)
1693
1694 self.assertEqual(
1695 str('unicode remains unicode'),
1696 'unicode remains unicode'
1697 )
1698
1699 for text in ('ascii', '\xe9', '\u20ac', '\U0010FFFF'):
1700 subclass = StrSubclass(text)
1701 self.assertEqual(str(subclass), text)
1702 self.assertEqual(len(subclass), len(text))
1703 if text == 'ascii':
1704 self.assertEqual(subclass.encode('ascii'), b'ascii')
1705 self.assertEqual(subclass.encode('utf-8'), b'ascii')
1706
1707 self.assertEqual(
1708 str('strings are converted to unicode'),
1709 'strings are converted to unicode'
1710 )
1711
1712 class StringCompat:
1713 def __init__(self, x):
1714 self.x = x
1715 def __str__(self):
1716 return self.x
1717
1718 self.assertEqual(
1719 str(StringCompat('__str__ compatible objects are recognized')),
1720 '__str__ compatible objects are recognized'
1721 )
1722
1723 # unicode(obj) is compatible to str():
1724
1725 o = StringCompat('unicode(obj) is compatible to str()')
1726 self.assertEqual(str(o), 'unicode(obj) is compatible to str()')
1727 self.assertEqual(str(o), 'unicode(obj) is compatible to str()')
1728
1729 for obj in (123, 123.45, 123):
1730 self.assertEqual(str(obj), str(str(obj)))
1731
1732 # unicode(obj, encoding, error) tests (this maps to
1733 # PyUnicode_FromEncodedObject() at C level)
1734
1735 self.assertRaises(
1736 TypeError,
1737 str,
1738 'decoding unicode is not supported',
1739 'utf-8',
1740 'strict'
1741 )
1742
1743 self.assertEqual(
1744 str(b'strings are decoded to unicode', 'utf-8', 'strict'),
1745 'strings are decoded to unicode'
1746 )
1747
1748 self.assertEqual(

Callers

nothing calls this directly

Calls 7

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

Tested by

no test coverage detected