blob: 70439f85c89555ef08dca3d7c566f896f2b967cb [file] [log] [blame]
Barry Warsaw8bee7612004-08-25 02:22:30 +00001import unittest
Serhiy Storchaka717ea082016-06-03 10:38:02 +03002import string
Barry Warsaw12827c12004-09-10 03:08:08 +00003from string import Template
Walter Dörwald0fd583c2003-02-21 12:53:50 +00004
Raymond Hettinger674f2412004-08-23 23:23:54 +00005
Walter Dörwald0fd583c2003-02-21 12:53:50 +00006class ModuleTest(unittest.TestCase):
7
8 def test_attrs(self):
R David Murray0a8f43e2015-04-13 20:04:29 -04009 # While the exact order of the items in these attributes is not
10 # technically part of the "language spec", in practice there is almost
11 # certainly user code that depends on the order, so de-facto it *is*
12 # part of the spec.
13 self.assertEqual(string.whitespace, ' \t\n\r\x0b\x0c')
14 self.assertEqual(string.ascii_lowercase, 'abcdefghijklmnopqrstuvwxyz')
15 self.assertEqual(string.ascii_uppercase, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')
16 self.assertEqual(string.ascii_letters, string.ascii_lowercase + string.ascii_uppercase)
17 self.assertEqual(string.digits, '0123456789')
18 self.assertEqual(string.hexdigits, string.digits + 'abcdefABCDEF')
19 self.assertEqual(string.octdigits, '01234567')
20 self.assertEqual(string.punctuation, '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~')
21 self.assertEqual(string.printable, string.digits + string.ascii_lowercase + string.ascii_uppercase + string.punctuation + string.whitespace)
Walter Dörwald0fd583c2003-02-21 12:53:50 +000022
Ezio Melotti2c6a9492009-09-26 12:19:30 +000023 def test_capwords(self):
24 self.assertEqual(string.capwords('abc def ghi'), 'Abc Def Ghi')
25 self.assertEqual(string.capwords('abc\tdef\nghi'), 'Abc Def Ghi')
26 self.assertEqual(string.capwords('abc\t def \nghi'), 'Abc Def Ghi')
27 self.assertEqual(string.capwords('ABC DEF GHI'), 'Abc Def Ghi')
28 self.assertEqual(string.capwords('ABC-DEF-GHI', '-'), 'Abc-Def-Ghi')
29 self.assertEqual(string.capwords('ABC-def DEF-ghi GHI'), 'Abc-def Def-ghi Ghi')
Ezio Melottia40bdda2009-09-26 12:33:22 +000030 self.assertEqual(string.capwords(' aBc DeF '), 'Abc Def')
31 self.assertEqual(string.capwords('\taBc\tDeF\t'), 'Abc Def')
32 self.assertEqual(string.capwords('\taBc\tDeF\t', '\t'), '\tAbc\tDef\t')
Ezio Melotti2c6a9492009-09-26 12:19:30 +000033
Nick Coghlan62ecb6a2011-05-31 19:40:11 +100034 def test_basic_formatter(self):
Eric Smith8c663262007-08-25 02:26:07 +000035 fmt = string.Formatter()
36 self.assertEqual(fmt.format("foo"), "foo")
Eric Smith7ade6482007-08-26 22:27:13 +000037 self.assertEqual(fmt.format("foo{0}", "bar"), "foobar")
38 self.assertEqual(fmt.format("foo{1}{0}-{1}", "bar", 6), "foo6bar-6")
Serhiy Storchaka8ffe9172015-03-24 22:28:43 +020039 self.assertRaises(TypeError, fmt.format)
40 self.assertRaises(TypeError, string.Formatter.format)
41
42 def test_format_keyword_arguments(self):
43 fmt = string.Formatter()
44 self.assertEqual(fmt.format("-{arg}-", arg='test'), '-test-')
45 self.assertRaises(KeyError, fmt.format, "-{arg}-")
46 self.assertEqual(fmt.format("-{self}-", self='test'), '-test-')
47 self.assertRaises(KeyError, fmt.format, "-{self}-")
48 self.assertEqual(fmt.format("-{format_string}-", format_string='test'),
49 '-test-')
50 self.assertRaises(KeyError, fmt.format, "-{format_string}-")
Serhiy Storchakab876df42015-03-24 22:30:46 +020051 with self.assertWarnsRegex(DeprecationWarning, "format_string"):
52 self.assertEqual(fmt.format(arg='test', format_string="-{arg}-"),
53 '-test-')
Eric Smith7ade6482007-08-26 22:27:13 +000054
Eric V. Smith7ce90742014-04-14 16:43:50 -040055 def test_auto_numbering(self):
56 fmt = string.Formatter()
57 self.assertEqual(fmt.format('foo{}{}', 'bar', 6),
58 'foo{}{}'.format('bar', 6))
59 self.assertEqual(fmt.format('foo{1}{num}{1}', None, 'bar', num=6),
60 'foo{1}{num}{1}'.format(None, 'bar', num=6))
61 self.assertEqual(fmt.format('{:^{}}', 'bar', 6),
62 '{:^{}}'.format('bar', 6))
Eric V. Smith85976b12015-09-29 10:27:38 -040063 self.assertEqual(fmt.format('{:^{}} {}', 'bar', 6, 'X'),
64 '{:^{}} {}'.format('bar', 6, 'X'))
Eric V. Smith7ce90742014-04-14 16:43:50 -040065 self.assertEqual(fmt.format('{:^{pad}}{}', 'foo', 'bar', pad=6),
66 '{:^{pad}}{}'.format('foo', 'bar', pad=6))
67
68 with self.assertRaises(ValueError):
69 fmt.format('foo{1}{}', 'bar', 6)
70
71 with self.assertRaises(ValueError):
72 fmt.format('foo{}{1}', 'bar', 6)
73
Nick Coghlan62ecb6a2011-05-31 19:40:11 +100074 def test_conversion_specifiers(self):
75 fmt = string.Formatter()
76 self.assertEqual(fmt.format("-{arg!r}-", arg='test'), "-'test'-")
77 self.assertEqual(fmt.format("{0!s}", 'test'), 'test')
78 self.assertRaises(ValueError, fmt.format, "{0!h}", 'test')
R David Murraye56bf972012-08-19 17:26:34 -040079 # issue13579
80 self.assertEqual(fmt.format("{0!a}", 42), '42')
81 self.assertEqual(fmt.format("{0!a}", string.ascii_letters),
82 "'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'")
83 self.assertEqual(fmt.format("{0!a}", chr(255)), "'\\xff'")
84 self.assertEqual(fmt.format("{0!a}", chr(256)), "'\\u0100'")
Nick Coghlan62ecb6a2011-05-31 19:40:11 +100085
86 def test_name_lookup(self):
87 fmt = string.Formatter()
88 class AnyAttr:
89 def __getattr__(self, attr):
90 return attr
91 x = AnyAttr()
92 self.assertEqual(fmt.format("{0.lumber}{0.jack}", x), 'lumberjack')
93 with self.assertRaises(AttributeError):
94 fmt.format("{0.lumber}{0.jack}", '')
95
96 def test_index_lookup(self):
97 fmt = string.Formatter()
98 lookup = ["eggs", "and", "spam"]
99 self.assertEqual(fmt.format("{0[2]}{0[0]}", lookup), 'spameggs')
100 with self.assertRaises(IndexError):
101 fmt.format("{0[2]}{0[0]}", [])
102 with self.assertRaises(KeyError):
103 fmt.format("{0[2]}{0[0]}", {})
104
105 def test_override_get_value(self):
Eric Smith7ade6482007-08-26 22:27:13 +0000106 class NamespaceFormatter(string.Formatter):
107 def __init__(self, namespace={}):
108 string.Formatter.__init__(self)
109 self.namespace = namespace
110
111 def get_value(self, key, args, kwds):
112 if isinstance(key, str):
113 try:
114 # Check explicitly passed arguments first
115 return kwds[key]
116 except KeyError:
117 return self.namespace[key]
118 else:
119 string.Formatter.get_value(key, args, kwds)
120
121 fmt = NamespaceFormatter({'greeting':'hello'})
122 self.assertEqual(fmt.format("{greeting}, world!"), 'hello, world!')
Eric Smith8c663262007-08-25 02:26:07 +0000123
124
Nick Coghlan62ecb6a2011-05-31 19:40:11 +1000125 def test_override_format_field(self):
Eric Smith81936692007-08-31 01:14:01 +0000126 class CallFormatter(string.Formatter):
127 def format_field(self, value, format_spec):
128 return format(value(), format_spec)
129
130 fmt = CallFormatter()
131 self.assertEqual(fmt.format('*{0}*', lambda : 'result'), '*result*')
132
133
Nick Coghlan62ecb6a2011-05-31 19:40:11 +1000134 def test_override_convert_field(self):
Eric Smith81936692007-08-31 01:14:01 +0000135 class XFormatter(string.Formatter):
136 def convert_field(self, value, conversion):
137 if conversion == 'x':
138 return None
Nick Coghlan62ecb6a2011-05-31 19:40:11 +1000139 return super().convert_field(value, conversion)
Eric Smith81936692007-08-31 01:14:01 +0000140
141 fmt = XFormatter()
142 self.assertEqual(fmt.format("{0!r}:{0!x}", 'foo', 'foo'), "'foo':None")
143
144
Nick Coghlan62ecb6a2011-05-31 19:40:11 +1000145 def test_override_parse(self):
Eric Smith81936692007-08-31 01:14:01 +0000146 class BarFormatter(string.Formatter):
147 # returns an iterable that contains tuples of the form:
148 # (literal_text, field_name, format_spec, conversion)
149 def parse(self, format_string):
150 for field in format_string.split('|'):
151 if field[0] == '+':
152 # it's markup
153 field_name, _, format_spec = field[1:].partition(':')
154 yield '', field_name, format_spec, None
155 else:
156 yield field, None, None, None
157
158 fmt = BarFormatter()
159 self.assertEqual(fmt.format('*|+0:^10s|*', 'foo'), '* foo *')
160
Nick Coghlan62ecb6a2011-05-31 19:40:11 +1000161 def test_check_unused_args(self):
Eric Smith3bcc42a2007-08-31 02:26:31 +0000162 class CheckAllUsedFormatter(string.Formatter):
163 def check_unused_args(self, used_args, args, kwargs):
Ezio Melotti42da6632011-03-15 05:18:48 +0200164 # Track which arguments actually got used
Eric Smith3bcc42a2007-08-31 02:26:31 +0000165 unused_args = set(kwargs.keys())
166 unused_args.update(range(0, len(args)))
167
168 for arg in used_args:
169 unused_args.remove(arg)
170
171 if unused_args:
172 raise ValueError("unused arguments")
173
174 fmt = CheckAllUsedFormatter()
175 self.assertEqual(fmt.format("{0}", 10), "10")
176 self.assertEqual(fmt.format("{0}{i}", 10, i=100), "10100")
177 self.assertEqual(fmt.format("{0}{i}{1}", 10, 20, i=100), "1010020")
178 self.assertRaises(ValueError, fmt.format, "{0}{i}{1}", 10, 20, i=100, j=0)
179 self.assertRaises(ValueError, fmt.format, "{0}", 10, 20)
180 self.assertRaises(ValueError, fmt.format, "{0}", 10, 20, i=100)
181 self.assertRaises(ValueError, fmt.format, "{i}", 10, 20, i=100)
182
Nick Coghlan62ecb6a2011-05-31 19:40:11 +1000183 def test_vformat_recursion_limit(self):
184 fmt = string.Formatter()
185 args = ()
186 kwargs = dict(i=100)
187 with self.assertRaises(ValueError) as err:
188 fmt._vformat("{i}", args, kwargs, set(), -1)
189 self.assertIn("recursion", str(err.exception))
Nick Coghland25fd4d2011-03-15 08:54:37 +1000190
191
Serhiy Storchaka717ea082016-06-03 10:38:02 +0300192# Template tests (formerly housed in test_pep292.py)
193
Barry Warsaw12827c12004-09-10 03:08:08 +0000194class Bag:
195 pass
196
197class Mapping:
198 def __getitem__(self, name):
199 obj = self
200 for part in name.split('.'):
201 try:
202 obj = getattr(obj, part)
203 except AttributeError:
204 raise KeyError(name)
205 return obj
206
Barry Warsaw8bee7612004-08-25 02:22:30 +0000207
208class TestTemplate(unittest.TestCase):
Barry Warsaw8bee7612004-08-25 02:22:30 +0000209 def test_regular_templates(self):
210 s = Template('$who likes to eat a bag of $what worth $$100')
Barry Warsaw12827c12004-09-10 03:08:08 +0000211 self.assertEqual(s.substitute(dict(who='tim', what='ham')),
Barry Warsaw8bee7612004-08-25 02:22:30 +0000212 'tim likes to eat a bag of ham worth $100')
Barry Warsaw12827c12004-09-10 03:08:08 +0000213 self.assertRaises(KeyError, s.substitute, dict(who='tim'))
Serhiy Storchaka8ffe9172015-03-24 22:28:43 +0200214 self.assertRaises(TypeError, Template.substitute)
Barry Warsaw8bee7612004-08-25 02:22:30 +0000215
216 def test_regular_templates_with_braces(self):
217 s = Template('$who likes ${what} for ${meal}')
Barry Warsaw12827c12004-09-10 03:08:08 +0000218 d = dict(who='tim', what='ham', meal='dinner')
219 self.assertEqual(s.substitute(d), 'tim likes ham for dinner')
220 self.assertRaises(KeyError, s.substitute,
221 dict(who='tim', what='ham'))
Barry Warsaw8bee7612004-08-25 02:22:30 +0000222
223 def test_escapes(self):
224 eq = self.assertEqual
225 s = Template('$who likes to eat a bag of $$what worth $$100')
Barry Warsaw12827c12004-09-10 03:08:08 +0000226 eq(s.substitute(dict(who='tim', what='ham')),
Barry Warsaw8bee7612004-08-25 02:22:30 +0000227 'tim likes to eat a bag of $what worth $100')
228 s = Template('$who likes $$')
Barry Warsaw12827c12004-09-10 03:08:08 +0000229 eq(s.substitute(dict(who='tim', what='ham')), 'tim likes $')
Barry Warsaw8bee7612004-08-25 02:22:30 +0000230
231 def test_percents(self):
Barry Warsaw12827c12004-09-10 03:08:08 +0000232 eq = self.assertEqual
Barry Warsaw8bee7612004-08-25 02:22:30 +0000233 s = Template('%(foo)s $foo ${foo}')
Barry Warsaw12827c12004-09-10 03:08:08 +0000234 d = dict(foo='baz')
235 eq(s.substitute(d), '%(foo)s baz baz')
236 eq(s.safe_substitute(d), '%(foo)s baz baz')
Barry Warsaw8bee7612004-08-25 02:22:30 +0000237
238 def test_stringification(self):
Barry Warsaw12827c12004-09-10 03:08:08 +0000239 eq = self.assertEqual
Barry Warsaw8bee7612004-08-25 02:22:30 +0000240 s = Template('tim has eaten $count bags of ham today')
Barry Warsaw12827c12004-09-10 03:08:08 +0000241 d = dict(count=7)
242 eq(s.substitute(d), 'tim has eaten 7 bags of ham today')
243 eq(s.safe_substitute(d), 'tim has eaten 7 bags of ham today')
244 s = Template('tim has eaten ${count} bags of ham today')
245 eq(s.substitute(d), 'tim has eaten 7 bags of ham today')
Barry Warsaw8bee7612004-08-25 02:22:30 +0000246
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000247 def test_tupleargs(self):
248 eq = self.assertEqual
249 s = Template('$who ate ${meal}')
250 d = dict(who=('tim', 'fred'), meal=('ham', 'kung pao'))
251 eq(s.substitute(d), "('tim', 'fred') ate ('ham', 'kung pao')")
252 eq(s.safe_substitute(d), "('tim', 'fred') ate ('ham', 'kung pao')")
253
Barry Warsaw8bee7612004-08-25 02:22:30 +0000254 def test_SafeTemplate(self):
255 eq = self.assertEqual
Barry Warsaw12827c12004-09-10 03:08:08 +0000256 s = Template('$who likes ${what} for ${meal}')
257 eq(s.safe_substitute(dict(who='tim')), 'tim likes ${what} for ${meal}')
258 eq(s.safe_substitute(dict(what='ham')), '$who likes ham for ${meal}')
259 eq(s.safe_substitute(dict(what='ham', meal='dinner')),
Barry Warsaw8bee7612004-08-25 02:22:30 +0000260 '$who likes ham for dinner')
Barry Warsaw12827c12004-09-10 03:08:08 +0000261 eq(s.safe_substitute(dict(who='tim', what='ham')),
Barry Warsaw8bee7612004-08-25 02:22:30 +0000262 'tim likes ham for ${meal}')
Barry Warsaw12827c12004-09-10 03:08:08 +0000263 eq(s.safe_substitute(dict(who='tim', what='ham', meal='dinner')),
Barry Warsaw8bee7612004-08-25 02:22:30 +0000264 'tim likes ham for dinner')
265
266 def test_invalid_placeholders(self):
267 raises = self.assertRaises
268 s = Template('$who likes $')
Barry Warsaw12827c12004-09-10 03:08:08 +0000269 raises(ValueError, s.substitute, dict(who='tim'))
Barry Warsaw8bee7612004-08-25 02:22:30 +0000270 s = Template('$who likes ${what)')
Barry Warsaw12827c12004-09-10 03:08:08 +0000271 raises(ValueError, s.substitute, dict(who='tim'))
Barry Warsaw8bee7612004-08-25 02:22:30 +0000272 s = Template('$who likes $100')
Barry Warsaw12827c12004-09-10 03:08:08 +0000273 raises(ValueError, s.substitute, dict(who='tim'))
274
Barry Warsaw12827c12004-09-10 03:08:08 +0000275 def test_idpattern_override(self):
276 class PathPattern(Template):
277 idpattern = r'[_a-z][._a-z0-9]*'
278 m = Mapping()
279 m.bag = Bag()
280 m.bag.foo = Bag()
281 m.bag.foo.who = 'tim'
282 m.bag.what = 'ham'
283 s = PathPattern('$bag.foo.who likes to eat a bag of $bag.what')
284 self.assertEqual(s.substitute(m), 'tim likes to eat a bag of ham')
285
286 def test_pattern_override(self):
287 class MyPattern(Template):
288 pattern = r"""
289 (?P<escaped>@{2}) |
290 @(?P<named>[_a-z][._a-z0-9]*) |
291 @{(?P<braced>[_a-z][._a-z0-9]*)} |
Barry Warsaw3e773fb2004-09-13 20:53:27 +0000292 (?P<invalid>@)
Barry Warsaw12827c12004-09-10 03:08:08 +0000293 """
294 m = Mapping()
295 m.bag = Bag()
296 m.bag.foo = Bag()
297 m.bag.foo.who = 'tim'
298 m.bag.what = 'ham'
299 s = MyPattern('@bag.foo.who likes to eat a bag of @bag.what')
300 self.assertEqual(s.substitute(m), 'tim likes to eat a bag of ham')
301
Neal Norwitz6627a962004-10-17 16:27:18 +0000302 class BadPattern(Template):
303 pattern = r"""
304 (?P<badname>.*) |
305 (?P<escaped>@{2}) |
306 @(?P<named>[_a-z][._a-z0-9]*) |
307 @{(?P<braced>[_a-z][._a-z0-9]*)} |
308 (?P<invalid>@) |
309 """
310 s = BadPattern('@bag.foo.who likes to eat a bag of @bag.what')
311 self.assertRaises(ValueError, s.substitute, {})
312 self.assertRaises(ValueError, s.safe_substitute, {})
313
Florent Xiclunaeb19dce2010-09-18 23:34:07 +0000314 def test_braced_override(self):
315 class MyTemplate(Template):
316 pattern = r"""
317 \$(?:
318 (?P<escaped>$) |
319 (?P<named>[_a-z][_a-z0-9]*) |
320 @@(?P<braced>[_a-z][_a-z0-9]*)@@ |
321 (?P<invalid>) |
322 )
323 """
324
325 tmpl = 'PyCon in $@@location@@'
326 t = MyTemplate(tmpl)
327 self.assertRaises(KeyError, t.substitute, {})
328 val = t.substitute({'location': 'Cleveland'})
329 self.assertEqual(val, 'PyCon in Cleveland')
330
331 def test_braced_override_safe(self):
332 class MyTemplate(Template):
333 pattern = r"""
334 \$(?:
335 (?P<escaped>$) |
336 (?P<named>[_a-z][_a-z0-9]*) |
337 @@(?P<braced>[_a-z][_a-z0-9]*)@@ |
338 (?P<invalid>) |
339 )
340 """
341
342 tmpl = 'PyCon in $@@location@@'
343 t = MyTemplate(tmpl)
344 self.assertEqual(t.safe_substitute(), tmpl)
345 val = t.safe_substitute({'location': 'Cleveland'})
346 self.assertEqual(val, 'PyCon in Cleveland')
347
Nick Coghlan62ecb6a2011-05-31 19:40:11 +1000348 def test_invalid_with_no_lines(self):
349 # The error formatting for invalid templates
350 # has a special case for no data that the default
351 # pattern can't trigger (always has at least '$')
352 # So we craft a pattern that is always invalid
353 # with no leading data.
354 class MyTemplate(Template):
355 pattern = r"""
356 (?P<invalid>) |
357 unreachable(
358 (?P<named>) |
359 (?P<braced>) |
360 (?P<escaped>)
361 )
362 """
363 s = MyTemplate('')
364 with self.assertRaises(ValueError) as err:
365 s.substitute({})
366 self.assertIn('line 1, col 1', str(err.exception))
367
Barry Warsaw12827c12004-09-10 03:08:08 +0000368 def test_unicode_values(self):
369 s = Template('$who likes $what')
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000370 d = dict(who='t\xffm', what='f\xfe\fed')
371 self.assertEqual(s.substitute(d), 't\xffm likes f\xfe\x0ced')
Barry Warsaw8bee7612004-08-25 02:22:30 +0000372
Barry Warsaw302bd582004-09-13 14:35:59 +0000373 def test_keyword_arguments(self):
374 eq = self.assertEqual
375 s = Template('$who likes $what')
376 eq(s.substitute(who='tim', what='ham'), 'tim likes ham')
377 eq(s.substitute(dict(who='tim'), what='ham'), 'tim likes ham')
378 eq(s.substitute(dict(who='fred', what='kung pao'),
379 who='tim', what='ham'),
380 'tim likes ham')
381 s = Template('the mapping is $mapping')
382 eq(s.substitute(dict(foo='none'), mapping='bozo'),
383 'the mapping is bozo')
384 eq(s.substitute(dict(mapping='one'), mapping='two'),
385 'the mapping is two')
386
Serhiy Storchaka8ffe9172015-03-24 22:28:43 +0200387 s = Template('the self is $self')
388 eq(s.substitute(self='bozo'), 'the self is bozo')
389
Barry Warsaw302bd582004-09-13 14:35:59 +0000390 def test_keyword_arguments_safe(self):
391 eq = self.assertEqual
Barry Warsawc7cd20c2004-09-13 15:24:43 +0000392 raises = self.assertRaises
Barry Warsaw302bd582004-09-13 14:35:59 +0000393 s = Template('$who likes $what')
394 eq(s.safe_substitute(who='tim', what='ham'), 'tim likes ham')
395 eq(s.safe_substitute(dict(who='tim'), what='ham'), 'tim likes ham')
396 eq(s.safe_substitute(dict(who='fred', what='kung pao'),
397 who='tim', what='ham'),
398 'tim likes ham')
399 s = Template('the mapping is $mapping')
400 eq(s.safe_substitute(dict(foo='none'), mapping='bozo'),
401 'the mapping is bozo')
402 eq(s.safe_substitute(dict(mapping='one'), mapping='two'),
403 'the mapping is two')
Barry Warsawc7cd20c2004-09-13 15:24:43 +0000404 d = dict(mapping='one')
405 raises(TypeError, s.substitute, d, {})
406 raises(TypeError, s.safe_substitute, d, {})
Barry Warsaw302bd582004-09-13 14:35:59 +0000407
Serhiy Storchaka8ffe9172015-03-24 22:28:43 +0200408 s = Template('the self is $self')
409 eq(s.safe_substitute(self='bozo'), 'the self is bozo')
410
Raymond Hettinger6d191112004-09-14 02:34:08 +0000411 def test_delimiter_override(self):
Barry Warsaw8c72eae2004-11-01 03:52:43 +0000412 eq = self.assertEqual
413 raises = self.assertRaises
Raymond Hettinger6d191112004-09-14 02:34:08 +0000414 class AmpersandTemplate(Template):
415 delimiter = '&'
416 s = AmpersandTemplate('this &gift is for &{who} &&')
Barry Warsaw8c72eae2004-11-01 03:52:43 +0000417 eq(s.substitute(gift='bud', who='you'), 'this bud is for you &')
418 raises(KeyError, s.substitute)
419 eq(s.safe_substitute(gift='bud', who='you'), 'this bud is for you &')
420 eq(s.safe_substitute(), 'this &gift is for &{who} &')
Raymond Hettinger6d191112004-09-14 02:34:08 +0000421 s = AmpersandTemplate('this &gift is for &{who} &')
Barry Warsaw8c72eae2004-11-01 03:52:43 +0000422 raises(ValueError, s.substitute, dict(gift='bud', who='you'))
423 eq(s.safe_substitute(), 'this &gift is for &{who} &')
424
Georg Brandl89fad142010-03-14 10:23:39 +0000425 class PieDelims(Template):
426 delimiter = '@'
427 s = PieDelims('@who likes to eat a bag of @{what} worth $100')
428 self.assertEqual(s.substitute(dict(who='tim', what='ham')),
429 'tim likes to eat a bag of ham worth $100')
430
Barry Warsaw8bee7612004-08-25 02:22:30 +0000431
Barry Warsaw8bee7612004-08-25 02:22:30 +0000432if __name__ == '__main__':
Zachary Ware38c707e2015-04-13 15:00:43 -0500433 unittest.main()