14adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao# Copyright (C) 2004 Python Software Foundation 24adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao# Author: barry@python.org (Barry Warsaw) 34adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao# License: http://www.opensource.org/licenses/PythonSoftFoundation.php 44adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao 54adfde8bc82dd39f59e0445588c3e599ada477dJosh Gaoimport unittest 64adfde8bc82dd39f59e0445588c3e599ada477dJosh Gaofrom string import Template 74adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao 84adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao 94adfde8bc82dd39f59e0445588c3e599ada477dJosh Gaoclass Bag: 104adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao pass 114adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao 124adfde8bc82dd39f59e0445588c3e599ada477dJosh Gaoclass Mapping: 134adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao def __getitem__(self, name): 144adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao obj = self 154adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao for part in name.split('.'): 164adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao try: 174adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao obj = getattr(obj, part) 184adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao except AttributeError: 194adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao raise KeyError(name) 204adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao return obj 214adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao 224adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao 234adfde8bc82dd39f59e0445588c3e599ada477dJosh Gaoclass TestTemplate(unittest.TestCase): 244adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao def test_regular_templates(self): 254adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao s = Template('$who likes to eat a bag of $what worth $$100') 264adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao self.assertEqual(s.substitute(dict(who='tim', what='ham')), 274adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao 'tim likes to eat a bag of ham worth $100') 284adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao self.assertRaises(KeyError, s.substitute, dict(who='tim')) 294adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao 304adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao def test_regular_templates_with_braces(self): 314adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao s = Template('$who likes ${what} for ${meal}') 324adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao d = dict(who='tim', what='ham', meal='dinner') 334adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao self.assertEqual(s.substitute(d), 'tim likes ham for dinner') 344adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao self.assertRaises(KeyError, s.substitute, 354adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao dict(who='tim', what='ham')) 364adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao 374adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao def test_escapes(self): 384adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao eq = self.assertEqual 394adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao s = Template('$who likes to eat a bag of $$what worth $$100') 404adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao eq(s.substitute(dict(who='tim', what='ham')), 414adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao 'tim likes to eat a bag of $what worth $100') 424adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao s = Template('$who likes $$') 434adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao eq(s.substitute(dict(who='tim', what='ham')), 'tim likes $') 444adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao 454adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao def test_percents(self): 464adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao eq = self.assertEqual 474adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao s = Template('%(foo)s $foo ${foo}') 484adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao d = dict(foo='baz') 494adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao eq(s.substitute(d), '%(foo)s baz baz') 504adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao eq(s.safe_substitute(d), '%(foo)s baz baz') 514adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao 524adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao def test_stringification(self): 534adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao eq = self.assertEqual 544adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao s = Template('tim has eaten $count bags of ham today') 554adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao d = dict(count=7) 564adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao eq(s.substitute(d), 'tim has eaten 7 bags of ham today') 574adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao eq(s.safe_substitute(d), 'tim has eaten 7 bags of ham today') 584adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao s = Template('tim has eaten ${count} bags of ham today') 594adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao eq(s.substitute(d), 'tim has eaten 7 bags of ham today') 604adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao 614adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao def test_tupleargs(self): 624adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao eq = self.assertEqual 634adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao s = Template('$who ate ${meal}') 644adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao d = dict(who=('tim', 'fred'), meal=('ham', 'kung pao')) 654adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao eq(s.substitute(d), "('tim', 'fred') ate ('ham', 'kung pao')") 664adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao eq(s.safe_substitute(d), "('tim', 'fred') ate ('ham', 'kung pao')") 674adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao 684adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao def test_SafeTemplate(self): 694adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao eq = self.assertEqual 704adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao s = Template('$who likes ${what} for ${meal}') 714adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao eq(s.safe_substitute(dict(who='tim')), 'tim likes ${what} for ${meal}') 724adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao eq(s.safe_substitute(dict(what='ham')), '$who likes ham for ${meal}') 734adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao eq(s.safe_substitute(dict(what='ham', meal='dinner')), 744adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao '$who likes ham for dinner') 754adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao eq(s.safe_substitute(dict(who='tim', what='ham')), 764adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao 'tim likes ham for ${meal}') 774adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao eq(s.safe_substitute(dict(who='tim', what='ham', meal='dinner')), 784adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao 'tim likes ham for dinner') 794adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao 804adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao def test_invalid_placeholders(self): 814adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao raises = self.assertRaises 824adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao s = Template('$who likes $') 834adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao raises(ValueError, s.substitute, dict(who='tim')) 844adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao s = Template('$who likes ${what)') 854adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao raises(ValueError, s.substitute, dict(who='tim')) 864adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao s = Template('$who likes $100') 874adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao raises(ValueError, s.substitute, dict(who='tim')) 884adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao 894adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao def test_idpattern_override(self): 904adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao class PathPattern(Template): 914adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao idpattern = r'[_a-z][._a-z0-9]*' 924adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao m = Mapping() 934adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao m.bag = Bag() 944adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao m.bag.foo = Bag() 954adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao m.bag.foo.who = 'tim' 964adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao m.bag.what = 'ham' 974adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao s = PathPattern('$bag.foo.who likes to eat a bag of $bag.what') 984adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao self.assertEqual(s.substitute(m), 'tim likes to eat a bag of ham') 994adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao 1004adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao def test_pattern_override(self): 1014adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao class MyPattern(Template): 1024adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao pattern = r""" 1034adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao (?P<escaped>@{2}) | 1044adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao @(?P<named>[_a-z][._a-z0-9]*) | 1054adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao @{(?P<braced>[_a-z][._a-z0-9]*)} | 1064adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao (?P<invalid>@) 1074adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao """ 1084adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao m = Mapping() 1094adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao m.bag = Bag() 1104adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao m.bag.foo = Bag() 1114adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao m.bag.foo.who = 'tim' 1124adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao m.bag.what = 'ham' 1134adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao s = MyPattern('@bag.foo.who likes to eat a bag of @bag.what') 1144adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao self.assertEqual(s.substitute(m), 'tim likes to eat a bag of ham') 1154adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao 1164adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao class BadPattern(Template): 1174adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao pattern = r""" 1184adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao (?P<badname>.*) | 1194adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao (?P<escaped>@{2}) | 1204adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao @(?P<named>[_a-z][._a-z0-9]*) | 1214adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao @{(?P<braced>[_a-z][._a-z0-9]*)} | 1224adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao (?P<invalid>@) | 1234adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao """ 1244adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao s = BadPattern('@bag.foo.who likes to eat a bag of @bag.what') 1254adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao self.assertRaises(ValueError, s.substitute, {}) 1264adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao self.assertRaises(ValueError, s.safe_substitute, {}) 1274adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao 1284adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao def test_unicode_values(self): 1294adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao s = Template('$who likes $what') 1304adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao d = dict(who=u't\xffm', what=u'f\xfe\fed') 1314adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao self.assertEqual(s.substitute(d), u't\xffm likes f\xfe\x0ced') 1324adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao 1334adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao def test_keyword_arguments(self): 1344adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao eq = self.assertEqual 1354adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao s = Template('$who likes $what') 1364adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao eq(s.substitute(who='tim', what='ham'), 'tim likes ham') 1374adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao eq(s.substitute(dict(who='tim'), what='ham'), 'tim likes ham') 1384adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao eq(s.substitute(dict(who='fred', what='kung pao'), 1394adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao who='tim', what='ham'), 1404adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao 'tim likes ham') 1414adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao s = Template('the mapping is $mapping') 1424adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao eq(s.substitute(dict(foo='none'), mapping='bozo'), 1434adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao 'the mapping is bozo') 1444adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao eq(s.substitute(dict(mapping='one'), mapping='two'), 1454adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao 'the mapping is two') 1464adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao 1474adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao def test_keyword_arguments_safe(self): 1484adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao eq = self.assertEqual 1494adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao raises = self.assertRaises 1504adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao s = Template('$who likes $what') 1514adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao eq(s.safe_substitute(who='tim', what='ham'), 'tim likes ham') 1524adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao eq(s.safe_substitute(dict(who='tim'), what='ham'), 'tim likes ham') 1534adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao eq(s.safe_substitute(dict(who='fred', what='kung pao'), 1544adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao who='tim', what='ham'), 1554adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao 'tim likes ham') 1564adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao s = Template('the mapping is $mapping') 1574adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao eq(s.safe_substitute(dict(foo='none'), mapping='bozo'), 1584adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao 'the mapping is bozo') 1594adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao eq(s.safe_substitute(dict(mapping='one'), mapping='two'), 1604adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao 'the mapping is two') 1614adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao d = dict(mapping='one') 1624adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao raises(TypeError, s.substitute, d, {}) 1634adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao raises(TypeError, s.safe_substitute, d, {}) 1644adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao 1654adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao def test_delimiter_override(self): 1664adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao eq = self.assertEqual 1674adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao raises = self.assertRaises 1684adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao class AmpersandTemplate(Template): 1694adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao delimiter = '&' 1704adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao s = AmpersandTemplate('this &gift is for &{who} &&') 1714adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao eq(s.substitute(gift='bud', who='you'), 'this bud is for you &') 1724adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao raises(KeyError, s.substitute) 1734adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao eq(s.safe_substitute(gift='bud', who='you'), 'this bud is for you &') 1744adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao eq(s.safe_substitute(), 'this &gift is for &{who} &') 1754adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao s = AmpersandTemplate('this &gift is for &{who} &') 1764adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao raises(ValueError, s.substitute, dict(gift='bud', who='you')) 1774adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao eq(s.safe_substitute(), 'this &gift is for &{who} &') 1784adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao 1794adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao class PieDelims(Template): 1804adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao delimiter = '@' 1814adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao s = PieDelims('@who likes to eat a bag of @{what} worth $100') 1824adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao self.assertEqual(s.substitute(dict(who='tim', what='ham')), 1834adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao 'tim likes to eat a bag of ham worth $100') 1844adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao 1854adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao 1864adfde8bc82dd39f59e0445588c3e599ada477dJosh Gaodef test_main(): 1874adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao from test import test_support 1884adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao test_classes = [TestTemplate,] 1894adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao test_support.run_unittest(*test_classes) 1904adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao 1914adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao 1924adfde8bc82dd39f59e0445588c3e599ada477dJosh Gaoif __name__ == '__main__': 1934adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao test_main() 194