Skip to content

Commit c8976f4

Browse files
committed
Add support for Interpolation default values.
1 parent 88423e5 commit c8976f4

File tree

2 files changed

+33
-15
lines changed

2 files changed

+33
-15
lines changed

Lib/test/test_string/_support.py

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,38 @@
22

33

44
class TStringBaseCase:
5+
def assertInterpolationEqual(self, i, exp):
6+
"""Test Interpolation equality.
7+
8+
The *i* argument must be an Interpolation instance.
9+
10+
The *exp* argument must be a tuple of the form
11+
(value, expression, conversion, format_spec) where the final three
12+
items may be omitted and are assumed to be '', None and '' respectively.
13+
"""
14+
# Merge in defaults for expression, conversion, and format_spec
15+
defaults = ('', None, '')
16+
expected = exp + defaults[len(exp) - 1:]
17+
18+
actual = (i.value, i.expression, i.conversion, i.format_spec)
19+
self.assertEqual(actual, expected)
20+
21+
522
def assertTStringEqual(self, t, strings, interpolations):
623
"""Test template string literal equality.
724
825
The *strings* argument must be a tuple of strings equal to *t.strings*.
926
1027
The *interpolations* argument must be a sequence of tuples which are
1128
compared against *t.interpolations*. Each tuple consists of
12-
(value, expression, conversion, format_spec), though the final two
13-
items may be omitted, and are assumed to be None and '' respectively.
29+
(value, expression, conversion, format_spec), though the final three
30+
items may be omitted and are assumed to be '' None, and '' respectively.
1431
"""
1532
self.assertEqual(t.strings, strings)
1633
self.assertEqual(len(t.interpolations), len(interpolations))
1734

1835
for i, exp in zip(t.interpolations, interpolations, strict=True):
19-
if len(exp) == 4:
20-
actual = (i.value, i.expression, i.conversion, i.format_spec)
21-
self.assertEqual(actual, exp)
22-
continue
23-
24-
if len(exp) == 3:
25-
self.assertEqual((i.value, i.expression, i.conversion), exp)
26-
self.assertEqual(i.format_spec, '')
27-
continue
28-
29-
self.assertEqual((i.value, i.expression), exp)
30-
self.assertEqual(i.format_spec, '')
31-
self.assertIsNone(i.conversion)
36+
self.assertInterpolationEqual(i, exp)
3237

3338

3439
def convert(value, conversion):

Lib/test/test_string/test_templatelib.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,19 @@ def test_basic_creation(self):
4545
self.assertEqual(len(t.interpolations), 0)
4646
self.assertEqual(fstring(t), 'Hello,\nworld')
4747

48+
def test_interpolation_creation(self):
49+
i = Interpolation('Maria', 'name', 'a', 'fmt')
50+
self.assertInterpolationEqual(i, ('Maria', 'name', 'a', 'fmt'))
51+
52+
i = Interpolation('Maria', 'name', 'a')
53+
self.assertInterpolationEqual(i, ('Maria', 'name', 'a'))
54+
55+
i = Interpolation('Maria', 'name')
56+
self.assertInterpolationEqual(i, ('Maria', 'name'))
57+
58+
i = Interpolation('Maria')
59+
self.assertInterpolationEqual(i, ('Maria',))
60+
4861
def test_creation_interleaving(self):
4962
# Should add strings on either side
5063
t = Template(Interpolation('Maria', 'name', None, ''))

0 commit comments

Comments
 (0)