blob: 6da8d2e3e1c80009c21e1cafba62c263ebfa2df3 [file] [log] [blame]
Barry Warsaw8bee7612004-08-25 02:22:30 +00001# Copyright (C) 2004 Python Software Foundation
2# Author: barry@python.org (Barry Warsaw)
3# License: http://www.opensource.org/licenses/PythonSoftFoundation.php
4
5import unittest
Barry Warsaw12827c12004-09-10 03:08:08 +00006from string import Template
7
8
9class Bag:
10 pass
11
12class Mapping:
13 def __getitem__(self, name):
14 obj = self
15 for part in name.split('.'):
16 try:
17 obj = getattr(obj, part)
18 except AttributeError:
19 raise KeyError(name)
20 return obj
21
Barry Warsaw8bee7612004-08-25 02:22:30 +000022
23class TestTemplate(unittest.TestCase):
Barry Warsaw8bee7612004-08-25 02:22:30 +000024 def test_regular_templates(self):
25 s = Template('$who likes to eat a bag of $what worth $$100')
Barry Warsaw12827c12004-09-10 03:08:08 +000026 self.assertEqual(s.substitute(dict(who='tim', what='ham')),
Barry Warsaw8bee7612004-08-25 02:22:30 +000027 'tim likes to eat a bag of ham worth $100')
Barry Warsaw12827c12004-09-10 03:08:08 +000028 self.assertRaises(KeyError, s.substitute, dict(who='tim'))
Barry Warsaw8bee7612004-08-25 02:22:30 +000029
30 def test_regular_templates_with_braces(self):
31 s = Template('$who likes ${what} for ${meal}')
Barry Warsaw12827c12004-09-10 03:08:08 +000032 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'))
Barry Warsaw8bee7612004-08-25 02:22:30 +000036
37 def test_escapes(self):
38 eq = self.assertEqual
39 s = Template('$who likes to eat a bag of $$what worth $$100')
Barry Warsaw12827c12004-09-10 03:08:08 +000040 eq(s.substitute(dict(who='tim', what='ham')),
Barry Warsaw8bee7612004-08-25 02:22:30 +000041 'tim likes to eat a bag of $what worth $100')
42 s = Template('$who likes $$')
Barry Warsaw12827c12004-09-10 03:08:08 +000043 eq(s.substitute(dict(who='tim', what='ham')), 'tim likes $')
Barry Warsaw8bee7612004-08-25 02:22:30 +000044
45 def test_percents(self):
Barry Warsaw12827c12004-09-10 03:08:08 +000046 eq = self.assertEqual
Barry Warsaw8bee7612004-08-25 02:22:30 +000047 s = Template('%(foo)s $foo ${foo}')
Barry Warsaw12827c12004-09-10 03:08:08 +000048 d = dict(foo='baz')
49 eq(s.substitute(d), '%(foo)s baz baz')
50 eq(s.safe_substitute(d), '%(foo)s baz baz')
Barry Warsaw8bee7612004-08-25 02:22:30 +000051
52 def test_stringification(self):
Barry Warsaw12827c12004-09-10 03:08:08 +000053 eq = self.assertEqual
Barry Warsaw8bee7612004-08-25 02:22:30 +000054 s = Template('tim has eaten $count bags of ham today')
Barry Warsaw12827c12004-09-10 03:08:08 +000055 d = dict(count=7)
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')
Barry Warsaw8bee7612004-08-25 02:22:30 +000060
Thomas Wouters0e3f5912006-08-11 14:57:12 +000061 def test_tupleargs(self):
62 eq = self.assertEqual
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')")
67
Barry Warsaw8bee7612004-08-25 02:22:30 +000068 def test_SafeTemplate(self):
69 eq = self.assertEqual
Barry Warsaw12827c12004-09-10 03:08:08 +000070 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')),
Barry Warsaw8bee7612004-08-25 02:22:30 +000074 '$who likes ham for dinner')
Barry Warsaw12827c12004-09-10 03:08:08 +000075 eq(s.safe_substitute(dict(who='tim', what='ham')),
Barry Warsaw8bee7612004-08-25 02:22:30 +000076 'tim likes ham for ${meal}')
Barry Warsaw12827c12004-09-10 03:08:08 +000077 eq(s.safe_substitute(dict(who='tim', what='ham', meal='dinner')),
Barry Warsaw8bee7612004-08-25 02:22:30 +000078 'tim likes ham for dinner')
79
80 def test_invalid_placeholders(self):
81 raises = self.assertRaises
82 s = Template('$who likes $')
Barry Warsaw12827c12004-09-10 03:08:08 +000083 raises(ValueError, s.substitute, dict(who='tim'))
Barry Warsaw8bee7612004-08-25 02:22:30 +000084 s = Template('$who likes ${what)')
Barry Warsaw12827c12004-09-10 03:08:08 +000085 raises(ValueError, s.substitute, dict(who='tim'))
Barry Warsaw8bee7612004-08-25 02:22:30 +000086 s = Template('$who likes $100')
Barry Warsaw12827c12004-09-10 03:08:08 +000087 raises(ValueError, s.substitute, dict(who='tim'))
88
Barry Warsaw12827c12004-09-10 03:08:08 +000089 def test_idpattern_override(self):
90 class PathPattern(Template):
91 idpattern = r'[_a-z][._a-z0-9]*'
92 m = Mapping()
93 m.bag = Bag()
94 m.bag.foo = Bag()
95 m.bag.foo.who = 'tim'
96 m.bag.what = 'ham'
97 s = PathPattern('$bag.foo.who likes to eat a bag of $bag.what')
98 self.assertEqual(s.substitute(m), 'tim likes to eat a bag of ham')
99
100 def test_pattern_override(self):
101 class MyPattern(Template):
102 pattern = r"""
103 (?P<escaped>@{2}) |
104 @(?P<named>[_a-z][._a-z0-9]*) |
105 @{(?P<braced>[_a-z][._a-z0-9]*)} |
Barry Warsaw3e773fb2004-09-13 20:53:27 +0000106 (?P<invalid>@)
Barry Warsaw12827c12004-09-10 03:08:08 +0000107 """
108 m = Mapping()
109 m.bag = Bag()
110 m.bag.foo = Bag()
111 m.bag.foo.who = 'tim'
112 m.bag.what = 'ham'
113 s = MyPattern('@bag.foo.who likes to eat a bag of @bag.what')
114 self.assertEqual(s.substitute(m), 'tim likes to eat a bag of ham')
115
Neal Norwitz6627a962004-10-17 16:27:18 +0000116 class BadPattern(Template):
117 pattern = r"""
118 (?P<badname>.*) |
119 (?P<escaped>@{2}) |
120 @(?P<named>[_a-z][._a-z0-9]*) |
121 @{(?P<braced>[_a-z][._a-z0-9]*)} |
122 (?P<invalid>@) |
123 """
124 s = BadPattern('@bag.foo.who likes to eat a bag of @bag.what')
125 self.assertRaises(ValueError, s.substitute, {})
126 self.assertRaises(ValueError, s.safe_substitute, {})
127
Florent Xiclunaeb19dce2010-09-18 23:34:07 +0000128 def test_braced_override(self):
129 class MyTemplate(Template):
130 pattern = r"""
131 \$(?:
132 (?P<escaped>$) |
133 (?P<named>[_a-z][_a-z0-9]*) |
134 @@(?P<braced>[_a-z][_a-z0-9]*)@@ |
135 (?P<invalid>) |
136 )
137 """
138
139 tmpl = 'PyCon in $@@location@@'
140 t = MyTemplate(tmpl)
141 self.assertRaises(KeyError, t.substitute, {})
142 val = t.substitute({'location': 'Cleveland'})
143 self.assertEqual(val, 'PyCon in Cleveland')
144
145 def test_braced_override_safe(self):
146 class MyTemplate(Template):
147 pattern = r"""
148 \$(?:
149 (?P<escaped>$) |
150 (?P<named>[_a-z][_a-z0-9]*) |
151 @@(?P<braced>[_a-z][_a-z0-9]*)@@ |
152 (?P<invalid>) |
153 )
154 """
155
156 tmpl = 'PyCon in $@@location@@'
157 t = MyTemplate(tmpl)
158 self.assertEqual(t.safe_substitute(), tmpl)
159 val = t.safe_substitute({'location': 'Cleveland'})
160 self.assertEqual(val, 'PyCon in Cleveland')
161
Nick Coghlan62ecb6a2011-05-31 19:40:11 +1000162 def test_invalid_with_no_lines(self):
163 # The error formatting for invalid templates
164 # has a special case for no data that the default
165 # pattern can't trigger (always has at least '$')
166 # So we craft a pattern that is always invalid
167 # with no leading data.
168 class MyTemplate(Template):
169 pattern = r"""
170 (?P<invalid>) |
171 unreachable(
172 (?P<named>) |
173 (?P<braced>) |
174 (?P<escaped>)
175 )
176 """
177 s = MyTemplate('')
178 with self.assertRaises(ValueError) as err:
179 s.substitute({})
180 self.assertIn('line 1, col 1', str(err.exception))
181
Barry Warsaw12827c12004-09-10 03:08:08 +0000182 def test_unicode_values(self):
183 s = Template('$who likes $what')
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000184 d = dict(who='t\xffm', what='f\xfe\fed')
185 self.assertEqual(s.substitute(d), 't\xffm likes f\xfe\x0ced')
Barry Warsaw8bee7612004-08-25 02:22:30 +0000186
Barry Warsaw302bd582004-09-13 14:35:59 +0000187 def test_keyword_arguments(self):
188 eq = self.assertEqual
189 s = Template('$who likes $what')
190 eq(s.substitute(who='tim', what='ham'), 'tim likes ham')
191 eq(s.substitute(dict(who='tim'), what='ham'), 'tim likes ham')
192 eq(s.substitute(dict(who='fred', what='kung pao'),
193 who='tim', what='ham'),
194 'tim likes ham')
195 s = Template('the mapping is $mapping')
196 eq(s.substitute(dict(foo='none'), mapping='bozo'),
197 'the mapping is bozo')
198 eq(s.substitute(dict(mapping='one'), mapping='two'),
199 'the mapping is two')
200
201 def test_keyword_arguments_safe(self):
202 eq = self.assertEqual
Barry Warsawc7cd20c2004-09-13 15:24:43 +0000203 raises = self.assertRaises
Barry Warsaw302bd582004-09-13 14:35:59 +0000204 s = Template('$who likes $what')
205 eq(s.safe_substitute(who='tim', what='ham'), 'tim likes ham')
206 eq(s.safe_substitute(dict(who='tim'), what='ham'), 'tim likes ham')
207 eq(s.safe_substitute(dict(who='fred', what='kung pao'),
208 who='tim', what='ham'),
209 'tim likes ham')
210 s = Template('the mapping is $mapping')
211 eq(s.safe_substitute(dict(foo='none'), mapping='bozo'),
212 'the mapping is bozo')
213 eq(s.safe_substitute(dict(mapping='one'), mapping='two'),
214 'the mapping is two')
Barry Warsawc7cd20c2004-09-13 15:24:43 +0000215 d = dict(mapping='one')
216 raises(TypeError, s.substitute, d, {})
217 raises(TypeError, s.safe_substitute, d, {})
Barry Warsaw302bd582004-09-13 14:35:59 +0000218
Raymond Hettinger6d191112004-09-14 02:34:08 +0000219 def test_delimiter_override(self):
Barry Warsaw8c72eae2004-11-01 03:52:43 +0000220 eq = self.assertEqual
221 raises = self.assertRaises
Raymond Hettinger6d191112004-09-14 02:34:08 +0000222 class AmpersandTemplate(Template):
223 delimiter = '&'
224 s = AmpersandTemplate('this &gift is for &{who} &&')
Barry Warsaw8c72eae2004-11-01 03:52:43 +0000225 eq(s.substitute(gift='bud', who='you'), 'this bud is for you &')
226 raises(KeyError, s.substitute)
227 eq(s.safe_substitute(gift='bud', who='you'), 'this bud is for you &')
228 eq(s.safe_substitute(), 'this &gift is for &{who} &')
Raymond Hettinger6d191112004-09-14 02:34:08 +0000229 s = AmpersandTemplate('this &gift is for &{who} &')
Barry Warsaw8c72eae2004-11-01 03:52:43 +0000230 raises(ValueError, s.substitute, dict(gift='bud', who='you'))
231 eq(s.safe_substitute(), 'this &gift is for &{who} &')
232
Georg Brandl89fad142010-03-14 10:23:39 +0000233 class PieDelims(Template):
234 delimiter = '@'
235 s = PieDelims('@who likes to eat a bag of @{what} worth $100')
236 self.assertEqual(s.substitute(dict(who='tim', what='ham')),
237 'tim likes to eat a bag of ham worth $100')
238
Barry Warsaw8bee7612004-08-25 02:22:30 +0000239
240def test_main():
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000241 from test import support
Raymond Hettinger6d191112004-09-14 02:34:08 +0000242 test_classes = [TestTemplate,]
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000243 support.run_unittest(*test_classes)
Barry Warsaw8bee7612004-08-25 02:22:30 +0000244
245
246if __name__ == '__main__':
Raymond Hettinger6d191112004-09-14 02:34:08 +0000247 test_main()