1 | n/a | # Copyright (C) 2004 Python Software Foundation |
---|
2 | n/a | # Author: barry@python.org (Barry Warsaw) |
---|
3 | n/a | # License: http://www.opensource.org/licenses/PythonSoftFoundation.php |
---|
4 | n/a | |
---|
5 | n/a | import unittest |
---|
6 | n/a | from string import Template |
---|
7 | n/a | |
---|
8 | n/a | |
---|
9 | n/a | class Bag: |
---|
10 | n/a | pass |
---|
11 | n/a | |
---|
12 | n/a | class Mapping: |
---|
13 | n/a | def __getitem__(self, name): |
---|
14 | n/a | obj = self |
---|
15 | n/a | for part in name.split('.'): |
---|
16 | n/a | try: |
---|
17 | n/a | obj = getattr(obj, part) |
---|
18 | n/a | except AttributeError: |
---|
19 | n/a | raise KeyError(name) |
---|
20 | n/a | return obj |
---|
21 | n/a | |
---|
22 | n/a | |
---|
23 | n/a | class TestTemplate(unittest.TestCase): |
---|
24 | n/a | def test_regular_templates(self): |
---|
25 | n/a | s = Template('$who likes to eat a bag of $what worth $$100') |
---|
26 | n/a | self.assertEqual(s.substitute(dict(who='tim', what='ham')), |
---|
27 | n/a | 'tim likes to eat a bag of ham worth $100') |
---|
28 | n/a | self.assertRaises(KeyError, s.substitute, dict(who='tim')) |
---|
29 | n/a | |
---|
30 | n/a | def test_regular_templates_with_braces(self): |
---|
31 | n/a | s = Template('$who likes ${what} for ${meal}') |
---|
32 | n/a | d = dict(who='tim', what='ham', meal='dinner') |
---|
33 | n/a | self.assertEqual(s.substitute(d), 'tim likes ham for dinner') |
---|
34 | n/a | self.assertRaises(KeyError, s.substitute, |
---|
35 | n/a | dict(who='tim', what='ham')) |
---|
36 | n/a | |
---|
37 | n/a | def test_escapes(self): |
---|
38 | n/a | eq = self.assertEqual |
---|
39 | n/a | s = Template('$who likes to eat a bag of $$what worth $$100') |
---|
40 | n/a | eq(s.substitute(dict(who='tim', what='ham')), |
---|
41 | n/a | 'tim likes to eat a bag of $what worth $100') |
---|
42 | n/a | s = Template('$who likes $$') |
---|
43 | n/a | eq(s.substitute(dict(who='tim', what='ham')), 'tim likes $') |
---|
44 | n/a | |
---|
45 | n/a | def test_percents(self): |
---|
46 | n/a | eq = self.assertEqual |
---|
47 | n/a | s = Template('%(foo)s $foo ${foo}') |
---|
48 | n/a | d = dict(foo='baz') |
---|
49 | n/a | eq(s.substitute(d), '%(foo)s baz baz') |
---|
50 | n/a | eq(s.safe_substitute(d), '%(foo)s baz baz') |
---|
51 | n/a | |
---|
52 | n/a | def test_stringification(self): |
---|
53 | n/a | eq = self.assertEqual |
---|
54 | n/a | s = Template('tim has eaten $count bags of ham today') |
---|
55 | n/a | d = dict(count=7) |
---|
56 | n/a | eq(s.substitute(d), 'tim has eaten 7 bags of ham today') |
---|
57 | n/a | eq(s.safe_substitute(d), 'tim has eaten 7 bags of ham today') |
---|
58 | n/a | s = Template('tim has eaten ${count} bags of ham today') |
---|
59 | n/a | eq(s.substitute(d), 'tim has eaten 7 bags of ham today') |
---|
60 | n/a | |
---|
61 | n/a | def test_tupleargs(self): |
---|
62 | n/a | eq = self.assertEqual |
---|
63 | n/a | s = Template('$who ate ${meal}') |
---|
64 | n/a | d = dict(who=('tim', 'fred'), meal=('ham', 'kung pao')) |
---|
65 | n/a | eq(s.substitute(d), "('tim', 'fred') ate ('ham', 'kung pao')") |
---|
66 | n/a | eq(s.safe_substitute(d), "('tim', 'fred') ate ('ham', 'kung pao')") |
---|
67 | n/a | |
---|
68 | n/a | def test_SafeTemplate(self): |
---|
69 | n/a | eq = self.assertEqual |
---|
70 | n/a | s = Template('$who likes ${what} for ${meal}') |
---|
71 | n/a | eq(s.safe_substitute(dict(who='tim')), 'tim likes ${what} for ${meal}') |
---|
72 | n/a | eq(s.safe_substitute(dict(what='ham')), '$who likes ham for ${meal}') |
---|
73 | n/a | eq(s.safe_substitute(dict(what='ham', meal='dinner')), |
---|
74 | n/a | '$who likes ham for dinner') |
---|
75 | n/a | eq(s.safe_substitute(dict(who='tim', what='ham')), |
---|
76 | n/a | 'tim likes ham for ${meal}') |
---|
77 | n/a | eq(s.safe_substitute(dict(who='tim', what='ham', meal='dinner')), |
---|
78 | n/a | 'tim likes ham for dinner') |
---|
79 | n/a | |
---|
80 | n/a | def test_invalid_placeholders(self): |
---|
81 | n/a | raises = self.assertRaises |
---|
82 | n/a | s = Template('$who likes $') |
---|
83 | n/a | raises(ValueError, s.substitute, dict(who='tim')) |
---|
84 | n/a | s = Template('$who likes ${what)') |
---|
85 | n/a | raises(ValueError, s.substitute, dict(who='tim')) |
---|
86 | n/a | s = Template('$who likes $100') |
---|
87 | n/a | raises(ValueError, s.substitute, dict(who='tim')) |
---|
88 | n/a | |
---|
89 | n/a | def test_idpattern_override(self): |
---|
90 | n/a | class PathPattern(Template): |
---|
91 | n/a | idpattern = r'[_a-z][._a-z0-9]*' |
---|
92 | n/a | m = Mapping() |
---|
93 | n/a | m.bag = Bag() |
---|
94 | n/a | m.bag.foo = Bag() |
---|
95 | n/a | m.bag.foo.who = 'tim' |
---|
96 | n/a | m.bag.what = 'ham' |
---|
97 | n/a | s = PathPattern('$bag.foo.who likes to eat a bag of $bag.what') |
---|
98 | n/a | self.assertEqual(s.substitute(m), 'tim likes to eat a bag of ham') |
---|
99 | n/a | |
---|
100 | n/a | def test_pattern_override(self): |
---|
101 | n/a | class MyPattern(Template): |
---|
102 | n/a | pattern = r""" |
---|
103 | n/a | (?P<escaped>@{2}) | |
---|
104 | n/a | @(?P<named>[_a-z][._a-z0-9]*) | |
---|
105 | n/a | @{(?P<braced>[_a-z][._a-z0-9]*)} | |
---|
106 | n/a | (?P<invalid>@) |
---|
107 | n/a | """ |
---|
108 | n/a | m = Mapping() |
---|
109 | n/a | m.bag = Bag() |
---|
110 | n/a | m.bag.foo = Bag() |
---|
111 | n/a | m.bag.foo.who = 'tim' |
---|
112 | n/a | m.bag.what = 'ham' |
---|
113 | n/a | s = MyPattern('@bag.foo.who likes to eat a bag of @bag.what') |
---|
114 | n/a | self.assertEqual(s.substitute(m), 'tim likes to eat a bag of ham') |
---|
115 | n/a | |
---|
116 | n/a | class BadPattern(Template): |
---|
117 | n/a | pattern = r""" |
---|
118 | n/a | (?P<badname>.*) | |
---|
119 | n/a | (?P<escaped>@{2}) | |
---|
120 | n/a | @(?P<named>[_a-z][._a-z0-9]*) | |
---|
121 | n/a | @{(?P<braced>[_a-z][._a-z0-9]*)} | |
---|
122 | n/a | (?P<invalid>@) | |
---|
123 | n/a | """ |
---|
124 | n/a | s = BadPattern('@bag.foo.who likes to eat a bag of @bag.what') |
---|
125 | n/a | self.assertRaises(ValueError, s.substitute, {}) |
---|
126 | n/a | self.assertRaises(ValueError, s.safe_substitute, {}) |
---|
127 | n/a | |
---|
128 | n/a | def test_braced_override(self): |
---|
129 | n/a | class MyTemplate(Template): |
---|
130 | n/a | pattern = r""" |
---|
131 | n/a | \$(?: |
---|
132 | n/a | (?P<escaped>$) | |
---|
133 | n/a | (?P<named>[_a-z][_a-z0-9]*) | |
---|
134 | n/a | @@(?P<braced>[_a-z][_a-z0-9]*)@@ | |
---|
135 | n/a | (?P<invalid>) | |
---|
136 | n/a | ) |
---|
137 | n/a | """ |
---|
138 | n/a | |
---|
139 | n/a | tmpl = 'PyCon in $@@location@@' |
---|
140 | n/a | t = MyTemplate(tmpl) |
---|
141 | n/a | self.assertRaises(KeyError, t.substitute, {}) |
---|
142 | n/a | val = t.substitute({'location': 'Cleveland'}) |
---|
143 | n/a | self.assertEqual(val, 'PyCon in Cleveland') |
---|
144 | n/a | |
---|
145 | n/a | def test_braced_override_safe(self): |
---|
146 | n/a | class MyTemplate(Template): |
---|
147 | n/a | pattern = r""" |
---|
148 | n/a | \$(?: |
---|
149 | n/a | (?P<escaped>$) | |
---|
150 | n/a | (?P<named>[_a-z][_a-z0-9]*) | |
---|
151 | n/a | @@(?P<braced>[_a-z][_a-z0-9]*)@@ | |
---|
152 | n/a | (?P<invalid>) | |
---|
153 | n/a | ) |
---|
154 | n/a | """ |
---|
155 | n/a | |
---|
156 | n/a | tmpl = 'PyCon in $@@location@@' |
---|
157 | n/a | t = MyTemplate(tmpl) |
---|
158 | n/a | self.assertEqual(t.safe_substitute(), tmpl) |
---|
159 | n/a | val = t.safe_substitute({'location': 'Cleveland'}) |
---|
160 | n/a | self.assertEqual(val, 'PyCon in Cleveland') |
---|
161 | n/a | |
---|
162 | n/a | def test_invalid_with_no_lines(self): |
---|
163 | n/a | # The error formatting for invalid templates |
---|
164 | n/a | # has a special case for no data that the default |
---|
165 | n/a | # pattern can't trigger (always has at least '$') |
---|
166 | n/a | # So we craft a pattern that is always invalid |
---|
167 | n/a | # with no leading data. |
---|
168 | n/a | class MyTemplate(Template): |
---|
169 | n/a | pattern = r""" |
---|
170 | n/a | (?P<invalid>) | |
---|
171 | n/a | unreachable( |
---|
172 | n/a | (?P<named>) | |
---|
173 | n/a | (?P<braced>) | |
---|
174 | n/a | (?P<escaped>) |
---|
175 | n/a | ) |
---|
176 | n/a | """ |
---|
177 | n/a | s = MyTemplate('') |
---|
178 | n/a | with self.assertRaises(ValueError) as err: |
---|
179 | n/a | s.substitute({}) |
---|
180 | n/a | self.assertIn('line 1, col 1', str(err.exception)) |
---|
181 | n/a | |
---|
182 | n/a | def test_unicode_values(self): |
---|
183 | n/a | s = Template('$who likes $what') |
---|
184 | n/a | d = dict(who='t\xffm', what='f\xfe\fed') |
---|
185 | n/a | self.assertEqual(s.substitute(d), 't\xffm likes f\xfe\x0ced') |
---|
186 | n/a | |
---|
187 | n/a | def test_keyword_arguments(self): |
---|
188 | n/a | eq = self.assertEqual |
---|
189 | n/a | s = Template('$who likes $what') |
---|
190 | n/a | eq(s.substitute(who='tim', what='ham'), 'tim likes ham') |
---|
191 | n/a | eq(s.substitute(dict(who='tim'), what='ham'), 'tim likes ham') |
---|
192 | n/a | eq(s.substitute(dict(who='fred', what='kung pao'), |
---|
193 | n/a | who='tim', what='ham'), |
---|
194 | n/a | 'tim likes ham') |
---|
195 | n/a | s = Template('the mapping is $mapping') |
---|
196 | n/a | eq(s.substitute(dict(foo='none'), mapping='bozo'), |
---|
197 | n/a | 'the mapping is bozo') |
---|
198 | n/a | eq(s.substitute(dict(mapping='one'), mapping='two'), |
---|
199 | n/a | 'the mapping is two') |
---|
200 | n/a | |
---|
201 | n/a | def test_keyword_arguments_safe(self): |
---|
202 | n/a | eq = self.assertEqual |
---|
203 | n/a | raises = self.assertRaises |
---|
204 | n/a | s = Template('$who likes $what') |
---|
205 | n/a | eq(s.safe_substitute(who='tim', what='ham'), 'tim likes ham') |
---|
206 | n/a | eq(s.safe_substitute(dict(who='tim'), what='ham'), 'tim likes ham') |
---|
207 | n/a | eq(s.safe_substitute(dict(who='fred', what='kung pao'), |
---|
208 | n/a | who='tim', what='ham'), |
---|
209 | n/a | 'tim likes ham') |
---|
210 | n/a | s = Template('the mapping is $mapping') |
---|
211 | n/a | eq(s.safe_substitute(dict(foo='none'), mapping='bozo'), |
---|
212 | n/a | 'the mapping is bozo') |
---|
213 | n/a | eq(s.safe_substitute(dict(mapping='one'), mapping='two'), |
---|
214 | n/a | 'the mapping is two') |
---|
215 | n/a | d = dict(mapping='one') |
---|
216 | n/a | raises(TypeError, s.substitute, d, {}) |
---|
217 | n/a | raises(TypeError, s.safe_substitute, d, {}) |
---|
218 | n/a | |
---|
219 | n/a | def test_delimiter_override(self): |
---|
220 | n/a | eq = self.assertEqual |
---|
221 | n/a | raises = self.assertRaises |
---|
222 | n/a | class AmpersandTemplate(Template): |
---|
223 | n/a | delimiter = '&' |
---|
224 | n/a | s = AmpersandTemplate('this &gift is for &{who} &&') |
---|
225 | n/a | eq(s.substitute(gift='bud', who='you'), 'this bud is for you &') |
---|
226 | n/a | raises(KeyError, s.substitute) |
---|
227 | n/a | eq(s.safe_substitute(gift='bud', who='you'), 'this bud is for you &') |
---|
228 | n/a | eq(s.safe_substitute(), 'this &gift is for &{who} &') |
---|
229 | n/a | s = AmpersandTemplate('this &gift is for &{who} &') |
---|
230 | n/a | raises(ValueError, s.substitute, dict(gift='bud', who='you')) |
---|
231 | n/a | eq(s.safe_substitute(), 'this &gift is for &{who} &') |
---|
232 | n/a | |
---|
233 | n/a | class PieDelims(Template): |
---|
234 | n/a | delimiter = '@' |
---|
235 | n/a | s = PieDelims('@who likes to eat a bag of @{what} worth $100') |
---|
236 | n/a | self.assertEqual(s.substitute(dict(who='tim', what='ham')), |
---|
237 | n/a | 'tim likes to eat a bag of ham worth $100') |
---|
238 | n/a | |
---|
239 | n/a | |
---|
240 | n/a | def test_main(): |
---|
241 | n/a | from test import support |
---|
242 | n/a | test_classes = [TestTemplate,] |
---|
243 | n/a | support.run_unittest(*test_classes) |
---|
244 | n/a | |
---|
245 | n/a | |
---|
246 | n/a | if __name__ == '__main__': |
---|
247 | n/a | test_main() |
---|