Changeset 391 for python/trunk/Lib/test/test_str.py
- Timestamp:
- Mar 19, 2014, 11:31:01 PM (11 years ago)
- Location:
- python/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
python/trunk
-
Property svn:mergeinfo
set to
/python/vendor/Python-2.7.6 merged eligible /python/vendor/current merged eligible
-
Property svn:mergeinfo
set to
-
python/trunk/Lib/test/test_str.py
r2 r391 36 36 self.assertRaises(OverflowError, '%c'.__mod__, 0x1234) 37 37 38 @test_support.cpython_only 39 def test_formatting_huge_precision(self): 40 from _testcapi import INT_MAX 41 format_string = "%.{}f".format(INT_MAX + 1) 42 with self.assertRaises(ValueError): 43 result = format_string % 2.34 44 45 def test_formatting_huge_width(self): 46 format_string = "%{}f".format(sys.maxsize + 1) 47 with self.assertRaises(ValueError): 48 result = format_string % 2.34 49 38 50 def test_conversion(self): 39 51 # Make sure __str__() behaves properly … … 87 99 return "not unicode" 88 100 89 self.assert _(str(Foo0()).startswith("<")) # this is different from __unicode__101 self.assertTrue(str(Foo0()).startswith("<")) # this is different from __unicode__ 90 102 self.assertEqual(str(Foo1()), "foo") 91 103 self.assertEqual(str(Foo2()), "foo") … … 277 289 self.assertEqual('{0:abc}'.format(C()), 'abc') 278 290 279 # !r and !s coer sions291 # !r and !s coercions 280 292 self.assertEqual('{0!s}'.format('Hello'), 'Hello') 281 293 self.assertEqual('{0!s:}'.format('Hello'), 'Hello') … … 291 303 self.assertEqual('{0}'.format([1]), '[1]') 292 304 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 305 self.assertEqual('{0:d}'.format(G('data')), 'G(data)') 296 self.assertEqual('{0:>15s}'.format(G('data')), ' string is data')297 306 self.assertEqual('{0!s}'.format(G('data')), 'string is data') 307 308 msg = 'object.__format__ with a non-empty format string is deprecated' 309 with test_support.check_warnings((msg, PendingDeprecationWarning)): 310 self.assertEqual('{0:^10}'.format(E('data')), ' E(data) ') 311 self.assertEqual('{0:^10s}'.format(E('data')), ' E(data) ') 312 self.assertEqual('{0:>15s}'.format(G('data')), ' string is data') 298 313 299 314 self.assertEqual("{0:date: %Y-%m-%d}".format(I(year=2007, … … 348 363 self.assertRaises(ValueError, "{0!rs}".format, 0) 349 364 self.assertRaises(ValueError, "{!}".format) 350 self.assertRaises( ValueError, "{:}".format)351 self.assertRaises( ValueError, "{:s}".format)352 self.assertRaises( ValueError, "{}".format)365 self.assertRaises(IndexError, "{:}".format) 366 self.assertRaises(IndexError, "{:s}".format) 367 self.assertRaises(IndexError, "{}".format) 353 368 354 369 # issue 6089 … … 369 384 self.assertRaises(ValueError, "{0:=s}".format, '') 370 385 386 def test_format_huge_precision(self): 387 format_string = ".{}f".format(sys.maxsize + 1) 388 with self.assertRaises(ValueError): 389 result = format(2.34, format_string) 390 391 def test_format_huge_width(self): 392 format_string = "{}f".format(sys.maxsize + 1) 393 with self.assertRaises(ValueError): 394 result = format(2.34, format_string) 395 396 def test_format_huge_item_number(self): 397 format_string = "{{{}:.6f}}".format(sys.maxsize + 1) 398 with self.assertRaises(ValueError): 399 result = format_string.format(2.34) 400 401 def test_format_auto_numbering(self): 402 class C: 403 def __init__(self, x=100): 404 self._x = x 405 def __format__(self, spec): 406 return spec 407 408 self.assertEqual('{}'.format(10), '10') 409 self.assertEqual('{:5}'.format('s'), 's ') 410 self.assertEqual('{!r}'.format('s'), "'s'") 411 self.assertEqual('{._x}'.format(C(10)), '10') 412 self.assertEqual('{[1]}'.format([1, 2]), '2') 413 self.assertEqual('{[a]}'.format({'a':4, 'b':2}), '4') 414 self.assertEqual('a{}b{}c'.format(0, 1), 'a0b1c') 415 416 self.assertEqual('a{:{}}b'.format('x', '^10'), 'a x b') 417 self.assertEqual('a{:{}x}b'.format(20, '#'), 'a0x14b') 418 419 # can't mix and match numbering and auto-numbering 420 self.assertRaises(ValueError, '{}{1}'.format, 1, 2) 421 self.assertRaises(ValueError, '{1}{}'.format, 1, 2) 422 self.assertRaises(ValueError, '{:{1}}'.format, 1, 2) 423 self.assertRaises(ValueError, '{0:{}}'.format, 1, 2) 424 425 # can mix and match auto-numbering and named 426 self.assertEqual('{f}{}'.format(4, f='test'), 'test4') 427 self.assertEqual('{}{f}'.format(4, f='test'), '4test') 428 self.assertEqual('{:{f}}{g}{}'.format(1, 3, g='g', f=2), ' 1g3') 429 self.assertEqual('{f:{}}{}{g}'.format(2, 4, f=1, g='g'), ' 14g') 430 371 431 def test_buffer_is_readonly(self): 372 432 self.assertRaises(TypeError, sys.stdin.readinto, b"") 373 433 434 def test_encode_and_decode_kwargs(self): 435 self.assertEqual('abcde'.encode('ascii', 'replace'), 436 'abcde'.encode('ascii', errors='replace')) 437 self.assertEqual('abcde'.encode('ascii', 'ignore'), 438 'abcde'.encode(encoding='ascii', errors='ignore')) 439 self.assertEqual('Andr\202 x'.decode('ascii', 'ignore'), 440 'Andr\202 x'.decode('ascii', errors='ignore')) 441 self.assertEqual('Andr\202 x'.decode('ascii', 'replace'), 442 'Andr\202 x'.decode(encoding='ascii', errors='replace')) 443 444 def test_startswith_endswith_errors(self): 445 with self.assertRaises(UnicodeDecodeError): 446 '\xff'.startswith(u'x') 447 with self.assertRaises(UnicodeDecodeError): 448 '\xff'.endswith(u'x') 449 for meth in ('foo'.startswith, 'foo'.endswith): 450 with self.assertRaises(TypeError) as cm: 451 meth(['f']) 452 exc = str(cm.exception) 453 self.assertIn('unicode', exc) 454 self.assertIn('str', exc) 455 self.assertIn('tuple', exc) 374 456 375 457 def test_main():
Note:
See TracChangeset
for help on using the changeset viewer.