|
1 | 1 | import pickle |
2 | 2 | import unittest |
3 | 3 | from collections.abc import Iterator, Iterable |
4 | | -from string.templatelib import Template, Interpolation |
| 4 | +from string.templatelib import Template, Interpolation, convert |
5 | 5 |
|
6 | 6 | from test.test_string._support import TStringBaseCase, fstring |
7 | 7 |
|
@@ -169,5 +169,25 @@ def test_exhausted(self): |
169 | 169 | self.assertRaises(StopIteration, next, template_iter) |
170 | 170 |
|
171 | 171 |
|
| 172 | +class TestFunctions(unittest.TestCase): |
| 173 | + def test_convert(self): |
| 174 | + from fractions import Fraction |
| 175 | + |
| 176 | + for obj in ('Café', None, 3.14, Fraction(1, 2)): |
| 177 | + with self.subTest(f'{obj=}'): |
| 178 | + self.assertEqual(convert(obj, None), obj) |
| 179 | + self.assertEqual(convert(obj, 's'), str(obj)) |
| 180 | + self.assertEqual(convert(obj, 'r'), repr(obj)) |
| 181 | + self.assertEqual(convert(obj, 'a'), ascii(obj)) |
| 182 | + |
| 183 | + # Invalid conversion specifier |
| 184 | + with self.assertRaises(ValueError): |
| 185 | + convert(obj, 'z') |
| 186 | + with self.assertRaises(ValueError): |
| 187 | + convert(obj, 1) |
| 188 | + with self.assertRaises(ValueError): |
| 189 | + convert(obj, object()) |
| 190 | + |
| 191 | + |
172 | 192 | if __name__ == '__main__': |
173 | 193 | unittest.main() |
0 commit comments