From 7462fa654ba50b3ab19f3bea37816b5894990618 Mon Sep 17 00:00:00 2001 From: Nick Coghlan Date: Wed, 16 Mar 2011 14:30:45 -0400 Subject: Backport improved test coverage for string.py --- Lib/test/test_pep292.py | 13 +++++++++++++ Lib/test/test_string.py | 24 ++++++++++++++++++++++++ Misc/ACKS | 1 + Misc/NEWS | 3 +++ 4 files changed, 41 insertions(+) diff --git a/Lib/test/test_pep292.py b/Lib/test/test_pep292.py index a967649..119c7ea 100644 --- a/Lib/test/test_pep292.py +++ b/Lib/test/test_pep292.py @@ -42,6 +42,19 @@ class TestTemplate(unittest.TestCase): s = Template('$who likes $$') eq(s.substitute(dict(who='tim', what='ham')), 'tim likes $') + def test_invalid(self): + class MyPattern(Template): + pattern = r""" + (?: + (?P) | + (?P%(delim)s) | + @(?P%(id)s) | + @{(?P%(id)s)} + ) + """ + s = MyPattern('$') + self.assertRaises(ValueError, s.substitute, dict()) + def test_percents(self): eq = self.assertEqual s = Template('%(foo)s $foo ${foo}') diff --git a/Lib/test/test_string.py b/Lib/test/test_string.py index f46be63..a352ee3 100644 --- a/Lib/test/test_string.py +++ b/Lib/test/test_string.py @@ -112,6 +112,30 @@ class ModuleTest(unittest.TestCase): self.assertRaises(ValueError, fmt.format, "{0}", 10, 20, i=100) self.assertRaises(ValueError, fmt.format, "{i}", 10, 20, i=100) + def test_vformat_assert(self): + cls = string.Formatter() + kwargs = { + "i": 100 + } + self.assertRaises(ValueError, cls._vformat, + cls.format, "{0}", kwargs, set(), -2) + + def test_convert_field(self): + cls = string.Formatter() + self.assertEqual(cls.format("{0!s}", 'foo'), 'foo') + self.assertRaises(ValueError, cls.format, "{0!h}", 'foo') + + def test_get_field(self): + cls = string.Formatter() + class MyClass: + name = 'lumberjack' + x = MyClass() + self.assertEqual(cls.format("{0.name}", x), 'lumberjack') + + lookup = ["eggs", "and", "spam"] + self.assertEqual(cls.format("{0[2]}", lookup), 'spam') + + def test_main(): support.run_unittest(ModuleTest) diff --git a/Misc/ACKS b/Misc/ACKS index 5c1bdc5..1e2b83d 100644 --- a/Misc/ACKS +++ b/Misc/ACKS @@ -32,6 +32,7 @@ Oliver Andrich Ross Andrus Jon Anglin Éric Araujo +Alicia Arlen Jason Asbahr David Ascher Chris AtLee diff --git a/Misc/NEWS b/Misc/NEWS index b44650c..419b0d5 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -122,6 +122,9 @@ Tools/Demos Tests ----- +- Issue #11505: improves test coverage of string.py. Patch by Alicia + Arlen. + - Issue #11548: Improve test coverage of the shutil module. Patch by Evan Dandrea. -- cgit v0.12