aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/Lib/test/test_string
diff options
context:
space:
mode:
Diffstat (limited to 'Lib/test/test_string')
-rw-r--r--Lib/test/test_string/_support.py45
-rw-r--r--Lib/test/test_string/test_string.py8
-rw-r--r--Lib/test/test_string/test_templatelib.py20
3 files changed, 56 insertions, 17 deletions
diff --git a/Lib/test/test_string/_support.py b/Lib/test/test_string/_support.py
index eaa3354a559..e1d7f6f6500 100644
--- a/Lib/test/test_string/_support.py
+++ b/Lib/test/test_string/_support.py
@@ -1,35 +1,46 @@
-import unittest
from string.templatelib import Interpolation
class TStringBaseCase:
+ def assertInterpolationEqual(self, i, exp):
+ """Test Interpolation equality.
+
+ The *i* argument must be an Interpolation instance.
+
+ The *exp* argument must be a tuple of the form
+ (value, expression, conversion, format_spec) where the final three
+ items may be omitted and are assumed to be '', None and '' respectively.
+ """
+ if len(exp) == 4:
+ actual = (i.value, i.expression, i.conversion, i.format_spec)
+ self.assertEqual(actual, exp)
+ elif len(exp) == 3:
+ self.assertEqual((i.value, i.expression, i.conversion), exp)
+ self.assertEqual(i.format_spec, "")
+ elif len(exp) == 2:
+ self.assertEqual((i.value, i.expression), exp)
+ self.assertEqual(i.conversion, None)
+ self.assertEqual(i.format_spec, "")
+ elif len(exp) == 1:
+ self.assertEqual((i.value,), exp)
+ self.assertEqual(i.expression, "")
+ self.assertEqual(i.conversion, None)
+ self.assertEqual(i.format_spec, "")
+
def assertTStringEqual(self, t, strings, interpolations):
"""Test template string literal equality.
The *strings* argument must be a tuple of strings equal to *t.strings*.
The *interpolations* argument must be a sequence of tuples which are
- compared against *t.interpolations*. Each tuple consists of
- (value, expression, conversion, format_spec), though the final two
- items may be omitted, and are assumed to be None and '' respectively.
+ compared against *t.interpolations*. Each tuple must match the form
+ described in the `assertInterpolationEqual` method.
"""
self.assertEqual(t.strings, strings)
self.assertEqual(len(t.interpolations), len(interpolations))
for i, exp in zip(t.interpolations, interpolations, strict=True):
- if len(exp) == 4:
- actual = (i.value, i.expression, i.conversion, i.format_spec)
- self.assertEqual(actual, exp)
- continue
-
- if len(exp) == 3:
- self.assertEqual((i.value, i.expression, i.conversion), exp)
- self.assertEqual(i.format_spec, '')
- continue
-
- self.assertEqual((i.value, i.expression), exp)
- self.assertEqual(i.format_spec, '')
- self.assertIsNone(i.conversion)
+ self.assertInterpolationEqual(i, exp)
def convert(value, conversion):
diff --git a/Lib/test/test_string/test_string.py b/Lib/test/test_string/test_string.py
index f6d112d8a93..5394fe4e12c 100644
--- a/Lib/test/test_string/test_string.py
+++ b/Lib/test/test_string/test_string.py
@@ -2,6 +2,14 @@ import unittest
import string
from string import Template
import types
+from test.support import cpython_only
+from test.support.import_helper import ensure_lazy_imports
+
+
+class LazyImportTest(unittest.TestCase):
+ @cpython_only
+ def test_lazy_import(self):
+ ensure_lazy_imports("base64", {"re", "collections"})
class ModuleTest(unittest.TestCase):
diff --git a/Lib/test/test_string/test_templatelib.py b/Lib/test/test_string/test_templatelib.py
index 5b9490c2be6..adaf590e64d 100644
--- a/Lib/test/test_string/test_templatelib.py
+++ b/Lib/test/test_string/test_templatelib.py
@@ -45,6 +45,19 @@ world"""
self.assertEqual(len(t.interpolations), 0)
self.assertEqual(fstring(t), 'Hello,\nworld')
+ def test_interpolation_creation(self):
+ i = Interpolation('Maria', 'name', 'a', 'fmt')
+ self.assertInterpolationEqual(i, ('Maria', 'name', 'a', 'fmt'))
+
+ i = Interpolation('Maria', 'name', 'a')
+ self.assertInterpolationEqual(i, ('Maria', 'name', 'a'))
+
+ i = Interpolation('Maria', 'name')
+ self.assertInterpolationEqual(i, ('Maria', 'name'))
+
+ i = Interpolation('Maria')
+ self.assertInterpolationEqual(i, ('Maria',))
+
def test_creation_interleaving(self):
# Should add strings on either side
t = Template(Interpolation('Maria', 'name', None, ''))
@@ -148,6 +161,13 @@ class TemplateIterTests(unittest.TestCase):
self.assertEqual(res[1].format_spec, '')
self.assertEqual(res[2], ' yz')
+ def test_exhausted(self):
+ # See https://github.com/python/cpython/issues/134119.
+ template_iter = iter(t"{1}")
+ self.assertIsInstance(next(template_iter), Interpolation)
+ self.assertRaises(StopIteration, next, template_iter)
+ self.assertRaises(StopIteration, next, template_iter)
+
if __name__ == '__main__':
unittest.main()