4 from test
import test_support
, string_tests
8 string_tests
.CommonTest
,
9 string_tests
.MixinStrUnicodeUserStringTest
,
10 string_tests
.MixinStrUserStringTest
,
11 string_tests
.MixinStrUnicodeTest
,
16 # We don't need to propagate to str
17 def fixtype(self
, obj
):
20 def test_basic_creation(self
):
21 self
.assertEqual(str(''), '')
22 self
.assertEqual(str(0), '0')
23 self
.assertEqual(str(0L), '0')
24 self
.assertEqual(str(()), '()')
25 self
.assertEqual(str([]), '[]')
26 self
.assertEqual(str({}), '{}')
29 self
.assertEqual(str(a
), '[[...]]')
32 self
.assertEqual(str(a
), '{0: {...}}')
34 def test_formatting(self
):
35 string_tests
.MixinStrUnicodeUserStringTest
.test_formatting(self
)
36 self
.assertRaises(OverflowError, '%c'.__mod
__, 0x1234)
38 def test_conversion(self
):
39 # Make sure __str__() behaves properly
41 def __unicode__(self
):
68 def __unicode__(self
):
74 def __unicode__(self
):
78 def __new__(cls
, content
=""):
79 return str.__new
__(cls
, 2*content
)
86 def __unicode__(self
):
89 self
.assertTrue(str(Foo0()).startswith("<")) # this is different from __unicode__
90 self
.assertEqual(str(Foo1()), "foo")
91 self
.assertEqual(str(Foo2()), "foo")
92 self
.assertEqual(str(Foo3()), "foo")
93 self
.assertEqual(str(Foo4("bar")), "foo")
94 self
.assertEqual(str(Foo5("bar")), "foo")
95 self
.assertEqual(str(Foo6("bar")), "foos")
96 self
.assertEqual(str(Foo7("bar")), "foos")
97 self
.assertEqual(str(Foo8("foo")), "foofoo")
98 self
.assertEqual(str(Foo9("foo")), "string")
99 self
.assertEqual(unicode(Foo9("foo")), u
"not unicode")
101 def test_expandtabs_overflows_gracefully(self
):
102 # This test only affects 32-bit platforms because expandtabs can only take
103 # an int as the max value, not a 64-bit C long. If expandtabs is changed
104 # to take a 64-bit long, this test should apply to all platforms.
105 if sys
.maxint
> (1 << 32) or struct
.calcsize('P') != 4:
107 self
.assertRaises(OverflowError, 't\tt\t'.expandtabs
, sys
.maxint
)
109 def test__format__(self
):
110 def test(value
, format
, expected
):
111 # test both with and without the trailing 's'
112 self
.assertEqual(value
.__format
__(format
), expected
)
113 self
.assertEqual(value
.__format
__(format
+ 's'), expected
)
116 test('abc', '', 'abc')
117 test('abc', '.3', 'abc')
118 test('ab', '.3', 'ab')
119 test('abcdef', '.3', 'abc')
120 test('abcdef', '.0', '')
121 test('abc', '3.3', 'abc')
122 test('abc', '2.3', 'abc')
123 test('abc', '2.2', 'ab')
124 test('abc', '3.2', 'ab ')
125 test('result', 'x<0', 'result')
126 test('result', 'x<5', 'result')
127 test('result', 'x<6', 'result')
128 test('result', 'x<7', 'resultx')
129 test('result', 'x<8', 'resultxx')
130 test('result', ' <7', 'result ')
131 test('result', '<7', 'result ')
132 test('result', '>7', ' result')
133 test('result', '>8', ' result')
134 test('result', '^8', ' result ')
135 test('result', '^9', ' result ')
136 test('result', '^10', ' result ')
137 test('a', '10000', 'a' + ' ' * 9999)
138 test('', '10000', ' ' * 10000)
139 test('', '10000000', ' ' * 10000000)
141 def test_format(self
):
142 self
.assertEqual(''.format(), '')
143 self
.assertEqual('a'.format(), 'a')
144 self
.assertEqual('ab'.format(), 'ab')
145 self
.assertEqual('a{{'.format(), 'a{')
146 self
.assertEqual('a}}'.format(), 'a}')
147 self
.assertEqual('{{b'.format(), '{b')
148 self
.assertEqual('}}b'.format(), '}b')
149 self
.assertEqual('a{{b'.format(), 'a{b')
151 # examples from the PEP:
153 self
.assertEqual("My name is {0}".format('Fred'), "My name is Fred")
154 self
.assertEqual("My name is {0[name]}".format(dict(name
='Fred')),
156 self
.assertEqual("My name is {0} :-{{}}".format('Fred'),
157 "My name is Fred :-{}")
159 d
= datetime
.date(2007, 8, 18)
160 self
.assertEqual("The year is {0.year}".format(d
),
163 # classes we'll use for testing
165 def __init__(self
, x
=100):
167 def __format__(self
, spec
):
171 def __init__(self
, x
):
173 def __format__(self
, spec
):
176 # class with __str__, but no __format__
178 def __init__(self
, x
):
181 return 'E(' + self
.x
+ ')'
183 # class with __repr__, but no __format__ or __str__
185 def __init__(self
, x
):
188 return 'F(' + self
.x
+ ')'
190 # class with __format__ that forwards to string, for some format_spec's
192 def __init__(self
, x
):
195 return "string is " + self
.x
196 def __format__(self
, format_spec
):
197 if format_spec
== 'd':
198 return 'G(' + self
.x
+ ')'
199 return object.__format
__(self
, format_spec
)
201 # class that returns a bad type from __format__
203 def __format__(self
, format_spec
):
206 class I(datetime
.date
):
207 def __format__(self
, format_spec
):
208 return self
.strftime(format_spec
)
211 def __format__(self
, format_spec
):
212 return int.__format
__(self
* 2, format_spec
)
215 self
.assertEqual(''.format(), '')
216 self
.assertEqual('abc'.format(), 'abc')
217 self
.assertEqual('{0}'.format('abc'), 'abc')
218 self
.assertEqual('{0:}'.format('abc'), 'abc')
219 self
.assertEqual('X{0}'.format('abc'), 'Xabc')
220 self
.assertEqual('{0}X'.format('abc'), 'abcX')
221 self
.assertEqual('X{0}Y'.format('abc'), 'XabcY')
222 self
.assertEqual('{1}'.format(1, 'abc'), 'abc')
223 self
.assertEqual('X{1}'.format(1, 'abc'), 'Xabc')
224 self
.assertEqual('{1}X'.format(1, 'abc'), 'abcX')
225 self
.assertEqual('X{1}Y'.format(1, 'abc'), 'XabcY')
226 self
.assertEqual('{0}'.format(-15), '-15')
227 self
.assertEqual('{0}{1}'.format(-15, 'abc'), '-15abc')
228 self
.assertEqual('{0}X{1}'.format(-15, 'abc'), '-15Xabc')
229 self
.assertEqual('{{'.format(), '{')
230 self
.assertEqual('}}'.format(), '}')
231 self
.assertEqual('{{}}'.format(), '{}')
232 self
.assertEqual('{{x}}'.format(), '{x}')
233 self
.assertEqual('{{{0}}}'.format(123), '{123}')
234 self
.assertEqual('{{{{0}}}}'.format(), '{{0}}')
235 self
.assertEqual('}}{{'.format(), '}{')
236 self
.assertEqual('}}x{{'.format(), '}x{')
239 self
.assertEqual("{0[foo-bar]}".format({'foo-bar':'baz'}), 'baz')
240 self
.assertEqual("{0[foo bar]}".format({'foo bar':'baz'}), 'baz')
241 self
.assertEqual("{0[ ]}".format({' ':3}), '3')
243 self
.assertEqual('{foo._x}'.format(foo
=C(20)), '20')
244 self
.assertEqual('{1}{0}'.format(D(10), D(20)), '2010')
245 self
.assertEqual('{0._x.x}'.format(C(D('abc'))), 'abc')
246 self
.assertEqual('{0[0]}'.format(['abc', 'def']), 'abc')
247 self
.assertEqual('{0[1]}'.format(['abc', 'def']), 'def')
248 self
.assertEqual('{0[1][0]}'.format(['abc', ['def']]), 'def')
249 self
.assertEqual('{0[1][0].x}'.format(['abc', [D('def')]]), 'def')
252 self
.assertEqual('{0:.3s}'.format('abc'), 'abc')
253 self
.assertEqual('{0:.3s}'.format('ab'), 'ab')
254 self
.assertEqual('{0:.3s}'.format('abcdef'), 'abc')
255 self
.assertEqual('{0:.0s}'.format('abcdef'), '')
256 self
.assertEqual('{0:3.3s}'.format('abc'), 'abc')
257 self
.assertEqual('{0:2.3s}'.format('abc'), 'abc')
258 self
.assertEqual('{0:2.2s}'.format('abc'), 'ab')
259 self
.assertEqual('{0:3.2s}'.format('abc'), 'ab ')
260 self
.assertEqual('{0:x<0s}'.format('result'), 'result')
261 self
.assertEqual('{0:x<5s}'.format('result'), 'result')
262 self
.assertEqual('{0:x<6s}'.format('result'), 'result')
263 self
.assertEqual('{0:x<7s}'.format('result'), 'resultx')
264 self
.assertEqual('{0:x<8s}'.format('result'), 'resultxx')
265 self
.assertEqual('{0: <7s}'.format('result'), 'result ')
266 self
.assertEqual('{0:<7s}'.format('result'), 'result ')
267 self
.assertEqual('{0:>7s}'.format('result'), ' result')
268 self
.assertEqual('{0:>8s}'.format('result'), ' result')
269 self
.assertEqual('{0:^8s}'.format('result'), ' result ')
270 self
.assertEqual('{0:^9s}'.format('result'), ' result ')
271 self
.assertEqual('{0:^10s}'.format('result'), ' result ')
272 self
.assertEqual('{0:10000}'.format('a'), 'a' + ' ' * 9999)
273 self
.assertEqual('{0:10000}'.format(''), ' ' * 10000)
274 self
.assertEqual('{0:10000000}'.format(''), ' ' * 10000000)
276 # format specifiers for user defined type
277 self
.assertEqual('{0:abc}'.format(C()), 'abc')
279 # !r and !s coersions
280 self
.assertEqual('{0!s}'.format('Hello'), 'Hello')
281 self
.assertEqual('{0!s:}'.format('Hello'), 'Hello')
282 self
.assertEqual('{0!s:15}'.format('Hello'), 'Hello ')
283 self
.assertEqual('{0!s:15s}'.format('Hello'), 'Hello ')
284 self
.assertEqual('{0!r}'.format('Hello'), "'Hello'")
285 self
.assertEqual('{0!r:}'.format('Hello'), "'Hello'")
286 self
.assertEqual('{0!r}'.format(F('Hello')), 'F(Hello)')
288 # test fallback to object.__format__
289 self
.assertEqual('{0}'.format({}), '{}')
290 self
.assertEqual('{0}'.format([]), '[]')
291 self
.assertEqual('{0}'.format([1]), '[1]')
292 self
.assertEqual('{0}'.format(E('data')), 'E(data)')
293 self
.assertEqual('{0:^10}'.format(E('data')), ' E(data) ')
294 self
.assertEqual('{0:^10s}'.format(E('data')), ' E(data) ')
295 self
.assertEqual('{0:d}'.format(G('data')), 'G(data)')
296 self
.assertEqual('{0:>15s}'.format(G('data')), ' string is data')
297 self
.assertEqual('{0!s}'.format(G('data')), 'string is data')
299 self
.assertEqual("{0:date: %Y-%m-%d}".format(I(year
=2007,
304 # test deriving from a builtin type and overriding __format__
305 self
.assertEqual("{0}".format(J(10)), "20")
308 # string format specifiers
309 self
.assertEqual('{0:}'.format('a'), 'a')
311 # computed format specifiers
312 self
.assertEqual("{0:.{1}}".format('hello world', 5), 'hello')
313 self
.assertEqual("{0:.{1}s}".format('hello world', 5), 'hello')
314 self
.assertEqual("{0:.{precision}s}".format('hello world', precision
=5), 'hello')
315 self
.assertEqual("{0:{width}.{precision}s}".format('hello world', width
=10, precision
=5), 'hello ')
316 self
.assertEqual("{0:{width}.{precision}s}".format('hello world', width
='10', precision
='5'), 'hello ')
318 # test various errors
319 self
.assertRaises(ValueError, '{'.format
)
320 self
.assertRaises(ValueError, '}'.format
)
321 self
.assertRaises(ValueError, 'a{'.format
)
322 self
.assertRaises(ValueError, 'a}'.format
)
323 self
.assertRaises(ValueError, '{a'.format
)
324 self
.assertRaises(ValueError, '}a'.format
)
325 self
.assertRaises(IndexError, '{0}'.format
)
326 self
.assertRaises(IndexError, '{1}'.format
, 'abc')
327 self
.assertRaises(KeyError, '{x}'.format
)
328 self
.assertRaises(ValueError, "}{".format
)
329 self
.assertRaises(ValueError, "{".format
)
330 self
.assertRaises(ValueError, "}".format
)
331 self
.assertRaises(ValueError, "abc{0:{}".format
)
332 self
.assertRaises(ValueError, "{0".format
)
333 self
.assertRaises(IndexError, "{0.}".format
)
334 self
.assertRaises(ValueError, "{0.}".format
, 0)
335 self
.assertRaises(IndexError, "{0[}".format
)
336 self
.assertRaises(ValueError, "{0[}".format
, [])
337 self
.assertRaises(KeyError, "{0]}".format
)
338 self
.assertRaises(ValueError, "{0.[]}".format
, 0)
339 self
.assertRaises(ValueError, "{0..foo}".format
, 0)
340 self
.assertRaises(ValueError, "{0[0}".format
, 0)
341 self
.assertRaises(ValueError, "{0[0:foo}".format
, 0)
342 self
.assertRaises(KeyError, "{c]}".format
)
343 self
.assertRaises(ValueError, "{{ {{{0}}".format
, 0)
344 self
.assertRaises(ValueError, "{0}}".format
, 0)
345 self
.assertRaises(KeyError, "{foo}".format
, bar
=3)
346 self
.assertRaises(ValueError, "{0!x}".format
, 3)
347 self
.assertRaises(ValueError, "{0!}".format
, 0)
348 self
.assertRaises(ValueError, "{0!rs}".format
, 0)
349 self
.assertRaises(ValueError, "{!}".format
)
350 self
.assertRaises(IndexError, "{:}".format
)
351 self
.assertRaises(IndexError, "{:s}".format
)
352 self
.assertRaises(IndexError, "{}".format
)
355 self
.assertRaises(ValueError, "{0[0]x}".format
, [None])
356 self
.assertRaises(ValueError, "{0[0](10)}".format
, [None])
358 # can't have a replacement on the field name portion
359 self
.assertRaises(TypeError, '{0[{1}]}'.format
, 'abcdefg', 4)
361 # exceed maximum recursion depth
362 self
.assertRaises(ValueError, "{0:{1:{2}}}".format
, 'abc', 's', '')
363 self
.assertRaises(ValueError, "{0:{1:{2:{3:{4:{5:{6}}}}}}}".format
,
364 0, 1, 2, 3, 4, 5, 6, 7)
366 # string format spec errors
367 self
.assertRaises(ValueError, "{0:-s}".format
, '')
368 self
.assertRaises(ValueError, format
, "", "-")
369 self
.assertRaises(ValueError, "{0:=s}".format
, '')
371 def test_format_auto_numbering(self
):
373 def __init__(self
, x
=100):
375 def __format__(self
, spec
):
378 self
.assertEqual('{}'.format(10), '10')
379 self
.assertEqual('{:5}'.format('s'), 's ')
380 self
.assertEqual('{!r}'.format('s'), "'s'")
381 self
.assertEqual('{._x}'.format(C(10)), '10')
382 self
.assertEqual('{[1]}'.format([1, 2]), '2')
383 self
.assertEqual('{[a]}'.format({'a':4, 'b':2}), '4')
384 self
.assertEqual('a{}b{}c'.format(0, 1), 'a0b1c')
386 self
.assertEqual('a{:{}}b'.format('x', '^10'), 'a x b')
387 self
.assertEqual('a{:{}x}b'.format(20, '#'), 'a0x14b')
389 # can't mix and match numbering and auto-numbering
390 self
.assertRaises(ValueError, '{}{1}'.format
, 1, 2)
391 self
.assertRaises(ValueError, '{1}{}'.format
, 1, 2)
392 self
.assertRaises(ValueError, '{:{1}}'.format
, 1, 2)
393 self
.assertRaises(ValueError, '{0:{}}'.format
, 1, 2)
395 # can mix and match auto-numbering and named
396 self
.assertEqual('{f}{}'.format(4, f
='test'), 'test4')
397 self
.assertEqual('{}{f}'.format(4, f
='test'), '4test')
398 self
.assertEqual('{:{f}}{g}{}'.format(1, 3, g
='g', f
=2), ' 1g3')
399 self
.assertEqual('{f:{}}{}{g}'.format(2, 4, f
=1, g
='g'), ' 14g')
401 def test_buffer_is_readonly(self
):
402 self
.assertRaises(TypeError, sys
.stdin
.readinto
, b
"")
404 def test_encode_and_decode_kwargs(self
):
405 self
.assertEqual('abcde'.encode('ascii', 'replace'),
406 'abcde'.encode('ascii', errors
='replace'))
407 self
.assertEqual('abcde'.encode('ascii', 'ignore'),
408 'abcde'.encode(encoding
='ascii', errors
='ignore'))
409 self
.assertEqual('Andr\202 x'.decode('ascii', 'ignore'),
410 'Andr\202 x'.decode('ascii', errors
='ignore'))
411 self
.assertEqual('Andr\202 x'.decode('ascii', 'replace'),
412 'Andr\202 x'.decode(encoding
='ascii', errors
='replace'))
417 test_support
.run_unittest(StrTest
)
419 if __name__
== "__main__":