blob: 5d37e164cfcd28c2f9993c00ff43f2beab7d1932 [file] [log] [blame]
Walter Dörwald0fd583c2003-02-21 12:53:50 +00001import unittest, string
Walter Dörwald0fd583c2003-02-21 12:53:50 +00002
Raymond Hettinger674f2412004-08-23 23:23:54 +00003
Walter Dörwald0fd583c2003-02-21 12:53:50 +00004class ModuleTest(unittest.TestCase):
5
6 def test_attrs(self):
R David Murray0a8f43e2015-04-13 20:04:29 -04007 # While the exact order of the items in these attributes is not
8 # technically part of the "language spec", in practice there is almost
9 # certainly user code that depends on the order, so de-facto it *is*
10 # part of the spec.
11 self.assertEqual(string.whitespace, ' \t\n\r\x0b\x0c')
12 self.assertEqual(string.ascii_lowercase, 'abcdefghijklmnopqrstuvwxyz')
13 self.assertEqual(string.ascii_uppercase, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')
14 self.assertEqual(string.ascii_letters, string.ascii_lowercase + string.ascii_uppercase)
15 self.assertEqual(string.digits, '0123456789')
16 self.assertEqual(string.hexdigits, string.digits + 'abcdefABCDEF')
17 self.assertEqual(string.octdigits, '01234567')
18 self.assertEqual(string.punctuation, '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~')
19 self.assertEqual(string.printable, string.digits + string.ascii_lowercase + string.ascii_uppercase + string.punctuation + string.whitespace)
Walter Dörwald0fd583c2003-02-21 12:53:50 +000020
Ezio Melotti2c6a9492009-09-26 12:19:30 +000021 def test_capwords(self):
22 self.assertEqual(string.capwords('abc def ghi'), 'Abc Def Ghi')
23 self.assertEqual(string.capwords('abc\tdef\nghi'), 'Abc Def Ghi')
24 self.assertEqual(string.capwords('abc\t def \nghi'), 'Abc Def Ghi')
25 self.assertEqual(string.capwords('ABC DEF GHI'), 'Abc Def Ghi')
26 self.assertEqual(string.capwords('ABC-DEF-GHI', '-'), 'Abc-Def-Ghi')
27 self.assertEqual(string.capwords('ABC-def DEF-ghi GHI'), 'Abc-def Def-ghi Ghi')
Ezio Melottia40bdda2009-09-26 12:33:22 +000028 self.assertEqual(string.capwords(' aBc DeF '), 'Abc Def')
29 self.assertEqual(string.capwords('\taBc\tDeF\t'), 'Abc Def')
30 self.assertEqual(string.capwords('\taBc\tDeF\t', '\t'), '\tAbc\tDef\t')
Ezio Melotti2c6a9492009-09-26 12:19:30 +000031
Nick Coghlan62ecb6a2011-05-31 19:40:11 +100032 def test_basic_formatter(self):
Eric Smith8c663262007-08-25 02:26:07 +000033 fmt = string.Formatter()
34 self.assertEqual(fmt.format("foo"), "foo")
Eric Smith7ade6482007-08-26 22:27:13 +000035 self.assertEqual(fmt.format("foo{0}", "bar"), "foobar")
36 self.assertEqual(fmt.format("foo{1}{0}-{1}", "bar", 6), "foo6bar-6")
Serhiy Storchaka8ffe9172015-03-24 22:28:43 +020037 self.assertRaises(TypeError, fmt.format)
38 self.assertRaises(TypeError, string.Formatter.format)
39
40 def test_format_keyword_arguments(self):
41 fmt = string.Formatter()
42 self.assertEqual(fmt.format("-{arg}-", arg='test'), '-test-')
43 self.assertRaises(KeyError, fmt.format, "-{arg}-")
44 self.assertEqual(fmt.format("-{self}-", self='test'), '-test-')
45 self.assertRaises(KeyError, fmt.format, "-{self}-")
46 self.assertEqual(fmt.format("-{format_string}-", format_string='test'),
47 '-test-')
48 self.assertRaises(KeyError, fmt.format, "-{format_string}-")
Serhiy Storchakab876df42015-03-24 22:30:46 +020049 with self.assertWarnsRegex(DeprecationWarning, "format_string"):
50 self.assertEqual(fmt.format(arg='test', format_string="-{arg}-"),
51 '-test-')
Eric Smith7ade6482007-08-26 22:27:13 +000052
Eric V. Smith7ce90742014-04-14 16:43:50 -040053 def test_auto_numbering(self):
54 fmt = string.Formatter()
55 self.assertEqual(fmt.format('foo{}{}', 'bar', 6),
56 'foo{}{}'.format('bar', 6))
57 self.assertEqual(fmt.format('foo{1}{num}{1}', None, 'bar', num=6),
58 'foo{1}{num}{1}'.format(None, 'bar', num=6))
59 self.assertEqual(fmt.format('{:^{}}', 'bar', 6),
60 '{:^{}}'.format('bar', 6))
61 self.assertEqual(fmt.format('{:^{pad}}{}', 'foo', 'bar', pad=6),
62 '{:^{pad}}{}'.format('foo', 'bar', pad=6))
63
64 with self.assertRaises(ValueError):
65 fmt.format('foo{1}{}', 'bar', 6)
66
67 with self.assertRaises(ValueError):
68 fmt.format('foo{}{1}', 'bar', 6)
69
Nick Coghlan62ecb6a2011-05-31 19:40:11 +100070 def test_conversion_specifiers(self):
71 fmt = string.Formatter()
72 self.assertEqual(fmt.format("-{arg!r}-", arg='test'), "-'test'-")
73 self.assertEqual(fmt.format("{0!s}", 'test'), 'test')
74 self.assertRaises(ValueError, fmt.format, "{0!h}", 'test')
R David Murraye56bf972012-08-19 17:26:34 -040075 # issue13579
76 self.assertEqual(fmt.format("{0!a}", 42), '42')
77 self.assertEqual(fmt.format("{0!a}", string.ascii_letters),
78 "'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'")
79 self.assertEqual(fmt.format("{0!a}", chr(255)), "'\\xff'")
80 self.assertEqual(fmt.format("{0!a}", chr(256)), "'\\u0100'")
Nick Coghlan62ecb6a2011-05-31 19:40:11 +100081
82 def test_name_lookup(self):
83 fmt = string.Formatter()
84 class AnyAttr:
85 def __getattr__(self, attr):
86 return attr
87 x = AnyAttr()
88 self.assertEqual(fmt.format("{0.lumber}{0.jack}", x), 'lumberjack')
89 with self.assertRaises(AttributeError):
90 fmt.format("{0.lumber}{0.jack}", '')
91
92 def test_index_lookup(self):
93 fmt = string.Formatter()
94 lookup = ["eggs", "and", "spam"]
95 self.assertEqual(fmt.format("{0[2]}{0[0]}", lookup), 'spameggs')
96 with self.assertRaises(IndexError):
97 fmt.format("{0[2]}{0[0]}", [])
98 with self.assertRaises(KeyError):
99 fmt.format("{0[2]}{0[0]}", {})
100
101 def test_override_get_value(self):
Eric Smith7ade6482007-08-26 22:27:13 +0000102 class NamespaceFormatter(string.Formatter):
103 def __init__(self, namespace={}):
104 string.Formatter.__init__(self)
105 self.namespace = namespace
106
107 def get_value(self, key, args, kwds):
108 if isinstance(key, str):
109 try:
110 # Check explicitly passed arguments first
111 return kwds[key]
112 except KeyError:
113 return self.namespace[key]
114 else:
115 string.Formatter.get_value(key, args, kwds)
116
117 fmt = NamespaceFormatter({'greeting':'hello'})
118 self.assertEqual(fmt.format("{greeting}, world!"), 'hello, world!')
Eric Smith8c663262007-08-25 02:26:07 +0000119
120
Nick Coghlan62ecb6a2011-05-31 19:40:11 +1000121 def test_override_format_field(self):
Eric Smith81936692007-08-31 01:14:01 +0000122 class CallFormatter(string.Formatter):
123 def format_field(self, value, format_spec):
124 return format(value(), format_spec)
125
126 fmt = CallFormatter()
127 self.assertEqual(fmt.format('*{0}*', lambda : 'result'), '*result*')
128
129
Nick Coghlan62ecb6a2011-05-31 19:40:11 +1000130 def test_override_convert_field(self):
Eric Smith81936692007-08-31 01:14:01 +0000131 class XFormatter(string.Formatter):
132 def convert_field(self, value, conversion):
133 if conversion == 'x':
134 return None
Nick Coghlan62ecb6a2011-05-31 19:40:11 +1000135 return super().convert_field(value, conversion)
Eric Smith81936692007-08-31 01:14:01 +0000136
137 fmt = XFormatter()
138 self.assertEqual(fmt.format("{0!r}:{0!x}", 'foo', 'foo'), "'foo':None")
139
140
Nick Coghlan62ecb6a2011-05-31 19:40:11 +1000141 def test_override_parse(self):
Eric Smith81936692007-08-31 01:14:01 +0000142 class BarFormatter(string.Formatter):
143 # returns an iterable that contains tuples of the form:
144 # (literal_text, field_name, format_spec, conversion)
145 def parse(self, format_string):
146 for field in format_string.split('|'):
147 if field[0] == '+':
148 # it's markup
149 field_name, _, format_spec = field[1:].partition(':')
150 yield '', field_name, format_spec, None
151 else:
152 yield field, None, None, None
153
154 fmt = BarFormatter()
155 self.assertEqual(fmt.format('*|+0:^10s|*', 'foo'), '* foo *')
156
Nick Coghlan62ecb6a2011-05-31 19:40:11 +1000157 def test_check_unused_args(self):
Eric Smith3bcc42a2007-08-31 02:26:31 +0000158 class CheckAllUsedFormatter(string.Formatter):
159 def check_unused_args(self, used_args, args, kwargs):
Ezio Melotti42da6632011-03-15 05:18:48 +0200160 # Track which arguments actually got used
Eric Smith3bcc42a2007-08-31 02:26:31 +0000161 unused_args = set(kwargs.keys())
162 unused_args.update(range(0, len(args)))
163
164 for arg in used_args:
165 unused_args.remove(arg)
166
167 if unused_args:
168 raise ValueError("unused arguments")
169
170 fmt = CheckAllUsedFormatter()
171 self.assertEqual(fmt.format("{0}", 10), "10")
172 self.assertEqual(fmt.format("{0}{i}", 10, i=100), "10100")
173 self.assertEqual(fmt.format("{0}{i}{1}", 10, 20, i=100), "1010020")
174 self.assertRaises(ValueError, fmt.format, "{0}{i}{1}", 10, 20, i=100, j=0)
175 self.assertRaises(ValueError, fmt.format, "{0}", 10, 20)
176 self.assertRaises(ValueError, fmt.format, "{0}", 10, 20, i=100)
177 self.assertRaises(ValueError, fmt.format, "{i}", 10, 20, i=100)
178
Nick Coghlan62ecb6a2011-05-31 19:40:11 +1000179 def test_vformat_recursion_limit(self):
180 fmt = string.Formatter()
181 args = ()
182 kwargs = dict(i=100)
183 with self.assertRaises(ValueError) as err:
184 fmt._vformat("{i}", args, kwargs, set(), -1)
185 self.assertIn("recursion", str(err.exception))
Nick Coghland25fd4d2011-03-15 08:54:37 +1000186
187
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000188if __name__ == "__main__":
Zachary Ware38c707e2015-04-13 15:00:43 -0500189 unittest.main()