blob: fd5256c77405e4f8e5b24ac531f05618d0a686d5 [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'))
Serhiy Storchaka8ffe9172015-03-24 22:28:43 +020029 self.assertRaises(TypeError, Template.substitute)
Barry Warsaw8bee7612004-08-25 02:22:30 +000030
31 def test_regular_templates_with_braces(self):
32 s = Template('$who likes ${what} for ${meal}')
Barry Warsaw12827c12004-09-10 03:08:08 +000033 d = dict(who='tim', what='ham', meal='dinner')
34 self.assertEqual(s.substitute(d), 'tim likes ham for dinner')
35 self.assertRaises(KeyError, s.substitute,
36 dict(who='tim', what='ham'))
Barry Warsaw8bee7612004-08-25 02:22:30 +000037
38 def test_escapes(self):
39 eq = self.assertEqual
40 s = Template('$who likes to eat a bag of $$what worth $$100')
Barry Warsaw12827c12004-09-10 03:08:08 +000041 eq(s.substitute(dict(who='tim', what='ham')),
Barry Warsaw8bee7612004-08-25 02:22:30 +000042 'tim likes to eat a bag of $what worth $100')
43 s = Template('$who likes $$')
Barry Warsaw12827c12004-09-10 03:08:08 +000044 eq(s.substitute(dict(who='tim', what='ham')), 'tim likes $')
Barry Warsaw8bee7612004-08-25 02:22:30 +000045
46 def test_percents(self):
Barry Warsaw12827c12004-09-10 03:08:08 +000047 eq = self.assertEqual
Barry Warsaw8bee7612004-08-25 02:22:30 +000048 s = Template('%(foo)s $foo ${foo}')
Barry Warsaw12827c12004-09-10 03:08:08 +000049 d = dict(foo='baz')
50 eq(s.substitute(d), '%(foo)s baz baz')
51 eq(s.safe_substitute(d), '%(foo)s baz baz')
Barry Warsaw8bee7612004-08-25 02:22:30 +000052
53 def test_stringification(self):
Barry Warsaw12827c12004-09-10 03:08:08 +000054 eq = self.assertEqual
Barry Warsaw8bee7612004-08-25 02:22:30 +000055 s = Template('tim has eaten $count bags of ham today')
Barry Warsaw12827c12004-09-10 03:08:08 +000056 d = dict(count=7)
57 eq(s.substitute(d), 'tim has eaten 7 bags of ham today')
58 eq(s.safe_substitute(d), 'tim has eaten 7 bags of ham today')
59 s = Template('tim has eaten ${count} bags of ham today')
60 eq(s.substitute(d), 'tim has eaten 7 bags of ham today')
Barry Warsaw8bee7612004-08-25 02:22:30 +000061
Thomas Wouters0e3f5912006-08-11 14:57:12 +000062 def test_tupleargs(self):
63 eq = self.assertEqual
64 s = Template('$who ate ${meal}')
65 d = dict(who=('tim', 'fred'), meal=('ham', 'kung pao'))
66 eq(s.substitute(d), "('tim', 'fred') ate ('ham', 'kung pao')")
67 eq(s.safe_substitute(d), "('tim', 'fred') ate ('ham', 'kung pao')")
68
Barry Warsaw8bee7612004-08-25 02:22:30 +000069 def test_SafeTemplate(self):
70 eq = self.assertEqual
Barry Warsaw12827c12004-09-10 03:08:08 +000071 s = Template('$who likes ${what} for ${meal}')
72 eq(s.safe_substitute(dict(who='tim')), 'tim likes ${what} for ${meal}')
73 eq(s.safe_substitute(dict(what='ham')), '$who likes ham for ${meal}')
74 eq(s.safe_substitute(dict(what='ham', meal='dinner')),
Barry Warsaw8bee7612004-08-25 02:22:30 +000075 '$who likes ham for dinner')
Barry Warsaw12827c12004-09-10 03:08:08 +000076 eq(s.safe_substitute(dict(who='tim', what='ham')),
Barry Warsaw8bee7612004-08-25 02:22:30 +000077 'tim likes ham for ${meal}')
Barry Warsaw12827c12004-09-10 03:08:08 +000078 eq(s.safe_substitute(dict(who='tim', what='ham', meal='dinner')),
Barry Warsaw8bee7612004-08-25 02:22:30 +000079 'tim likes ham for dinner')
80
81 def test_invalid_placeholders(self):
82 raises = self.assertRaises
83 s = Template('$who likes $')
Barry Warsaw12827c12004-09-10 03:08:08 +000084 raises(ValueError, s.substitute, dict(who='tim'))
Barry Warsaw8bee7612004-08-25 02:22:30 +000085 s = Template('$who likes ${what)')
Barry Warsaw12827c12004-09-10 03:08:08 +000086 raises(ValueError, s.substitute, dict(who='tim'))
Barry Warsaw8bee7612004-08-25 02:22:30 +000087 s = Template('$who likes $100')
Barry Warsaw12827c12004-09-10 03:08:08 +000088 raises(ValueError, s.substitute, dict(who='tim'))
89
Barry Warsaw12827c12004-09-10 03:08:08 +000090 def test_idpattern_override(self):
91 class PathPattern(Template):
92 idpattern = r'[_a-z][._a-z0-9]*'
93 m = Mapping()
94 m.bag = Bag()
95 m.bag.foo = Bag()
96 m.bag.foo.who = 'tim'
97 m.bag.what = 'ham'
98 s = PathPattern('$bag.foo.who likes to eat a bag of $bag.what')
99 self.assertEqual(s.substitute(m), 'tim likes to eat a bag of ham')
100
101 def test_pattern_override(self):
102 class MyPattern(Template):
103 pattern = r"""
104 (?P<escaped>@{2}) |
105 @(?P<named>[_a-z][._a-z0-9]*) |
106 @{(?P<braced>[_a-z][._a-z0-9]*)} |
Barry Warsaw3e773fb2004-09-13 20:53:27 +0000107 (?P<invalid>@)
Barry Warsaw12827c12004-09-10 03:08:08 +0000108 """
109 m = Mapping()
110 m.bag = Bag()
111 m.bag.foo = Bag()
112 m.bag.foo.who = 'tim'
113 m.bag.what = 'ham'
114 s = MyPattern('@bag.foo.who likes to eat a bag of @bag.what')
115 self.assertEqual(s.substitute(m), 'tim likes to eat a bag of ham')
116
Neal Norwitz6627a962004-10-17 16:27:18 +0000117 class BadPattern(Template):
118 pattern = r"""
119 (?P<badname>.*) |
120 (?P<escaped>@{2}) |
121 @(?P<named>[_a-z][._a-z0-9]*) |
122 @{(?P<braced>[_a-z][._a-z0-9]*)} |
123 (?P<invalid>@) |
124 """
125 s = BadPattern('@bag.foo.who likes to eat a bag of @bag.what')
126 self.assertRaises(ValueError, s.substitute, {})
127 self.assertRaises(ValueError, s.safe_substitute, {})
128
Florent Xiclunaeb19dce2010-09-18 23:34:07 +0000129 def test_braced_override(self):
130 class MyTemplate(Template):
131 pattern = r"""
132 \$(?:
133 (?P<escaped>$) |
134 (?P<named>[_a-z][_a-z0-9]*) |
135 @@(?P<braced>[_a-z][_a-z0-9]*)@@ |
136 (?P<invalid>) |
137 )
138 """
139
140 tmpl = 'PyCon in $@@location@@'
141 t = MyTemplate(tmpl)
142 self.assertRaises(KeyError, t.substitute, {})
143 val = t.substitute({'location': 'Cleveland'})
144 self.assertEqual(val, 'PyCon in Cleveland')
145
146 def test_braced_override_safe(self):
147 class MyTemplate(Template):
148 pattern = r"""
149 \$(?:
150 (?P<escaped>$) |
151 (?P<named>[_a-z][_a-z0-9]*) |
152 @@(?P<braced>[_a-z][_a-z0-9]*)@@ |
153 (?P<invalid>) |
154 )
155 """
156
157 tmpl = 'PyCon in $@@location@@'
158 t = MyTemplate(tmpl)
159 self.assertEqual(t.safe_substitute(), tmpl)
160 val = t.safe_substitute({'location': 'Cleveland'})
161 self.assertEqual(val, 'PyCon in Cleveland')
162
Nick Coghlan62ecb6a2011-05-31 19:40:11 +1000163 def test_invalid_with_no_lines(self):
164 # The error formatting for invalid templates
165 # has a special case for no data that the default
166 # pattern can't trigger (always has at least '$')
167 # So we craft a pattern that is always invalid
168 # with no leading data.
169 class MyTemplate(Template):
170 pattern = r"""
171 (?P<invalid>) |
172 unreachable(
173 (?P<named>) |
174 (?P<braced>) |
175 (?P<escaped>)
176 )
177 """
178 s = MyTemplate('')
179 with self.assertRaises(ValueError) as err:
180 s.substitute({})
181 self.assertIn('line 1, col 1', str(err.exception))
182
Barry Warsaw12827c12004-09-10 03:08:08 +0000183 def test_unicode_values(self):
184 s = Template('$who likes $what')
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000185 d = dict(who='t\xffm', what='f\xfe\fed')
186 self.assertEqual(s.substitute(d), 't\xffm likes f\xfe\x0ced')
Barry Warsaw8bee7612004-08-25 02:22:30 +0000187
Barry Warsaw302bd582004-09-13 14:35:59 +0000188 def test_keyword_arguments(self):
189 eq = self.assertEqual
190 s = Template('$who likes $what')
191 eq(s.substitute(who='tim', what='ham'), 'tim likes ham')
192 eq(s.substitute(dict(who='tim'), what='ham'), 'tim likes ham')
193 eq(s.substitute(dict(who='fred', what='kung pao'),
194 who='tim', what='ham'),
195 'tim likes ham')
196 s = Template('the mapping is $mapping')
197 eq(s.substitute(dict(foo='none'), mapping='bozo'),
198 'the mapping is bozo')
199 eq(s.substitute(dict(mapping='one'), mapping='two'),
200 'the mapping is two')
201
Serhiy Storchaka8ffe9172015-03-24 22:28:43 +0200202 s = Template('the self is $self')
203 eq(s.substitute(self='bozo'), 'the self is bozo')
204
Barry Warsaw302bd582004-09-13 14:35:59 +0000205 def test_keyword_arguments_safe(self):
206 eq = self.assertEqual
Barry Warsawc7cd20c2004-09-13 15:24:43 +0000207 raises = self.assertRaises
Barry Warsaw302bd582004-09-13 14:35:59 +0000208 s = Template('$who likes $what')
209 eq(s.safe_substitute(who='tim', what='ham'), 'tim likes ham')
210 eq(s.safe_substitute(dict(who='tim'), what='ham'), 'tim likes ham')
211 eq(s.safe_substitute(dict(who='fred', what='kung pao'),
212 who='tim', what='ham'),
213 'tim likes ham')
214 s = Template('the mapping is $mapping')
215 eq(s.safe_substitute(dict(foo='none'), mapping='bozo'),
216 'the mapping is bozo')
217 eq(s.safe_substitute(dict(mapping='one'), mapping='two'),
218 'the mapping is two')
Barry Warsawc7cd20c2004-09-13 15:24:43 +0000219 d = dict(mapping='one')
220 raises(TypeError, s.substitute, d, {})
221 raises(TypeError, s.safe_substitute, d, {})
Barry Warsaw302bd582004-09-13 14:35:59 +0000222
Serhiy Storchaka8ffe9172015-03-24 22:28:43 +0200223 s = Template('the self is $self')
224 eq(s.safe_substitute(self='bozo'), 'the self is bozo')
225
Raymond Hettinger6d191112004-09-14 02:34:08 +0000226 def test_delimiter_override(self):
Barry Warsaw8c72eae2004-11-01 03:52:43 +0000227 eq = self.assertEqual
228 raises = self.assertRaises
Raymond Hettinger6d191112004-09-14 02:34:08 +0000229 class AmpersandTemplate(Template):
230 delimiter = '&'
231 s = AmpersandTemplate('this &gift is for &{who} &&')
Barry Warsaw8c72eae2004-11-01 03:52:43 +0000232 eq(s.substitute(gift='bud', who='you'), 'this bud is for you &')
233 raises(KeyError, s.substitute)
234 eq(s.safe_substitute(gift='bud', who='you'), 'this bud is for you &')
235 eq(s.safe_substitute(), 'this &gift is for &{who} &')
Raymond Hettinger6d191112004-09-14 02:34:08 +0000236 s = AmpersandTemplate('this &gift is for &{who} &')
Barry Warsaw8c72eae2004-11-01 03:52:43 +0000237 raises(ValueError, s.substitute, dict(gift='bud', who='you'))
238 eq(s.safe_substitute(), 'this &gift is for &{who} &')
239
Georg Brandl89fad142010-03-14 10:23:39 +0000240 class PieDelims(Template):
241 delimiter = '@'
242 s = PieDelims('@who likes to eat a bag of @{what} worth $100')
243 self.assertEqual(s.substitute(dict(who='tim', what='ham')),
244 'tim likes to eat a bag of ham worth $100')
245
Barry Warsaw8bee7612004-08-25 02:22:30 +0000246
247def test_main():
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000248 from test import support
Raymond Hettinger6d191112004-09-14 02:34:08 +0000249 test_classes = [TestTemplate,]
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000250 support.run_unittest(*test_classes)
Barry Warsaw8bee7612004-08-25 02:22:30 +0000251
252
253if __name__ == '__main__':
Raymond Hettinger6d191112004-09-14 02:34:08 +0000254 test_main()