MCPcopy Index your code
hub / github.com/RustPython/RustPython / test_basic_re_sub

Method test_basic_re_sub

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

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls 10

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

Tested by

no test coverage detected