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')) |
Barry Warsaw | 8bee761 | 2004-08-25 02:22:30 +0000 | [diff] [blame] | 29 | |
| 30 | def test_regular_templates_with_braces(self): |
| 31 | s = Template('$who likes ${what} for ${meal}') |
Barry Warsaw | 12827c1 | 2004-09-10 03:08:08 +0000 | [diff] [blame] | 32 | 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 Warsaw | 8bee761 | 2004-08-25 02:22:30 +0000 | [diff] [blame] | 36 | |
| 37 | def test_escapes(self): |
| 38 | eq = self.assertEqual |
| 39 | s = Template('$who likes to eat a bag of $$what worth $$100') |
Barry Warsaw | 12827c1 | 2004-09-10 03:08:08 +0000 | [diff] [blame] | 40 | eq(s.substitute(dict(who='tim', what='ham')), |
Barry Warsaw | 8bee761 | 2004-08-25 02:22:30 +0000 | [diff] [blame] | 41 | 'tim likes to eat a bag of $what worth $100') |
| 42 | s = Template('$who likes $$') |
Barry Warsaw | 12827c1 | 2004-09-10 03:08:08 +0000 | [diff] [blame] | 43 | eq(s.substitute(dict(who='tim', what='ham')), 'tim likes $') |
Barry Warsaw | 8bee761 | 2004-08-25 02:22:30 +0000 | [diff] [blame] | 44 | |
Nick Coghlan | 7462fa6 | 2011-03-16 14:30:45 -0400 | [diff] [blame] | 45 | 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 Warsaw | 8bee761 | 2004-08-25 02:22:30 +0000 | [diff] [blame] | 58 | def test_percents(self): |
Barry Warsaw | 12827c1 | 2004-09-10 03:08:08 +0000 | [diff] [blame] | 59 | eq = self.assertEqual |
Barry Warsaw | 8bee761 | 2004-08-25 02:22:30 +0000 | [diff] [blame] | 60 | s = Template('%(foo)s $foo ${foo}') |
Barry Warsaw | 12827c1 | 2004-09-10 03:08:08 +0000 | [diff] [blame] | 61 | d = dict(foo='baz') |
| 62 | eq(s.substitute(d), '%(foo)s baz baz') |
| 63 | eq(s.safe_substitute(d), '%(foo)s baz baz') |
Barry Warsaw | 8bee761 | 2004-08-25 02:22:30 +0000 | [diff] [blame] | 64 | |
| 65 | def test_stringification(self): |
Barry Warsaw | 12827c1 | 2004-09-10 03:08:08 +0000 | [diff] [blame] | 66 | eq = self.assertEqual |
Barry Warsaw | 8bee761 | 2004-08-25 02:22:30 +0000 | [diff] [blame] | 67 | s = Template('tim has eaten $count bags of ham today') |
Barry Warsaw | 12827c1 | 2004-09-10 03:08:08 +0000 | [diff] [blame] | 68 | 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 Warsaw | 8bee761 | 2004-08-25 02:22:30 +0000 | [diff] [blame] | 73 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 74 | 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 Warsaw | 8bee761 | 2004-08-25 02:22:30 +0000 | [diff] [blame] | 81 | def test_SafeTemplate(self): |
| 82 | eq = self.assertEqual |
Barry Warsaw | 12827c1 | 2004-09-10 03:08:08 +0000 | [diff] [blame] | 83 | 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 Warsaw | 8bee761 | 2004-08-25 02:22:30 +0000 | [diff] [blame] | 87 | '$who likes ham for dinner') |
Barry Warsaw | 12827c1 | 2004-09-10 03:08:08 +0000 | [diff] [blame] | 88 | eq(s.safe_substitute(dict(who='tim', what='ham')), |
Barry Warsaw | 8bee761 | 2004-08-25 02:22:30 +0000 | [diff] [blame] | 89 | 'tim likes ham for ${meal}') |
Barry Warsaw | 12827c1 | 2004-09-10 03:08:08 +0000 | [diff] [blame] | 90 | eq(s.safe_substitute(dict(who='tim', what='ham', meal='dinner')), |
Barry Warsaw | 8bee761 | 2004-08-25 02:22:30 +0000 | [diff] [blame] | 91 | 'tim likes ham for dinner') |
| 92 | |
| 93 | def test_invalid_placeholders(self): |
| 94 | raises = self.assertRaises |
| 95 | s = Template('$who likes $') |
Barry Warsaw | 12827c1 | 2004-09-10 03:08:08 +0000 | [diff] [blame] | 96 | raises(ValueError, s.substitute, dict(who='tim')) |
Barry Warsaw | 8bee761 | 2004-08-25 02:22:30 +0000 | [diff] [blame] | 97 | s = Template('$who likes ${what)') |
Barry Warsaw | 12827c1 | 2004-09-10 03:08:08 +0000 | [diff] [blame] | 98 | raises(ValueError, s.substitute, dict(who='tim')) |
Barry Warsaw | 8bee761 | 2004-08-25 02:22:30 +0000 | [diff] [blame] | 99 | s = Template('$who likes $100') |
Barry Warsaw | 12827c1 | 2004-09-10 03:08:08 +0000 | [diff] [blame] | 100 | raises(ValueError, s.substitute, dict(who='tim')) |
| 101 | |
Barry Warsaw | 12827c1 | 2004-09-10 03:08:08 +0000 | [diff] [blame] | 102 | 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 Warsaw | 3e773fb | 2004-09-13 20:53:27 +0000 | [diff] [blame] | 119 | (?P<invalid>@) |
Barry Warsaw | 12827c1 | 2004-09-10 03:08:08 +0000 | [diff] [blame] | 120 | """ |
| 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 Norwitz | 6627a96 | 2004-10-17 16:27:18 +0000 | [diff] [blame] | 129 | 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 Xicluna | eb19dce | 2010-09-18 23:34:07 +0000 | [diff] [blame] | 141 | 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 Warsaw | 12827c1 | 2004-09-10 03:08:08 +0000 | [diff] [blame] | 175 | def test_unicode_values(self): |
| 176 | s = Template('$who likes $what') |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 177 | d = dict(who='t\xffm', what='f\xfe\fed') |
| 178 | self.assertEqual(s.substitute(d), 't\xffm likes f\xfe\x0ced') |
Barry Warsaw | 8bee761 | 2004-08-25 02:22:30 +0000 | [diff] [blame] | 179 | |
Barry Warsaw | 302bd58 | 2004-09-13 14:35:59 +0000 | [diff] [blame] | 180 | 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 Warsaw | c7cd20c | 2004-09-13 15:24:43 +0000 | [diff] [blame] | 196 | raises = self.assertRaises |
Barry Warsaw | 302bd58 | 2004-09-13 14:35:59 +0000 | [diff] [blame] | 197 | 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 Warsaw | c7cd20c | 2004-09-13 15:24:43 +0000 | [diff] [blame] | 208 | d = dict(mapping='one') |
| 209 | raises(TypeError, s.substitute, d, {}) |
| 210 | raises(TypeError, s.safe_substitute, d, {}) |
Barry Warsaw | 302bd58 | 2004-09-13 14:35:59 +0000 | [diff] [blame] | 211 | |
Raymond Hettinger | 6d19111 | 2004-09-14 02:34:08 +0000 | [diff] [blame] | 212 | def test_delimiter_override(self): |
Barry Warsaw | 8c72eae | 2004-11-01 03:52:43 +0000 | [diff] [blame] | 213 | eq = self.assertEqual |
| 214 | raises = self.assertRaises |
Raymond Hettinger | 6d19111 | 2004-09-14 02:34:08 +0000 | [diff] [blame] | 215 | class AmpersandTemplate(Template): |
| 216 | delimiter = '&' |
| 217 | s = AmpersandTemplate('this &gift is for &{who} &&') |
Barry Warsaw | 8c72eae | 2004-11-01 03:52:43 +0000 | [diff] [blame] | 218 | 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 Hettinger | 6d19111 | 2004-09-14 02:34:08 +0000 | [diff] [blame] | 222 | s = AmpersandTemplate('this &gift is for &{who} &') |
Barry Warsaw | 8c72eae | 2004-11-01 03:52:43 +0000 | [diff] [blame] | 223 | raises(ValueError, s.substitute, dict(gift='bud', who='you')) |
| 224 | eq(s.safe_substitute(), 'this &gift is for &{who} &') |
| 225 | |
Georg Brandl | 89fad14 | 2010-03-14 10:23:39 +0000 | [diff] [blame] | 226 | 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 Warsaw | 8bee761 | 2004-08-25 02:22:30 +0000 | [diff] [blame] | 232 | |
| 233 | def test_main(): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 234 | from test import support |
Raymond Hettinger | 6d19111 | 2004-09-14 02:34:08 +0000 | [diff] [blame] | 235 | test_classes = [TestTemplate,] |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 236 | support.run_unittest(*test_classes) |
Barry Warsaw | 8bee761 | 2004-08-25 02:22:30 +0000 | [diff] [blame] | 237 | |
| 238 | |
| 239 | if __name__ == '__main__': |
Raymond Hettinger | 6d19111 | 2004-09-14 02:34:08 +0000 | [diff] [blame] | 240 | test_main() |