blob: 119c7ea8a1f1c3c3f22775e4488af3163036acbf [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
Nick Coghlan7462fa62011-03-16 14:30:45 -040045 def test_invalid(self):
46 class MyPattern(Template):
47 pattern = r"""
48 (?:
49 (?P<invalid>) |
50 (?P<escaped>%(delim)s) |
51 @(?P<named>%(id)s) |
52 @{(?P<braced>%(id)s)}
53 )
54 """
55 s = MyPattern('$')
56 self.assertRaises(ValueError, s.substitute, dict())
57
Barry Warsaw8bee7612004-08-25 02:22:30 +000058 def test_percents(self):
Barry Warsaw12827c12004-09-10 03:08:08 +000059 eq = self.assertEqual
Barry Warsaw8bee7612004-08-25 02:22:30 +000060 s = Template('%(foo)s $foo ${foo}')
Barry Warsaw12827c12004-09-10 03:08:08 +000061 d = dict(foo='baz')
62 eq(s.substitute(d), '%(foo)s baz baz')
63 eq(s.safe_substitute(d), '%(foo)s baz baz')
Barry Warsaw8bee7612004-08-25 02:22:30 +000064
65 def test_stringification(self):
Barry Warsaw12827c12004-09-10 03:08:08 +000066 eq = self.assertEqual
Barry Warsaw8bee7612004-08-25 02:22:30 +000067 s = Template('tim has eaten $count bags of ham today')
Barry Warsaw12827c12004-09-10 03:08:08 +000068 d = dict(count=7)
69 eq(s.substitute(d), 'tim has eaten 7 bags of ham today')
70 eq(s.safe_substitute(d), 'tim has eaten 7 bags of ham today')
71 s = Template('tim has eaten ${count} bags of ham today')
72 eq(s.substitute(d), 'tim has eaten 7 bags of ham today')
Barry Warsaw8bee7612004-08-25 02:22:30 +000073
Thomas Wouters0e3f5912006-08-11 14:57:12 +000074 def test_tupleargs(self):
75 eq = self.assertEqual
76 s = Template('$who ate ${meal}')
77 d = dict(who=('tim', 'fred'), meal=('ham', 'kung pao'))
78 eq(s.substitute(d), "('tim', 'fred') ate ('ham', 'kung pao')")
79 eq(s.safe_substitute(d), "('tim', 'fred') ate ('ham', 'kung pao')")
80
Barry Warsaw8bee7612004-08-25 02:22:30 +000081 def test_SafeTemplate(self):
82 eq = self.assertEqual
Barry Warsaw12827c12004-09-10 03:08:08 +000083 s = Template('$who likes ${what} for ${meal}')
84 eq(s.safe_substitute(dict(who='tim')), 'tim likes ${what} for ${meal}')
85 eq(s.safe_substitute(dict(what='ham')), '$who likes ham for ${meal}')
86 eq(s.safe_substitute(dict(what='ham', meal='dinner')),
Barry Warsaw8bee7612004-08-25 02:22:30 +000087 '$who likes ham for dinner')
Barry Warsaw12827c12004-09-10 03:08:08 +000088 eq(s.safe_substitute(dict(who='tim', what='ham')),
Barry Warsaw8bee7612004-08-25 02:22:30 +000089 'tim likes ham for ${meal}')
Barry Warsaw12827c12004-09-10 03:08:08 +000090 eq(s.safe_substitute(dict(who='tim', what='ham', meal='dinner')),
Barry Warsaw8bee7612004-08-25 02:22:30 +000091 'tim likes ham for dinner')
92
93 def test_invalid_placeholders(self):
94 raises = self.assertRaises
95 s = Template('$who likes $')
Barry Warsaw12827c12004-09-10 03:08:08 +000096 raises(ValueError, s.substitute, dict(who='tim'))
Barry Warsaw8bee7612004-08-25 02:22:30 +000097 s = Template('$who likes ${what)')
Barry Warsaw12827c12004-09-10 03:08:08 +000098 raises(ValueError, s.substitute, dict(who='tim'))
Barry Warsaw8bee7612004-08-25 02:22:30 +000099 s = Template('$who likes $100')
Barry Warsaw12827c12004-09-10 03:08:08 +0000100 raises(ValueError, s.substitute, dict(who='tim'))
101
Barry Warsaw12827c12004-09-10 03:08:08 +0000102 def test_idpattern_override(self):
103 class PathPattern(Template):
104 idpattern = r'[_a-z][._a-z0-9]*'
105 m = Mapping()
106 m.bag = Bag()
107 m.bag.foo = Bag()
108 m.bag.foo.who = 'tim'
109 m.bag.what = 'ham'
110 s = PathPattern('$bag.foo.who likes to eat a bag of $bag.what')
111 self.assertEqual(s.substitute(m), 'tim likes to eat a bag of ham')
112
113 def test_pattern_override(self):
114 class MyPattern(Template):
115 pattern = r"""
116 (?P<escaped>@{2}) |
117 @(?P<named>[_a-z][._a-z0-9]*) |
118 @{(?P<braced>[_a-z][._a-z0-9]*)} |
Barry Warsaw3e773fb2004-09-13 20:53:27 +0000119 (?P<invalid>@)
Barry Warsaw12827c12004-09-10 03:08:08 +0000120 """
121 m = Mapping()
122 m.bag = Bag()
123 m.bag.foo = Bag()
124 m.bag.foo.who = 'tim'
125 m.bag.what = 'ham'
126 s = MyPattern('@bag.foo.who likes to eat a bag of @bag.what')
127 self.assertEqual(s.substitute(m), 'tim likes to eat a bag of ham')
128
Neal Norwitz6627a962004-10-17 16:27:18 +0000129 class BadPattern(Template):
130 pattern = r"""
131 (?P<badname>.*) |
132 (?P<escaped>@{2}) |
133 @(?P<named>[_a-z][._a-z0-9]*) |
134 @{(?P<braced>[_a-z][._a-z0-9]*)} |
135 (?P<invalid>@) |
136 """
137 s = BadPattern('@bag.foo.who likes to eat a bag of @bag.what')
138 self.assertRaises(ValueError, s.substitute, {})
139 self.assertRaises(ValueError, s.safe_substitute, {})
140
Florent Xiclunaeb19dce2010-09-18 23:34:07 +0000141 def test_braced_override(self):
142 class MyTemplate(Template):
143 pattern = r"""
144 \$(?:
145 (?P<escaped>$) |
146 (?P<named>[_a-z][_a-z0-9]*) |
147 @@(?P<braced>[_a-z][_a-z0-9]*)@@ |
148 (?P<invalid>) |
149 )
150 """
151
152 tmpl = 'PyCon in $@@location@@'
153 t = MyTemplate(tmpl)
154 self.assertRaises(KeyError, t.substitute, {})
155 val = t.substitute({'location': 'Cleveland'})
156 self.assertEqual(val, 'PyCon in Cleveland')
157
158 def test_braced_override_safe(self):
159 class MyTemplate(Template):
160 pattern = r"""
161 \$(?:
162 (?P<escaped>$) |
163 (?P<named>[_a-z][_a-z0-9]*) |
164 @@(?P<braced>[_a-z][_a-z0-9]*)@@ |
165 (?P<invalid>) |
166 )
167 """
168
169 tmpl = 'PyCon in $@@location@@'
170 t = MyTemplate(tmpl)
171 self.assertEqual(t.safe_substitute(), tmpl)
172 val = t.safe_substitute({'location': 'Cleveland'})
173 self.assertEqual(val, 'PyCon in Cleveland')
174
Barry Warsaw12827c12004-09-10 03:08:08 +0000175 def test_unicode_values(self):
176 s = Template('$who likes $what')
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000177 d = dict(who='t\xffm', what='f\xfe\fed')
178 self.assertEqual(s.substitute(d), 't\xffm likes f\xfe\x0ced')
Barry Warsaw8bee7612004-08-25 02:22:30 +0000179
Barry Warsaw302bd582004-09-13 14:35:59 +0000180 def test_keyword_arguments(self):
181 eq = self.assertEqual
182 s = Template('$who likes $what')
183 eq(s.substitute(who='tim', what='ham'), 'tim likes ham')
184 eq(s.substitute(dict(who='tim'), what='ham'), 'tim likes ham')
185 eq(s.substitute(dict(who='fred', what='kung pao'),
186 who='tim', what='ham'),
187 'tim likes ham')
188 s = Template('the mapping is $mapping')
189 eq(s.substitute(dict(foo='none'), mapping='bozo'),
190 'the mapping is bozo')
191 eq(s.substitute(dict(mapping='one'), mapping='two'),
192 'the mapping is two')
193
194 def test_keyword_arguments_safe(self):
195 eq = self.assertEqual
Barry Warsawc7cd20c2004-09-13 15:24:43 +0000196 raises = self.assertRaises
Barry Warsaw302bd582004-09-13 14:35:59 +0000197 s = Template('$who likes $what')
198 eq(s.safe_substitute(who='tim', what='ham'), 'tim likes ham')
199 eq(s.safe_substitute(dict(who='tim'), what='ham'), 'tim likes ham')
200 eq(s.safe_substitute(dict(who='fred', what='kung pao'),
201 who='tim', what='ham'),
202 'tim likes ham')
203 s = Template('the mapping is $mapping')
204 eq(s.safe_substitute(dict(foo='none'), mapping='bozo'),
205 'the mapping is bozo')
206 eq(s.safe_substitute(dict(mapping='one'), mapping='two'),
207 'the mapping is two')
Barry Warsawc7cd20c2004-09-13 15:24:43 +0000208 d = dict(mapping='one')
209 raises(TypeError, s.substitute, d, {})
210 raises(TypeError, s.safe_substitute, d, {})
Barry Warsaw302bd582004-09-13 14:35:59 +0000211
Raymond Hettinger6d191112004-09-14 02:34:08 +0000212 def test_delimiter_override(self):
Barry Warsaw8c72eae2004-11-01 03:52:43 +0000213 eq = self.assertEqual
214 raises = self.assertRaises
Raymond Hettinger6d191112004-09-14 02:34:08 +0000215 class AmpersandTemplate(Template):
216 delimiter = '&'
217 s = AmpersandTemplate('this &gift is for &{who} &&')
Barry Warsaw8c72eae2004-11-01 03:52:43 +0000218 eq(s.substitute(gift='bud', who='you'), 'this bud is for you &')
219 raises(KeyError, s.substitute)
220 eq(s.safe_substitute(gift='bud', who='you'), 'this bud is for you &')
221 eq(s.safe_substitute(), 'this &gift is for &{who} &')
Raymond Hettinger6d191112004-09-14 02:34:08 +0000222 s = AmpersandTemplate('this &gift is for &{who} &')
Barry Warsaw8c72eae2004-11-01 03:52:43 +0000223 raises(ValueError, s.substitute, dict(gift='bud', who='you'))
224 eq(s.safe_substitute(), 'this &gift is for &{who} &')
225
Georg Brandl89fad142010-03-14 10:23:39 +0000226 class PieDelims(Template):
227 delimiter = '@'
228 s = PieDelims('@who likes to eat a bag of @{what} worth $100')
229 self.assertEqual(s.substitute(dict(who='tim', what='ham')),
230 'tim likes to eat a bag of ham worth $100')
231
Barry Warsaw8bee7612004-08-25 02:22:30 +0000232
233def test_main():
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000234 from test import support
Raymond Hettinger6d191112004-09-14 02:34:08 +0000235 test_classes = [TestTemplate,]
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000236 support.run_unittest(*test_classes)
Barry Warsaw8bee7612004-08-25 02:22:30 +0000237
238
239if __name__ == '__main__':
Raymond Hettinger6d191112004-09-14 02:34:08 +0000240 test_main()