]> granicus.if.org Git - python/commitdiff
Close #11505: Improve string.py coverage
authorNick Coghlan <ncoghlan@gmail.com>
Mon, 14 Mar 2011 22:54:37 +0000 (08:54 +1000)
committerNick Coghlan <ncoghlan@gmail.com>
Mon, 14 Mar 2011 22:54:37 +0000 (08:54 +1000)
Lib/test/test_pep292.py
Lib/test/test_string.py
Misc/ACKS
Misc/NEWS

index a9676495e97eb761ed741926d4c0371e55672097..a1e52e92795a1720a0e93ed1eaa6a47c0ce44e94 100644 (file)
@@ -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<invalid>)            |
+            (?P<escaped>%(delim)s)   |
+            @(?P<named>%(id)s)       |
+            @{(?P<braced>%(id)s)}
+            )
+            """
+        s = MyPattern('$')
+        self.assertRaises(ValueError, s.substitute, dict())
+
     def test_percents(self):
         eq = self.assertEqual
         s = Template('%(foo)s $foo ${foo}')
index b495d69717a58e470174158290c211cd9e7dbbe3..c4b186a2739f69119d16d89e99104b54449a3a27 100644 (file)
@@ -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)
 
index 117cd8d97e16a88a6d25deb159b3358e128d3eee..0c5a26da809844c3482cad6d0dd965d1ba2872df 100644 (file)
--- 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
index 483e6607e95a30a1d3d6b28fb2e9eb584b1bdf31..56b1f4fb1ba1022c555da6bc1423c37377a77ca2 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -197,6 +197,8 @@ Tools/Demos
 Tests
 -----
 
+- Issue #11505: improves test coverage of string.py
+
 - Issue #11490: test_subprocess:test_leaking_fds_on_error no longer gives a
   false positive if the last directory in the path is inaccessible.