mirror of
https://github.com/python/cpython.git
synced 2025-12-08 06:10:17 +00:00
GH-132661: Add `string.templatelib.convert()` (#135217)
This commit is contained in:
parent
c89a66feb1
commit
5b969fd645
2 changed files with 33 additions and 6 deletions
|
|
@ -1,7 +1,7 @@
|
|||
import pickle
|
||||
import unittest
|
||||
from collections.abc import Iterator, Iterable
|
||||
from string.templatelib import Template, Interpolation
|
||||
from string.templatelib import Template, Interpolation, convert
|
||||
|
||||
from test.test_string._support import TStringBaseCase, fstring
|
||||
|
||||
|
|
@ -169,5 +169,25 @@ def test_exhausted(self):
|
|||
self.assertRaises(StopIteration, next, template_iter)
|
||||
|
||||
|
||||
class TestFunctions(unittest.TestCase):
|
||||
def test_convert(self):
|
||||
from fractions import Fraction
|
||||
|
||||
for obj in ('Café', None, 3.14, Fraction(1, 2)):
|
||||
with self.subTest(f'{obj=}'):
|
||||
self.assertEqual(convert(obj, None), obj)
|
||||
self.assertEqual(convert(obj, 's'), str(obj))
|
||||
self.assertEqual(convert(obj, 'r'), repr(obj))
|
||||
self.assertEqual(convert(obj, 'a'), ascii(obj))
|
||||
|
||||
# Invalid conversion specifier
|
||||
with self.assertRaises(ValueError):
|
||||
convert(obj, 'z')
|
||||
with self.assertRaises(ValueError):
|
||||
convert(obj, 1)
|
||||
with self.assertRaises(ValueError):
|
||||
convert(obj, object())
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue