10a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Copyright (C) 2004 Python Software Foundation 20a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Author: barry@python.org (Barry Warsaw) 30a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# License: http://www.opensource.org/licenses/PythonSoftFoundation.php 40a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 50a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoimport unittest 60a8c90248264a8b26970b4473770bcc3df8515fJosh Gaofrom string import Template 70a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 80a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 90a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass Bag: 100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao pass 110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 120a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass Mapping: 130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __getitem__(self, name): 140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao obj = self 150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao for part in name.split('.'): 160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao try: 170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao obj = getattr(obj, part) 180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao except AttributeError: 190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao raise KeyError(name) 200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return obj 210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 230a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass TestTemplate(unittest.TestCase): 240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_regular_templates(self): 250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao s = Template('$who likes to eat a bag of $what worth $$100') 260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(s.substitute(dict(who='tim', what='ham')), 270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 'tim likes to eat a bag of ham worth $100') 280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(KeyError, s.substitute, dict(who='tim')) 290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_regular_templates_with_braces(self): 310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao s = Template('$who likes ${what} for ${meal}') 320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao d = dict(who='tim', what='ham', meal='dinner') 330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(s.substitute(d), 'tim likes ham for dinner') 340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(KeyError, s.substitute, 350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao dict(who='tim', what='ham')) 360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_escapes(self): 380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao eq = self.assertEqual 390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao s = Template('$who likes to eat a bag of $$what worth $$100') 400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao eq(s.substitute(dict(who='tim', what='ham')), 410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 'tim likes to eat a bag of $what worth $100') 420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao s = Template('$who likes $$') 430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao eq(s.substitute(dict(who='tim', what='ham')), 'tim likes $') 440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_percents(self): 460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao eq = self.assertEqual 470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao s = Template('%(foo)s $foo ${foo}') 480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao d = dict(foo='baz') 490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao eq(s.substitute(d), '%(foo)s baz baz') 500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao eq(s.safe_substitute(d), '%(foo)s baz baz') 510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_stringification(self): 530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao eq = self.assertEqual 540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao s = Template('tim has eaten $count bags of ham today') 550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao d = dict(count=7) 560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao eq(s.substitute(d), 'tim has eaten 7 bags of ham today') 570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao eq(s.safe_substitute(d), 'tim has eaten 7 bags of ham today') 580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao s = Template('tim has eaten ${count} bags of ham today') 590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao eq(s.substitute(d), 'tim has eaten 7 bags of ham today') 600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_tupleargs(self): 620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao eq = self.assertEqual 630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao s = Template('$who ate ${meal}') 640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao d = dict(who=('tim', 'fred'), meal=('ham', 'kung pao')) 650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao eq(s.substitute(d), "('tim', 'fred') ate ('ham', 'kung pao')") 660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao eq(s.safe_substitute(d), "('tim', 'fred') ate ('ham', 'kung pao')") 670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_SafeTemplate(self): 690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao eq = self.assertEqual 700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao s = Template('$who likes ${what} for ${meal}') 710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao eq(s.safe_substitute(dict(who='tim')), 'tim likes ${what} for ${meal}') 720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao eq(s.safe_substitute(dict(what='ham')), '$who likes ham for ${meal}') 730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao eq(s.safe_substitute(dict(what='ham', meal='dinner')), 740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao '$who likes ham for dinner') 750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao eq(s.safe_substitute(dict(who='tim', what='ham')), 760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 'tim likes ham for ${meal}') 770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao eq(s.safe_substitute(dict(who='tim', what='ham', meal='dinner')), 780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 'tim likes ham for dinner') 790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_invalid_placeholders(self): 810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao raises = self.assertRaises 820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao s = Template('$who likes $') 830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao raises(ValueError, s.substitute, dict(who='tim')) 840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao s = Template('$who likes ${what)') 850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao raises(ValueError, s.substitute, dict(who='tim')) 860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao s = Template('$who likes $100') 870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao raises(ValueError, s.substitute, dict(who='tim')) 880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_idpattern_override(self): 900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao class PathPattern(Template): 910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao idpattern = r'[_a-z][._a-z0-9]*' 920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao m = Mapping() 930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao m.bag = Bag() 940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao m.bag.foo = Bag() 950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao m.bag.foo.who = 'tim' 960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao m.bag.what = 'ham' 970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao s = PathPattern('$bag.foo.who likes to eat a bag of $bag.what') 980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(s.substitute(m), 'tim likes to eat a bag of ham') 990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_pattern_override(self): 1010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao class MyPattern(Template): 1020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao pattern = r""" 1030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao (?P<escaped>@{2}) | 1040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao @(?P<named>[_a-z][._a-z0-9]*) | 1050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao @{(?P<braced>[_a-z][._a-z0-9]*)} | 1060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao (?P<invalid>@) 1070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 1080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao m = Mapping() 1090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao m.bag = Bag() 1100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao m.bag.foo = Bag() 1110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao m.bag.foo.who = 'tim' 1120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao m.bag.what = 'ham' 1130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao s = MyPattern('@bag.foo.who likes to eat a bag of @bag.what') 1140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(s.substitute(m), 'tim likes to eat a bag of ham') 1150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao class BadPattern(Template): 1170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao pattern = r""" 1180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao (?P<badname>.*) | 1190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao (?P<escaped>@{2}) | 1200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao @(?P<named>[_a-z][._a-z0-9]*) | 1210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao @{(?P<braced>[_a-z][._a-z0-9]*)} | 1220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao (?P<invalid>@) | 1230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 1240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao s = BadPattern('@bag.foo.who likes to eat a bag of @bag.what') 1250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(ValueError, s.substitute, {}) 1260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(ValueError, s.safe_substitute, {}) 1270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_unicode_values(self): 1290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao s = Template('$who likes $what') 1300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao d = dict(who=u't\xffm', what=u'f\xfe\fed') 1310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(s.substitute(d), u't\xffm likes f\xfe\x0ced') 1320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_keyword_arguments(self): 1340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao eq = self.assertEqual 1350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao s = Template('$who likes $what') 1360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao eq(s.substitute(who='tim', what='ham'), 'tim likes ham') 1370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao eq(s.substitute(dict(who='tim'), what='ham'), 'tim likes ham') 1380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao eq(s.substitute(dict(who='fred', what='kung pao'), 1390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao who='tim', what='ham'), 1400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 'tim likes ham') 1410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao s = Template('the mapping is $mapping') 1420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao eq(s.substitute(dict(foo='none'), mapping='bozo'), 1430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 'the mapping is bozo') 1440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao eq(s.substitute(dict(mapping='one'), mapping='two'), 1450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 'the mapping is two') 1460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_keyword_arguments_safe(self): 1480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao eq = self.assertEqual 1490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao raises = self.assertRaises 1500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao s = Template('$who likes $what') 1510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao eq(s.safe_substitute(who='tim', what='ham'), 'tim likes ham') 1520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao eq(s.safe_substitute(dict(who='tim'), what='ham'), 'tim likes ham') 1530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao eq(s.safe_substitute(dict(who='fred', what='kung pao'), 1540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao who='tim', what='ham'), 1550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 'tim likes ham') 1560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao s = Template('the mapping is $mapping') 1570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao eq(s.safe_substitute(dict(foo='none'), mapping='bozo'), 1580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 'the mapping is bozo') 1590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao eq(s.safe_substitute(dict(mapping='one'), mapping='two'), 1600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 'the mapping is two') 1610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao d = dict(mapping='one') 1620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao raises(TypeError, s.substitute, d, {}) 1630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao raises(TypeError, s.safe_substitute, d, {}) 1640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_delimiter_override(self): 1660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao eq = self.assertEqual 1670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao raises = self.assertRaises 1680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao class AmpersandTemplate(Template): 1690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao delimiter = '&' 1700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao s = AmpersandTemplate('this &gift is for &{who} &&') 1710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao eq(s.substitute(gift='bud', who='you'), 'this bud is for you &') 1720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao raises(KeyError, s.substitute) 1730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao eq(s.safe_substitute(gift='bud', who='you'), 'this bud is for you &') 1740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao eq(s.safe_substitute(), 'this &gift is for &{who} &') 1750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao s = AmpersandTemplate('this &gift is for &{who} &') 1760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao raises(ValueError, s.substitute, dict(gift='bud', who='you')) 1770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao eq(s.safe_substitute(), 'this &gift is for &{who} &') 1780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao class PieDelims(Template): 1800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao delimiter = '@' 1810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao s = PieDelims('@who likes to eat a bag of @{what} worth $100') 1820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(s.substitute(dict(who='tim', what='ham')), 1830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 'tim likes to eat a bag of ham worth $100') 1840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1860a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef test_main(): 1870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao from test import test_support 1880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao test_classes = [TestTemplate,] 1890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao test_support.run_unittest(*test_classes) 1900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1920a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoif __name__ == '__main__': 1930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao test_main() 194