annotate Lib/test/test_string.py @ 70817:dd775b1561ba

Fix markup.
author Georg Brandl <georg@python.org>
date Tue, 14 Jun 2011 21:09:55 +0200
parents f1a213d034af
children 4feb10457c13
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27963
diff changeset
1 import unittest, string
47201
9c930a32cebb #2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents: 43144
diff changeset
2 from test import support
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27963
diff changeset
3
33246
03440272028c SF Patch #1007087: Return new string for single subclass joins (Bug #1001011)
Raymond Hettinger <python@rcn.com>
parents: 28917
diff changeset
4
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27963
diff changeset
5 class ModuleTest(unittest.TestCase):
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27963
diff changeset
6
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27963
diff changeset
7 def test_attrs(self):
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27963
diff changeset
8 string.whitespace
42585
b623777f22b0 Remove string.{letters,lowercase,uppercase}.
Martin v. Löwis <martin@v.loewis.de>
parents: 41480
diff changeset
9 string.ascii_lowercase
b623777f22b0 Remove string.{letters,lowercase,uppercase}.
Martin v. Löwis <martin@v.loewis.de>
parents: 41480
diff changeset
10 string.ascii_uppercase
b623777f22b0 Remove string.{letters,lowercase,uppercase}.
Martin v. Löwis <martin@v.loewis.de>
parents: 41480
diff changeset
11 string.ascii_letters
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27963
diff changeset
12 string.digits
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27963
diff changeset
13 string.hexdigits
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27963
diff changeset
14 string.octdigits
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27963
diff changeset
15 string.punctuation
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27963
diff changeset
16 string.printable
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27963
diff changeset
17
56231
bf9bef70f7e8 Merged revisions 75072 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 53094
diff changeset
18 def test_capwords(self):
bf9bef70f7e8 Merged revisions 75072 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 53094
diff changeset
19 self.assertEqual(string.capwords('abc def ghi'), 'Abc Def Ghi')
bf9bef70f7e8 Merged revisions 75072 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 53094
diff changeset
20 self.assertEqual(string.capwords('abc\tdef\nghi'), 'Abc Def Ghi')
bf9bef70f7e8 Merged revisions 75072 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 53094
diff changeset
21 self.assertEqual(string.capwords('abc\t def \nghi'), 'Abc Def Ghi')
bf9bef70f7e8 Merged revisions 75072 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 53094
diff changeset
22 self.assertEqual(string.capwords('ABC DEF GHI'), 'Abc Def Ghi')
bf9bef70f7e8 Merged revisions 75072 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 53094
diff changeset
23 self.assertEqual(string.capwords('ABC-DEF-GHI', '-'), 'Abc-Def-Ghi')
bf9bef70f7e8 Merged revisions 75072 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 53094
diff changeset
24 self.assertEqual(string.capwords('ABC-def DEF-ghi GHI'), 'Abc-def Def-ghi Ghi')
56233
f3c595214abc Merged revisions 75074 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 56231
diff changeset
25 self.assertEqual(string.capwords(' aBc DeF '), 'Abc Def')
f3c595214abc Merged revisions 75074 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 56231
diff changeset
26 self.assertEqual(string.capwords('\taBc\tDeF\t'), 'Abc Def')
f3c595214abc Merged revisions 75074 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 56231
diff changeset
27 self.assertEqual(string.capwords('\taBc\tDeF\t', '\t'), '\tAbc\tDef\t')
56231
bf9bef70f7e8 Merged revisions 75072 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 53094
diff changeset
28
70547
f1a213d034af Tidy up the additional string module tests added at the Pycon sprints (closes #11505)
Nick Coghlan <ncoghlan@gmail.com>
parents: 68611
diff changeset
29 def test_basic_formatter(self):
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42585
diff changeset
30 fmt = string.Formatter()
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42585
diff changeset
31 self.assertEqual(fmt.format("foo"), "foo")
42875
5fd65e250843 PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents: 42817
diff changeset
32 self.assertEqual(fmt.format("foo{0}", "bar"), "foobar")
5fd65e250843 PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents: 42817
diff changeset
33 self.assertEqual(fmt.format("foo{1}{0}-{1}", "bar", 6), "foo6bar-6")
70547
f1a213d034af Tidy up the additional string module tests added at the Pycon sprints (closes #11505)
Nick Coghlan <ncoghlan@gmail.com>
parents: 68611
diff changeset
34
f1a213d034af Tidy up the additional string module tests added at the Pycon sprints (closes #11505)
Nick Coghlan <ncoghlan@gmail.com>
parents: 68611
diff changeset
35 def test_conversion_specifiers(self):
f1a213d034af Tidy up the additional string module tests added at the Pycon sprints (closes #11505)
Nick Coghlan <ncoghlan@gmail.com>
parents: 68611
diff changeset
36 fmt = string.Formatter()
42875
5fd65e250843 PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents: 42817
diff changeset
37 self.assertEqual(fmt.format("-{arg!r}-", arg='test'), "-'test'-")
70547
f1a213d034af Tidy up the additional string module tests added at the Pycon sprints (closes #11505)
Nick Coghlan <ncoghlan@gmail.com>
parents: 68611
diff changeset
38 self.assertEqual(fmt.format("{0!s}", 'test'), 'test')
f1a213d034af Tidy up the additional string module tests added at the Pycon sprints (closes #11505)
Nick Coghlan <ncoghlan@gmail.com>
parents: 68611
diff changeset
39 self.assertRaises(ValueError, fmt.format, "{0!h}", 'test')
42875
5fd65e250843 PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents: 42817
diff changeset
40
70547
f1a213d034af Tidy up the additional string module tests added at the Pycon sprints (closes #11505)
Nick Coghlan <ncoghlan@gmail.com>
parents: 68611
diff changeset
41 def test_name_lookup(self):
f1a213d034af Tidy up the additional string module tests added at the Pycon sprints (closes #11505)
Nick Coghlan <ncoghlan@gmail.com>
parents: 68611
diff changeset
42 fmt = string.Formatter()
f1a213d034af Tidy up the additional string module tests added at the Pycon sprints (closes #11505)
Nick Coghlan <ncoghlan@gmail.com>
parents: 68611
diff changeset
43 class AnyAttr:
f1a213d034af Tidy up the additional string module tests added at the Pycon sprints (closes #11505)
Nick Coghlan <ncoghlan@gmail.com>
parents: 68611
diff changeset
44 def __getattr__(self, attr):
f1a213d034af Tidy up the additional string module tests added at the Pycon sprints (closes #11505)
Nick Coghlan <ncoghlan@gmail.com>
parents: 68611
diff changeset
45 return attr
f1a213d034af Tidy up the additional string module tests added at the Pycon sprints (closes #11505)
Nick Coghlan <ncoghlan@gmail.com>
parents: 68611
diff changeset
46 x = AnyAttr()
f1a213d034af Tidy up the additional string module tests added at the Pycon sprints (closes #11505)
Nick Coghlan <ncoghlan@gmail.com>
parents: 68611
diff changeset
47 self.assertEqual(fmt.format("{0.lumber}{0.jack}", x), 'lumberjack')
f1a213d034af Tidy up the additional string module tests added at the Pycon sprints (closes #11505)
Nick Coghlan <ncoghlan@gmail.com>
parents: 68611
diff changeset
48 with self.assertRaises(AttributeError):
f1a213d034af Tidy up the additional string module tests added at the Pycon sprints (closes #11505)
Nick Coghlan <ncoghlan@gmail.com>
parents: 68611
diff changeset
49 fmt.format("{0.lumber}{0.jack}", '')
f1a213d034af Tidy up the additional string module tests added at the Pycon sprints (closes #11505)
Nick Coghlan <ncoghlan@gmail.com>
parents: 68611
diff changeset
50
f1a213d034af Tidy up the additional string module tests added at the Pycon sprints (closes #11505)
Nick Coghlan <ncoghlan@gmail.com>
parents: 68611
diff changeset
51 def test_index_lookup(self):
f1a213d034af Tidy up the additional string module tests added at the Pycon sprints (closes #11505)
Nick Coghlan <ncoghlan@gmail.com>
parents: 68611
diff changeset
52 fmt = string.Formatter()
f1a213d034af Tidy up the additional string module tests added at the Pycon sprints (closes #11505)
Nick Coghlan <ncoghlan@gmail.com>
parents: 68611
diff changeset
53 lookup = ["eggs", "and", "spam"]
f1a213d034af Tidy up the additional string module tests added at the Pycon sprints (closes #11505)
Nick Coghlan <ncoghlan@gmail.com>
parents: 68611
diff changeset
54 self.assertEqual(fmt.format("{0[2]}{0[0]}", lookup), 'spameggs')
f1a213d034af Tidy up the additional string module tests added at the Pycon sprints (closes #11505)
Nick Coghlan <ncoghlan@gmail.com>
parents: 68611
diff changeset
55 with self.assertRaises(IndexError):
f1a213d034af Tidy up the additional string module tests added at the Pycon sprints (closes #11505)
Nick Coghlan <ncoghlan@gmail.com>
parents: 68611
diff changeset
56 fmt.format("{0[2]}{0[0]}", [])
f1a213d034af Tidy up the additional string module tests added at the Pycon sprints (closes #11505)
Nick Coghlan <ncoghlan@gmail.com>
parents: 68611
diff changeset
57 with self.assertRaises(KeyError):
f1a213d034af Tidy up the additional string module tests added at the Pycon sprints (closes #11505)
Nick Coghlan <ncoghlan@gmail.com>
parents: 68611
diff changeset
58 fmt.format("{0[2]}{0[0]}", {})
f1a213d034af Tidy up the additional string module tests added at the Pycon sprints (closes #11505)
Nick Coghlan <ncoghlan@gmail.com>
parents: 68611
diff changeset
59
f1a213d034af Tidy up the additional string module tests added at the Pycon sprints (closes #11505)
Nick Coghlan <ncoghlan@gmail.com>
parents: 68611
diff changeset
60 def test_override_get_value(self):
42875
5fd65e250843 PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents: 42817
diff changeset
61 class NamespaceFormatter(string.Formatter):
5fd65e250843 PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents: 42817
diff changeset
62 def __init__(self, namespace={}):
5fd65e250843 PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents: 42817
diff changeset
63 string.Formatter.__init__(self)
5fd65e250843 PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents: 42817
diff changeset
64 self.namespace = namespace
5fd65e250843 PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents: 42817
diff changeset
65
5fd65e250843 PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents: 42817
diff changeset
66 def get_value(self, key, args, kwds):
5fd65e250843 PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents: 42817
diff changeset
67 if isinstance(key, str):
5fd65e250843 PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents: 42817
diff changeset
68 try:
5fd65e250843 PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents: 42817
diff changeset
69 # Check explicitly passed arguments first
5fd65e250843 PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents: 42817
diff changeset
70 return kwds[key]
5fd65e250843 PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents: 42817
diff changeset
71 except KeyError:
5fd65e250843 PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents: 42817
diff changeset
72 return self.namespace[key]
5fd65e250843 PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents: 42817
diff changeset
73 else:
5fd65e250843 PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents: 42817
diff changeset
74 string.Formatter.get_value(key, args, kwds)
5fd65e250843 PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents: 42817
diff changeset
75
5fd65e250843 PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents: 42817
diff changeset
76 fmt = NamespaceFormatter({'greeting':'hello'})
5fd65e250843 PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents: 42817
diff changeset
77 self.assertEqual(fmt.format("{greeting}, world!"), 'hello, world!')
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42585
diff changeset
78
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42585
diff changeset
79
70547
f1a213d034af Tidy up the additional string module tests added at the Pycon sprints (closes #11505)
Nick Coghlan <ncoghlan@gmail.com>
parents: 68611
diff changeset
80 def test_override_format_field(self):
43110
cdb7b9f82cc4 Added test cases for string.Formatter subclassing.
Eric Smith <eric@trueblade.com>
parents: 42875
diff changeset
81 class CallFormatter(string.Formatter):
cdb7b9f82cc4 Added test cases for string.Formatter subclassing.
Eric Smith <eric@trueblade.com>
parents: 42875
diff changeset
82 def format_field(self, value, format_spec):
cdb7b9f82cc4 Added test cases for string.Formatter subclassing.
Eric Smith <eric@trueblade.com>
parents: 42875
diff changeset
83 return format(value(), format_spec)
cdb7b9f82cc4 Added test cases for string.Formatter subclassing.
Eric Smith <eric@trueblade.com>
parents: 42875
diff changeset
84
cdb7b9f82cc4 Added test cases for string.Formatter subclassing.
Eric Smith <eric@trueblade.com>
parents: 42875
diff changeset
85 fmt = CallFormatter()
cdb7b9f82cc4 Added test cases for string.Formatter subclassing.
Eric Smith <eric@trueblade.com>
parents: 42875
diff changeset
86 self.assertEqual(fmt.format('*{0}*', lambda : 'result'), '*result*')
cdb7b9f82cc4 Added test cases for string.Formatter subclassing.
Eric Smith <eric@trueblade.com>
parents: 42875
diff changeset
87
cdb7b9f82cc4 Added test cases for string.Formatter subclassing.
Eric Smith <eric@trueblade.com>
parents: 42875
diff changeset
88
70547
f1a213d034af Tidy up the additional string module tests added at the Pycon sprints (closes #11505)
Nick Coghlan <ncoghlan@gmail.com>
parents: 68611
diff changeset
89 def test_override_convert_field(self):
43110
cdb7b9f82cc4 Added test cases for string.Formatter subclassing.
Eric Smith <eric@trueblade.com>
parents: 42875
diff changeset
90 class XFormatter(string.Formatter):
cdb7b9f82cc4 Added test cases for string.Formatter subclassing.
Eric Smith <eric@trueblade.com>
parents: 42875
diff changeset
91 def convert_field(self, value, conversion):
cdb7b9f82cc4 Added test cases for string.Formatter subclassing.
Eric Smith <eric@trueblade.com>
parents: 42875
diff changeset
92 if conversion == 'x':
cdb7b9f82cc4 Added test cases for string.Formatter subclassing.
Eric Smith <eric@trueblade.com>
parents: 42875
diff changeset
93 return None
70547
f1a213d034af Tidy up the additional string module tests added at the Pycon sprints (closes #11505)
Nick Coghlan <ncoghlan@gmail.com>
parents: 68611
diff changeset
94 return super().convert_field(value, conversion)
43110
cdb7b9f82cc4 Added test cases for string.Formatter subclassing.
Eric Smith <eric@trueblade.com>
parents: 42875
diff changeset
95
cdb7b9f82cc4 Added test cases for string.Formatter subclassing.
Eric Smith <eric@trueblade.com>
parents: 42875
diff changeset
96 fmt = XFormatter()
cdb7b9f82cc4 Added test cases for string.Formatter subclassing.
Eric Smith <eric@trueblade.com>
parents: 42875
diff changeset
97 self.assertEqual(fmt.format("{0!r}:{0!x}", 'foo', 'foo'), "'foo':None")
cdb7b9f82cc4 Added test cases for string.Formatter subclassing.
Eric Smith <eric@trueblade.com>
parents: 42875
diff changeset
98
cdb7b9f82cc4 Added test cases for string.Formatter subclassing.
Eric Smith <eric@trueblade.com>
parents: 42875
diff changeset
99
70547
f1a213d034af Tidy up the additional string module tests added at the Pycon sprints (closes #11505)
Nick Coghlan <ncoghlan@gmail.com>
parents: 68611
diff changeset
100 def test_override_parse(self):
43110
cdb7b9f82cc4 Added test cases for string.Formatter subclassing.
Eric Smith <eric@trueblade.com>
parents: 42875
diff changeset
101 class BarFormatter(string.Formatter):
cdb7b9f82cc4 Added test cases for string.Formatter subclassing.
Eric Smith <eric@trueblade.com>
parents: 42875
diff changeset
102 # returns an iterable that contains tuples of the form:
cdb7b9f82cc4 Added test cases for string.Formatter subclassing.
Eric Smith <eric@trueblade.com>
parents: 42875
diff changeset
103 # (literal_text, field_name, format_spec, conversion)
cdb7b9f82cc4 Added test cases for string.Formatter subclassing.
Eric Smith <eric@trueblade.com>
parents: 42875
diff changeset
104 def parse(self, format_string):
cdb7b9f82cc4 Added test cases for string.Formatter subclassing.
Eric Smith <eric@trueblade.com>
parents: 42875
diff changeset
105 for field in format_string.split('|'):
cdb7b9f82cc4 Added test cases for string.Formatter subclassing.
Eric Smith <eric@trueblade.com>
parents: 42875
diff changeset
106 if field[0] == '+':
cdb7b9f82cc4 Added test cases for string.Formatter subclassing.
Eric Smith <eric@trueblade.com>
parents: 42875
diff changeset
107 # it's markup
cdb7b9f82cc4 Added test cases for string.Formatter subclassing.
Eric Smith <eric@trueblade.com>
parents: 42875
diff changeset
108 field_name, _, format_spec = field[1:].partition(':')
cdb7b9f82cc4 Added test cases for string.Formatter subclassing.
Eric Smith <eric@trueblade.com>
parents: 42875
diff changeset
109 yield '', field_name, format_spec, None
cdb7b9f82cc4 Added test cases for string.Formatter subclassing.
Eric Smith <eric@trueblade.com>
parents: 42875
diff changeset
110 else:
cdb7b9f82cc4 Added test cases for string.Formatter subclassing.
Eric Smith <eric@trueblade.com>
parents: 42875
diff changeset
111 yield field, None, None, None
cdb7b9f82cc4 Added test cases for string.Formatter subclassing.
Eric Smith <eric@trueblade.com>
parents: 42875
diff changeset
112
cdb7b9f82cc4 Added test cases for string.Formatter subclassing.
Eric Smith <eric@trueblade.com>
parents: 42875
diff changeset
113 fmt = BarFormatter()
cdb7b9f82cc4 Added test cases for string.Formatter subclassing.
Eric Smith <eric@trueblade.com>
parents: 42875
diff changeset
114 self.assertEqual(fmt.format('*|+0:^10s|*', 'foo'), '* foo *')
cdb7b9f82cc4 Added test cases for string.Formatter subclassing.
Eric Smith <eric@trueblade.com>
parents: 42875
diff changeset
115
70547
f1a213d034af Tidy up the additional string module tests added at the Pycon sprints (closes #11505)
Nick Coghlan <ncoghlan@gmail.com>
parents: 68611
diff changeset
116 def test_check_unused_args(self):
43112
90c715bc7d30 Changed signature of string.Formatter.get_field, per suggestion by
Eric Smith <eric@trueblade.com>
parents: 43110
diff changeset
117 class CheckAllUsedFormatter(string.Formatter):
90c715bc7d30 Changed signature of string.Formatter.get_field, per suggestion by
Eric Smith <eric@trueblade.com>
parents: 43110
diff changeset
118 def check_unused_args(self, used_args, args, kwargs):
68488
0991b40e895d #11515: fix several typos. Patch by Piotr Kasprzyk.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 56233
diff changeset
119 # Track which arguments actually got used
43112
90c715bc7d30 Changed signature of string.Formatter.get_field, per suggestion by
Eric Smith <eric@trueblade.com>
parents: 43110
diff changeset
120 unused_args = set(kwargs.keys())
90c715bc7d30 Changed signature of string.Formatter.get_field, per suggestion by
Eric Smith <eric@trueblade.com>
parents: 43110
diff changeset
121 unused_args.update(range(0, len(args)))
90c715bc7d30 Changed signature of string.Formatter.get_field, per suggestion by
Eric Smith <eric@trueblade.com>
parents: 43110
diff changeset
122
90c715bc7d30 Changed signature of string.Formatter.get_field, per suggestion by
Eric Smith <eric@trueblade.com>
parents: 43110
diff changeset
123 for arg in used_args:
90c715bc7d30 Changed signature of string.Formatter.get_field, per suggestion by
Eric Smith <eric@trueblade.com>
parents: 43110
diff changeset
124 unused_args.remove(arg)
90c715bc7d30 Changed signature of string.Formatter.get_field, per suggestion by
Eric Smith <eric@trueblade.com>
parents: 43110
diff changeset
125
90c715bc7d30 Changed signature of string.Formatter.get_field, per suggestion by
Eric Smith <eric@trueblade.com>
parents: 43110
diff changeset
126 if unused_args:
90c715bc7d30 Changed signature of string.Formatter.get_field, per suggestion by
Eric Smith <eric@trueblade.com>
parents: 43110
diff changeset
127 raise ValueError("unused arguments")
90c715bc7d30 Changed signature of string.Formatter.get_field, per suggestion by
Eric Smith <eric@trueblade.com>
parents: 43110
diff changeset
128
90c715bc7d30 Changed signature of string.Formatter.get_field, per suggestion by
Eric Smith <eric@trueblade.com>
parents: 43110
diff changeset
129 fmt = CheckAllUsedFormatter()
90c715bc7d30 Changed signature of string.Formatter.get_field, per suggestion by
Eric Smith <eric@trueblade.com>
parents: 43110
diff changeset
130 self.assertEqual(fmt.format("{0}", 10), "10")
90c715bc7d30 Changed signature of string.Formatter.get_field, per suggestion by
Eric Smith <eric@trueblade.com>
parents: 43110
diff changeset
131 self.assertEqual(fmt.format("{0}{i}", 10, i=100), "10100")
90c715bc7d30 Changed signature of string.Formatter.get_field, per suggestion by
Eric Smith <eric@trueblade.com>
parents: 43110
diff changeset
132 self.assertEqual(fmt.format("{0}{i}{1}", 10, 20, i=100), "1010020")
90c715bc7d30 Changed signature of string.Formatter.get_field, per suggestion by
Eric Smith <eric@trueblade.com>
parents: 43110
diff changeset
133 self.assertRaises(ValueError, fmt.format, "{0}{i}{1}", 10, 20, i=100, j=0)
90c715bc7d30 Changed signature of string.Formatter.get_field, per suggestion by
Eric Smith <eric@trueblade.com>
parents: 43110
diff changeset
134 self.assertRaises(ValueError, fmt.format, "{0}", 10, 20)
90c715bc7d30 Changed signature of string.Formatter.get_field, per suggestion by
Eric Smith <eric@trueblade.com>
parents: 43110
diff changeset
135 self.assertRaises(ValueError, fmt.format, "{0}", 10, 20, i=100)
90c715bc7d30 Changed signature of string.Formatter.get_field, per suggestion by
Eric Smith <eric@trueblade.com>
parents: 43110
diff changeset
136 self.assertRaises(ValueError, fmt.format, "{i}", 10, 20, i=100)
90c715bc7d30 Changed signature of string.Formatter.get_field, per suggestion by
Eric Smith <eric@trueblade.com>
parents: 43110
diff changeset
137
70547
f1a213d034af Tidy up the additional string module tests added at the Pycon sprints (closes #11505)
Nick Coghlan <ncoghlan@gmail.com>
parents: 68611
diff changeset
138 def test_vformat_recursion_limit(self):
f1a213d034af Tidy up the additional string module tests added at the Pycon sprints (closes #11505)
Nick Coghlan <ncoghlan@gmail.com>
parents: 68611
diff changeset
139 fmt = string.Formatter()
f1a213d034af Tidy up the additional string module tests added at the Pycon sprints (closes #11505)
Nick Coghlan <ncoghlan@gmail.com>
parents: 68611
diff changeset
140 args = ()
f1a213d034af Tidy up the additional string module tests added at the Pycon sprints (closes #11505)
Nick Coghlan <ncoghlan@gmail.com>
parents: 68611
diff changeset
141 kwargs = dict(i=100)
f1a213d034af Tidy up the additional string module tests added at the Pycon sprints (closes #11505)
Nick Coghlan <ncoghlan@gmail.com>
parents: 68611
diff changeset
142 with self.assertRaises(ValueError) as err:
f1a213d034af Tidy up the additional string module tests added at the Pycon sprints (closes #11505)
Nick Coghlan <ncoghlan@gmail.com>
parents: 68611
diff changeset
143 fmt._vformat("{i}", args, kwargs, set(), -1)
f1a213d034af Tidy up the additional string module tests added at the Pycon sprints (closes #11505)
Nick Coghlan <ncoghlan@gmail.com>
parents: 68611
diff changeset
144 self.assertIn("recursion", str(err.exception))
68610
cdd667e02fe3 Backport improved test coverage for string.py
Nick Coghlan <ncoghlan@gmail.com>
parents: 68488
diff changeset
145
cdd667e02fe3 Backport improved test coverage for string.py
Nick Coghlan <ncoghlan@gmail.com>
parents: 68488
diff changeset
146
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27963
diff changeset
147 def test_main():
47201
9c930a32cebb #2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents: 43144
diff changeset
148 support.run_unittest(ModuleTest)
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27963
diff changeset
149
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27963
diff changeset
150 if __name__ == "__main__":
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 27963
diff changeset
151 test_main()