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 |
| 6 | from string import Template, SafeTemplate |
| 7 | |
| 8 | class TestTemplate(unittest.TestCase): |
| 9 | |
| 10 | def test_regular_templates(self): |
| 11 | s = Template('$who likes to eat a bag of $what worth $$100') |
| 12 | self.assertEqual(s % dict(who='tim', what='ham'), |
| 13 | 'tim likes to eat a bag of ham worth $100') |
| 14 | self.assertRaises(KeyError, lambda s, d: s % d, s, dict(who='tim')) |
| 15 | |
| 16 | def test_regular_templates_with_braces(self): |
| 17 | s = Template('$who likes ${what} for ${meal}') |
| 18 | self.assertEqual(s % dict(who='tim', what='ham', meal='dinner'), |
| 19 | 'tim likes ham for dinner') |
| 20 | self.assertRaises(KeyError, lambda s, d: s % d, |
| 21 | s, dict(who='tim', what='ham')) |
| 22 | |
| 23 | def test_escapes(self): |
| 24 | eq = self.assertEqual |
| 25 | s = Template('$who likes to eat a bag of $$what worth $$100') |
| 26 | eq(s % dict(who='tim', what='ham'), |
| 27 | 'tim likes to eat a bag of $what worth $100') |
| 28 | s = Template('$who likes $$') |
| 29 | eq(s % dict(who='tim', what='ham'), 'tim likes $') |
| 30 | |
| 31 | def test_percents(self): |
| 32 | s = Template('%(foo)s $foo ${foo}') |
| 33 | self.assertEqual(s % dict(foo='baz'), '%(foo)s baz baz') |
| 34 | s = SafeTemplate('%(foo)s $foo ${foo}') |
| 35 | self.assertEqual(s % dict(foo='baz'), '%(foo)s baz baz') |
| 36 | |
| 37 | def test_stringification(self): |
| 38 | s = Template('tim has eaten $count bags of ham today') |
| 39 | self.assertEqual(s % dict(count=7), |
| 40 | 'tim has eaten 7 bags of ham today') |
| 41 | s = SafeTemplate('tim has eaten $count bags of ham today') |
| 42 | self.assertEqual(s % dict(count=7), |
| 43 | 'tim has eaten 7 bags of ham today') |
| 44 | s = SafeTemplate('tim has eaten ${count} bags of ham today') |
| 45 | self.assertEqual(s % dict(count=7), |
| 46 | 'tim has eaten 7 bags of ham today') |
| 47 | |
| 48 | def test_SafeTemplate(self): |
| 49 | eq = self.assertEqual |
| 50 | s = SafeTemplate('$who likes ${what} for ${meal}') |
| 51 | eq(s % dict(who='tim'), |
| 52 | 'tim likes ${what} for ${meal}') |
| 53 | eq(s % dict(what='ham'), |
| 54 | '$who likes ham for ${meal}') |
| 55 | eq(s % dict(what='ham', meal='dinner'), |
| 56 | '$who likes ham for dinner') |
| 57 | eq(s % dict(who='tim', what='ham'), |
| 58 | 'tim likes ham for ${meal}') |
| 59 | eq(s % dict(who='tim', what='ham', meal='dinner'), |
| 60 | 'tim likes ham for dinner') |
| 61 | |
| 62 | def test_invalid_placeholders(self): |
| 63 | raises = self.assertRaises |
| 64 | s = Template('$who likes $') |
| 65 | raises(ValueError, lambda s, d: s % d, s, dict(who='tim')) |
| 66 | s = Template('$who likes ${what)') |
| 67 | raises(ValueError, lambda s, d: s % d, s, dict(who='tim')) |
| 68 | s = Template('$who likes $100') |
| 69 | raises(ValueError, lambda s, d: s % d, s, dict(who='tim')) |
| 70 | |
| 71 | |
| 72 | def suite(): |
| 73 | suite = unittest.TestSuite() |
| 74 | suite.addTest(unittest.makeSuite(TestTemplate)) |
| 75 | return suite |
| 76 | |
| 77 | |
| 78 | def test_main(): |
| 79 | from test import test_support |
| 80 | test_support.run_suite(suite()) |
| 81 | |
| 82 | |
| 83 | if __name__ == '__main__': |
| 84 | unittest.main() |