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

Method test_format

Lib/test/test_str.py:1080–1406  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

1078 '\U00100000'.rjust(3, '\U00010000')
1079
1080 def test_format(self):
1081 self.assertEqual(''.format(), '')
1082 self.assertEqual('a'.format(), 'a')
1083 self.assertEqual('ab'.format(), 'ab')
1084 self.assertEqual('a{{'.format(), 'a{')
1085 self.assertEqual('a}}'.format(), 'a}')
1086 self.assertEqual('{{b'.format(), '{b')
1087 self.assertEqual('}}b'.format(), '}b')
1088 self.assertEqual('a{{b'.format(), 'a{b')
1089
1090 # examples from the PEP:
1091 import datetime
1092 self.assertEqual("My name is {0}".format('Fred'), "My name is Fred")
1093 self.assertEqual("My name is {0[name]}".format(dict(name='Fred')),
1094 "My name is Fred")
1095 self.assertEqual("My name is {0} :-{{}}".format('Fred'),
1096 "My name is Fred :-{}")
1097
1098 d = datetime.date(2007, 8, 18)
1099 self.assertEqual("The year is {0.year}".format(d),
1100 "The year is 2007")
1101
1102 # classes we'll use for testing
1103 class C:
1104 def __init__(self, x=100):
1105 self._x = x
1106 def __format__(self, spec):
1107 return spec
1108
1109 class D:
1110 def __init__(self, x):
1111 self.x = x
1112 def __format__(self, spec):
1113 return str(self.x)
1114
1115 # class with __str__, but no __format__
1116 class E:
1117 def __init__(self, x):
1118 self.x = x
1119 def __str__(self):
1120 return 'E(' + self.x + ')'
1121
1122 # class with __repr__, but no __format__ or __str__
1123 class F:
1124 def __init__(self, x):
1125 self.x = x
1126 def __repr__(self):
1127 return 'F(' + self.x + ')'
1128
1129 # class with __format__ that forwards to string, for some format_spec's
1130 class G:
1131 def __init__(self, x):
1132 self.x = x
1133 def __str__(self):
1134 return "string is " + self.x
1135 def __format__(self, format_spec):
1136 if format_spec == 'd':
1137 return 'G(' + self.x + ')'

Callers

nothing calls this directly

Calls 14

dateMethod · 0.80
assertRaisesRegexMethod · 0.80
CClass · 0.70
DClass · 0.70
FClass · 0.70
GClass · 0.70
EClass · 0.70
IClass · 0.70
JClass · 0.70
MClass · 0.70
NClass · 0.70
assertEqualMethod · 0.45

Tested by

no test coverage detected