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

Method test_basic_re_sub

Lib/test/test_re.py:122–165  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

120 return str(int_value + 1)
121
122 def test_basic_re_sub(self):
123 self.assertTypedEqual(re.sub('y', 'a', 'xyz'), 'xaz')
124 self.assertTypedEqual(re.sub('y', S('a'), S('xyz')), 'xaz')
125 self.assertTypedEqual(re.sub(b'y', b'a', b'xyz'), b'xaz')
126 self.assertTypedEqual(re.sub(b'y', B(b'a'), B(b'xyz')), b'xaz')
127 self.assertTypedEqual(re.sub(b'y', bytearray(b'a'), bytearray(b'xyz')), b'xaz')
128 self.assertTypedEqual(re.sub(b'y', memoryview(b'a'), memoryview(b'xyz')), b'xaz')
129 for y in ("\xe0", "\u0430", "\U0001d49c"):
130 self.assertEqual(re.sub(y, 'a', 'x%sz' % y), 'xaz')
131
132 self.assertEqual(re.sub("(?i)b+", "x", "bbbb BBBB"), 'x x')
133 self.assertEqual(re.sub(r'\d+', self.bump_num, '08.2 -2 23x99y'),
134 '9.3 -3 24x100y')
135 with self.assertWarns(DeprecationWarning) as w:
136 self.assertEqual(re.sub(r'\d+', self.bump_num, '08.2 -2 23x99y', 3),
137 '9.3 -3 23x99y')
138 self.assertEqual(w.filename, __file__)
139 self.assertEqual(re.sub(r'\d+', self.bump_num, '08.2 -2 23x99y', count=3),
140 '9.3 -3 23x99y')
141
142 self.assertEqual(re.sub('.', lambda m: r"\n", 'x'), '\\n')
143 self.assertEqual(re.sub('.', r"\n", 'x'), '\n')
144
145 s = r"\1\1"
146 self.assertEqual(re.sub('(.)', s, 'x'), 'xx')
147 self.assertEqual(re.sub('(.)', s.replace('\\', r'\\'), 'x'), s)
148 self.assertEqual(re.sub('(.)', lambda m: s, 'x'), s)
149
150 self.assertEqual(re.sub('(?P<a>x)', r'\g<a>\g<a>', 'xx'), 'xxxx')
151 self.assertEqual(re.sub('(?P<a>x)', r'\g<a>\g<1>', 'xx'), 'xxxx')
152 self.assertEqual(re.sub('(?P<unk>x)', r'\g<unk>\g<unk>', 'xx'), 'xxxx')
153 self.assertEqual(re.sub('(?P<unk>x)', r'\g<1>\g<1>', 'xx'), 'xxxx')
154 self.assertEqual(re.sub('()x', r'\g<0>\g<0>', 'xx'), 'xxxx')
155
156 self.assertEqual(re.sub('a', r'\t\n\v\r\f\a\b', 'a'), '\t\n\v\r\f\a\b')
157 self.assertEqual(re.sub('a', '\t\n\v\r\f\a\b', 'a'), '\t\n\v\r\f\a\b')
158 self.assertEqual(re.sub('a', '\t\n\v\r\f\a\b', 'a'),
159 (chr(9)+chr(10)+chr(11)+chr(13)+chr(12)+chr(7)+chr(8)))
160 for c in 'cdehijklmopqsuwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ':
161 with self.subTest(c):
162 with self.assertRaises(re.PatternError):
163 self.assertEqual(re.sub('a', '\\' + c, 'a'), '\\' + c)
164
165 self.assertEqual(re.sub(r'^\s*', 'X', 'test'), 'Xtest')
166
167 def test_bug_449964(self):
168 # fails for group followed by other escape

Callers

nothing calls this directly

Calls 9

assertTypedEqualMethod · 0.95
assertWarnsMethod · 0.80
SClass · 0.70
BClass · 0.70
subMethod · 0.45
assertEqualMethod · 0.45
replaceMethod · 0.45
subTestMethod · 0.45
assertRaisesMethod · 0.45

Tested by

no test coverage detected