diff options
author | Barry Warsaw <barry@python.org> | 2017-09-04 20:32:10 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-09-04 20:32:10 (GMT) |
commit | ba4279683f8eb8f59be10d12547ea89480614388 (patch) | |
tree | a04e6ac5d0855275d27308072b05c7635423ed9e /Lib/test/test_string.py | |
parent | f9f17346d722b6f073a048b41ec0d6adf336d1d2 (diff) | |
download | cpython-ba4279683f8eb8f59be10d12547ea89480614388.zip cpython-ba4279683f8eb8f59be10d12547ea89480614388.tar.gz cpython-ba4279683f8eb8f59be10d12547ea89480614388.tar.bz2 |
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``.
Diffstat (limited to 'Lib/test/test_string.py')
-rw-r--r-- | Lib/test/test_string.py | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/Lib/test/test_string.py b/Lib/test/test_string.py index a7b8aad..6e241ac 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""" |