| 1 | n/a | import unittest |
|---|
| 2 | n/a | import string |
|---|
| 3 | n/a | from string import Template |
|---|
| 4 | n/a | |
|---|
| 5 | n/a | |
|---|
| 6 | n/a | class ModuleTest(unittest.TestCase): |
|---|
| 7 | n/a | |
|---|
| 8 | n/a | def test_attrs(self): |
|---|
| 9 | n/a | # While the exact order of the items in these attributes is not |
|---|
| 10 | n/a | # technically part of the "language spec", in practice there is almost |
|---|
| 11 | n/a | # certainly user code that depends on the order, so de-facto it *is* |
|---|
| 12 | n/a | # part of the spec. |
|---|
| 13 | n/a | self.assertEqual(string.whitespace, ' \t\n\r\x0b\x0c') |
|---|
| 14 | n/a | self.assertEqual(string.ascii_lowercase, 'abcdefghijklmnopqrstuvwxyz') |
|---|
| 15 | n/a | self.assertEqual(string.ascii_uppercase, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ') |
|---|
| 16 | n/a | self.assertEqual(string.ascii_letters, string.ascii_lowercase + string.ascii_uppercase) |
|---|
| 17 | n/a | self.assertEqual(string.digits, '0123456789') |
|---|
| 18 | n/a | self.assertEqual(string.hexdigits, string.digits + 'abcdefABCDEF') |
|---|
| 19 | n/a | self.assertEqual(string.octdigits, '01234567') |
|---|
| 20 | n/a | self.assertEqual(string.punctuation, '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~') |
|---|
| 21 | n/a | self.assertEqual(string.printable, string.digits + string.ascii_lowercase + string.ascii_uppercase + string.punctuation + string.whitespace) |
|---|
| 22 | n/a | |
|---|
| 23 | n/a | def test_capwords(self): |
|---|
| 24 | n/a | self.assertEqual(string.capwords('abc def ghi'), 'Abc Def Ghi') |
|---|
| 25 | n/a | self.assertEqual(string.capwords('abc\tdef\nghi'), 'Abc Def Ghi') |
|---|
| 26 | n/a | self.assertEqual(string.capwords('abc\t def \nghi'), 'Abc Def Ghi') |
|---|
| 27 | n/a | self.assertEqual(string.capwords('ABC DEF GHI'), 'Abc Def Ghi') |
|---|
| 28 | n/a | self.assertEqual(string.capwords('ABC-DEF-GHI', '-'), 'Abc-Def-Ghi') |
|---|
| 29 | n/a | self.assertEqual(string.capwords('ABC-def DEF-ghi GHI'), 'Abc-def Def-ghi Ghi') |
|---|
| 30 | n/a | self.assertEqual(string.capwords(' aBc DeF '), 'Abc Def') |
|---|
| 31 | n/a | self.assertEqual(string.capwords('\taBc\tDeF\t'), 'Abc Def') |
|---|
| 32 | n/a | self.assertEqual(string.capwords('\taBc\tDeF\t', '\t'), '\tAbc\tDef\t') |
|---|
| 33 | n/a | |
|---|
| 34 | n/a | def test_basic_formatter(self): |
|---|
| 35 | n/a | fmt = string.Formatter() |
|---|
| 36 | n/a | self.assertEqual(fmt.format("foo"), "foo") |
|---|
| 37 | n/a | self.assertEqual(fmt.format("foo{0}", "bar"), "foobar") |
|---|
| 38 | n/a | self.assertEqual(fmt.format("foo{1}{0}-{1}", "bar", 6), "foo6bar-6") |
|---|
| 39 | n/a | self.assertRaises(TypeError, fmt.format) |
|---|
| 40 | n/a | self.assertRaises(TypeError, string.Formatter.format) |
|---|
| 41 | n/a | |
|---|
| 42 | n/a | def test_format_keyword_arguments(self): |
|---|
| 43 | n/a | fmt = string.Formatter() |
|---|
| 44 | n/a | self.assertEqual(fmt.format("-{arg}-", arg='test'), '-test-') |
|---|
| 45 | n/a | self.assertRaises(KeyError, fmt.format, "-{arg}-") |
|---|
| 46 | n/a | self.assertEqual(fmt.format("-{self}-", self='test'), '-test-') |
|---|
| 47 | n/a | self.assertRaises(KeyError, fmt.format, "-{self}-") |
|---|
| 48 | n/a | self.assertEqual(fmt.format("-{format_string}-", format_string='test'), |
|---|
| 49 | n/a | '-test-') |
|---|
| 50 | n/a | self.assertRaises(KeyError, fmt.format, "-{format_string}-") |
|---|
| 51 | n/a | with self.assertRaisesRegex(TypeError, "format_string"): |
|---|
| 52 | n/a | fmt.format(format_string="-{arg}-", arg='test') |
|---|
| 53 | n/a | |
|---|
| 54 | n/a | def test_auto_numbering(self): |
|---|
| 55 | n/a | fmt = string.Formatter() |
|---|
| 56 | n/a | self.assertEqual(fmt.format('foo{}{}', 'bar', 6), |
|---|
| 57 | n/a | 'foo{}{}'.format('bar', 6)) |
|---|
| 58 | n/a | self.assertEqual(fmt.format('foo{1}{num}{1}', None, 'bar', num=6), |
|---|
| 59 | n/a | 'foo{1}{num}{1}'.format(None, 'bar', num=6)) |
|---|
| 60 | n/a | self.assertEqual(fmt.format('{:^{}}', 'bar', 6), |
|---|
| 61 | n/a | '{:^{}}'.format('bar', 6)) |
|---|
| 62 | n/a | self.assertEqual(fmt.format('{:^{}} {}', 'bar', 6, 'X'), |
|---|
| 63 | n/a | '{:^{}} {}'.format('bar', 6, 'X')) |
|---|
| 64 | n/a | self.assertEqual(fmt.format('{:^{pad}}{}', 'foo', 'bar', pad=6), |
|---|
| 65 | n/a | '{:^{pad}}{}'.format('foo', 'bar', pad=6)) |
|---|
| 66 | n/a | |
|---|
| 67 | n/a | with self.assertRaises(ValueError): |
|---|
| 68 | n/a | fmt.format('foo{1}{}', 'bar', 6) |
|---|
| 69 | n/a | |
|---|
| 70 | n/a | with self.assertRaises(ValueError): |
|---|
| 71 | n/a | fmt.format('foo{}{1}', 'bar', 6) |
|---|
| 72 | n/a | |
|---|
| 73 | n/a | def test_conversion_specifiers(self): |
|---|
| 74 | n/a | fmt = string.Formatter() |
|---|
| 75 | n/a | self.assertEqual(fmt.format("-{arg!r}-", arg='test'), "-'test'-") |
|---|
| 76 | n/a | self.assertEqual(fmt.format("{0!s}", 'test'), 'test') |
|---|
| 77 | n/a | self.assertRaises(ValueError, fmt.format, "{0!h}", 'test') |
|---|
| 78 | n/a | # issue13579 |
|---|
| 79 | n/a | self.assertEqual(fmt.format("{0!a}", 42), '42') |
|---|
| 80 | n/a | self.assertEqual(fmt.format("{0!a}", string.ascii_letters), |
|---|
| 81 | n/a | "'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'") |
|---|
| 82 | n/a | self.assertEqual(fmt.format("{0!a}", chr(255)), "'\\xff'") |
|---|
| 83 | n/a | self.assertEqual(fmt.format("{0!a}", chr(256)), "'\\u0100'") |
|---|
| 84 | n/a | |
|---|
| 85 | n/a | def test_name_lookup(self): |
|---|
| 86 | n/a | fmt = string.Formatter() |
|---|
| 87 | n/a | class AnyAttr: |
|---|
| 88 | n/a | def __getattr__(self, attr): |
|---|
| 89 | n/a | return attr |
|---|
| 90 | n/a | x = AnyAttr() |
|---|
| 91 | n/a | self.assertEqual(fmt.format("{0.lumber}{0.jack}", x), 'lumberjack') |
|---|
| 92 | n/a | with self.assertRaises(AttributeError): |
|---|
| 93 | n/a | fmt.format("{0.lumber}{0.jack}", '') |
|---|
| 94 | n/a | |
|---|
| 95 | n/a | def test_index_lookup(self): |
|---|
| 96 | n/a | fmt = string.Formatter() |
|---|
| 97 | n/a | lookup = ["eggs", "and", "spam"] |
|---|
| 98 | n/a | self.assertEqual(fmt.format("{0[2]}{0[0]}", lookup), 'spameggs') |
|---|
| 99 | n/a | with self.assertRaises(IndexError): |
|---|
| 100 | n/a | fmt.format("{0[2]}{0[0]}", []) |
|---|
| 101 | n/a | with self.assertRaises(KeyError): |
|---|
| 102 | n/a | fmt.format("{0[2]}{0[0]}", {}) |
|---|
| 103 | n/a | |
|---|
| 104 | n/a | def test_override_get_value(self): |
|---|
| 105 | n/a | class NamespaceFormatter(string.Formatter): |
|---|
| 106 | n/a | def __init__(self, namespace={}): |
|---|
| 107 | n/a | string.Formatter.__init__(self) |
|---|
| 108 | n/a | self.namespace = namespace |
|---|
| 109 | n/a | |
|---|
| 110 | n/a | def get_value(self, key, args, kwds): |
|---|
| 111 | n/a | if isinstance(key, str): |
|---|
| 112 | n/a | try: |
|---|
| 113 | n/a | # Check explicitly passed arguments first |
|---|
| 114 | n/a | return kwds[key] |
|---|
| 115 | n/a | except KeyError: |
|---|
| 116 | n/a | return self.namespace[key] |
|---|
| 117 | n/a | else: |
|---|
| 118 | n/a | string.Formatter.get_value(key, args, kwds) |
|---|
| 119 | n/a | |
|---|
| 120 | n/a | fmt = NamespaceFormatter({'greeting':'hello'}) |
|---|
| 121 | n/a | self.assertEqual(fmt.format("{greeting}, world!"), 'hello, world!') |
|---|
| 122 | n/a | |
|---|
| 123 | n/a | |
|---|
| 124 | n/a | def test_override_format_field(self): |
|---|
| 125 | n/a | class CallFormatter(string.Formatter): |
|---|
| 126 | n/a | def format_field(self, value, format_spec): |
|---|
| 127 | n/a | return format(value(), format_spec) |
|---|
| 128 | n/a | |
|---|
| 129 | n/a | fmt = CallFormatter() |
|---|
| 130 | n/a | self.assertEqual(fmt.format('*{0}*', lambda : 'result'), '*result*') |
|---|
| 131 | n/a | |
|---|
| 132 | n/a | |
|---|
| 133 | n/a | def test_override_convert_field(self): |
|---|
| 134 | n/a | class XFormatter(string.Formatter): |
|---|
| 135 | n/a | def convert_field(self, value, conversion): |
|---|
| 136 | n/a | if conversion == 'x': |
|---|
| 137 | n/a | return None |
|---|
| 138 | n/a | return super().convert_field(value, conversion) |
|---|
| 139 | n/a | |
|---|
| 140 | n/a | fmt = XFormatter() |
|---|
| 141 | n/a | self.assertEqual(fmt.format("{0!r}:{0!x}", 'foo', 'foo'), "'foo':None") |
|---|
| 142 | n/a | |
|---|
| 143 | n/a | |
|---|
| 144 | n/a | def test_override_parse(self): |
|---|
| 145 | n/a | class BarFormatter(string.Formatter): |
|---|
| 146 | n/a | # returns an iterable that contains tuples of the form: |
|---|
| 147 | n/a | # (literal_text, field_name, format_spec, conversion) |
|---|
| 148 | n/a | def parse(self, format_string): |
|---|
| 149 | n/a | for field in format_string.split('|'): |
|---|
| 150 | n/a | if field[0] == '+': |
|---|
| 151 | n/a | # it's markup |
|---|
| 152 | n/a | field_name, _, format_spec = field[1:].partition(':') |
|---|
| 153 | n/a | yield '', field_name, format_spec, None |
|---|
| 154 | n/a | else: |
|---|
| 155 | n/a | yield field, None, None, None |
|---|
| 156 | n/a | |
|---|
| 157 | n/a | fmt = BarFormatter() |
|---|
| 158 | n/a | self.assertEqual(fmt.format('*|+0:^10s|*', 'foo'), '* foo *') |
|---|
| 159 | n/a | |
|---|
| 160 | n/a | def test_check_unused_args(self): |
|---|
| 161 | n/a | class CheckAllUsedFormatter(string.Formatter): |
|---|
| 162 | n/a | def check_unused_args(self, used_args, args, kwargs): |
|---|
| 163 | n/a | # Track which arguments actually got used |
|---|
| 164 | n/a | unused_args = set(kwargs.keys()) |
|---|
| 165 | n/a | unused_args.update(range(0, len(args))) |
|---|
| 166 | n/a | |
|---|
| 167 | n/a | for arg in used_args: |
|---|
| 168 | n/a | unused_args.remove(arg) |
|---|
| 169 | n/a | |
|---|
| 170 | n/a | if unused_args: |
|---|
| 171 | n/a | raise ValueError("unused arguments") |
|---|
| 172 | n/a | |
|---|
| 173 | n/a | fmt = CheckAllUsedFormatter() |
|---|
| 174 | n/a | self.assertEqual(fmt.format("{0}", 10), "10") |
|---|
| 175 | n/a | self.assertEqual(fmt.format("{0}{i}", 10, i=100), "10100") |
|---|
| 176 | n/a | self.assertEqual(fmt.format("{0}{i}{1}", 10, 20, i=100), "1010020") |
|---|
| 177 | n/a | self.assertRaises(ValueError, fmt.format, "{0}{i}{1}", 10, 20, i=100, j=0) |
|---|
| 178 | n/a | self.assertRaises(ValueError, fmt.format, "{0}", 10, 20) |
|---|
| 179 | n/a | self.assertRaises(ValueError, fmt.format, "{0}", 10, 20, i=100) |
|---|
| 180 | n/a | self.assertRaises(ValueError, fmt.format, "{i}", 10, 20, i=100) |
|---|
| 181 | n/a | |
|---|
| 182 | n/a | def test_vformat_recursion_limit(self): |
|---|
| 183 | n/a | fmt = string.Formatter() |
|---|
| 184 | n/a | args = () |
|---|
| 185 | n/a | kwargs = dict(i=100) |
|---|
| 186 | n/a | with self.assertRaises(ValueError) as err: |
|---|
| 187 | n/a | fmt._vformat("{i}", args, kwargs, set(), -1) |
|---|
| 188 | n/a | self.assertIn("recursion", str(err.exception)) |
|---|
| 189 | n/a | |
|---|
| 190 | n/a | |
|---|
| 191 | n/a | # Template tests (formerly housed in test_pep292.py) |
|---|
| 192 | n/a | |
|---|
| 193 | n/a | class Bag: |
|---|
| 194 | n/a | pass |
|---|
| 195 | n/a | |
|---|
| 196 | n/a | class Mapping: |
|---|
| 197 | n/a | def __getitem__(self, name): |
|---|
| 198 | n/a | obj = self |
|---|
| 199 | n/a | for part in name.split('.'): |
|---|
| 200 | n/a | try: |
|---|
| 201 | n/a | obj = getattr(obj, part) |
|---|
| 202 | n/a | except AttributeError: |
|---|
| 203 | n/a | raise KeyError(name) |
|---|
| 204 | n/a | return obj |
|---|
| 205 | n/a | |
|---|
| 206 | n/a | |
|---|
| 207 | n/a | class TestTemplate(unittest.TestCase): |
|---|
| 208 | n/a | def test_regular_templates(self): |
|---|
| 209 | n/a | s = Template('$who likes to eat a bag of $what worth $$100') |
|---|
| 210 | n/a | self.assertEqual(s.substitute(dict(who='tim', what='ham')), |
|---|
| 211 | n/a | 'tim likes to eat a bag of ham worth $100') |
|---|
| 212 | n/a | self.assertRaises(KeyError, s.substitute, dict(who='tim')) |
|---|
| 213 | n/a | self.assertRaises(TypeError, Template.substitute) |
|---|
| 214 | n/a | |
|---|
| 215 | n/a | def test_regular_templates_with_braces(self): |
|---|
| 216 | n/a | s = Template('$who likes ${what} for ${meal}') |
|---|
| 217 | n/a | d = dict(who='tim', what='ham', meal='dinner') |
|---|
| 218 | n/a | self.assertEqual(s.substitute(d), 'tim likes ham for dinner') |
|---|
| 219 | n/a | self.assertRaises(KeyError, s.substitute, |
|---|
| 220 | n/a | dict(who='tim', what='ham')) |
|---|
| 221 | n/a | |
|---|
| 222 | n/a | def test_escapes(self): |
|---|
| 223 | n/a | eq = self.assertEqual |
|---|
| 224 | n/a | s = Template('$who likes to eat a bag of $$what worth $$100') |
|---|
| 225 | n/a | eq(s.substitute(dict(who='tim', what='ham')), |
|---|
| 226 | n/a | 'tim likes to eat a bag of $what worth $100') |
|---|
| 227 | n/a | s = Template('$who likes $$') |
|---|
| 228 | n/a | eq(s.substitute(dict(who='tim', what='ham')), 'tim likes $') |
|---|
| 229 | n/a | |
|---|
| 230 | n/a | def test_percents(self): |
|---|
| 231 | n/a | eq = self.assertEqual |
|---|
| 232 | n/a | s = Template('%(foo)s $foo ${foo}') |
|---|
| 233 | n/a | d = dict(foo='baz') |
|---|
| 234 | n/a | eq(s.substitute(d), '%(foo)s baz baz') |
|---|
| 235 | n/a | eq(s.safe_substitute(d), '%(foo)s baz baz') |
|---|
| 236 | n/a | |
|---|
| 237 | n/a | def test_stringification(self): |
|---|
| 238 | n/a | eq = self.assertEqual |
|---|
| 239 | n/a | s = Template('tim has eaten $count bags of ham today') |
|---|
| 240 | n/a | d = dict(count=7) |
|---|
| 241 | n/a | eq(s.substitute(d), 'tim has eaten 7 bags of ham today') |
|---|
| 242 | n/a | eq(s.safe_substitute(d), 'tim has eaten 7 bags of ham today') |
|---|
| 243 | n/a | s = Template('tim has eaten ${count} bags of ham today') |
|---|
| 244 | n/a | eq(s.substitute(d), 'tim has eaten 7 bags of ham today') |
|---|
| 245 | n/a | |
|---|
| 246 | n/a | def test_tupleargs(self): |
|---|
| 247 | n/a | eq = self.assertEqual |
|---|
| 248 | n/a | s = Template('$who ate ${meal}') |
|---|
| 249 | n/a | d = dict(who=('tim', 'fred'), meal=('ham', 'kung pao')) |
|---|
| 250 | n/a | eq(s.substitute(d), "('tim', 'fred') ate ('ham', 'kung pao')") |
|---|
| 251 | n/a | eq(s.safe_substitute(d), "('tim', 'fred') ate ('ham', 'kung pao')") |
|---|
| 252 | n/a | |
|---|
| 253 | n/a | def test_SafeTemplate(self): |
|---|
| 254 | n/a | eq = self.assertEqual |
|---|
| 255 | n/a | s = Template('$who likes ${what} for ${meal}') |
|---|
| 256 | n/a | eq(s.safe_substitute(dict(who='tim')), 'tim likes ${what} for ${meal}') |
|---|
| 257 | n/a | eq(s.safe_substitute(dict(what='ham')), '$who likes ham for ${meal}') |
|---|
| 258 | n/a | eq(s.safe_substitute(dict(what='ham', meal='dinner')), |
|---|
| 259 | n/a | '$who likes ham for dinner') |
|---|
| 260 | n/a | eq(s.safe_substitute(dict(who='tim', what='ham')), |
|---|
| 261 | n/a | 'tim likes ham for ${meal}') |
|---|
| 262 | n/a | eq(s.safe_substitute(dict(who='tim', what='ham', meal='dinner')), |
|---|
| 263 | n/a | 'tim likes ham for dinner') |
|---|
| 264 | n/a | |
|---|
| 265 | n/a | def test_invalid_placeholders(self): |
|---|
| 266 | n/a | raises = self.assertRaises |
|---|
| 267 | n/a | s = Template('$who likes $') |
|---|
| 268 | n/a | raises(ValueError, s.substitute, dict(who='tim')) |
|---|
| 269 | n/a | s = Template('$who likes ${what)') |
|---|
| 270 | n/a | raises(ValueError, s.substitute, dict(who='tim')) |
|---|
| 271 | n/a | s = Template('$who likes $100') |
|---|
| 272 | n/a | raises(ValueError, s.substitute, dict(who='tim')) |
|---|
| 273 | n/a | |
|---|
| 274 | n/a | def test_idpattern_override(self): |
|---|
| 275 | n/a | class PathPattern(Template): |
|---|
| 276 | n/a | idpattern = r'[_a-z][._a-z0-9]*' |
|---|
| 277 | n/a | m = Mapping() |
|---|
| 278 | n/a | m.bag = Bag() |
|---|
| 279 | n/a | m.bag.foo = Bag() |
|---|
| 280 | n/a | m.bag.foo.who = 'tim' |
|---|
| 281 | n/a | m.bag.what = 'ham' |
|---|
| 282 | n/a | s = PathPattern('$bag.foo.who likes to eat a bag of $bag.what') |
|---|
| 283 | n/a | self.assertEqual(s.substitute(m), 'tim likes to eat a bag of ham') |
|---|
| 284 | n/a | |
|---|
| 285 | n/a | def test_pattern_override(self): |
|---|
| 286 | n/a | class MyPattern(Template): |
|---|
| 287 | n/a | pattern = r""" |
|---|
| 288 | n/a | (?P<escaped>@{2}) | |
|---|
| 289 | n/a | @(?P<named>[_a-z][._a-z0-9]*) | |
|---|
| 290 | n/a | @{(?P<braced>[_a-z][._a-z0-9]*)} | |
|---|
| 291 | n/a | (?P<invalid>@) |
|---|
| 292 | n/a | """ |
|---|
| 293 | n/a | m = Mapping() |
|---|
| 294 | n/a | m.bag = Bag() |
|---|
| 295 | n/a | m.bag.foo = Bag() |
|---|
| 296 | n/a | m.bag.foo.who = 'tim' |
|---|
| 297 | n/a | m.bag.what = 'ham' |
|---|
| 298 | n/a | s = MyPattern('@bag.foo.who likes to eat a bag of @bag.what') |
|---|
| 299 | n/a | self.assertEqual(s.substitute(m), 'tim likes to eat a bag of ham') |
|---|
| 300 | n/a | |
|---|
| 301 | n/a | class BadPattern(Template): |
|---|
| 302 | n/a | pattern = r""" |
|---|
| 303 | n/a | (?P<badname>.*) | |
|---|
| 304 | n/a | (?P<escaped>@{2}) | |
|---|
| 305 | n/a | @(?P<named>[_a-z][._a-z0-9]*) | |
|---|
| 306 | n/a | @{(?P<braced>[_a-z][._a-z0-9]*)} | |
|---|
| 307 | n/a | (?P<invalid>@) | |
|---|
| 308 | n/a | """ |
|---|
| 309 | n/a | s = BadPattern('@bag.foo.who likes to eat a bag of @bag.what') |
|---|
| 310 | n/a | self.assertRaises(ValueError, s.substitute, {}) |
|---|
| 311 | n/a | self.assertRaises(ValueError, s.safe_substitute, {}) |
|---|
| 312 | n/a | |
|---|
| 313 | n/a | def test_braced_override(self): |
|---|
| 314 | n/a | class MyTemplate(Template): |
|---|
| 315 | n/a | pattern = r""" |
|---|
| 316 | n/a | \$(?: |
|---|
| 317 | n/a | (?P<escaped>$) | |
|---|
| 318 | n/a | (?P<named>[_a-z][_a-z0-9]*) | |
|---|
| 319 | n/a | @@(?P<braced>[_a-z][_a-z0-9]*)@@ | |
|---|
| 320 | n/a | (?P<invalid>) | |
|---|
| 321 | n/a | ) |
|---|
| 322 | n/a | """ |
|---|
| 323 | n/a | |
|---|
| 324 | n/a | tmpl = 'PyCon in $@@location@@' |
|---|
| 325 | n/a | t = MyTemplate(tmpl) |
|---|
| 326 | n/a | self.assertRaises(KeyError, t.substitute, {}) |
|---|
| 327 | n/a | val = t.substitute({'location': 'Cleveland'}) |
|---|
| 328 | n/a | self.assertEqual(val, 'PyCon in Cleveland') |
|---|
| 329 | n/a | |
|---|
| 330 | n/a | def test_braced_override_safe(self): |
|---|
| 331 | n/a | class MyTemplate(Template): |
|---|
| 332 | n/a | pattern = r""" |
|---|
| 333 | n/a | \$(?: |
|---|
| 334 | n/a | (?P<escaped>$) | |
|---|
| 335 | n/a | (?P<named>[_a-z][_a-z0-9]*) | |
|---|
| 336 | n/a | @@(?P<braced>[_a-z][_a-z0-9]*)@@ | |
|---|
| 337 | n/a | (?P<invalid>) | |
|---|
| 338 | n/a | ) |
|---|
| 339 | n/a | """ |
|---|
| 340 | n/a | |
|---|
| 341 | n/a | tmpl = 'PyCon in $@@location@@' |
|---|
| 342 | n/a | t = MyTemplate(tmpl) |
|---|
| 343 | n/a | self.assertEqual(t.safe_substitute(), tmpl) |
|---|
| 344 | n/a | val = t.safe_substitute({'location': 'Cleveland'}) |
|---|
| 345 | n/a | self.assertEqual(val, 'PyCon in Cleveland') |
|---|
| 346 | n/a | |
|---|
| 347 | n/a | def test_invalid_with_no_lines(self): |
|---|
| 348 | n/a | # The error formatting for invalid templates |
|---|
| 349 | n/a | # has a special case for no data that the default |
|---|
| 350 | n/a | # pattern can't trigger (always has at least '$') |
|---|
| 351 | n/a | # So we craft a pattern that is always invalid |
|---|
| 352 | n/a | # with no leading data. |
|---|
| 353 | n/a | class MyTemplate(Template): |
|---|
| 354 | n/a | pattern = r""" |
|---|
| 355 | n/a | (?P<invalid>) | |
|---|
| 356 | n/a | unreachable( |
|---|
| 357 | n/a | (?P<named>) | |
|---|
| 358 | n/a | (?P<braced>) | |
|---|
| 359 | n/a | (?P<escaped>) |
|---|
| 360 | n/a | ) |
|---|
| 361 | n/a | """ |
|---|
| 362 | n/a | s = MyTemplate('') |
|---|
| 363 | n/a | with self.assertRaises(ValueError) as err: |
|---|
| 364 | n/a | s.substitute({}) |
|---|
| 365 | n/a | self.assertIn('line 1, col 1', str(err.exception)) |
|---|
| 366 | n/a | |
|---|
| 367 | n/a | def test_unicode_values(self): |
|---|
| 368 | n/a | s = Template('$who likes $what') |
|---|
| 369 | n/a | d = dict(who='t\xffm', what='f\xfe\fed') |
|---|
| 370 | n/a | self.assertEqual(s.substitute(d), 't\xffm likes f\xfe\x0ced') |
|---|
| 371 | n/a | |
|---|
| 372 | n/a | def test_keyword_arguments(self): |
|---|
| 373 | n/a | eq = self.assertEqual |
|---|
| 374 | n/a | s = Template('$who likes $what') |
|---|
| 375 | n/a | eq(s.substitute(who='tim', what='ham'), 'tim likes ham') |
|---|
| 376 | n/a | eq(s.substitute(dict(who='tim'), what='ham'), 'tim likes ham') |
|---|
| 377 | n/a | eq(s.substitute(dict(who='fred', what='kung pao'), |
|---|
| 378 | n/a | who='tim', what='ham'), |
|---|
| 379 | n/a | 'tim likes ham') |
|---|
| 380 | n/a | s = Template('the mapping is $mapping') |
|---|
| 381 | n/a | eq(s.substitute(dict(foo='none'), mapping='bozo'), |
|---|
| 382 | n/a | 'the mapping is bozo') |
|---|
| 383 | n/a | eq(s.substitute(dict(mapping='one'), mapping='two'), |
|---|
| 384 | n/a | 'the mapping is two') |
|---|
| 385 | n/a | |
|---|
| 386 | n/a | s = Template('the self is $self') |
|---|
| 387 | n/a | eq(s.substitute(self='bozo'), 'the self is bozo') |
|---|
| 388 | n/a | |
|---|
| 389 | n/a | def test_keyword_arguments_safe(self): |
|---|
| 390 | n/a | eq = self.assertEqual |
|---|
| 391 | n/a | raises = self.assertRaises |
|---|
| 392 | n/a | s = Template('$who likes $what') |
|---|
| 393 | n/a | eq(s.safe_substitute(who='tim', what='ham'), 'tim likes ham') |
|---|
| 394 | n/a | eq(s.safe_substitute(dict(who='tim'), what='ham'), 'tim likes ham') |
|---|
| 395 | n/a | eq(s.safe_substitute(dict(who='fred', what='kung pao'), |
|---|
| 396 | n/a | who='tim', what='ham'), |
|---|
| 397 | n/a | 'tim likes ham') |
|---|
| 398 | n/a | s = Template('the mapping is $mapping') |
|---|
| 399 | n/a | eq(s.safe_substitute(dict(foo='none'), mapping='bozo'), |
|---|
| 400 | n/a | 'the mapping is bozo') |
|---|
| 401 | n/a | eq(s.safe_substitute(dict(mapping='one'), mapping='two'), |
|---|
| 402 | n/a | 'the mapping is two') |
|---|
| 403 | n/a | d = dict(mapping='one') |
|---|
| 404 | n/a | raises(TypeError, s.substitute, d, {}) |
|---|
| 405 | n/a | raises(TypeError, s.safe_substitute, d, {}) |
|---|
| 406 | n/a | |
|---|
| 407 | n/a | s = Template('the self is $self') |
|---|
| 408 | n/a | eq(s.safe_substitute(self='bozo'), 'the self is bozo') |
|---|
| 409 | n/a | |
|---|
| 410 | n/a | def test_delimiter_override(self): |
|---|
| 411 | n/a | eq = self.assertEqual |
|---|
| 412 | n/a | raises = self.assertRaises |
|---|
| 413 | n/a | class AmpersandTemplate(Template): |
|---|
| 414 | n/a | delimiter = '&' |
|---|
| 415 | n/a | s = AmpersandTemplate('this &gift is for &{who} &&') |
|---|
| 416 | n/a | eq(s.substitute(gift='bud', who='you'), 'this bud is for you &') |
|---|
| 417 | n/a | raises(KeyError, s.substitute) |
|---|
| 418 | n/a | eq(s.safe_substitute(gift='bud', who='you'), 'this bud is for you &') |
|---|
| 419 | n/a | eq(s.safe_substitute(), 'this &gift is for &{who} &') |
|---|
| 420 | n/a | s = AmpersandTemplate('this &gift is for &{who} &') |
|---|
| 421 | n/a | raises(ValueError, s.substitute, dict(gift='bud', who='you')) |
|---|
| 422 | n/a | eq(s.safe_substitute(), 'this &gift is for &{who} &') |
|---|
| 423 | n/a | |
|---|
| 424 | n/a | class PieDelims(Template): |
|---|
| 425 | n/a | delimiter = '@' |
|---|
| 426 | n/a | s = PieDelims('@who likes to eat a bag of @{what} worth $100') |
|---|
| 427 | n/a | self.assertEqual(s.substitute(dict(who='tim', what='ham')), |
|---|
| 428 | n/a | 'tim likes to eat a bag of ham worth $100') |
|---|
| 429 | n/a | |
|---|
| 430 | n/a | |
|---|
| 431 | n/a | if __name__ == '__main__': |
|---|
| 432 | n/a | unittest.main() |
|---|