cpython: 7dac80e0dd5e (original) (raw)
Mercurial > cpython
changeset 101642:7dac80e0dd5e 2.7
Issue #27185: Merge test_pep292.py into test_string_merged.py. [#27185]
Serhiy Storchaka storchaka@gmail.com | |
---|---|
date | Fri, 03 Jun 2016 09:36:15 +0300 |
parents | 27f5eb630499(current diff)8218b1309178(diff) |
children | e2e309fb2b1e |
files | Lib/test/test_pep292.py Lib/test/test_string_merged.py |
diffstat | 2 files changed, 227 insertions(+), 240 deletions(-)[+] [-] Lib/test/test_pep292.py 234 Lib/test/test_string_merged.py 233 |
line wrap: on
line diff
deleted file mode 100644 --- a/Lib/test/test_pep292.py +++ /dev/null @@ -1,234 +0,0 @@ -# Copyright (C) 2004 Python Software Foundation -# Author: barry@python.org (Barry Warsaw) -# License: http://www.opensource.org/licenses/PythonSoftFoundation.php[](#l1.7) - -import unittest -from string import Template - - -class Bag:
- def getitem(self, name):
obj = self[](#l1.18)
for part in name.split('.'):[](#l1.19)
try:[](#l1.20)
obj = getattr(obj, part)[](#l1.21)
except AttributeError:[](#l1.22)
raise KeyError(name)[](#l1.23)
return obj[](#l1.24)
- - -class TestTemplate(unittest.TestCase):
- def test_regular_templates(self):
s = Template('$who likes to eat a bag of <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>w</mi><mi>h</mi><mi>a</mi><mi>t</mi><mi>w</mi><mi>o</mi><mi>r</mi><mi>t</mi><mi>h</mi></mrow><annotation encoding="application/x-tex">what worth </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6944em;"></span><span class="mord mathnormal" style="margin-right:0.02691em;">w</span><span class="mord mathnormal">ha</span><span class="mord mathnormal" style="margin-right:0.02691em;">tw</span><span class="mord mathnormal" style="margin-right:0.02778em;">or</span><span class="mord mathnormal">t</span><span class="mord mathnormal">h</span></span></span></span>$100')[](#l1.29)
self.assertEqual(s.substitute(dict(who='tim', what='ham')),[](#l1.30)
'tim likes to eat a bag of ham worth $100')[](#l1.31)
self.assertRaises(KeyError, s.substitute, dict(who='tim'))[](#l1.32)
self.assertRaises(TypeError, Template.substitute)[](#l1.33)
- def test_regular_templates_with_braces(self):
s = Template('$who likes <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mrow><mi>w</mi><mi>h</mi><mi>a</mi><mi>t</mi></mrow><mi>f</mi><mi>o</mi><mi>r</mi></mrow><annotation encoding="application/x-tex">{what} for </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8889em;vertical-align:-0.1944em;"></span><span class="mord"><span class="mord mathnormal" style="margin-right:0.02691em;">w</span><span class="mord mathnormal">ha</span><span class="mord mathnormal">t</span></span><span class="mord mathnormal" style="margin-right:0.10764em;">f</span><span class="mord mathnormal" style="margin-right:0.02778em;">or</span></span></span></span>{meal}')[](#l1.36)
d = dict(who='tim', what='ham', meal='dinner')[](#l1.37)
self.assertEqual(s.substitute(d), 'tim likes ham for dinner')[](#l1.38)
self.assertRaises(KeyError, s.substitute,[](#l1.39)
dict(who='tim', what='ham'))[](#l1.40)
- def test_escapes(self):
eq = self.assertEqual[](#l1.43)
s = Template('$who likes to eat a bag of <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow></mrow><annotation encoding="application/x-tex"></annotation></semantics></math></span><span class="katex-html" aria-hidden="true"></span></span>what worth <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow></mrow><annotation encoding="application/x-tex"></annotation></semantics></math></span><span class="katex-html" aria-hidden="true"></span></span>100')[](#l1.44)
eq(s.substitute(dict(who='tim', what='ham')),[](#l1.45)
'tim likes to eat a bag of <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>w</mi><mi>h</mi><mi>a</mi><mi>t</mi><mi>w</mi><mi>o</mi><mi>r</mi><mi>t</mi><mi>h</mi></mrow><annotation encoding="application/x-tex">what worth </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6944em;"></span><span class="mord mathnormal" style="margin-right:0.02691em;">w</span><span class="mord mathnormal">ha</span><span class="mord mathnormal" style="margin-right:0.02691em;">tw</span><span class="mord mathnormal" style="margin-right:0.02778em;">or</span><span class="mord mathnormal">t</span><span class="mord mathnormal">h</span></span></span></span>100')[](#l1.46)
s = Template('$who likes <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow></mrow><annotation encoding="application/x-tex"></annotation></semantics></math></span><span class="katex-html" aria-hidden="true"></span></span>')[](#l1.47)
eq(s.substitute(dict(who='tim', what='ham')), 'tim likes $')[](#l1.48)
- def test_percents(self):
eq = self.assertEqual[](#l1.51)
s = Template('%(foo)s <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>f</mi><mi>o</mi><mi>o</mi></mrow><annotation encoding="application/x-tex">foo </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8889em;vertical-align:-0.1944em;"></span><span class="mord mathnormal" style="margin-right:0.10764em;">f</span><span class="mord mathnormal">oo</span></span></span></span>{foo}')[](#l1.52)
d = dict(foo='baz')[](#l1.53)
eq(s.substitute(d), '%(foo)s baz baz')[](#l1.54)
eq(s.safe_substitute(d), '%(foo)s baz baz')[](#l1.55)
- def test_stringification(self):
eq = self.assertEqual[](#l1.58)
s = Template('tim has eaten $count bags of ham today')[](#l1.59)
d = dict(count=7)[](#l1.60)
eq(s.substitute(d), 'tim has eaten 7 bags of ham today')[](#l1.61)
eq(s.safe_substitute(d), 'tim has eaten 7 bags of ham today')[](#l1.62)
s = Template('tim has eaten ${count} bags of ham today')[](#l1.63)
eq(s.substitute(d), 'tim has eaten 7 bags of ham today')[](#l1.64)
- def test_tupleargs(self):
eq = self.assertEqual[](#l1.67)
s = Template('$who ate ${meal}')[](#l1.68)
d = dict(who=('tim', 'fred'), meal=('ham', 'kung pao'))[](#l1.69)
eq(s.substitute(d), "('tim', 'fred') ate ('ham', 'kung pao')")[](#l1.70)
eq(s.safe_substitute(d), "('tim', 'fred') ate ('ham', 'kung pao')")[](#l1.71)
- def test_SafeTemplate(self):
eq = self.assertEqual[](#l1.74)
s = Template('$who likes <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mrow><mi>w</mi><mi>h</mi><mi>a</mi><mi>t</mi></mrow><mi>f</mi><mi>o</mi><mi>r</mi></mrow><annotation encoding="application/x-tex">{what} for </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8889em;vertical-align:-0.1944em;"></span><span class="mord"><span class="mord mathnormal" style="margin-right:0.02691em;">w</span><span class="mord mathnormal">ha</span><span class="mord mathnormal">t</span></span><span class="mord mathnormal" style="margin-right:0.10764em;">f</span><span class="mord mathnormal" style="margin-right:0.02778em;">or</span></span></span></span>{meal}')[](#l1.75)
eq(s.safe_substitute(dict(who='tim')), 'tim likes <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mrow><mi>w</mi><mi>h</mi><mi>a</mi><mi>t</mi></mrow><mi>f</mi><mi>o</mi><mi>r</mi></mrow><annotation encoding="application/x-tex">{what} for </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8889em;vertical-align:-0.1944em;"></span><span class="mord"><span class="mord mathnormal" style="margin-right:0.02691em;">w</span><span class="mord mathnormal">ha</span><span class="mord mathnormal">t</span></span><span class="mord mathnormal" style="margin-right:0.10764em;">f</span><span class="mord mathnormal" style="margin-right:0.02778em;">or</span></span></span></span>{meal}')[](#l1.76)
eq(s.safe_substitute(dict(what='ham')), '$who likes ham for ${meal}')[](#l1.77)
eq(s.safe_substitute(dict(what='ham', meal='dinner')),[](#l1.78)
'$who likes ham for dinner')[](#l1.79)
eq(s.safe_substitute(dict(who='tim', what='ham')),[](#l1.80)
'tim likes ham for ${meal}')[](#l1.81)
eq(s.safe_substitute(dict(who='tim', what='ham', meal='dinner')),[](#l1.82)
'tim likes ham for dinner')[](#l1.83)
- def test_invalid_placeholders(self):
raises = self.assertRaises[](#l1.86)
s = Template('$who likes $')[](#l1.87)
raises(ValueError, s.substitute, dict(who='tim'))[](#l1.88)
s = Template('$who likes ${what)')[](#l1.89)
raises(ValueError, s.substitute, dict(who='tim'))[](#l1.90)
s = Template('$who likes $100')[](#l1.91)
raises(ValueError, s.substitute, dict(who='tim'))[](#l1.92)
- def test_idpattern_override(self):
class PathPattern(Template):[](#l1.95)
idpattern = r'[_a-z][._a-z0-9]*'[](#l1.96)
m = Mapping()[](#l1.97)
m.bag = Bag()[](#l1.98)
m.bag.foo = Bag()[](#l1.99)
m.bag.foo.who = 'tim'[](#l1.100)
m.bag.what = 'ham'[](#l1.101)
s = PathPattern('$bag.foo.who likes to eat a bag of $bag.what')[](#l1.102)
self.assertEqual(s.substitute(m), 'tim likes to eat a bag of ham')[](#l1.103)
- def test_pattern_override(self):
class MyPattern(Template):[](#l1.106)
pattern = r"""[](#l1.107)
(?P<escaped>@{2}) |[](#l1.108)
@(?P<named>[_a-z][._a-z0-9]*) |[](#l1.109)
@{(?P<braced>[_a-z][._a-z0-9]*)} |[](#l1.110)
(?P<invalid>@)[](#l1.111)
"""[](#l1.112)
m = Mapping()[](#l1.113)
m.bag = Bag()[](#l1.114)
m.bag.foo = Bag()[](#l1.115)
m.bag.foo.who = 'tim'[](#l1.116)
m.bag.what = 'ham'[](#l1.117)
s = MyPattern('@bag.foo.who likes to eat a bag of @bag.what')[](#l1.118)
self.assertEqual(s.substitute(m), 'tim likes to eat a bag of ham')[](#l1.119)
class BadPattern(Template):[](#l1.121)
pattern = r"""[](#l1.122)
(?P<badname>.*) |[](#l1.123)
(?P<escaped>@{2}) |[](#l1.124)
@(?P<named>[_a-z][._a-z0-9]*) |[](#l1.125)
@{(?P<braced>[_a-z][._a-z0-9]*)} |[](#l1.126)
(?P<invalid>@) |[](#l1.127)
"""[](#l1.128)
s = BadPattern('@bag.foo.who likes to eat a bag of @bag.what')[](#l1.129)
self.assertRaises(ValueError, s.substitute, {})[](#l1.130)
self.assertRaises(ValueError, s.safe_substitute, {})[](#l1.131)
- def test_braced_override(self):
class MyTemplate(Template):[](#l1.134)
pattern = r"""[](#l1.135)
\$(?:[](#l1.136)
(?P<escaped>$) |[](#l1.137)
(?P<named>[_a-z][_a-z0-9]*) |[](#l1.138)
@@(?P<braced>[_a-z][_a-z0-9]*)@@ |[](#l1.139)
(?P<invalid>) |[](#l1.140)
)[](#l1.141)
"""[](#l1.142)
tmpl = 'PyCon in $@@location@@'[](#l1.144)
t = MyTemplate(tmpl)[](#l1.145)
self.assertRaises(KeyError, t.substitute, {})[](#l1.146)
val = t.substitute({'location': 'Cleveland'})[](#l1.147)
self.assertEqual(val, 'PyCon in Cleveland')[](#l1.148)
- def test_braced_override_safe(self):
class MyTemplate(Template):[](#l1.151)
pattern = r"""[](#l1.152)
\$(?:[](#l1.153)
(?P<escaped>$) |[](#l1.154)
(?P<named>[_a-z][_a-z0-9]*) |[](#l1.155)
@@(?P<braced>[_a-z][_a-z0-9]*)@@ |[](#l1.156)
(?P<invalid>) |[](#l1.157)
)[](#l1.158)
"""[](#l1.159)
tmpl = 'PyCon in $@@location@@'[](#l1.161)
t = MyTemplate(tmpl)[](#l1.162)
self.assertEqual(t.safe_substitute(), tmpl)[](#l1.163)
val = t.safe_substitute({'location': 'Cleveland'})[](#l1.164)
self.assertEqual(val, 'PyCon in Cleveland')[](#l1.165)
- def test_unicode_values(self):
s = Template('$who likes $what')[](#l1.168)
d = dict(who=u't\xffm', what=u'f\xfe\fed')[](#l1.169)
self.assertEqual(s.substitute(d), u't\xffm likes f\xfe\x0ced')[](#l1.170)
- def test_keyword_arguments(self):
eq = self.assertEqual[](#l1.173)
s = Template('$who likes $what')[](#l1.174)
eq(s.substitute(who='tim', what='ham'), 'tim likes ham')[](#l1.175)
eq(s.substitute(dict(who='tim'), what='ham'), 'tim likes ham')[](#l1.176)
eq(s.substitute(dict(who='fred', what='kung pao'),[](#l1.177)
who='tim', what='ham'),[](#l1.178)
'tim likes ham')[](#l1.179)
s = Template('the mapping is $mapping')[](#l1.180)
eq(s.substitute(dict(foo='none'), mapping='bozo'),[](#l1.181)
'the mapping is bozo')[](#l1.182)
eq(s.substitute(dict(mapping='one'), mapping='two'),[](#l1.183)
'the mapping is two')[](#l1.184)
s = Template('the self is $self')[](#l1.186)
eq(s.substitute(self='bozo'), 'the self is bozo')[](#l1.187)
- def test_keyword_arguments_safe(self):
eq = self.assertEqual[](#l1.190)
raises = self.assertRaises[](#l1.191)
s = Template('$who likes $what')[](#l1.192)
eq(s.safe_substitute(who='tim', what='ham'), 'tim likes ham')[](#l1.193)
eq(s.safe_substitute(dict(who='tim'), what='ham'), 'tim likes ham')[](#l1.194)
eq(s.safe_substitute(dict(who='fred', what='kung pao'),[](#l1.195)
who='tim', what='ham'),[](#l1.196)
'tim likes ham')[](#l1.197)
s = Template('the mapping is $mapping')[](#l1.198)
eq(s.safe_substitute(dict(foo='none'), mapping='bozo'),[](#l1.199)
'the mapping is bozo')[](#l1.200)
eq(s.safe_substitute(dict(mapping='one'), mapping='two'),[](#l1.201)
'the mapping is two')[](#l1.202)
d = dict(mapping='one')[](#l1.203)
raises(TypeError, s.substitute, d, {})[](#l1.204)
raises(TypeError, s.safe_substitute, d, {})[](#l1.205)
s = Template('the self is $self')[](#l1.207)
eq(s.safe_substitute(self='bozo'), 'the self is bozo')[](#l1.208)
- def test_delimiter_override(self):
eq = self.assertEqual[](#l1.211)
raises = self.assertRaises[](#l1.212)
class AmpersandTemplate(Template):[](#l1.213)
delimiter = '&'[](#l1.214)
s = AmpersandTemplate('this &gift is for &{who} &&')[](#l1.215)
eq(s.substitute(gift='bud', who='you'), 'this bud is for you &')[](#l1.216)
raises(KeyError, s.substitute)[](#l1.217)
eq(s.safe_substitute(gift='bud', who='you'), 'this bud is for you &')[](#l1.218)
eq(s.safe_substitute(), 'this &gift is for &{who} &')[](#l1.219)
s = AmpersandTemplate('this &gift is for &{who} &')[](#l1.220)
raises(ValueError, s.substitute, dict(gift='bud', who='you'))[](#l1.221)
eq(s.safe_substitute(), 'this &gift is for &{who} &')[](#l1.222)
class PieDelims(Template):[](#l1.224)
delimiter = '@'[](#l1.225)
s = PieDelims('@who likes to eat a bag of @{what} worth $100')[](#l1.226)
self.assertEqual(s.substitute(dict(who='tim', what='ham')),[](#l1.227)
'tim likes to eat a bag of ham worth $100')[](#l1.228)
- from test import test_support
- test_classes = [TestTemplate,]
- test_support.run_unittest(*test_classes)
--- a/Lib/test/test_string_merged.py +++ b/Lib/test/test_string_merged.py @@ -1,4 +1,6 @@ -import unittest, string +import unittest +import string +from string import Template from test import test_support, string_tests from UserList import UserList @@ -59,8 +61,6 @@ class StringTest( self.fail('exception not raised') - - class ModuleTest(unittest.TestCase): def test_attrs(self): @@ -219,8 +219,229 @@ class BytesAliasTest(unittest.TestCase): self.assertTrue(type(b""), str) self.assertTrue(type(br""), str) -def test_main():
+ +# Template tests (formerly housed in test_pep292.py) + +class Bag:
- def getitem(self, name):
obj = self[](#l2.34)
for part in name.split('.'):[](#l2.35)
try:[](#l2.36)
obj = getattr(obj, part)[](#l2.37)
except AttributeError:[](#l2.38)
raise KeyError(name)[](#l2.39)
return obj[](#l2.40)
+ + +class TestTemplate(unittest.TestCase):
- def test_regular_templates(self):
s = Template('$who likes to eat a bag of <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>w</mi><mi>h</mi><mi>a</mi><mi>t</mi><mi>w</mi><mi>o</mi><mi>r</mi><mi>t</mi><mi>h</mi></mrow><annotation encoding="application/x-tex">what worth </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6944em;"></span><span class="mord mathnormal" style="margin-right:0.02691em;">w</span><span class="mord mathnormal">ha</span><span class="mord mathnormal" style="margin-right:0.02691em;">tw</span><span class="mord mathnormal" style="margin-right:0.02778em;">or</span><span class="mord mathnormal">t</span><span class="mord mathnormal">h</span></span></span></span>$100')[](#l2.45)
self.assertEqual(s.substitute(dict(who='tim', what='ham')),[](#l2.46)
'tim likes to eat a bag of ham worth $100')[](#l2.47)
self.assertRaises(KeyError, s.substitute, dict(who='tim'))[](#l2.48)
self.assertRaises(TypeError, Template.substitute)[](#l2.49)
- def test_regular_templates_with_braces(self):
s = Template('$who likes <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mrow><mi>w</mi><mi>h</mi><mi>a</mi><mi>t</mi></mrow><mi>f</mi><mi>o</mi><mi>r</mi></mrow><annotation encoding="application/x-tex">{what} for </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8889em;vertical-align:-0.1944em;"></span><span class="mord"><span class="mord mathnormal" style="margin-right:0.02691em;">w</span><span class="mord mathnormal">ha</span><span class="mord mathnormal">t</span></span><span class="mord mathnormal" style="margin-right:0.10764em;">f</span><span class="mord mathnormal" style="margin-right:0.02778em;">or</span></span></span></span>{meal}')[](#l2.52)
d = dict(who='tim', what='ham', meal='dinner')[](#l2.53)
self.assertEqual(s.substitute(d), 'tim likes ham for dinner')[](#l2.54)
self.assertRaises(KeyError, s.substitute,[](#l2.55)
dict(who='tim', what='ham'))[](#l2.56)
- def test_escapes(self):
eq = self.assertEqual[](#l2.59)
s = Template('$who likes to eat a bag of <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow></mrow><annotation encoding="application/x-tex"></annotation></semantics></math></span><span class="katex-html" aria-hidden="true"></span></span>what worth <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow></mrow><annotation encoding="application/x-tex"></annotation></semantics></math></span><span class="katex-html" aria-hidden="true"></span></span>100')[](#l2.60)
eq(s.substitute(dict(who='tim', what='ham')),[](#l2.61)
'tim likes to eat a bag of <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>w</mi><mi>h</mi><mi>a</mi><mi>t</mi><mi>w</mi><mi>o</mi><mi>r</mi><mi>t</mi><mi>h</mi></mrow><annotation encoding="application/x-tex">what worth </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6944em;"></span><span class="mord mathnormal" style="margin-right:0.02691em;">w</span><span class="mord mathnormal">ha</span><span class="mord mathnormal" style="margin-right:0.02691em;">tw</span><span class="mord mathnormal" style="margin-right:0.02778em;">or</span><span class="mord mathnormal">t</span><span class="mord mathnormal">h</span></span></span></span>100')[](#l2.62)
s = Template('$who likes <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow></mrow><annotation encoding="application/x-tex"></annotation></semantics></math></span><span class="katex-html" aria-hidden="true"></span></span>')[](#l2.63)
eq(s.substitute(dict(who='tim', what='ham')), 'tim likes $')[](#l2.64)
- def test_percents(self):
eq = self.assertEqual[](#l2.67)
s = Template('%(foo)s <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>f</mi><mi>o</mi><mi>o</mi></mrow><annotation encoding="application/x-tex">foo </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8889em;vertical-align:-0.1944em;"></span><span class="mord mathnormal" style="margin-right:0.10764em;">f</span><span class="mord mathnormal">oo</span></span></span></span>{foo}')[](#l2.68)
d = dict(foo='baz')[](#l2.69)
eq(s.substitute(d), '%(foo)s baz baz')[](#l2.70)
eq(s.safe_substitute(d), '%(foo)s baz baz')[](#l2.71)
- def test_stringification(self):
eq = self.assertEqual[](#l2.74)
s = Template('tim has eaten $count bags of ham today')[](#l2.75)
d = dict(count=7)[](#l2.76)
eq(s.substitute(d), 'tim has eaten 7 bags of ham today')[](#l2.77)
eq(s.safe_substitute(d), 'tim has eaten 7 bags of ham today')[](#l2.78)
s = Template('tim has eaten ${count} bags of ham today')[](#l2.79)
eq(s.substitute(d), 'tim has eaten 7 bags of ham today')[](#l2.80)
- def test_tupleargs(self):
eq = self.assertEqual[](#l2.83)
s = Template('$who ate ${meal}')[](#l2.84)
d = dict(who=('tim', 'fred'), meal=('ham', 'kung pao'))[](#l2.85)
eq(s.substitute(d), "('tim', 'fred') ate ('ham', 'kung pao')")[](#l2.86)
eq(s.safe_substitute(d), "('tim', 'fred') ate ('ham', 'kung pao')")[](#l2.87)
- def test_SafeTemplate(self):
eq = self.assertEqual[](#l2.90)
s = Template('$who likes <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mrow><mi>w</mi><mi>h</mi><mi>a</mi><mi>t</mi></mrow><mi>f</mi><mi>o</mi><mi>r</mi></mrow><annotation encoding="application/x-tex">{what} for </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8889em;vertical-align:-0.1944em;"></span><span class="mord"><span class="mord mathnormal" style="margin-right:0.02691em;">w</span><span class="mord mathnormal">ha</span><span class="mord mathnormal">t</span></span><span class="mord mathnormal" style="margin-right:0.10764em;">f</span><span class="mord mathnormal" style="margin-right:0.02778em;">or</span></span></span></span>{meal}')[](#l2.91)
eq(s.safe_substitute(dict(who='tim')), 'tim likes <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mrow><mi>w</mi><mi>h</mi><mi>a</mi><mi>t</mi></mrow><mi>f</mi><mi>o</mi><mi>r</mi></mrow><annotation encoding="application/x-tex">{what} for </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8889em;vertical-align:-0.1944em;"></span><span class="mord"><span class="mord mathnormal" style="margin-right:0.02691em;">w</span><span class="mord mathnormal">ha</span><span class="mord mathnormal">t</span></span><span class="mord mathnormal" style="margin-right:0.10764em;">f</span><span class="mord mathnormal" style="margin-right:0.02778em;">or</span></span></span></span>{meal}')[](#l2.92)
eq(s.safe_substitute(dict(what='ham')), '$who likes ham for ${meal}')[](#l2.93)
eq(s.safe_substitute(dict(what='ham', meal='dinner')),[](#l2.94)
'$who likes ham for dinner')[](#l2.95)
eq(s.safe_substitute(dict(who='tim', what='ham')),[](#l2.96)
'tim likes ham for ${meal}')[](#l2.97)
eq(s.safe_substitute(dict(who='tim', what='ham', meal='dinner')),[](#l2.98)
'tim likes ham for dinner')[](#l2.99)
- def test_invalid_placeholders(self):
raises = self.assertRaises[](#l2.102)
s = Template('$who likes $')[](#l2.103)
raises(ValueError, s.substitute, dict(who='tim'))[](#l2.104)
s = Template('$who likes ${what)')[](#l2.105)
raises(ValueError, s.substitute, dict(who='tim'))[](#l2.106)
s = Template('$who likes $100')[](#l2.107)
raises(ValueError, s.substitute, dict(who='tim'))[](#l2.108)
- def test_idpattern_override(self):
class PathPattern(Template):[](#l2.111)
idpattern = r'[_a-z][._a-z0-9]*'[](#l2.112)
m = Mapping()[](#l2.113)
m.bag = Bag()[](#l2.114)
m.bag.foo = Bag()[](#l2.115)
m.bag.foo.who = 'tim'[](#l2.116)
m.bag.what = 'ham'[](#l2.117)
s = PathPattern('$bag.foo.who likes to eat a bag of $bag.what')[](#l2.118)
self.assertEqual(s.substitute(m), 'tim likes to eat a bag of ham')[](#l2.119)
- def test_pattern_override(self):
class MyPattern(Template):[](#l2.122)
pattern = r"""[](#l2.123)
(?P<escaped>@{2}) |[](#l2.124)
@(?P<named>[_a-z][._a-z0-9]*) |[](#l2.125)
@{(?P<braced>[_a-z][._a-z0-9]*)} |[](#l2.126)
(?P<invalid>@)[](#l2.127)
"""[](#l2.128)
m = Mapping()[](#l2.129)
m.bag = Bag()[](#l2.130)
m.bag.foo = Bag()[](#l2.131)
m.bag.foo.who = 'tim'[](#l2.132)
m.bag.what = 'ham'[](#l2.133)
s = MyPattern('@bag.foo.who likes to eat a bag of @bag.what')[](#l2.134)
self.assertEqual(s.substitute(m), 'tim likes to eat a bag of ham')[](#l2.135)
class BadPattern(Template):[](#l2.138)
pattern = r"""[](#l2.139)
(?P<badname>.*) |[](#l2.140)
(?P<escaped>@{2}) |[](#l2.141)
@(?P<named>[_a-z][._a-z0-9]*) |[](#l2.142)
@{(?P<braced>[_a-z][._a-z0-9]*)} |[](#l2.143)
(?P<invalid>@) |[](#l2.144)
"""[](#l2.145)
s = BadPattern('@bag.foo.who likes to eat a bag of @bag.what')[](#l2.146)
self.assertRaises(ValueError, s.substitute, {})[](#l2.147)
self.assertRaises(ValueError, s.safe_substitute, {})[](#l2.148)
- def test_braced_override(self):
class MyTemplate(Template):[](#l2.151)
pattern = r"""[](#l2.152)
\$(?:[](#l2.153)
(?P<escaped>$) |[](#l2.154)
(?P<named>[_a-z][_a-z0-9]*) |[](#l2.155)
@@(?P<braced>[_a-z][_a-z0-9]*)@@ |[](#l2.156)
(?P<invalid>) |[](#l2.157)
)[](#l2.158)
"""[](#l2.159)
tmpl = 'PyCon in $@@location@@'[](#l2.161)
t = MyTemplate(tmpl)[](#l2.162)
self.assertRaises(KeyError, t.substitute, {})[](#l2.163)
val = t.substitute({'location': 'Cleveland'})[](#l2.164)
self.assertEqual(val, 'PyCon in Cleveland')[](#l2.165)
- def test_braced_override_safe(self):
class MyTemplate(Template):[](#l2.168)
pattern = r"""[](#l2.169)
\$(?:[](#l2.170)
(?P<escaped>$) |[](#l2.171)
(?P<named>[_a-z][_a-z0-9]*) |[](#l2.172)
@@(?P<braced>[_a-z][_a-z0-9]*)@@ |[](#l2.173)
(?P<invalid>) |[](#l2.174)
)[](#l2.175)
"""[](#l2.176)
tmpl = 'PyCon in $@@location@@'[](#l2.178)
t = MyTemplate(tmpl)[](#l2.179)
self.assertEqual(t.safe_substitute(), tmpl)[](#l2.180)
val = t.safe_substitute({'location': 'Cleveland'})[](#l2.181)
self.assertEqual(val, 'PyCon in Cleveland')[](#l2.182)
- def test_unicode_values(self):
s = Template('$who likes $what')[](#l2.185)
d = dict(who=u't\xffm', what=u'f\xfe\fed')[](#l2.186)
self.assertEqual(s.substitute(d), u't\xffm likes f\xfe\x0ced')[](#l2.187)
- def test_keyword_arguments(self):
eq = self.assertEqual[](#l2.190)
s = Template('$who likes $what')[](#l2.191)
eq(s.substitute(who='tim', what='ham'), 'tim likes ham')[](#l2.192)
eq(s.substitute(dict(who='tim'), what='ham'), 'tim likes ham')[](#l2.193)
eq(s.substitute(dict(who='fred', what='kung pao'),[](#l2.194)
who='tim', what='ham'),[](#l2.195)
'tim likes ham')[](#l2.196)
s = Template('the mapping is $mapping')[](#l2.197)
eq(s.substitute(dict(foo='none'), mapping='bozo'),[](#l2.198)
'the mapping is bozo')[](#l2.199)
eq(s.substitute(dict(mapping='one'), mapping='two'),[](#l2.200)
'the mapping is two')[](#l2.201)
s = Template('the self is $self')[](#l2.203)
eq(s.substitute(self='bozo'), 'the self is bozo')[](#l2.204)
- def test_keyword_arguments_safe(self):
eq = self.assertEqual[](#l2.207)
raises = self.assertRaises[](#l2.208)
s = Template('$who likes $what')[](#l2.209)
eq(s.safe_substitute(who='tim', what='ham'), 'tim likes ham')[](#l2.210)
eq(s.safe_substitute(dict(who='tim'), what='ham'), 'tim likes ham')[](#l2.211)
eq(s.safe_substitute(dict(who='fred', what='kung pao'),[](#l2.212)
who='tim', what='ham'),[](#l2.213)
'tim likes ham')[](#l2.214)
s = Template('the mapping is $mapping')[](#l2.215)
eq(s.safe_substitute(dict(foo='none'), mapping='bozo'),[](#l2.216)
'the mapping is bozo')[](#l2.217)
eq(s.safe_substitute(dict(mapping='one'), mapping='two'),[](#l2.218)
'the mapping is two')[](#l2.219)
d = dict(mapping='one')[](#l2.220)
raises(TypeError, s.substitute, d, {})[](#l2.221)
raises(TypeError, s.safe_substitute, d, {})[](#l2.222)
s = Template('the self is $self')[](#l2.224)
eq(s.safe_substitute(self='bozo'), 'the self is bozo')[](#l2.225)
- def test_delimiter_override(self):
eq = self.assertEqual[](#l2.228)
raises = self.assertRaises[](#l2.229)
class AmpersandTemplate(Template):[](#l2.230)
delimiter = '&'[](#l2.231)
s = AmpersandTemplate('this &gift is for &{who} &&')[](#l2.232)
eq(s.substitute(gift='bud', who='you'), 'this bud is for you &')[](#l2.233)
raises(KeyError, s.substitute)[](#l2.234)
eq(s.safe_substitute(gift='bud', who='you'), 'this bud is for you &')[](#l2.235)
eq(s.safe_substitute(), 'this &gift is for &{who} &')[](#l2.236)
s = AmpersandTemplate('this &gift is for &{who} &')[](#l2.237)
raises(ValueError, s.substitute, dict(gift='bud', who='you'))[](#l2.238)
eq(s.safe_substitute(), 'this &gift is for &{who} &')[](#l2.239)
class PieDelims(Template):[](#l2.241)
delimiter = '@'[](#l2.242)
s = PieDelims('@who likes to eat a bag of @{what} worth $100')[](#l2.243)
self.assertEqual(s.substitute(dict(who='tim', what='ham')),[](#l2.244)
'tim likes to eat a bag of ham worth $100')[](#l2.245)