summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_string.py
diff options
context:
space:
mode:
authorBen Kehoe <bkehoe@irobot.com>2022-01-11 19:15:42 (GMT)
committerGitHub <noreply@github.com>2022-01-11 19:15:42 (GMT)
commitdce642f24418c58e67fa31a686575c980c31dd37 (patch)
tree90e9eb86f921027c33819c29490de8be68990b56 /Lib/test/test_string.py
parentcf496d657a1a82eaf9ebfb47d721676fef6effa5 (diff)
downloadcpython-dce642f24418c58e67fa31a686575c980c31dd37.zip
cpython-dce642f24418c58e67fa31a686575c980c31dd37.tar.gz
cpython-dce642f24418c58e67fa31a686575c980c31dd37.tar.bz2
bpo-46307: Add string.Template.get_identifiers() method (GH-30493)
Add `string.Template.get_identifiers()` method that returns the identifiers within the template. By default, raises an error if it encounters an invalid identifier (like `substitute()`). The keyword-only argument `raise_on_invalid` can be set to `False` to ignore invalid identifiers (like `safe_substitute()`). Automerge-Triggered-By: GH:warsaw
Diffstat (limited to 'Lib/test/test_string.py')
-rw-r--r--Lib/test/test_string.py51
1 files changed, 51 insertions, 0 deletions
diff --git a/Lib/test/test_string.py b/Lib/test/test_string.py
index 0be28fd..824b89a 100644
--- a/Lib/test/test_string.py
+++ b/Lib/test/test_string.py
@@ -475,6 +475,57 @@ class TestTemplate(unittest.TestCase):
self.assertEqual(s.substitute(dict(who='tim', what='ham')),
'tim likes to eat a bag of ham worth $100')
+ def test_is_valid(self):
+ eq = self.assertEqual
+ s = Template('$who likes to eat a bag of ${what} worth $$100')
+ self.assertTrue(s.is_valid())
+
+ s = Template('$who likes to eat a bag of ${what} worth $100')
+ self.assertFalse(s.is_valid())
+
+ # if the pattern has an unrecognized capture group,
+ # it should raise ValueError like substitute and safe_substitute do
+ class BadPattern(Template):
+ pattern = r"""
+ (?P<badname>.*) |
+ (?P<escaped>@{2}) |
+ @(?P<named>[_a-z][._a-z0-9]*) |
+ @{(?P<braced>[_a-z][._a-z0-9]*)} |
+ (?P<invalid>@) |
+ """
+ s = BadPattern('@bag.foo.who likes to eat a bag of @bag.what')
+ self.assertRaises(ValueError, s.is_valid)
+
+ def test_get_identifiers(self):
+ eq = self.assertEqual
+ raises = self.assertRaises
+ s = Template('$who likes to eat a bag of ${what} worth $$100')
+ ids = s.get_identifiers()
+ eq(ids, ['who', 'what'])
+
+ # repeated identifiers only included once
+ s = Template('$who likes to eat a bag of ${what} worth $$100; ${who} likes to eat a bag of $what worth $$100')
+ ids = s.get_identifiers()
+ eq(ids, ['who', 'what'])
+
+ # invalid identifiers are ignored
+ s = Template('$who likes to eat a bag of ${what} worth $100')
+ ids = s.get_identifiers()
+ eq(ids, ['who', 'what'])
+
+ # if the pattern has an unrecognized capture group,
+ # it should raise ValueError like substitute and safe_substitute do
+ class BadPattern(Template):
+ pattern = r"""
+ (?P<badname>.*) |
+ (?P<escaped>@{2}) |
+ @(?P<named>[_a-z][._a-z0-9]*) |
+ @{(?P<braced>[_a-z][._a-z0-9]*)} |
+ (?P<invalid>@) |
+ """
+ s = BadPattern('@bag.foo.who likes to eat a bag of @bag.what')
+ self.assertRaises(ValueError, s.get_identifiers)
+
if __name__ == '__main__':
unittest.main()