+++ /dev/null
-# Copyright (C) 2004 Python Software Foundation\r
-# Author: barry@python.org (Barry Warsaw)\r
-# License: http://www.opensource.org/licenses/PythonSoftFoundation.php\r
-\r
-import unittest\r
-from string import Template\r
-\r
-\r
-class Bag:\r
- pass\r
-\r
-class Mapping:\r
- def __getitem__(self, name):\r
- obj = self\r
- for part in name.split('.'):\r
- try:\r
- obj = getattr(obj, part)\r
- except AttributeError:\r
- raise KeyError(name)\r
- return obj\r
-\r
-\r
-class TestTemplate(unittest.TestCase):\r
- def test_regular_templates(self):\r
- s = Template('$who likes to eat a bag of $what worth $$100')\r
- self.assertEqual(s.substitute(dict(who='tim', what='ham')),\r
- 'tim likes to eat a bag of ham worth $100')\r
- self.assertRaises(KeyError, s.substitute, dict(who='tim'))\r
-\r
- def test_regular_templates_with_braces(self):\r
- s = Template('$who likes ${what} for ${meal}')\r
- d = dict(who='tim', what='ham', meal='dinner')\r
- self.assertEqual(s.substitute(d), 'tim likes ham for dinner')\r
- self.assertRaises(KeyError, s.substitute,\r
- dict(who='tim', what='ham'))\r
-\r
- def test_escapes(self):\r
- eq = self.assertEqual\r
- s = Template('$who likes to eat a bag of $$what worth $$100')\r
- eq(s.substitute(dict(who='tim', what='ham')),\r
- 'tim likes to eat a bag of $what worth $100')\r
- s = Template('$who likes $$')\r
- eq(s.substitute(dict(who='tim', what='ham')), 'tim likes $')\r
-\r
- def test_percents(self):\r
- eq = self.assertEqual\r
- s = Template('%(foo)s $foo ${foo}')\r
- d = dict(foo='baz')\r
- eq(s.substitute(d), '%(foo)s baz baz')\r
- eq(s.safe_substitute(d), '%(foo)s baz baz')\r
-\r
- def test_stringification(self):\r
- eq = self.assertEqual\r
- s = Template('tim has eaten $count bags of ham today')\r
- d = dict(count=7)\r
- eq(s.substitute(d), 'tim has eaten 7 bags of ham today')\r
- eq(s.safe_substitute(d), 'tim has eaten 7 bags of ham today')\r
- s = Template('tim has eaten ${count} bags of ham today')\r
- eq(s.substitute(d), 'tim has eaten 7 bags of ham today')\r
-\r
- def test_tupleargs(self):\r
- eq = self.assertEqual\r
- s = Template('$who ate ${meal}')\r
- d = dict(who=('tim', 'fred'), meal=('ham', 'kung pao'))\r
- eq(s.substitute(d), "('tim', 'fred') ate ('ham', 'kung pao')")\r
- eq(s.safe_substitute(d), "('tim', 'fred') ate ('ham', 'kung pao')")\r
-\r
- def test_SafeTemplate(self):\r
- eq = self.assertEqual\r
- s = Template('$who likes ${what} for ${meal}')\r
- eq(s.safe_substitute(dict(who='tim')), 'tim likes ${what} for ${meal}')\r
- eq(s.safe_substitute(dict(what='ham')), '$who likes ham for ${meal}')\r
- eq(s.safe_substitute(dict(what='ham', meal='dinner')),\r
- '$who likes ham for dinner')\r
- eq(s.safe_substitute(dict(who='tim', what='ham')),\r
- 'tim likes ham for ${meal}')\r
- eq(s.safe_substitute(dict(who='tim', what='ham', meal='dinner')),\r
- 'tim likes ham for dinner')\r
-\r
- def test_invalid_placeholders(self):\r
- raises = self.assertRaises\r
- s = Template('$who likes $')\r
- raises(ValueError, s.substitute, dict(who='tim'))\r
- s = Template('$who likes ${what)')\r
- raises(ValueError, s.substitute, dict(who='tim'))\r
- s = Template('$who likes $100')\r
- raises(ValueError, s.substitute, dict(who='tim'))\r
-\r
- def test_idpattern_override(self):\r
- class PathPattern(Template):\r
- idpattern = r'[_a-z][._a-z0-9]*'\r
- m = Mapping()\r
- m.bag = Bag()\r
- m.bag.foo = Bag()\r
- m.bag.foo.who = 'tim'\r
- m.bag.what = 'ham'\r
- s = PathPattern('$bag.foo.who likes to eat a bag of $bag.what')\r
- self.assertEqual(s.substitute(m), 'tim likes to eat a bag of ham')\r
-\r
- def test_pattern_override(self):\r
- class MyPattern(Template):\r
- pattern = r"""\r
- (?P<escaped>@{2}) |\r
- @(?P<named>[_a-z][._a-z0-9]*) |\r
- @{(?P<braced>[_a-z][._a-z0-9]*)} |\r
- (?P<invalid>@)\r
- """\r
- m = Mapping()\r
- m.bag = Bag()\r
- m.bag.foo = Bag()\r
- m.bag.foo.who = 'tim'\r
- m.bag.what = 'ham'\r
- s = MyPattern('@bag.foo.who likes to eat a bag of @bag.what')\r
- self.assertEqual(s.substitute(m), 'tim likes to eat a bag of ham')\r
-\r
- class BadPattern(Template):\r
- pattern = r"""\r
- (?P<badname>.*) |\r
- (?P<escaped>@{2}) |\r
- @(?P<named>[_a-z][._a-z0-9]*) |\r
- @{(?P<braced>[_a-z][._a-z0-9]*)} |\r
- (?P<invalid>@) |\r
- """\r
- s = BadPattern('@bag.foo.who likes to eat a bag of @bag.what')\r
- self.assertRaises(ValueError, s.substitute, {})\r
- self.assertRaises(ValueError, s.safe_substitute, {})\r
-\r
- def test_unicode_values(self):\r
- s = Template('$who likes $what')\r
- d = dict(who=u't\xffm', what=u'f\xfe\fed')\r
- self.assertEqual(s.substitute(d), u't\xffm likes f\xfe\x0ced')\r
-\r
- def test_keyword_arguments(self):\r
- eq = self.assertEqual\r
- s = Template('$who likes $what')\r
- eq(s.substitute(who='tim', what='ham'), 'tim likes ham')\r
- eq(s.substitute(dict(who='tim'), what='ham'), 'tim likes ham')\r
- eq(s.substitute(dict(who='fred', what='kung pao'),\r
- who='tim', what='ham'),\r
- 'tim likes ham')\r
- s = Template('the mapping is $mapping')\r
- eq(s.substitute(dict(foo='none'), mapping='bozo'),\r
- 'the mapping is bozo')\r
- eq(s.substitute(dict(mapping='one'), mapping='two'),\r
- 'the mapping is two')\r
-\r
- def test_keyword_arguments_safe(self):\r
- eq = self.assertEqual\r
- raises = self.assertRaises\r
- s = Template('$who likes $what')\r
- eq(s.safe_substitute(who='tim', what='ham'), 'tim likes ham')\r
- eq(s.safe_substitute(dict(who='tim'), what='ham'), 'tim likes ham')\r
- eq(s.safe_substitute(dict(who='fred', what='kung pao'),\r
- who='tim', what='ham'),\r
- 'tim likes ham')\r
- s = Template('the mapping is $mapping')\r
- eq(s.safe_substitute(dict(foo='none'), mapping='bozo'),\r
- 'the mapping is bozo')\r
- eq(s.safe_substitute(dict(mapping='one'), mapping='two'),\r
- 'the mapping is two')\r
- d = dict(mapping='one')\r
- raises(TypeError, s.substitute, d, {})\r
- raises(TypeError, s.safe_substitute, d, {})\r
-\r
- def test_delimiter_override(self):\r
- eq = self.assertEqual\r
- raises = self.assertRaises\r
- class AmpersandTemplate(Template):\r
- delimiter = '&'\r
- s = AmpersandTemplate('this &gift is for &{who} &&')\r
- eq(s.substitute(gift='bud', who='you'), 'this bud is for you &')\r
- raises(KeyError, s.substitute)\r
- eq(s.safe_substitute(gift='bud', who='you'), 'this bud is for you &')\r
- eq(s.safe_substitute(), 'this &gift is for &{who} &')\r
- s = AmpersandTemplate('this &gift is for &{who} &')\r
- raises(ValueError, s.substitute, dict(gift='bud', who='you'))\r
- eq(s.safe_substitute(), 'this &gift is for &{who} &')\r
-\r
- class PieDelims(Template):\r
- delimiter = '@'\r
- s = PieDelims('@who likes to eat a bag of @{what} worth $100')\r
- self.assertEqual(s.substitute(dict(who='tim', what='ham')),\r
- 'tim likes to eat a bag of ham worth $100')\r
-\r
-\r
-def test_main():\r
- from test import test_support\r
- test_classes = [TestTemplate,]\r
- test_support.run_unittest(*test_classes)\r
-\r
-\r
-if __name__ == '__main__':\r
- test_main()\r