blob: 133cab7cfda36c57afe17879208ce0a1981ab842 [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 Storchaka40fd0e82015-03-24 22:27:50 +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 Woutersadd19112006-07-05 11:03:49 +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 Xiclunaff05e522010-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
Barry Warsaw12827c12004-09-10 03:08:08 +0000163 def test_unicode_values(self):
164 s = Template('$who likes $what')
165 d = dict(who=u't\xffm', what=u'f\xfe\fed')
166 self.assertEqual(s.substitute(d), u't\xffm likes f\xfe\x0ced')
Barry Warsaw8bee7612004-08-25 02:22:30 +0000167
Barry Warsaw302bd582004-09-13 14:35:59 +0000168 def test_keyword_arguments(self):
169 eq = self.assertEqual
170 s = Template('$who likes $what')
171 eq(s.substitute(who='tim', what='ham'), 'tim likes ham')
172 eq(s.substitute(dict(who='tim'), what='ham'), 'tim likes ham')
173 eq(s.substitute(dict(who='fred', what='kung pao'),
174 who='tim', what='ham'),
175 'tim likes ham')
176 s = Template('the mapping is $mapping')
177 eq(s.substitute(dict(foo='none'), mapping='bozo'),
178 'the mapping is bozo')
179 eq(s.substitute(dict(mapping='one'), mapping='two'),
180 'the mapping is two')
181
Serhiy Storchaka40fd0e82015-03-24 22:27:50 +0200182 s = Template('the self is $self')
183 eq(s.substitute(self='bozo'), 'the self is bozo')
184
Barry Warsaw302bd582004-09-13 14:35:59 +0000185 def test_keyword_arguments_safe(self):
186 eq = self.assertEqual
Barry Warsawc7cd20c2004-09-13 15:24:43 +0000187 raises = self.assertRaises
Barry Warsaw302bd582004-09-13 14:35:59 +0000188 s = Template('$who likes $what')
189 eq(s.safe_substitute(who='tim', what='ham'), 'tim likes ham')
190 eq(s.safe_substitute(dict(who='tim'), what='ham'), 'tim likes ham')
191 eq(s.safe_substitute(dict(who='fred', what='kung pao'),
192 who='tim', what='ham'),
193 'tim likes ham')
194 s = Template('the mapping is $mapping')
195 eq(s.safe_substitute(dict(foo='none'), mapping='bozo'),
196 'the mapping is bozo')
197 eq(s.safe_substitute(dict(mapping='one'), mapping='two'),
198 'the mapping is two')
Barry Warsawc7cd20c2004-09-13 15:24:43 +0000199 d = dict(mapping='one')
200 raises(TypeError, s.substitute, d, {})
201 raises(TypeError, s.safe_substitute, d, {})
Barry Warsaw302bd582004-09-13 14:35:59 +0000202
Serhiy Storchaka40fd0e82015-03-24 22:27:50 +0200203 s = Template('the self is $self')
204 eq(s.safe_substitute(self='bozo'), 'the self is bozo')
205
Raymond Hettinger6d191112004-09-14 02:34:08 +0000206 def test_delimiter_override(self):
Barry Warsaw8c72eae2004-11-01 03:52:43 +0000207 eq = self.assertEqual
208 raises = self.assertRaises
Raymond Hettinger6d191112004-09-14 02:34:08 +0000209 class AmpersandTemplate(Template):
210 delimiter = '&'
211 s = AmpersandTemplate('this &gift is for &{who} &&')
Barry Warsaw8c72eae2004-11-01 03:52:43 +0000212 eq(s.substitute(gift='bud', who='you'), 'this bud is for you &')
213 raises(KeyError, s.substitute)
214 eq(s.safe_substitute(gift='bud', who='you'), 'this bud is for you &')
215 eq(s.safe_substitute(), 'this &gift is for &{who} &')
Raymond Hettinger6d191112004-09-14 02:34:08 +0000216 s = AmpersandTemplate('this &gift is for &{who} &')
Barry Warsaw8c72eae2004-11-01 03:52:43 +0000217 raises(ValueError, s.substitute, dict(gift='bud', who='you'))
218 eq(s.safe_substitute(), 'this &gift is for &{who} &')
219
Georg Brandl7ae60182010-02-07 12:16:12 +0000220 class PieDelims(Template):
221 delimiter = '@'
222 s = PieDelims('@who likes to eat a bag of @{what} worth $100')
223 self.assertEqual(s.substitute(dict(who='tim', what='ham')),
224 'tim likes to eat a bag of ham worth $100')
225
Barry Warsaw8bee7612004-08-25 02:22:30 +0000226
227def test_main():
228 from test import test_support
Raymond Hettinger6d191112004-09-14 02:34:08 +0000229 test_classes = [TestTemplate,]
230 test_support.run_unittest(*test_classes)
Barry Warsaw8bee7612004-08-25 02:22:30 +0000231
232
233if __name__ == '__main__':
Raymond Hettinger6d191112004-09-14 02:34:08 +0000234 test_main()