1 # Copyright (C) 2004 Python Software Foundation
2 # Author: barry@python.org (Barry Warsaw)
3 # License: http://www.opensource.org/licenses/PythonSoftFoundation.php
6 from string
import Template
13 def __getitem__(self
, name
):
15 for part
in name
.split('.'):
17 obj
= getattr(obj
, part
)
18 except AttributeError:
23 class TestTemplate(unittest
.TestCase
):
24 def test_regular_templates(self
):
25 s
= Template('$who likes to eat a bag of $what worth $$100')
26 self
.assertEqual(s
.substitute(dict(who
='tim', what
='ham')),
27 'tim likes to eat a bag of ham worth $100')
28 self
.assertRaises(KeyError, s
.substitute
, dict(who
='tim'))
30 def test_regular_templates_with_braces(self
):
31 s
= Template('$who likes ${what} for ${meal}')
32 d
= dict(who
='tim', what
='ham', meal
='dinner')
33 self
.assertEqual(s
.substitute(d
), 'tim likes ham for dinner')
34 self
.assertRaises(KeyError, s
.substitute
,
35 dict(who
='tim', what
='ham'))
37 def test_escapes(self
):
39 s
= Template('$who likes to eat a bag of $$what worth $$100')
40 eq(s
.substitute(dict(who
='tim', what
='ham')),
41 'tim likes to eat a bag of $what worth $100')
42 s
= Template('$who likes $$')
43 eq(s
.substitute(dict(who
='tim', what
='ham')), 'tim likes $')
45 def test_percents(self
):
47 s
= Template('%(foo)s $foo ${foo}')
49 eq(s
.substitute(d
), '%(foo)s baz baz')
50 eq(s
.safe_substitute(d
), '%(foo)s baz baz')
52 def test_stringification(self
):
54 s
= Template('tim has eaten $count bags of ham today')
56 eq(s
.substitute(d
), 'tim has eaten 7 bags of ham today')
57 eq(s
.safe_substitute(d
), 'tim has eaten 7 bags of ham today')
58 s
= Template('tim has eaten ${count} bags of ham today')
59 eq(s
.substitute(d
), 'tim has eaten 7 bags of ham today')
61 def test_tupleargs(self
):
63 s
= Template('$who ate ${meal}')
64 d
= dict(who
=('tim', 'fred'), meal
=('ham', 'kung pao'))
65 eq(s
.substitute(d
), "('tim', 'fred') ate ('ham', 'kung pao')")
66 eq(s
.safe_substitute(d
), "('tim', 'fred') ate ('ham', 'kung pao')")
68 def test_SafeTemplate(self
):
70 s
= Template('$who likes ${what} for ${meal}')
71 eq(s
.safe_substitute(dict(who
='tim')), 'tim likes ${what} for ${meal}')
72 eq(s
.safe_substitute(dict(what
='ham')), '$who likes ham for ${meal}')
73 eq(s
.safe_substitute(dict(what
='ham', meal
='dinner')),
74 '$who likes ham for dinner')
75 eq(s
.safe_substitute(dict(who
='tim', what
='ham')),
76 'tim likes ham for ${meal}')
77 eq(s
.safe_substitute(dict(who
='tim', what
='ham', meal
='dinner')),
78 'tim likes ham for dinner')
80 def test_invalid_placeholders(self
):
81 raises
= self
.assertRaises
82 s
= Template('$who likes $')
83 raises(ValueError, s
.substitute
, dict(who
='tim'))
84 s
= Template('$who likes ${what)')
85 raises(ValueError, s
.substitute
, dict(who
='tim'))
86 s
= Template('$who likes $100')
87 raises(ValueError, s
.substitute
, dict(who
='tim'))
89 def test_delimiter_override(self
):
90 class PieDelims(Template
):
92 s
= PieDelims('@who likes to eat a bag of @{what} worth $100')
93 self
.assertEqual(s
.substitute(dict(who
='tim', what
='ham')),
94 'tim likes to eat a bag of ham worth $100')
96 def test_idpattern_override(self
):
97 class PathPattern(Template
):
98 idpattern
= r
'[_a-z][._a-z0-9]*'
102 m
.bag
.foo
.who
= 'tim'
104 s
= PathPattern('$bag.foo.who likes to eat a bag of $bag.what')
105 self
.assertEqual(s
.substitute(m
), 'tim likes to eat a bag of ham')
107 def test_pattern_override(self
):
108 class MyPattern(Template
):
111 @(?P<named>[_a-z][._a-z0-9]*) |
112 @{(?P<braced>[_a-z][._a-z0-9]*)} |
118 m
.bag
.foo
.who
= 'tim'
120 s
= MyPattern('@bag.foo.who likes to eat a bag of @bag.what')
121 self
.assertEqual(s
.substitute(m
), 'tim likes to eat a bag of ham')
123 class BadPattern(Template
):
127 @(?P<named>[_a-z][._a-z0-9]*) |
128 @{(?P<braced>[_a-z][._a-z0-9]*)} |
131 s
= BadPattern('@bag.foo.who likes to eat a bag of @bag.what')
132 self
.assertRaises(ValueError, s
.substitute
, {})
133 self
.assertRaises(ValueError, s
.safe_substitute
, {})
135 def test_unicode_values(self
):
136 s
= Template('$who likes $what')
137 d
= dict(who
=u
't\xffm', what
=u
'f\xfe\fed')
138 self
.assertEqual(s
.substitute(d
), u
't\xffm likes f\xfe\x0ced')
140 def test_keyword_arguments(self
):
141 eq
= self
.assertEqual
142 s
= Template('$who likes $what')
143 eq(s
.substitute(who
='tim', what
='ham'), 'tim likes ham')
144 eq(s
.substitute(dict(who
='tim'), what
='ham'), 'tim likes ham')
145 eq(s
.substitute(dict(who
='fred', what
='kung pao'),
146 who
='tim', what
='ham'),
148 s
= Template('the mapping is $mapping')
149 eq(s
.substitute(dict(foo
='none'), mapping
='bozo'),
150 'the mapping is bozo')
151 eq(s
.substitute(dict(mapping
='one'), mapping
='two'),
152 'the mapping is two')
154 def test_keyword_arguments_safe(self
):
155 eq
= self
.assertEqual
156 raises
= self
.assertRaises
157 s
= Template('$who likes $what')
158 eq(s
.safe_substitute(who
='tim', what
='ham'), 'tim likes ham')
159 eq(s
.safe_substitute(dict(who
='tim'), what
='ham'), 'tim likes ham')
160 eq(s
.safe_substitute(dict(who
='fred', what
='kung pao'),
161 who
='tim', what
='ham'),
163 s
= Template('the mapping is $mapping')
164 eq(s
.safe_substitute(dict(foo
='none'), mapping
='bozo'),
165 'the mapping is bozo')
166 eq(s
.safe_substitute(dict(mapping
='one'), mapping
='two'),
167 'the mapping is two')
168 d
= dict(mapping
='one')
169 raises(TypeError, s
.substitute
, d
, {})
170 raises(TypeError, s
.safe_substitute
, d
, {})
172 def test_delimiter_override(self
):
173 eq
= self
.assertEqual
174 raises
= self
.assertRaises
175 class AmpersandTemplate(Template
):
177 s
= AmpersandTemplate('this &gift is for &{who} &&')
178 eq(s
.substitute(gift
='bud', who
='you'), 'this bud is for you &')
179 raises(KeyError, s
.substitute
)
180 eq(s
.safe_substitute(gift
='bud', who
='you'), 'this bud is for you &')
181 eq(s
.safe_substitute(), 'this &gift is for &{who} &')
182 s
= AmpersandTemplate('this &gift is for &{who} &')
183 raises(ValueError, s
.substitute
, dict(gift
='bud', who
='you'))
184 eq(s
.safe_substitute(), 'this &gift is for &{who} &')
188 from test
import test_support
189 test_classes
= [TestTemplate
,]
190 test_support
.run_unittest(*test_classes
)
193 if __name__
== '__main__':