From ba4279683f8eb8f59be10d12547ea89480614388 Mon Sep 17 00:00:00 2001 From: Barry Warsaw Date: Mon, 4 Sep 2017 16:32:10 -0400 Subject: bpo-1198569: Allow string.Template braced pattern to be different (#3288) * bpo-1198569: Allow the braced pattern to be different ``string.Template`` subclasses can optionally define ``braceidpattern`` if they want to specify different placeholder patterns inside and outside the braces. If None (the default) it falls back to ``idpattern``. --- Lib/test/test_string.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to 'Lib/test/test_string.py') diff --git a/Lib/test/test_string.py b/Lib/test/test_string.py index a7b8aad8ab..6e241ac72a 100644 --- a/Lib/test/test_string.py +++ b/Lib/test/test_string.py @@ -282,6 +282,30 @@ class TestTemplate(unittest.TestCase): s = PathPattern('$bag.foo.who likes to eat a bag of $bag.what') self.assertEqual(s.substitute(m), 'tim likes to eat a bag of ham') + def test_idpattern_override_inside_outside(self): + # bpo-1198569: Allow the regexp inside and outside braces to be + # different when deriving from Template. + class MyPattern(Template): + idpattern = r'[a-z]+' + braceidpattern = r'[A-Z]+' + flags = 0 + m = dict(foo='foo', BAR='BAR') + s = MyPattern('$foo ${BAR}') + self.assertEqual(s.substitute(m), 'foo BAR') + + def test_idpattern_override_inside_outside_invalid_unbraced(self): + # bpo-1198569: Allow the regexp inside and outside braces to be + # different when deriving from Template. + class MyPattern(Template): + idpattern = r'[a-z]+' + braceidpattern = r'[A-Z]+' + flags = 0 + m = dict(foo='foo', BAR='BAR') + s = MyPattern('$FOO') + self.assertRaises(ValueError, s.substitute, m) + s = MyPattern('${bar}') + self.assertRaises(ValueError, s.substitute, m) + def test_pattern_override(self): class MyPattern(Template): pattern = r""" -- cgit v1.2.1