summaryrefslogtreecommitdiff
path: root/Lib/test
diff options
context:
space:
mode:
Diffstat (limited to 'Lib/test')
-rw-r--r--Lib/test/test_pep292.py7
-rw-r--r--Lib/test/test_string.py15
2 files changed, 22 insertions, 0 deletions
diff --git a/Lib/test/test_pep292.py b/Lib/test/test_pep292.py
index 6da8d2e3e1..fd5256c774 100644
--- a/Lib/test/test_pep292.py
+++ b/Lib/test/test_pep292.py
@@ -26,6 +26,7 @@ class TestTemplate(unittest.TestCase):
self.assertEqual(s.substitute(dict(who='tim', what='ham')),
'tim likes to eat a bag of ham worth $100')
self.assertRaises(KeyError, s.substitute, dict(who='tim'))
+ self.assertRaises(TypeError, Template.substitute)
def test_regular_templates_with_braces(self):
s = Template('$who likes ${what} for ${meal}')
@@ -198,6 +199,9 @@ class TestTemplate(unittest.TestCase):
eq(s.substitute(dict(mapping='one'), mapping='two'),
'the mapping is two')
+ s = Template('the self is $self')
+ eq(s.substitute(self='bozo'), 'the self is bozo')
+
def test_keyword_arguments_safe(self):
eq = self.assertEqual
raises = self.assertRaises
@@ -216,6 +220,9 @@ class TestTemplate(unittest.TestCase):
raises(TypeError, s.substitute, d, {})
raises(TypeError, s.safe_substitute, d, {})
+ s = Template('the self is $self')
+ eq(s.safe_substitute(self='bozo'), 'the self is bozo')
+
def test_delimiter_override(self):
eq = self.assertEqual
raises = self.assertRaises
diff --git a/Lib/test/test_string.py b/Lib/test/test_string.py
index 3e6507151e..f8731b812c 100644
--- a/Lib/test/test_string.py
+++ b/Lib/test/test_string.py
@@ -31,6 +31,21 @@ class ModuleTest(unittest.TestCase):
self.assertEqual(fmt.format("foo"), "foo")
self.assertEqual(fmt.format("foo{0}", "bar"), "foobar")
self.assertEqual(fmt.format("foo{1}{0}-{1}", "bar", 6), "foo6bar-6")
+ self.assertRaises(TypeError, fmt.format)
+ self.assertRaises(TypeError, string.Formatter.format)
+
+ def test_format_keyword_arguments(self):
+ fmt = string.Formatter()
+ self.assertEqual(fmt.format("-{arg}-", arg='test'), '-test-')
+ self.assertRaises(KeyError, fmt.format, "-{arg}-")
+ self.assertEqual(fmt.format("-{self}-", self='test'), '-test-')
+ self.assertRaises(KeyError, fmt.format, "-{self}-")
+ self.assertEqual(fmt.format("-{format_string}-", format_string='test'),
+ '-test-')
+ self.assertRaises(KeyError, fmt.format, "-{format_string}-")
+ with self.assertWarnsRegex(DeprecationWarning, "format_string"):
+ self.assertEqual(fmt.format(arg='test', format_string="-{arg}-"),
+ '-test-')
def test_auto_numbering(self):
fmt = string.Formatter()