[Python-checkins] python/dist/src/Lib/test test_pep292.py,NONE,1.1

bwarsaw at users.sourceforge.net bwarsaw at users.sourceforge.net
Wed Aug 25 04:22:32 CEST 2004


Update of /cvsroot/python/python/dist/src/Lib/test
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv23842/Lib/test

Added Files:
	test_pep292.py 
Log Message:
PEP 292 classes Template and SafeTemplate are added to the string module.
This patch includes test cases and documentation updates, as well as NEWS file
updates.

This patch also updates the sre modules so that they don't import the string
module, breaking direct circular imports.



--- NEW FILE: test_pep292.py ---
# Copyright (C) 2004 Python Software Foundation
# Author: barry at python.org (Barry Warsaw)
# License: http://www.opensource.org/licenses/PythonSoftFoundation.php

import unittest
from string import Template, SafeTemplate

class TestTemplate(unittest.TestCase):

    def test_regular_templates(self):
        s = Template('$who likes to eat a bag of $what worth $$100')
        self.assertEqual(s % dict(who='tim', what='ham'),
                         'tim likes to eat a bag of ham worth $100')
        self.assertRaises(KeyError, lambda s, d: s % d, s, dict(who='tim'))

    def test_regular_templates_with_braces(self):
        s = Template('$who likes ${what} for ${meal}')
        self.assertEqual(s % dict(who='tim', what='ham', meal='dinner'),
                         'tim likes ham for dinner')
        self.assertRaises(KeyError, lambda s, d: s % d,
                          s, dict(who='tim', what='ham'))

    def test_escapes(self):
        eq = self.assertEqual
        s = Template('$who likes to eat a bag of $$what worth $$100')
        eq(s % dict(who='tim', what='ham'),
           'tim likes to eat a bag of $what worth $100')
        s = Template('$who likes $$')
        eq(s % dict(who='tim', what='ham'), 'tim likes $')

    def test_percents(self):
        s = Template('%(foo)s $foo ${foo}')
        self.assertEqual(s % dict(foo='baz'), '%(foo)s baz baz')
        s = SafeTemplate('%(foo)s $foo ${foo}')
        self.assertEqual(s % dict(foo='baz'), '%(foo)s baz baz')

    def test_stringification(self):
        s = Template('tim has eaten $count bags of ham today')
        self.assertEqual(s % dict(count=7),
                         'tim has eaten 7 bags of ham today')
        s = SafeTemplate('tim has eaten $count bags of ham today')
        self.assertEqual(s % dict(count=7),
                         'tim has eaten 7 bags of ham today')
        s = SafeTemplate('tim has eaten ${count} bags of ham today')
        self.assertEqual(s % dict(count=7),
                         'tim has eaten 7 bags of ham today')

    def test_SafeTemplate(self):
        eq = self.assertEqual
        s = SafeTemplate('$who likes ${what} for ${meal}')
        eq(s % dict(who='tim'),
           'tim likes ${what} for ${meal}')
        eq(s % dict(what='ham'),
           '$who likes ham for ${meal}')
        eq(s % dict(what='ham', meal='dinner'),
           '$who likes ham for dinner')
        eq(s % dict(who='tim', what='ham'),
           'tim likes ham for ${meal}')
        eq(s % dict(who='tim', what='ham', meal='dinner'),
           'tim likes ham for dinner')

    def test_invalid_placeholders(self):
        raises = self.assertRaises
        s = Template('$who likes $')
        raises(ValueError, lambda s, d: s % d, s, dict(who='tim'))
        s = Template('$who likes ${what)')
        raises(ValueError, lambda s, d: s % d, s, dict(who='tim'))
        s = Template('$who likes $100')
        raises(ValueError, lambda s, d: s % d, s, dict(who='tim'))


def suite():
    suite = unittest.TestSuite()
    suite.addTest(unittest.makeSuite(TestTemplate))
    return suite


def test_main():
    from test import test_support
    test_support.run_suite(suite())


if __name__ == '__main__':
    unittest.main()



More information about the Python-checkins mailing list