]> granicus.if.org Git - python/commitdiff
Backport improved test coverage for string.py
authorNick Coghlan <ncoghlan@gmail.com>
Wed, 16 Mar 2011 18:30:45 +0000 (14:30 -0400)
committerNick Coghlan <ncoghlan@gmail.com>
Wed, 16 Mar 2011 18:30:45 +0000 (14:30 -0400)
Lib/test/test_pep292.py
Lib/test/test_string.py
Misc/ACKS
Misc/NEWS

index a9676495e97eb761ed741926d4c0371e55672097..119c7ea8a1f1c3c3f22775e4488af3163036acbf 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 f46be631ed8175f45c8a52f41746c79c9f5a5007..a352ee356054b5d68853024e24055928f29f4b11 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 5c1bdc5904c76817aca8170d00d9d629b0fecfab..1e2b83d4cedf5901f6993cd0c3c6326a789a7baa 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 b44650c44ca8667abeac560c6bd96248683e5d1f..419b0d5dcc0d5a7a6b1395ca0ed1b41770c197b2 100644 (file)
--- 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.