Barry Warsaw | 8bee761 | 2004-08-25 02:22:30 +0000 | [diff] [blame] | 1 | # Copyright (C) 2004 Python Software Foundation |
| 2 | # Author: barry@python.org (Barry Warsaw) |
| 3 | # License: http://www.opensource.org/licenses/PythonSoftFoundation.php |
| 4 | |
| 5 | import unittest |
Barry Warsaw | 12827c1 | 2004-09-10 03:08:08 +0000 | [diff] [blame] | 6 | from string import Template |
| 7 | |
| 8 | |
| 9 | class Bag: |
| 10 | pass |
| 11 | |
| 12 | class 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 Warsaw | 8bee761 | 2004-08-25 02:22:30 +0000 | [diff] [blame] | 22 | |
| 23 | class TestTemplate(unittest.TestCase): |
Barry Warsaw | 8bee761 | 2004-08-25 02:22:30 +0000 | [diff] [blame] | 24 | def test_regular_templates(self): |
| 25 | s = Template('$who likes to eat a bag of $what worth $$100') |
Barry Warsaw | 12827c1 | 2004-09-10 03:08:08 +0000 | [diff] [blame] | 26 | self.assertEqual(s.substitute(dict(who='tim', what='ham')), |
Barry Warsaw | 8bee761 | 2004-08-25 02:22:30 +0000 | [diff] [blame] | 27 | 'tim likes to eat a bag of ham worth $100') |
Barry Warsaw | 12827c1 | 2004-09-10 03:08:08 +0000 | [diff] [blame] | 28 | self.assertRaises(KeyError, s.substitute, dict(who='tim')) |
Serhiy Storchaka | 40fd0e8 | 2015-03-24 22:27:50 +0200 | [diff] [blame^] | 29 | self.assertRaises(TypeError, Template.substitute) |
Barry Warsaw | 8bee761 | 2004-08-25 02:22:30 +0000 | [diff] [blame] | 30 | |
| 31 | def test_regular_templates_with_braces(self): |
| 32 | s = Template('$who likes ${what} for ${meal}') |
Barry Warsaw | 12827c1 | 2004-09-10 03:08:08 +0000 | [diff] [blame] | 33 | 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 Warsaw | 8bee761 | 2004-08-25 02:22:30 +0000 | [diff] [blame] | 37 | |
| 38 | def test_escapes(self): |
| 39 | eq = self.assertEqual |
| 40 | s = Template('$who likes to eat a bag of $$what worth $$100') |
Barry Warsaw | 12827c1 | 2004-09-10 03:08:08 +0000 | [diff] [blame] | 41 | eq(s.substitute(dict(who='tim', what='ham')), |
Barry Warsaw | 8bee761 | 2004-08-25 02:22:30 +0000 | [diff] [blame] | 42 | 'tim likes to eat a bag of $what worth $100') |
| 43 | s = Template('$who likes $$') |
Barry Warsaw | 12827c1 | 2004-09-10 03:08:08 +0000 | [diff] [blame] | 44 | eq(s.substitute(dict(who='tim', what='ham')), 'tim likes $') |
Barry Warsaw | 8bee761 | 2004-08-25 02:22:30 +0000 | [diff] [blame] | 45 | |
| 46 | def test_percents(self): |
Barry Warsaw | 12827c1 | 2004-09-10 03:08:08 +0000 | [diff] [blame] | 47 | eq = self.assertEqual |
Barry Warsaw | 8bee761 | 2004-08-25 02:22:30 +0000 | [diff] [blame] | 48 | s = Template('%(foo)s $foo ${foo}') |
Barry Warsaw | 12827c1 | 2004-09-10 03:08:08 +0000 | [diff] [blame] | 49 | d = dict(foo='baz') |
| 50 | eq(s.substitute(d), '%(foo)s baz baz') |
| 51 | eq(s.safe_substitute(d), '%(foo)s baz baz') |
Barry Warsaw | 8bee761 | 2004-08-25 02:22:30 +0000 | [diff] [blame] | 52 | |
| 53 | def test_stringification(self): |
Barry Warsaw | 12827c1 | 2004-09-10 03:08:08 +0000 | [diff] [blame] | 54 | eq = self.assertEqual |
Barry Warsaw | 8bee761 | 2004-08-25 02:22:30 +0000 | [diff] [blame] | 55 | s = Template('tim has eaten $count bags of ham today') |
Barry Warsaw | 12827c1 | 2004-09-10 03:08:08 +0000 | [diff] [blame] | 56 | 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 Warsaw | 8bee761 | 2004-08-25 02:22:30 +0000 | [diff] [blame] | 61 | |
Thomas Wouters | add1911 | 2006-07-05 11:03:49 +0000 | [diff] [blame] | 62 | 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 Warsaw | 8bee761 | 2004-08-25 02:22:30 +0000 | [diff] [blame] | 69 | def test_SafeTemplate(self): |
| 70 | eq = self.assertEqual |
Barry Warsaw | 12827c1 | 2004-09-10 03:08:08 +0000 | [diff] [blame] | 71 | 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 Warsaw | 8bee761 | 2004-08-25 02:22:30 +0000 | [diff] [blame] | 75 | '$who likes ham for dinner') |
Barry Warsaw | 12827c1 | 2004-09-10 03:08:08 +0000 | [diff] [blame] | 76 | eq(s.safe_substitute(dict(who='tim', what='ham')), |
Barry Warsaw | 8bee761 | 2004-08-25 02:22:30 +0000 | [diff] [blame] | 77 | 'tim likes ham for ${meal}') |
Barry Warsaw | 12827c1 | 2004-09-10 03:08:08 +0000 | [diff] [blame] | 78 | eq(s.safe_substitute(dict(who='tim', what='ham', meal='dinner')), |
Barry Warsaw | 8bee761 | 2004-08-25 02:22:30 +0000 | [diff] [blame] | 79 | 'tim likes ham for dinner') |
| 80 | |
| 81 | def test_invalid_placeholders(self): |
| 82 | raises = self.assertRaises |
| 83 | s = Template('$who likes $') |
Barry Warsaw | 12827c1 | 2004-09-10 03:08:08 +0000 | [diff] [blame] | 84 | raises(ValueError, s.substitute, dict(who='tim')) |
Barry Warsaw | 8bee761 | 2004-08-25 02:22:30 +0000 | [diff] [blame] | 85 | s = Template('$who likes ${what)') |
Barry Warsaw | 12827c1 | 2004-09-10 03:08:08 +0000 | [diff] [blame] | 86 | raises(ValueError, s.substitute, dict(who='tim')) |
Barry Warsaw | 8bee761 | 2004-08-25 02:22:30 +0000 | [diff] [blame] | 87 | s = Template('$who likes $100') |
Barry Warsaw | 12827c1 | 2004-09-10 03:08:08 +0000 | [diff] [blame] | 88 | raises(ValueError, s.substitute, dict(who='tim')) |
| 89 | |
Barry Warsaw | 12827c1 | 2004-09-10 03:08:08 +0000 | [diff] [blame] | 90 | 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 Warsaw | 3e773fb | 2004-09-13 20:53:27 +0000 | [diff] [blame] | 107 | (?P<invalid>@) |
Barry Warsaw | 12827c1 | 2004-09-10 03:08:08 +0000 | [diff] [blame] | 108 | """ |
| 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 Norwitz | 6627a96 | 2004-10-17 16:27:18 +0000 | [diff] [blame] | 117 | 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 Xicluna | ff05e52 | 2010-09-18 23:34:07 +0000 | [diff] [blame] | 129 | 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 Warsaw | 12827c1 | 2004-09-10 03:08:08 +0000 | [diff] [blame] | 163 | 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 Warsaw | 8bee761 | 2004-08-25 02:22:30 +0000 | [diff] [blame] | 167 | |
Barry Warsaw | 302bd58 | 2004-09-13 14:35:59 +0000 | [diff] [blame] | 168 | 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 Storchaka | 40fd0e8 | 2015-03-24 22:27:50 +0200 | [diff] [blame^] | 182 | s = Template('the self is $self') |
| 183 | eq(s.substitute(self='bozo'), 'the self is bozo') |
| 184 | |
Barry Warsaw | 302bd58 | 2004-09-13 14:35:59 +0000 | [diff] [blame] | 185 | def test_keyword_arguments_safe(self): |
| 186 | eq = self.assertEqual |
Barry Warsaw | c7cd20c | 2004-09-13 15:24:43 +0000 | [diff] [blame] | 187 | raises = self.assertRaises |
Barry Warsaw | 302bd58 | 2004-09-13 14:35:59 +0000 | [diff] [blame] | 188 | 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 Warsaw | c7cd20c | 2004-09-13 15:24:43 +0000 | [diff] [blame] | 199 | d = dict(mapping='one') |
| 200 | raises(TypeError, s.substitute, d, {}) |
| 201 | raises(TypeError, s.safe_substitute, d, {}) |
Barry Warsaw | 302bd58 | 2004-09-13 14:35:59 +0000 | [diff] [blame] | 202 | |
Serhiy Storchaka | 40fd0e8 | 2015-03-24 22:27:50 +0200 | [diff] [blame^] | 203 | s = Template('the self is $self') |
| 204 | eq(s.safe_substitute(self='bozo'), 'the self is bozo') |
| 205 | |
Raymond Hettinger | 6d19111 | 2004-09-14 02:34:08 +0000 | [diff] [blame] | 206 | def test_delimiter_override(self): |
Barry Warsaw | 8c72eae | 2004-11-01 03:52:43 +0000 | [diff] [blame] | 207 | eq = self.assertEqual |
| 208 | raises = self.assertRaises |
Raymond Hettinger | 6d19111 | 2004-09-14 02:34:08 +0000 | [diff] [blame] | 209 | class AmpersandTemplate(Template): |
| 210 | delimiter = '&' |
| 211 | s = AmpersandTemplate('this &gift is for &{who} &&') |
Barry Warsaw | 8c72eae | 2004-11-01 03:52:43 +0000 | [diff] [blame] | 212 | 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 Hettinger | 6d19111 | 2004-09-14 02:34:08 +0000 | [diff] [blame] | 216 | s = AmpersandTemplate('this &gift is for &{who} &') |
Barry Warsaw | 8c72eae | 2004-11-01 03:52:43 +0000 | [diff] [blame] | 217 | raises(ValueError, s.substitute, dict(gift='bud', who='you')) |
| 218 | eq(s.safe_substitute(), 'this &gift is for &{who} &') |
| 219 | |
Georg Brandl | 7ae6018 | 2010-02-07 12:16:12 +0000 | [diff] [blame] | 220 | 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 Warsaw | 8bee761 | 2004-08-25 02:22:30 +0000 | [diff] [blame] | 226 | |
| 227 | def test_main(): |
| 228 | from test import test_support |
Raymond Hettinger | 6d19111 | 2004-09-14 02:34:08 +0000 | [diff] [blame] | 229 | test_classes = [TestTemplate,] |
| 230 | test_support.run_unittest(*test_classes) |
Barry Warsaw | 8bee761 | 2004-08-25 02:22:30 +0000 | [diff] [blame] | 231 | |
| 232 | |
| 233 | if __name__ == '__main__': |
Raymond Hettinger | 6d19111 | 2004-09-14 02:34:08 +0000 | [diff] [blame] | 234 | test_main() |