summaryrefslogtreecommitdiffstats
path: root/Lib/test
diff options
context:
space:
mode:
authorEric Smith <eric@trueblade.com>2007-08-26 22:27:13 (GMT)
committerEric Smith <eric@trueblade.com>2007-08-26 22:27:13 (GMT)
commit7ade6485abde95c5cc9676ad3e476ba3aca98037 (patch)
treefa5710899c3e376f89eb6a6460e06f3feee62d58 /Lib/test
parent2bf4d5ba2881725bb7695bc0573bab0e2ca4fec5 (diff)
downloadcpython-7ade6485abde95c5cc9676ad3e476ba3aca98037.zip
cpython-7ade6485abde95c5cc9676ad3e476ba3aca98037.tar.gz
cpython-7ade6485abde95c5cc9676ad3e476ba3aca98037.tar.bz2
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Diffstat (limited to 'Lib/test')
-rw-r--r--Lib/test/test_string.py23
-rw-r--r--Lib/test/test_unicode.py9
2 files changed, 30 insertions, 2 deletions
diff --git a/Lib/test/test_string.py b/Lib/test/test_string.py
index ce9fe23..1706d2a 100644
--- a/Lib/test/test_string.py
+++ b/Lib/test/test_string.py
@@ -19,8 +19,27 @@ class ModuleTest(unittest.TestCase):
fmt = string.Formatter()
self.assertEqual(fmt.format("foo"), "foo")
- # Formatter not working you for lookups
- #self.assertEqual(fmt.format("foo{0}", "bar"), "foobar")
+ self.assertEqual(fmt.format("foo{0}", "bar"), "foobar")
+ self.assertEqual(fmt.format("foo{1}{0}-{1}", "bar", 6), "foo6bar-6")
+ self.assertEqual(fmt.format("-{arg!r}-", arg='test'), "-'test'-")
+
+ class NamespaceFormatter(string.Formatter):
+ def __init__(self, namespace={}):
+ string.Formatter.__init__(self)
+ self.namespace = namespace
+
+ def get_value(self, key, args, kwds):
+ if isinstance(key, str):
+ try:
+ # Check explicitly passed arguments first
+ return kwds[key]
+ except KeyError:
+ return self.namespace[key]
+ else:
+ string.Formatter.get_value(key, args, kwds)
+
+ fmt = NamespaceFormatter({'greeting':'hello'})
+ self.assertEqual(fmt.format("{greeting}, world!"), 'hello, world!')
def test_maketrans(self):
diff --git a/Lib/test/test_unicode.py b/Lib/test/test_unicode.py
index ff0e015..c39ec09 100644
--- a/Lib/test/test_unicode.py
+++ b/Lib/test/test_unicode.py
@@ -449,6 +449,10 @@ class UnicodeTest(
self.assertEqual('}}{{'.format(), '}{')
self.assertEqual('}}x{{'.format(), '}x{')
+ # weird field names
+ self.assertEqual("{0[foo-bar]}".format({'foo-bar':'baz'}), 'baz')
+ self.assertEqual("{0[foo bar]}".format({'foo bar':'baz'}), 'baz')
+
self.assertEqual('{foo._x}'.format(foo=C(20)), '20')
self.assertEqual('{1}{0}'.format(D(10), D(20)), '2010')
self.assertEqual('{0._x.x}'.format(C(D('abc'))), 'abc')
@@ -539,7 +543,11 @@ class UnicodeTest(
self.assertRaises(ValueError, "}".format)
self.assertRaises(ValueError, "abc{0:{}".format)
self.assertRaises(ValueError, "{0".format)
+ self.assertRaises(ValueError, "{0.}".format)
+ self.assertRaises(ValueError, "{0[}".format)
+ self.assertRaises(ValueError, "{0]}".format)
self.assertRaises(ValueError, "{0.[]}".format)
+ self.assertRaises(ValueError, "{0..foo}".format, 0)
self.assertRaises(ValueError, "{0[0}".format)
self.assertRaises(ValueError, "{0[0:foo}".format)
self.assertRaises(ValueError, "{c]}".format)
@@ -551,6 +559,7 @@ class UnicodeTest(
self.assertRaises(ValueError, "{0!rs}".format)
self.assertRaises(ValueError, "{!}".format)
self.assertRaises(ValueError, "{:}".format)
+ self.assertRaises(ValueError, "{:s}".format)
self.assertRaises(ValueError, "{}".format)
# can't have a replacement on the field name portion