MCPcopy Index your code
hub / github.com/python/cpython / test_formatting

Method test_formatting

Lib/test/string_tests.py:1394–1445  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

1392 self.fail('exception not raised')
1393
1394 def test_formatting(self):
1395 self.checkequal('+hello+', '+%s+', '__mod__', 'hello')
1396 self.checkequal('+10+', '+%d+', '__mod__', 10)
1397 self.checkequal('a', "%c", '__mod__', "a")
1398 self.checkequal('a', "%c", '__mod__', "a")
1399 self.checkequal('"', "%c", '__mod__', 34)
1400 self.checkequal('$', "%c", '__mod__', 36)
1401 self.checkequal('10', "%d", '__mod__', 10)
1402 self.checkequal('\x7f', "%c", '__mod__', 0x7f)
1403
1404 for ordinal in (-100, 0x200000):
1405 # unicode raises ValueError, str raises OverflowError
1406 self.checkraises((ValueError, OverflowError), '%c', '__mod__', ordinal)
1407
1408 longvalue = sys.maxsize + 10
1409 slongvalue = str(longvalue)
1410 self.checkequal(' 42', '%3ld', '__mod__', 42)
1411 self.checkequal('42', '%d', '__mod__', 42.0)
1412 self.checkequal(slongvalue, '%d', '__mod__', longvalue)
1413 self.checkcall('%d', '__mod__', float(longvalue))
1414 self.checkequal('0042.00', '%07.2f', '__mod__', 42)
1415 self.checkequal('0042.00', '%07.2F', '__mod__', 42)
1416
1417 self.checkraises(TypeError, 'abc', '__mod__')
1418 self.checkraises(TypeError, '%(foo)s', '__mod__', 42)
1419 self.checkraises(TypeError, '%s%s', '__mod__', (42,))
1420 self.checkraises(TypeError, '%c', '__mod__', (None,))
1421 self.checkraises(ValueError, '%(foo', '__mod__', {})
1422 self.checkraises(TypeError, '%(foo)s %(bar)s', '__mod__', ('foo', 42))
1423 self.checkraises(TypeError, '%d', '__mod__', "42") # not numeric
1424 self.checkraises(TypeError, '%d', '__mod__', (42+0j)) # no int conversion provided
1425
1426 # argument names with properly nested brackets are supported
1427 self.checkequal('bar', '%((foo))s', '__mod__', {'(foo)': 'bar'})
1428
1429 # 100 is a magic number in PyUnicode_Format, this forces a resize
1430 self.checkequal(103*'a'+'x', '%sx', '__mod__', 103*'a')
1431
1432 self.checkraises(TypeError, '%*s', '__mod__', ('foo', 'bar'))
1433 self.checkraises(TypeError, '%10.*f', '__mod__', ('foo', 42.))
1434 self.checkraises(ValueError, '%10', '__mod__', (42,))
1435
1436 # Outrageously large width or precision should raise ValueError.
1437 self.checkraises(ValueError, '%%%df' % (2**64), '__mod__', (3.2))
1438 self.checkraises(ValueError, '%%.%df' % (2**64), '__mod__', (3.2))
1439 self.checkraises(OverflowError, '%*s', '__mod__',
1440 (sys.maxsize + 1, ''))
1441 self.checkraises(OverflowError, '%.*f', '__mod__',
1442 (sys.maxsize + 1, 1. / 7))
1443
1444 class X(object): pass
1445 self.checkraises(TypeError, 'abc', '__mod__', X())
1446
1447 @support.cpython_only
1448 def test_formatting_c_limits(self):

Callers

nothing calls this directly

Calls 5

strFunction · 0.85
XClass · 0.70
checkequalMethod · 0.45
checkraisesMethod · 0.45
checkcallMethod · 0.45

Tested by

no test coverage detected