diff -r 54d7e9919876 Lib/test/test_string.py --- a/Lib/test/test_string.py Thu Mar 24 13:55:58 2016 +0100 +++ b/Lib/test/test_string.py Thu Jun 02 10:23:08 2016 -0700 @@ -186,6 +186,29 @@ fmt._vformat("{i}", args, kwargs, set(), -1) self.assertIn("recursion", str(err.exception)) + def test_basic_template_convert(self): + template = string.Template('The $animal says $sound') + sub = template.substitute(animal='duck', sound='quack') + self.assertEqual(sub, "The duck says quack") + + def test_invalid_args_for_substitution(self): + template = string.Template('The $animal says $sound') + self.assertRaises(TypeError, template.substitute, 'chicken', 'cluck') + self.assertRaises(TypeError, template.substitute, None) + + def test_invalid_placeholder_throws_value_error(self): + template = string.Template('The $thing costs $100') + self.assertRaises(ValueError, template.substitute, thing='chair') + + def test_placeholders_from_kws_take_precedence(self): + template = string.Template('The $animal says $sound') + mapping = {'animal': 'dog', 'sound': 'ruf'} + substitution = template.substitute(mapping, animal='cat', sound='meow') + self.assertEqual(substitution, 'The cat says meow') + + def test_safe_substitute(self): + template = string.Template('The $animal says $sound') + self.assertEqual(template.safe_substitute(animal='cow'), 'The cow says $sound') if __name__ == "__main__": unittest.main()