blob: 3ed159799f5cc8e10eef6507263252a4817eb3f4 [file] [log] [blame]
Yegappan Lakshmanan5e877ba2022-03-25 21:19:26 +00001" Tests for the substitute (:s) command
Bram Moolenaarcd055da2016-09-02 19:50:48 +02002
Bram Moolenaar9f6277b2020-02-11 22:04:02 +01003source shared.vim
Bram Moolenaar7a2217b2021-06-06 12:33:49 +02004source check.vim
Bram Moolenaara04f4572022-09-13 13:45:26 +01005source screendump.vim
Bram Moolenaar9f6277b2020-02-11 22:04:02 +01006
Bram Moolenaar1e115362019-01-09 23:01:02 +01007func Test_multiline_subst()
Bram Moolenaarcd055da2016-09-02 19:50:48 +02008 enew!
9 call append(0, ["1 aa",
10 \ "bb",
11 \ "cc",
12 \ "2 dd",
13 \ "ee",
14 \ "3 ef",
15 \ "gh",
16 \ "4 ij",
17 \ "5 a8",
18 \ "8b c9",
19 \ "9d",
20 \ "6 e7",
21 \ "77f",
22 \ "xxxxx"])
23
24 1
25 " test if replacing a line break works with a back reference
26 /^1/,/^2/s/\n\(.\)/ \1/
27 " test if inserting a line break works with a back reference
28 /^3/,/^4/s/\(.\)$/\r\1/
29 " test if replacing a line break with another line break works
30 /^5/,/^6/s/\(\_d\{3}\)/x\1x/
31 call assert_equal('1 aa bb cc 2 dd ee', getline(1))
32 call assert_equal('3 e', getline(2))
33 call assert_equal('f', getline(3))
34 call assert_equal('g', getline(4))
35 call assert_equal('h', getline(5))
36 call assert_equal('4 i', getline(6))
37 call assert_equal('j', getline(7))
38 call assert_equal('5 ax8', getline(8))
39 call assert_equal('8xb cx9', getline(9))
40 call assert_equal('9xd', getline(10))
41 call assert_equal('6 ex7', getline(11))
42 call assert_equal('7x7f', getline(12))
43 call assert_equal('xxxxx', getline(13))
44 enew!
Bram Moolenaar1e115362019-01-09 23:01:02 +010045endfunc
Bram Moolenaar8c50d502017-02-17 18:28:24 +010046
Bram Moolenaar1e115362019-01-09 23:01:02 +010047func Test_substitute_variants()
Bram Moolenaar8c50d502017-02-17 18:28:24 +010048 " Validate that all the 2-/3-letter variants which embed the flags into the
49 " command name actually work.
50 enew!
51 let ln = 'Testing string'
52 let variants = [
53 \ { 'cmd': ':s/Test/test/c', 'exp': 'testing string', 'prompt': 'y' },
54 \ { 'cmd': ':s/foo/bar/ce', 'exp': ln },
55 \ { 'cmd': ':s/t/r/cg', 'exp': 'Tesring srring', 'prompt': 'a' },
56 \ { 'cmd': ':s/t/r/ci', 'exp': 'resting string', 'prompt': 'y' },
57 \ { 'cmd': ':s/t/r/cI', 'exp': 'Tesring string', 'prompt': 'y' },
Bram Moolenaarea3db912020-02-02 15:32:13 +010058 \ { 'cmd': ':s/t/r/c', 'exp': 'Testing string', 'prompt': 'n' },
Bram Moolenaar8c50d502017-02-17 18:28:24 +010059 \ { 'cmd': ':s/t/r/cn', 'exp': ln },
60 \ { 'cmd': ':s/t/r/cp', 'exp': 'Tesring string', 'prompt': 'y' },
61 \ { 'cmd': ':s/t/r/cl', 'exp': 'Tesring string', 'prompt': 'y' },
62 \ { 'cmd': ':s/t/r/gc', 'exp': 'Tesring srring', 'prompt': 'a' },
Bram Moolenaarea3db912020-02-02 15:32:13 +010063 \ { 'cmd': ':s/i/I/gc', 'exp': 'TestIng string', 'prompt': 'l' },
Bram Moolenaar8c50d502017-02-17 18:28:24 +010064 \ { 'cmd': ':s/foo/bar/ge', 'exp': ln },
65 \ { 'cmd': ':s/t/r/g', 'exp': 'Tesring srring' },
66 \ { 'cmd': ':s/t/r/gi', 'exp': 'resring srring' },
67 \ { 'cmd': ':s/t/r/gI', 'exp': 'Tesring srring' },
68 \ { 'cmd': ':s/t/r/gn', 'exp': ln },
69 \ { 'cmd': ':s/t/r/gp', 'exp': 'Tesring srring' },
70 \ { 'cmd': ':s/t/r/gl', 'exp': 'Tesring srring' },
71 \ { 'cmd': ':s//r/gr', 'exp': 'Testr strr' },
72 \ { 'cmd': ':s/t/r/ic', 'exp': 'resting string', 'prompt': 'y' },
73 \ { 'cmd': ':s/foo/bar/ie', 'exp': ln },
74 \ { 'cmd': ':s/t/r/i', 'exp': 'resting string' },
75 \ { 'cmd': ':s/t/r/iI', 'exp': 'Tesring string' },
76 \ { 'cmd': ':s/t/r/in', 'exp': ln },
77 \ { 'cmd': ':s/t/r/ip', 'exp': 'resting string' },
78 \ { 'cmd': ':s//r/ir', 'exp': 'Testr string' },
79 \ { 'cmd': ':s/t/r/Ic', 'exp': 'Tesring string', 'prompt': 'y' },
80 \ { 'cmd': ':s/foo/bar/Ie', 'exp': ln },
81 \ { 'cmd': ':s/t/r/Ig', 'exp': 'Tesring srring' },
82 \ { 'cmd': ':s/t/r/Ii', 'exp': 'resting string' },
83 \ { 'cmd': ':s/t/r/I', 'exp': 'Tesring string' },
84 \ { 'cmd': ':s/t/r/Ip', 'exp': 'Tesring string' },
85 \ { 'cmd': ':s/t/r/Il', 'exp': 'Tesring string' },
86 \ { 'cmd': ':s//r/Ir', 'exp': 'Testr string' },
87 \ { 'cmd': ':s//r/rc', 'exp': 'Testr string', 'prompt': 'y' },
88 \ { 'cmd': ':s//r/rg', 'exp': 'Testr strr' },
89 \ { 'cmd': ':s//r/ri', 'exp': 'Testr string' },
90 \ { 'cmd': ':s//r/rI', 'exp': 'Testr string' },
91 \ { 'cmd': ':s//r/rn', 'exp': 'Testing string' },
92 \ { 'cmd': ':s//r/rp', 'exp': 'Testr string' },
93 \ { 'cmd': ':s//r/rl', 'exp': 'Testr string' },
94 \ { 'cmd': ':s//r/r', 'exp': 'Testr string' },
Bram Moolenaarea3db912020-02-02 15:32:13 +010095 \ { 'cmd': ':s/i/I/gc', 'exp': 'Testing string', 'prompt': 'q' },
Bram Moolenaar8c50d502017-02-17 18:28:24 +010096 \]
97
98 for var in variants
99 for run in [1, 2]
100 let cmd = var.cmd
101 if run == 2 && cmd =~ "/.*/.*/."
102 " Change :s/from/to/{flags} to :s{flags}
103 let cmd = substitute(cmd, '/.*/', '', '')
104 endif
105 call setline(1, [ln])
106 let msg = printf('using "%s"', cmd)
107 let @/='ing'
108 let v:errmsg = ''
109 call feedkeys(cmd . "\<CR>" . get(var, 'prompt', ''), 'ntx')
110 " No error should exist (matters for testing e flag)
111 call assert_equal('', v:errmsg, msg)
112 call assert_equal(var.exp, getline('.'), msg)
113 endfor
114 endfor
Bram Moolenaar1e115362019-01-09 23:01:02 +0100115endfunc
Bram Moolenaarba748c82017-02-26 14:00:07 +0100116
Bram Moolenaar94747162019-02-10 21:55:26 +0100117" Test the l, p, # flags.
118func Test_substitute_flags_lp()
119 new
120 call setline(1, "abc\tdef\<C-h>ghi")
121
122 let a = execute('s/a/a/p')
123 call assert_equal("\nabc def^Hghi", a)
124
125 let a = execute('s/a/a/l')
126 call assert_equal("\nabc^Idef^Hghi$", a)
127
128 let a = execute('s/a/a/#')
129 call assert_equal("\n 1 abc def^Hghi", a)
130
131 let a = execute('s/a/a/p#')
132 call assert_equal("\n 1 abc def^Hghi", a)
133
134 let a = execute('s/a/a/l#')
135 call assert_equal("\n 1 abc^Idef^Hghi$", a)
136
137 let a = execute('s/a/a/')
138 call assert_equal("", a)
139
140 bwipe!
141endfunc
142
Bram Moolenaarba748c82017-02-26 14:00:07 +0100143func Test_substitute_repeat()
144 " This caused an invalid memory access.
Bram Moolenaarb18b4962022-09-02 21:55:50 +0100145 split Xsubfile
Bram Moolenaarba748c82017-02-26 14:00:07 +0100146 s/^/x
147 call feedkeys("Qsc\<CR>y", 'tx')
148 bwipe!
149endfunc
Bram Moolenaar1a333bc2017-08-30 20:21:58 +0200150
Bram Moolenaard77aa4d2019-02-10 22:50:14 +0100151" Test %s/\n// which is implemented as a special case to use a
152" more efficient join rather than doing a regular substitution.
153func Test_substitute_join()
154 new
155
156 call setline(1, ["foo\tbar", "bar\<C-H>foo"])
157 let a = execute('%s/\n//')
158 call assert_equal("", a)
159 call assert_equal(["foo\tbarbar\<C-H>foo"], getline(1, '$'))
160 call assert_equal('\n', histget("search", -1))
161
162 call setline(1, ["foo\tbar", "bar\<C-H>foo"])
163 let a = execute('%s/\n//g')
164 call assert_equal("", a)
165 call assert_equal(["foo\tbarbar\<C-H>foo"], getline(1, '$'))
166 call assert_equal('\n', histget("search", -1))
167
168 call setline(1, ["foo\tbar", "bar\<C-H>foo"])
169 let a = execute('%s/\n//p')
170 call assert_equal("\nfoo barbar^Hfoo", a)
171 call assert_equal(["foo\tbarbar\<C-H>foo"], getline(1, '$'))
172 call assert_equal('\n', histget("search", -1))
173
174 call setline(1, ["foo\tbar", "bar\<C-H>foo"])
175 let a = execute('%s/\n//l')
176 call assert_equal("\nfoo^Ibarbar^Hfoo$", a)
177 call assert_equal(["foo\tbarbar\<C-H>foo"], getline(1, '$'))
178 call assert_equal('\n', histget("search", -1))
179
180 call setline(1, ["foo\tbar", "bar\<C-H>foo"])
181 let a = execute('%s/\n//#')
182 call assert_equal("\n 1 foo barbar^Hfoo", a)
183 call assert_equal(["foo\tbarbar\<C-H>foo"], getline(1, '$'))
184 call assert_equal('\n', histget("search", -1))
185
Bram Moolenaarea3db912020-02-02 15:32:13 +0100186 call setline(1, ['foo', 'bar', 'baz', 'qux'])
187 call execute('1,2s/\n//')
188 call assert_equal(['foobarbaz', 'qux'], getline(1, '$'))
189
Bram Moolenaard77aa4d2019-02-10 22:50:14 +0100190 bwipe!
191endfunc
192
193func Test_substitute_count()
194 new
195 call setline(1, ['foo foo', 'foo foo', 'foo foo', 'foo foo', 'foo foo'])
196 2
197
198 s/foo/bar/3
199 call assert_equal(['foo foo', 'bar foo', 'bar foo', 'bar foo', 'foo foo'],
200 \ getline(1, '$'))
201
202 call assert_fails('s/foo/bar/0', 'E939:')
203
Bram Moolenaarea3db912020-02-02 15:32:13 +0100204 call setline(1, ['foo foo', 'foo foo', 'foo foo', 'foo foo', 'foo foo'])
205 2,4s/foo/bar/ 10
206 call assert_equal(['foo foo', 'foo foo', 'foo foo', 'bar foo', 'bar foo'],
207 \ getline(1, '$'))
208
Christian Brabandtac637872023-11-14 20:45:48 +0100209 call assert_fails('s/./b/2147483647', 'E1510:')
Bram Moolenaard77aa4d2019-02-10 22:50:14 +0100210 bwipe!
211endfunc
212
213" Test substitute 'n' flag (report number of matches, do not substitute).
214func Test_substitute_flag_n()
215 new
216 let lines = ['foo foo', 'foo foo', 'foo foo', 'foo foo', 'foo foo']
217 call setline(1, lines)
218
219 call assert_equal("\n3 matches on 3 lines", execute('2,4s/foo/bar/n'))
220 call assert_equal("\n6 matches on 3 lines", execute('2,4s/foo/bar/gn'))
221
222 " c flag (confirm) should be ignored when using n flag.
223 call assert_equal("\n3 matches on 3 lines", execute('2,4s/foo/bar/nc'))
224
225 " No substitution should have been done.
226 call assert_equal(lines, getline(1, '$'))
227
Bram Moolenaarea3db912020-02-02 15:32:13 +0100228 %delete _
229 call setline(1, ['A', 'Bar', 'Baz'])
230 call assert_equal("\n1 match on 1 line", execute('s/\nB\@=//gn'))
231
Bram Moolenaard77aa4d2019-02-10 22:50:14 +0100232 bwipe!
233endfunc
234
235func Test_substitute_errors()
236 new
237 call setline(1, 'foobar')
238
239 call assert_fails('s/FOO/bar/', 'E486:')
240 call assert_fails('s/foo/bar/@', 'E488:')
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200241 call assert_fails('s/\(/bar/', 'E54:')
Bram Moolenaar5d98dc22020-01-29 21:57:34 +0100242 call assert_fails('s afooabara', 'E146:')
243 call assert_fails('s\\a', 'E10:')
Bram Moolenaard77aa4d2019-02-10 22:50:14 +0100244
245 setl nomodifiable
246 call assert_fails('s/foo/bar/', 'E21:')
247
Bram Moolenaar0e05de42020-03-25 22:23:46 +0100248 call assert_fails("let s=substitute([], 'a', 'A', 'g')", 'E730:')
249 call assert_fails("let s=substitute('abcda', [], 'A', 'g')", 'E730:')
250 call assert_fails("let s=substitute('abcda', 'a', [], 'g')", 'E730:')
251 call assert_fails("let s=substitute('abcda', 'a', 'A', [])", 'E730:')
Bram Moolenaar531be472020-09-23 22:38:05 +0200252 call assert_fails("let s=substitute('abc', '\\%(', 'A', 'g')", 'E53:')
Bram Moolenaar0e05de42020-03-25 22:23:46 +0100253
Bram Moolenaard77aa4d2019-02-10 22:50:14 +0100254 bwipe!
255endfunc
256
Bram Moolenaar1a333bc2017-08-30 20:21:58 +0200257" Test for *sub-replace-special* and *sub-replace-expression* on substitute().
258func Test_sub_replace_1()
259 " Run the tests with 'magic' on
260 set magic
261 set cpo&
262 call assert_equal('AA', substitute('A', 'A', '&&', ''))
263 call assert_equal('&', substitute('B', 'B', '\&', ''))
264 call assert_equal('C123456789987654321', substitute('C123456789', 'C\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)', '\0\9\8\7\6\5\4\3\2\1', ''))
265 call assert_equal('d', substitute('D', 'D', 'd', ''))
266 call assert_equal('~', substitute('E', 'E', '~', ''))
267 call assert_equal('~', substitute('F', 'F', '\~', ''))
268 call assert_equal('Gg', substitute('G', 'G', '\ugg', ''))
269 call assert_equal('Hh', substitute('H', 'H', '\Uh\Eh', ''))
270 call assert_equal('iI', substitute('I', 'I', '\lII', ''))
271 call assert_equal('jJ', substitute('J', 'J', '\LJ\EJ', ''))
272 call assert_equal('Kk', substitute('K', 'K', '\Uk\ek', ''))
273 call assert_equal("l\<C-V>\<C-M>l",
274 \ substitute('lLl', 'L', "\<C-V>\<C-M>", ''))
275 call assert_equal("m\<C-M>m", substitute('mMm', 'M', '\r', ''))
276 call assert_equal("n\<C-V>\<C-M>n",
277 \ substitute('nNn', 'N', "\\\<C-V>\<C-M>", ''))
278 call assert_equal("o\no", substitute('oOo', 'O', '\n', ''))
279 call assert_equal("p\<C-H>p", substitute('pPp', 'P', '\b', ''))
280 call assert_equal("q\tq", substitute('qQq', 'Q', '\t', ''))
281 call assert_equal('r\r', substitute('rRr', 'R', '\\', ''))
282 call assert_equal('scs', substitute('sSs', 'S', '\c', ''))
283 call assert_equal("u\nu", substitute('uUu', 'U', "\n", ''))
284 call assert_equal("v\<C-H>v", substitute('vVv', 'V', "\b", ''))
285 call assert_equal("w\\w", substitute('wWw', 'W', "\\", ''))
286 call assert_equal("x\<C-M>x", substitute('xXx', 'X', "\r", ''))
287 call assert_equal("YyyY", substitute('Y', 'Y', '\L\uyYy\l\EY', ''))
288 call assert_equal("zZZz", substitute('Z', 'Z', '\U\lZzZ\u\Ez', ''))
Bram Moolenaar004a6782020-04-11 17:09:31 +0200289 " \v or \V after $
290 call assert_equal('abxx', substitute('abcd', 'xy$\v|cd$', 'xx', ''))
291 call assert_equal('abxx', substitute('abcd', 'xy$\V\|cd\$', 'xx', ''))
Bram Moolenaar1a333bc2017-08-30 20:21:58 +0200292endfunc
293
294func Test_sub_replace_2()
295 " Run the tests with 'magic' off
296 set nomagic
297 set cpo&
298 call assert_equal('AA', substitute('A', 'A', '&&', ''))
299 call assert_equal('&', substitute('B', 'B', '\&', ''))
300 call assert_equal('C123456789987654321', substitute('C123456789', 'C\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)', '\0\9\8\7\6\5\4\3\2\1', ''))
301 call assert_equal('d', substitute('D', 'D', 'd', ''))
302 call assert_equal('~', substitute('E', 'E', '~', ''))
303 call assert_equal('~', substitute('F', 'F', '\~', ''))
304 call assert_equal('Gg', substitute('G', 'G', '\ugg', ''))
305 call assert_equal('Hh', substitute('H', 'H', '\Uh\Eh', ''))
306 call assert_equal('iI', substitute('I', 'I', '\lII', ''))
307 call assert_equal('jJ', substitute('J', 'J', '\LJ\EJ', ''))
308 call assert_equal('Kk', substitute('K', 'K', '\Uk\ek', ''))
309 call assert_equal("l\<C-V>\<C-M>l",
310 \ substitute('lLl', 'L', "\<C-V>\<C-M>", ''))
311 call assert_equal("m\<C-M>m", substitute('mMm', 'M', '\r', ''))
312 call assert_equal("n\<C-V>\<C-M>n",
313 \ substitute('nNn', 'N', "\\\<C-V>\<C-M>", ''))
314 call assert_equal("o\no", substitute('oOo', 'O', '\n', ''))
315 call assert_equal("p\<C-H>p", substitute('pPp', 'P', '\b', ''))
316 call assert_equal("q\tq", substitute('qQq', 'Q', '\t', ''))
317 call assert_equal('r\r', substitute('rRr', 'R', '\\', ''))
318 call assert_equal('scs', substitute('sSs', 'S', '\c', ''))
319 call assert_equal("t\<C-M>t", substitute('tTt', 'T', "\r", ''))
320 call assert_equal("u\nu", substitute('uUu', 'U', "\n", ''))
321 call assert_equal("v\<C-H>v", substitute('vVv', 'V', "\b", ''))
322 call assert_equal('w\w', substitute('wWw', 'W', "\\", ''))
323 call assert_equal('XxxX', substitute('X', 'X', '\L\uxXx\l\EX', ''))
324 call assert_equal('yYYy', substitute('Y', 'Y', '\U\lYyY\u\Ey', ''))
325endfunc
326
327func Test_sub_replace_3()
328 set magic&
329 set cpo&
330 call assert_equal('a\a', substitute('aAa', 'A', '\="\\"', ''))
331 call assert_equal('b\\b', substitute('bBb', 'B', '\="\\\\"', ''))
332 call assert_equal("c\rc", substitute('cCc', 'C', "\\=\"\r\"", ''))
333 call assert_equal("d\\\rd", substitute('dDd', 'D', "\\=\"\\\\\r\"", ''))
334 call assert_equal("e\\\\\re", substitute('eEe', 'E', "\\=\"\\\\\\\\\r\"", ''))
335 call assert_equal('f\rf', substitute('fFf', 'F', '\="\\r"', ''))
336 call assert_equal('j\nj', substitute('jJj', 'J', '\="\\n"', ''))
337 call assert_equal("k\<C-M>k", substitute('kKk', 'K', '\="\r"', ''))
338 call assert_equal("l\nl", substitute('lLl', 'L', '\="\n"', ''))
339endfunc
340
341" Test for submatch() on substitute().
342func Test_sub_replace_4()
343 set magic&
344 set cpo&
345 call assert_equal('a\a', substitute('aAa', 'A',
346 \ '\=substitute(submatch(0), ".", "\\", "")', ''))
347 call assert_equal('b\b', substitute('bBb', 'B',
348 \ '\=substitute(submatch(0), ".", "\\\\", "")', ''))
349 call assert_equal("c\<C-V>\<C-M>c", substitute('cCc', 'C', '\=substitute(submatch(0), ".", "\<C-V>\<C-M>", "")', ''))
350 call assert_equal("d\<C-V>\<C-M>d", substitute('dDd', 'D', '\=substitute(submatch(0), ".", "\\\<C-V>\<C-M>", "")', ''))
351 call assert_equal("e\\\<C-V>\<C-M>e", substitute('eEe', 'E', '\=substitute(submatch(0), ".", "\\\\\<C-V>\<C-M>", "")', ''))
352 call assert_equal("f\<C-M>f", substitute('fFf', 'F', '\=substitute(submatch(0), ".", "\\r", "")', ''))
353 call assert_equal("j\nj", substitute('jJj', 'J', '\=substitute(submatch(0), ".", "\\n", "")', ''))
354 call assert_equal("k\rk", substitute('kKk', 'K', '\=substitute(submatch(0), ".", "\r", "")', ''))
355 call assert_equal("l\nl", substitute('lLl', 'L', '\=substitute(submatch(0), ".", "\n", "")', ''))
356endfunc
357
358func Test_sub_replace_5()
359 set magic&
360 set cpo&
361 call assert_equal('A123456789987654321', substitute('A123456789',
362 \ 'A\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)',
363 \ '\=submatch(0) . submatch(9) . submatch(8) . ' .
364 \ 'submatch(7) . submatch(6) . submatch(5) . ' .
365 \ 'submatch(4) . submatch(3) . submatch(2) . submatch(1)',
366 \ ''))
367 call assert_equal("[['A123456789'], ['9'], ['8'], ['7'], ['6'], " .
368 \ "['5'], ['4'], ['3'], ['2'], ['1']]",
369 \ substitute('A123456789',
370 \ 'A\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)',
371 \ '\=string([submatch(0, 1), submatch(9, 1), ' .
Bram Moolenaarf6ed61e2019-09-07 19:05:09 +0200372 \ 'submatch(8, 1), 7->submatch(1), submatch(6, 1), ' .
Bram Moolenaar1a333bc2017-08-30 20:21:58 +0200373 \ 'submatch(5, 1), submatch(4, 1), submatch(3, 1), ' .
374 \ 'submatch(2, 1), submatch(1, 1)])',
375 \ ''))
376endfunc
377
378func Test_sub_replace_6()
379 set magic&
380 set cpo+=/
381 call assert_equal('a', substitute('A', 'A', 'a', ''))
382 call assert_equal('%', substitute('B', 'B', '%', ''))
383 set cpo-=/
384 call assert_equal('c', substitute('C', 'C', 'c', ''))
385 call assert_equal('%', substitute('D', 'D', '%', ''))
386endfunc
387
388func Test_sub_replace_7()
389 set magic&
390 set cpo&
391 call assert_equal('AA', substitute('AA', 'A.', '\=submatch(0)', ''))
392 call assert_equal("B\nB", substitute("B\nB", 'B.', '\=submatch(0)', ''))
393 call assert_equal("['B\n']B", substitute("B\nB", 'B.', '\=string(submatch(0, 1))', ''))
394 call assert_equal('-abab', substitute('-bb', '\zeb', 'a', 'g'))
395 call assert_equal('c-cbcbc', substitute('-bb', '\ze', 'c', 'g'))
396endfunc
397
398" Test for *:s%* on :substitute.
399func Test_sub_replace_8()
400 new
401 set magic&
402 set cpo&
403 $put =',,X'
404 s/\(^\|,\)\ze\(,\|X\)/\1N/g
405 call assert_equal('N,,NX', getline("$"))
406 $put =',,Y'
407 let cmd = ':s/\(^\|,\)\ze\(,\|Y\)/\1N/gc'
408 call feedkeys(cmd . "\<CR>a", "xt")
409 call assert_equal('N,,NY', getline("$"))
410 :$put =',,Z'
411 let cmd = ':s/\(^\|,\)\ze\(,\|Z\)/\1N/gc'
412 call feedkeys(cmd . "\<CR>yy", "xt")
413 call assert_equal('N,,NZ', getline("$"))
414 enew! | close
415endfunc
416
417func Test_sub_replace_9()
418 new
419 set magic&
420 set cpo&
421 $put ='xxx'
422 call feedkeys(":s/x/X/gc\<CR>yyq", "xt")
423 call assert_equal('XXx', getline("$"))
424 enew! | close
425endfunc
426
427func Test_sub_replace_10()
428 set magic&
429 set cpo&
430 call assert_equal('a1a2a3a', substitute('123', '\zs', 'a', 'g'))
431 call assert_equal('aaa', substitute('123', '\zs.', 'a', 'g'))
432 call assert_equal('1a2a3a', substitute('123', '.\zs', 'a', 'g'))
433 call assert_equal('a1a2a3a', substitute('123', '\ze', 'a', 'g'))
434 call assert_equal('a1a2a3', substitute('123', '\ze.', 'a', 'g'))
435 call assert_equal('aaa', substitute('123', '.\ze', 'a', 'g'))
436 call assert_equal('aa2a3a', substitute('123', '1\|\ze', 'a', 'g'))
437 call assert_equal('1aaa', substitute('123', '1\zs\|[23]', 'a', 'g'))
438endfunc
Bram Moolenaar15993ce2017-10-26 20:21:44 +0200439
Bram Moolenaarb0745b22019-11-09 22:28:11 +0100440func SubReplacer(text, submatches)
441 return a:text .. a:submatches[0] .. a:text
442endfunc
zeertzjq48db5da2022-09-16 12:10:03 +0100443func SubReplacerVar(text, ...)
444 return a:text .. a:1[0] .. a:text
445endfunc
zeertzjqabd58d82022-09-16 16:06:32 +0100446def SubReplacerVar9(text: string, ...args: list<list<string>>): string
447 return text .. args[0][0] .. text
448enddef
Bram Moolenaar4c054e92019-11-10 00:13:50 +0100449func SubReplacer20(t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12, t13, t14, t15, t16, t17, t18, t19, submatches)
450 return a:t3 .. a:submatches[0] .. a:t11
451endfunc
Bram Moolenaarb0745b22019-11-09 22:28:11 +0100452
453func Test_substitute_partial()
zeertzjq48db5da2022-09-16 12:10:03 +0100454 call assert_equal('1foo2foo3', substitute('123', '2', function('SubReplacer', ['foo']), 'g'))
455 call assert_equal('1foo2foo3', substitute('123', '2', function('SubReplacerVar', ['foo']), 'g'))
zeertzjqabd58d82022-09-16 16:06:32 +0100456 call assert_equal('1foo2foo3', substitute('123', '2', function('SubReplacerVar9', ['foo']), 'g'))
Bram Moolenaar4c054e92019-11-10 00:13:50 +0100457
zeertzjq48db5da2022-09-16 12:10:03 +0100458 " 19 arguments plus one is just OK
459 let Replacer = function('SubReplacer20', repeat(['foo'], 19))
460 call assert_equal('1foo2foo3', substitute('123', '2', Replacer, 'g'))
Bram Moolenaar4c054e92019-11-10 00:13:50 +0100461
zeertzjq48db5da2022-09-16 12:10:03 +0100462 " 20 arguments plus one is too many
463 let Replacer = function('SubReplacer20', repeat(['foo'], 20))
464 call assert_fails("call substitute('123', '2', Replacer, 'g')", 'E118:')
Bram Moolenaarb0745b22019-11-09 22:28:11 +0100465endfunc
466
Bram Moolenaar7a2217b2021-06-06 12:33:49 +0200467func Test_substitute_float()
Bram Moolenaar7a2217b2021-06-06 12:33:49 +0200468 call assert_equal('number 1.23', substitute('number ', '$', { -> 1.23 }, ''))
469 vim9 assert_equal('number 1.23', substitute('number ', '$', () => 1.23, ''))
470endfunc
471
Bram Moolenaar15993ce2017-10-26 20:21:44 +0200472" Tests for *sub-replace-special* and *sub-replace-expression* on :substitute.
473
474" Execute a list of :substitute command tests
475func Run_SubCmd_Tests(tests)
476 enew!
477 for t in a:tests
478 let start = line('.') + 1
479 let end = start + len(t[2]) - 1
480 exe "normal o" . t[0]
481 call cursor(start, 1)
482 exe t[1]
483 call assert_equal(t[2], getline(start, end), t[1])
484 endfor
485 enew!
486endfunc
487
488func Test_sub_cmd_1()
489 set magic
490 set cpo&
491
492 " List entry format: [input, cmd, output]
493 let tests = [['A', 's/A/&&/', ['AA']],
494 \ ['B', 's/B/\&/', ['&']],
495 \ ['C123456789', 's/C\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)/\0\9\8\7\6\5\4\3\2\1/', ['C123456789987654321']],
496 \ ['D', 's/D/d/', ['d']],
497 \ ['E', 's/E/~/', ['d']],
498 \ ['F', 's/F/\~/', ['~']],
499 \ ['G', 's/G/\ugg/', ['Gg']],
500 \ ['H', 's/H/\Uh\Eh/', ['Hh']],
501 \ ['I', 's/I/\lII/', ['iI']],
502 \ ['J', 's/J/\LJ\EJ/', ['jJ']],
503 \ ['K', 's/K/\Uk\ek/', ['Kk']],
504 \ ['lLl', "s/L/\<C-V>\<C-M>/", ["l\<C-V>", 'l']],
505 \ ['mMm', 's/M/\r/', ['m', 'm']],
506 \ ['nNn', "s/N/\\\<C-V>\<C-M>/", ["n\<C-V>", 'n']],
507 \ ['oOo', 's/O/\n/', ["o\no"]],
508 \ ['pPp', 's/P/\b/', ["p\<C-H>p"]],
509 \ ['qQq', 's/Q/\t/', ["q\tq"]],
510 \ ['rRr', 's/R/\\/', ['r\r']],
511 \ ['sSs', 's/S/\c/', ['scs']],
512 \ ['tTt', "s/T/\<C-V>\<C-J>/", ["t\<C-V>\<C-J>t"]],
513 \ ['U', 's/U/\L\uuUu\l\EU/', ['UuuU']],
zeertzjq3269efd2022-06-12 11:13:05 +0100514 \ ['V', 's/V/\U\lVvV\u\Ev/', ['vVVv']],
515 \ ['\', 's/\\/\\\\/', ['\\']]
Bram Moolenaar15993ce2017-10-26 20:21:44 +0200516 \ ]
517 call Run_SubCmd_Tests(tests)
518endfunc
519
520func Test_sub_cmd_2()
521 set nomagic
522 set cpo&
523
524 " List entry format: [input, cmd, output]
525 let tests = [['A', 's/A/&&/', ['&&']],
526 \ ['B', 's/B/\&/', ['B']],
527 \ ['C123456789', 's/\mC\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)/\0\9\8\7\6\5\4\3\2\1/', ['C123456789987654321']],
528 \ ['D', 's/D/d/', ['d']],
529 \ ['E', 's/E/~/', ['~']],
530 \ ['F', 's/F/\~/', ['~']],
531 \ ['G', 's/G/\ugg/', ['Gg']],
532 \ ['H', 's/H/\Uh\Eh/', ['Hh']],
533 \ ['I', 's/I/\lII/', ['iI']],
534 \ ['J', 's/J/\LJ\EJ/', ['jJ']],
535 \ ['K', 's/K/\Uk\ek/', ['Kk']],
536 \ ['lLl', "s/L/\<C-V>\<C-M>/", ["l\<C-V>", 'l']],
537 \ ['mMm', 's/M/\r/', ['m', 'm']],
538 \ ['nNn', "s/N/\\\<C-V>\<C-M>/", ["n\<C-V>", 'n']],
539 \ ['oOo', 's/O/\n/', ["o\no"]],
540 \ ['pPp', 's/P/\b/', ["p\<C-H>p"]],
541 \ ['qQq', 's/Q/\t/', ["q\tq"]],
542 \ ['rRr', 's/R/\\/', ['r\r']],
543 \ ['sSs', 's/S/\c/', ['scs']],
544 \ ['tTt', "s/T/\<C-V>\<C-J>/", ["t\<C-V>\<C-J>t"]],
545 \ ['U', 's/U/\L\uuUu\l\EU/', ['UuuU']],
zeertzjq3269efd2022-06-12 11:13:05 +0100546 \ ['V', 's/V/\U\lVvV\u\Ev/', ['vVVv']],
547 \ ['\', 's/\\/\\\\/', ['\\']]
Bram Moolenaar15993ce2017-10-26 20:21:44 +0200548 \ ]
549 call Run_SubCmd_Tests(tests)
550endfunc
551
552func Test_sub_cmd_3()
553 set nomagic
554 set cpo&
555
556 " List entry format: [input, cmd, output]
557 let tests = [['aAa', "s/A/\\='\\'/", ['a\a']],
558 \ ['bBb', "s/B/\\='\\\\'/", ['b\\b']],
559 \ ['cCc', "s/C/\\='\<C-V>\<C-M>'/", ["c\<C-V>", 'c']],
560 \ ['dDd', "s/D/\\='\\\<C-V>\<C-M>'/", ["d\\\<C-V>", 'd']],
561 \ ['eEe', "s/E/\\='\\\\\<C-V>\<C-M>'/", ["e\\\\\<C-V>", 'e']],
562 \ ['fFf', "s/F/\\='\r'/", ['f', 'f']],
563 \ ['gGg', "s/G/\\='\<C-V>\<C-J>'/", ["g\<C-V>", 'g']],
564 \ ['hHh', "s/H/\\='\\\<C-V>\<C-J>'/", ["h\\\<C-V>", 'h']],
565 \ ['iIi', "s/I/\\='\\\\\<C-V>\<C-J>'/", ["i\\\\\<C-V>", 'i']],
566 \ ['jJj', "s/J/\\='\n'/", ['j', 'j']],
567 \ ['kKk', 's/K/\="\r"/', ['k', 'k']],
568 \ ['lLl', 's/L/\="\n"/', ['l', 'l']]
569 \ ]
570 call Run_SubCmd_Tests(tests)
571endfunc
572
Bram Moolenaarf1699962019-08-31 17:48:19 +0200573" Test for submatch() on :substitute.
Bram Moolenaar15993ce2017-10-26 20:21:44 +0200574func Test_sub_cmd_4()
575 set magic&
576 set cpo&
577
578 " List entry format: [input, cmd, output]
579 let tests = [ ['aAa', "s/A/\\=substitute(submatch(0), '.', '\\', '')/",
Bram Moolenaar1e115362019-01-09 23:01:02 +0100580 \ ['a\a']],
Bram Moolenaar15993ce2017-10-26 20:21:44 +0200581 \ ['bBb', "s/B/\\=substitute(submatch(0), '.', '\\', '')/",
Bram Moolenaar1e115362019-01-09 23:01:02 +0100582 \ ['b\b']],
Bram Moolenaar15993ce2017-10-26 20:21:44 +0200583 \ ['cCc', "s/C/\\=substitute(submatch(0), '.', '\<C-V>\<C-M>', '')/",
584 \ ["c\<C-V>", 'c']],
585 \ ['dDd', "s/D/\\=substitute(submatch(0), '.', '\\\<C-V>\<C-M>', '')/",
586 \ ["d\<C-V>", 'd']],
587 \ ['eEe', "s/E/\\=substitute(submatch(0), '.', '\\\\\<C-V>\<C-M>', '')/",
588 \ ["e\\\<C-V>", 'e']],
589 \ ['fFf', "s/F/\\=substitute(submatch(0), '.', '\\r', '')/",
590 \ ['f', 'f']],
591 \ ['gGg', 's/G/\=substitute(submatch(0), ".", "\<C-V>\<C-J>", "")/',
592 \ ["g\<C-V>", 'g']],
593 \ ['hHh', 's/H/\=substitute(submatch(0), ".", "\\\<C-V>\<C-J>", "")/',
594 \ ["h\<C-V>", 'h']],
595 \ ['iIi', 's/I/\=substitute(submatch(0), ".", "\\\\\<C-V>\<C-J>", "")/',
596 \ ["i\\\<C-V>", 'i']],
597 \ ['jJj', "s/J/\\=substitute(submatch(0), '.', '\\n', '')/",
598 \ ['j', 'j']],
599 \ ['kKk', "s/K/\\=substitute(submatch(0), '.', '\\r', '')/",
600 \ ['k', 'k']],
601 \ ['lLl', "s/L/\\=substitute(submatch(0), '.', '\\n', '')/",
602 \ ['l', 'l']],
603 \ ]
604 call Run_SubCmd_Tests(tests)
605endfunc
606
607func Test_sub_cmd_5()
608 set magic&
609 set cpo&
610
611 " List entry format: [input, cmd, output]
612 let tests = [ ['A123456789', 's/A\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)/\=submatch(0) . submatch(9) . submatch(8) . submatch(7) . submatch(6) . submatch(5) . submatch(4) . submatch(3) . submatch(2) . submatch(1)/', ['A123456789987654321']],
613 \ ['B123456789', 's/B\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)/\=string([submatch(0, 1), submatch(9, 1), submatch(8, 1), submatch(7, 1), submatch(6, 1), submatch(5, 1), submatch(4, 1), submatch(3, 1), submatch(2, 1), submatch(1, 1)])/', ["[['B123456789'], ['9'], ['8'], ['7'], ['6'], ['5'], ['4'], ['3'], ['2'], ['1']]"]],
614 \ ]
615 call Run_SubCmd_Tests(tests)
616endfunc
617
618" Test for *:s%* on :substitute.
619func Test_sub_cmd_6()
620 set magic&
621 set cpo+=/
622
623 " List entry format: [input, cmd, output]
624 let tests = [ ['A', 's/A/a/', ['a']],
625 \ ['B', 's/B/%/', ['a']],
626 \ ]
627 call Run_SubCmd_Tests(tests)
628
629 set cpo-=/
630 let tests = [ ['C', 's/C/c/', ['c']],
631 \ ['D', 's/D/%/', ['%']],
632 \ ]
633 call Run_SubCmd_Tests(tests)
634
635 set cpo&
636endfunc
637
638" Test for :s replacing \n with line break.
639func Test_sub_cmd_7()
640 set magic&
641 set cpo&
642
643 " List entry format: [input, cmd, output]
644 let tests = [ ["A\<C-V>\<C-M>A", 's/A./\=submatch(0)/', ['A', 'A']],
645 \ ["B\<C-V>\<C-J>B", 's/B./\=submatch(0)/', ['B', 'B']],
646 \ ["C\<C-V>\<C-J>C", 's/C./\=strtrans(string(submatch(0, 1)))/', [strtrans("['C\<C-J>']C")]],
647 \ ["D\<C-V>\<C-J>\nD", 's/D.\nD/\=strtrans(string(submatch(0, 1)))/', [strtrans("['D\<C-J>', 'D']")]],
648 \ ["E\<C-V>\<C-J>\n\<C-V>\<C-J>\n\<C-V>\<C-J>\n\<C-V>\<C-J>\n\<C-V>\<C-J>E", 's/E\_.\{-}E/\=strtrans(string(submatch(0, 1)))/', [strtrans("['E\<C-J>', '\<C-J>', '\<C-J>', '\<C-J>', '\<C-J>E']")]],
649 \ ]
650 call Run_SubCmd_Tests(tests)
651
652 exe "normal oQ\nQ\<Esc>k"
Bram Moolenaare2e40752020-09-04 21:18:46 +0200653 call assert_fails('s/Q[^\n]Q/\=submatch(0)."foobar"/', 'E486:')
Bram Moolenaar15993ce2017-10-26 20:21:44 +0200654 enew!
655endfunc
656
657func TitleString()
658 let check = 'foo' =~ 'bar'
659 return ""
660endfunc
661
662func Test_sub_cmd_8()
663 set titlestring=%{TitleString()}
664
665 enew!
666 call append(0, ['', 'test_one', 'test_two'])
667 call cursor(1,1)
668 /^test_one/s/.*/\="foo\nbar"/
669 call assert_equal('foo', getline(2))
670 call assert_equal('bar', getline(3))
671 call feedkeys(':/^test_two/s/.*/\="foo\nbar"/c', "t")
672 call feedkeys("\<CR>y", "xt")
673 call assert_equal('foo', getline(4))
674 call assert_equal('bar', getline(5))
675
676 enew!
677 set titlestring&
678endfunc
Bram Moolenaar0e97b942019-03-27 22:53:53 +0100679
Bram Moolenaar80341bc2019-05-20 20:34:51 +0200680func Test_sub_cmd_9()
681 new
682 let input = ['1 aaa', '2 aaa', '3 aaa']
683 call setline(1, input)
684 func Foo()
685 return submatch(0)
686 endfunc
687 %s/aaa/\=Foo()/gn
688 call assert_equal(input, getline(1, '$'))
689 call assert_equal(1, &modifiable)
690
691 delfunc Foo
692 bw!
693endfunc
694
Bram Moolenaara04f4572022-09-13 13:45:26 +0100695func Test_sub_highlight_zero_match()
696 CheckRunVimInTerminal
697
698 let lines =<< trim END
699 call setline(1, ['one', 'two', 'three'])
700 END
701 call writefile(lines, 'XscriptSubHighlight', 'D')
702 let buf = RunVimInTerminal('-S XscriptSubHighlight', #{rows: 8, cols: 60})
703 call term_sendkeys(buf, ":%s/^/ /c\<CR>")
704 call VerifyScreenDump(buf, 'Test_sub_highlight_zer_match_1', {})
705
706 call term_sendkeys(buf, "\<Esc>")
707 call StopVimInTerminal(buf)
708endfunc
709
Bram Moolenaar0e97b942019-03-27 22:53:53 +0100710func Test_nocatch_sub_failure_handling()
Bram Moolenaar94722c52023-01-28 19:19:03 +0000711 " normal error results in all replacements
Bram Moolenaar80341bc2019-05-20 20:34:51 +0200712 func Foo()
Bram Moolenaar0e97b942019-03-27 22:53:53 +0100713 foobar
714 endfunc
715 new
716 call setline(1, ['1 aaa', '2 aaa', '3 aaa'])
zeertzjq3269efd2022-06-12 11:13:05 +0100717 " need silent! to avoid a delay when entering Insert mode
718 silent! %s/aaa/\=Foo()/g
Bram Moolenaar0e97b942019-03-27 22:53:53 +0100719 call assert_equal(['1 0', '2 0', '3 0'], getline(1, 3))
720
zeertzjq3269efd2022-06-12 11:13:05 +0100721 " Throw without try-catch causes abort after the first line.
Bram Moolenaar0e97b942019-03-27 22:53:53 +0100722 " We cannot test this, since it would stop executing the test script.
723
724 " try/catch does not result in any changes
725 func! Foo()
726 throw 'error'
727 endfunc
728 call setline(1, ['1 aaa', '2 aaa', '3 aaa'])
729 let error_caught = 0
730 try
731 %s/aaa/\=Foo()/g
732 catch
733 let error_caught = 1
734 endtry
735 call assert_equal(1, error_caught)
736 call assert_equal(['1 aaa', '2 aaa', '3 aaa'], getline(1, 3))
737
Bram Moolenaar6349e942019-05-18 13:41:22 +0200738 " Same, but using "n" flag so that "sandbox" gets set
739 call setline(1, ['1 aaa', '2 aaa', '3 aaa'])
740 let error_caught = 0
741 try
742 %s/aaa/\=Foo()/gn
743 catch
744 let error_caught = 1
745 endtry
746 call assert_equal(1, error_caught)
747 call assert_equal(['1 aaa', '2 aaa', '3 aaa'], getline(1, 3))
748
Bram Moolenaar80341bc2019-05-20 20:34:51 +0200749 delfunc Foo
Bram Moolenaar0e97b942019-03-27 22:53:53 +0100750 bwipe!
751endfunc
Bram Moolenaarc6b37db2019-04-27 18:00:34 +0200752
753" Test ":s/pat/sub/" with different ~s in sub.
754func Test_replace_with_tilde()
755 new
756 " Set the last replace string to empty
757 s/^$//
758 call append(0, ['- Bug in "vPPPP" on this text:'])
759 normal gg
760 s/u/~u~/
761 call assert_equal('- Bug in "vPPPP" on this text:', getline(1))
762 s/i/~u~/
763 call assert_equal('- Bug uuun "vPPPP" on this text:', getline(1))
764 s/o/~~~/
765 call assert_equal('- Bug uuun "vPPPP" uuuuuuuuun this text:', getline(1))
766 close!
767endfunc
768
769func Test_replace_keeppatterns()
770 new
771 a
772foobar
773
774substitute foo asdf
775
776one two
777.
778
779 normal gg
780 /^substitute
781 s/foo/bar/
782 call assert_equal('foo', @/)
783 call assert_equal('substitute bar asdf', getline('.'))
784
785 /^substitute
786 keeppatterns s/asdf/xyz/
787 call assert_equal('^substitute', @/)
788 call assert_equal('substitute bar xyz', getline('.'))
789
790 exe "normal /bar /e\<CR>"
791 call assert_equal(15, col('.'))
792 normal -
793 keeppatterns /xyz
794 call assert_equal('bar ', @/)
795 call assert_equal('substitute bar xyz', getline('.'))
796 exe "normal 0dn"
797 call assert_equal('xyz', getline('.'))
798
799 close!
800endfunc
Bram Moolenaarbb265962019-10-31 04:38:36 +0100801
802func Test_sub_beyond_end()
803 new
804 call setline(1, '#')
805 let @/ = '^#\n\zs'
806 s///e
807 call assert_equal('#', getline(1))
808 bwipe!
809endfunc
Bram Moolenaar5d98dc22020-01-29 21:57:34 +0100810
Bram Moolenaarea3db912020-02-02 15:32:13 +0100811" Test for repeating last substitution using :~ and :&r
812func Test_repeat_last_sub()
813 new
814 call setline(1, ['blue green yellow orange white'])
815 s/blue/red/
816 let @/ = 'yellow'
817 ~
818 let @/ = 'white'
819 :&r
820 let @/ = 'green'
821 s//gray
822 call assert_equal('red gray red orange red', getline(1))
823 close!
824endfunc
825
826" Test for Vi compatible substitution:
827" \/{string}/, \?{string}? and \&{string}&
828func Test_sub_vi_compatibility()
829 new
830 call setline(1, ['blue green yellow orange blue'])
831 let @/ = 'orange'
832 s\/white/
833 let @/ = 'blue'
834 s\?amber?
835 let @/ = 'white'
836 s\&green&
837 call assert_equal('amber green yellow white green', getline(1))
838 close!
Bram Moolenaar9fb7b422022-03-05 21:13:26 +0000839
840 call assert_fails('vim9cmd s\/white/', 'E1270:')
841 call assert_fails('vim9cmd s\?white?', 'E1270:')
842 call assert_fails('vim9cmd s\&white&', 'E1270:')
Bram Moolenaarea3db912020-02-02 15:32:13 +0100843endfunc
844
845" Test for substitute with the new text longer than the original text
846func Test_sub_expand_text()
847 new
848 call setline(1, 'abcabcabcabcabcabcabcabc')
849 s/b/\=repeat('B', 10)/g
850 call assert_equal(repeat('aBBBBBBBBBBc', 8), getline(1))
851 close!
852endfunc
853
Bram Moolenaar07ada5f2020-02-05 20:38:22 +0100854" Test for command failures when the last substitute pattern is not set.
855func Test_sub_with_no_last_pat()
Bram Moolenaar9f6277b2020-02-11 22:04:02 +0100856 let lines =<< trim [SCRIPT]
857 call assert_fails('~', 'E33:')
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200858 call assert_fails('s//abc/g', 'E35:')
859 call assert_fails('s\/bar', 'E35:')
860 call assert_fails('s\&bar&', 'E33:')
Bram Moolenaar9f6277b2020-02-11 22:04:02 +0100861 call writefile(v:errors, 'Xresult')
862 qall!
863 [SCRIPT]
Bram Moolenaar56564962022-10-10 22:39:42 +0100864 call writefile(lines, 'Xscript', 'D')
Bram Moolenaar9f6277b2020-02-11 22:04:02 +0100865 if RunVim([], [], '--clean -S Xscript')
866 call assert_equal([], readfile('Xresult'))
867 endif
Bram Moolenaar07ada5f2020-02-05 20:38:22 +0100868
Bram Moolenaar9f6277b2020-02-11 22:04:02 +0100869 let lines =<< trim [SCRIPT]
870 set cpo+=/
871 call assert_fails('s/abc/%/', 'E33:')
872 call writefile(v:errors, 'Xresult')
873 qall!
874 [SCRIPT]
875 call writefile(lines, 'Xscript')
876 if RunVim([], [], '--clean -S Xscript')
877 call assert_equal([], readfile('Xresult'))
878 endif
879
Bram Moolenaar9f6277b2020-02-11 22:04:02 +0100880 call delete('Xresult')
Bram Moolenaar07ada5f2020-02-05 20:38:22 +0100881endfunc
882
Bram Moolenaarca68ae12020-03-30 19:32:53 +0200883func Test_substitute()
884 call assert_equal('a1a2a3a', substitute('123', '\zs', 'a', 'g'))
Bram Moolenaar004a6782020-04-11 17:09:31 +0200885 " Substitute with special keys
886 call assert_equal("a\<End>c", substitute('abc', "a.c", "a\<End>c", ''))
887endfunc
888
889func Test_substitute_expr()
890 let g:val = 'XXX'
891 call assert_equal('XXX', substitute('yyy', 'y*', '\=g:val', ''))
892 call assert_equal('XXX', substitute('yyy', 'y*', {-> g:val}, ''))
893 call assert_equal("-\u1b \uf2-", substitute("-%1b %f2-", '%\(\x\x\)',
894 \ '\=nr2char("0x" . submatch(1))', 'g'))
895 call assert_equal("-\u1b \uf2-", substitute("-%1b %f2-", '%\(\x\x\)',
896 \ {-> nr2char("0x" . submatch(1))}, 'g'))
897
898 call assert_equal('231', substitute('123', '\(.\)\(.\)\(.\)',
899 \ {-> submatch(2) . submatch(3) . submatch(1)}, ''))
900
901 func Recurse()
902 return substitute('yyy', 'y\(.\)y', {-> submatch(1)}, '')
903 endfunc
904 " recursive call works
905 call assert_equal('-y-x-', substitute('xxx', 'x\(.\)x', {-> '-' . Recurse() . '-' . submatch(1) . '-'}, ''))
906
907 call assert_fails("let s=submatch([])", 'E745:')
908 call assert_fails("let s=submatch(2, [])", 'E745:')
909endfunc
910
911func Test_invalid_submatch()
912 " This was causing invalid memory access in Vim-7.4.2232 and older
913 call assert_fails("call substitute('x', '.', {-> submatch(10)}, '')", 'E935:')
914 call assert_fails('eval submatch(-1)', 'E935:')
915 call assert_equal('', submatch(0))
916 call assert_equal('', submatch(1))
917 call assert_equal([], submatch(0, 1))
918 call assert_equal([], submatch(1, 1))
919endfunc
920
Bram Moolenaar8a0dcf42020-09-06 15:14:45 +0200921func Test_submatch_list_concatenate()
922 let pat = 'A\(.\)'
923 let Rep = {-> string([submatch(0, 1)] + [[submatch(1)]])}
924 call substitute('A1', pat, Rep, '')->assert_equal("[['A1'], ['1']]")
925endfunc
926
Bram Moolenaar004a6782020-04-11 17:09:31 +0200927func Test_substitute_expr_arg()
928 call assert_equal('123456789-123456789=', substitute('123456789',
929 \ '\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)',
930 \ {m -> m[0] . '-' . m[1] . m[2] . m[3] . m[4] . m[5] . m[6] . m[7] . m[8] . m[9] . '='}, ''))
931
932 call assert_equal('123456-123456=789', substitute('123456789',
933 \ '\(.\)\(.\)\(.\)\(a*\)\(n*\)\(.\)\(.\)\(.\)\(x*\)',
934 \ {m -> m[0] . '-' . m[1] . m[2] . m[3] . m[4] . m[5] . m[6] . m[7] . m[8] . m[9] . '='}, ''))
935
936 call assert_equal('123456789-123456789x=', substitute('123456789',
937 \ '\(.\)\(.\)\(.*\)',
938 \ {m -> m[0] . '-' . m[1] . m[2] . m[3] . 'x' . m[4] . m[5] . m[6] . m[7] . m[8] . m[9] . '='}, ''))
939
940 call assert_fails("call substitute('xxx', '.', {m -> string(add(m, 'x'))}, '')", 'E742:')
941 call assert_fails("call substitute('xxx', '.', {m -> string(insert(m, 'x'))}, '')", 'E742:')
942 call assert_fails("call substitute('xxx', '.', {m -> string(extend(m, ['x']))}, '')", 'E742:')
943 call assert_fails("call substitute('xxx', '.', {m -> string(remove(m, 1))}, '')", 'E742:')
944endfunc
945
946" Test for using a function to supply the substitute string
947func Test_substitute_using_func()
948 func Xfunc()
949 return '1234'
950 endfunc
951 call assert_equal('a1234f', substitute('abcdef', 'b..e',
952 \ function("Xfunc"), ''))
953 delfunc Xfunc
954endfunc
955
956" Test for using submatch() with a multiline match
957func Test_substitute_multiline_submatch()
958 new
959 call setline(1, ['line1', 'line2', 'line3', 'line4'])
960 %s/^line1\(\_.\+\)line4$/\=submatch(1)/
961 call assert_equal(['', 'line2', 'line3', ''], getline(1, '$'))
962 close!
Bram Moolenaarca68ae12020-03-30 19:32:53 +0200963endfunc
964
Bram Moolenaardf365142021-05-03 20:01:45 +0200965func Test_substitute_skipped_range()
966 new
967 if 0
968 /1/5/2/2/\n
969 endif
970 call assert_equal([0, 1, 1, 0, 1], getcurpos())
971 bwipe!
972endfunc
973
Dominique Pellebfb2bb12021-08-14 21:11:51 +0200974" Test using the 'gdefault' option (when on, flag 'g' is default on).
975func Test_substitute_gdefault()
976 new
977
978 " First check without 'gdefault'
979 call setline(1, 'foo bar foo')
980 s/foo/FOO/
981 call assert_equal('FOO bar foo', getline(1))
982 call setline(1, 'foo bar foo')
983 s/foo/FOO/g
984 call assert_equal('FOO bar FOO', getline(1))
985 call setline(1, 'foo bar foo')
986 s/foo/FOO/gg
987 call assert_equal('FOO bar foo', getline(1))
988
989 " Then check with 'gdefault'
990 set gdefault
991 call setline(1, 'foo bar foo')
992 s/foo/FOO/
993 call assert_equal('FOO bar FOO', getline(1))
994 call setline(1, 'foo bar foo')
995 s/foo/FOO/g
996 call assert_equal('FOO bar foo', getline(1))
997 call setline(1, 'foo bar foo')
998 s/foo/FOO/gg
999 call assert_equal('FOO bar FOO', getline(1))
1000
1001 " Setting 'compatible' should reset 'gdefault'
1002 call assert_equal(1, &gdefault)
1003 set compatible
1004 call assert_equal(0, &gdefault)
1005 set nocompatible
1006 call assert_equal(0, &gdefault)
1007
1008 bw!
1009endfunc
1010
Bram Moolenaar37f47952022-01-29 14:21:51 +00001011" This was using "old_sub" after it was freed.
1012func Test_using_old_sub()
1013 set compatible maxfuncdepth=10
1014 new
1015 call setline(1, 'some text.')
1016 func Repl()
1017 ~
1018 s/
1019 endfunc
Bram Moolenaar44ddf192022-06-21 22:15:25 +01001020 silent! s/\%')/\=Repl()
Bram Moolenaar37f47952022-01-29 14:21:51 +00001021
1022 delfunc Repl
1023 bwipe!
1024 set nocompatible
1025endfunc
1026
Bram Moolenaare2bd8602022-05-18 13:11:57 +01001027" This was switching windows in between computing the length and using it.
1028func Test_sub_change_window()
1029 silent! lfile
1030 sil! norm o0000000000000000000000000000000000000000000000000000
1031 func Repl()
1032 lopen
1033 endfunc
1034 silent! s/\%')/\=Repl()
1035 bwipe!
1036 bwipe!
1037 delfunc Repl
1038endfunc
1039
Bram Moolenaar338f1fc2022-05-26 15:56:23 +01001040" This was undoign a change in between computing the length and using it.
1041func Do_Test_sub_undo_change()
1042 new
1043 norm o0000000000000000000000000000000000000000000000000000
1044 silent! s/\%')/\=Repl()
1045 bwipe!
1046endfunc
1047
1048func Test_sub_undo_change()
1049 func Repl()
1050 silent! norm g-
1051 endfunc
1052 call Do_Test_sub_undo_change()
1053
1054 func! Repl()
1055 silent earlier
1056 endfunc
1057 call Do_Test_sub_undo_change()
1058
1059 delfunc Repl
1060endfunc
1061
Bram Moolenaar71223e22022-05-30 15:23:09 +01001062" This was opening a command line window from the expression
1063func Test_sub_open_cmdline_win()
1064 " the error only happens in a very specific setup, run a new Vim instance to
1065 " get a clean starting point.
1066 let lines =<< trim [SCRIPT]
Bram Moolenaarbe990422022-05-30 16:01:42 +01001067 set vb t_vb=
Bram Moolenaar71223e22022-05-30 15:23:09 +01001068 norm o0000000000000000000000000000000000000000000000000000
1069 func Replace()
1070 norm q/
1071 endfunc
1072 s/\%')/\=Replace()
1073 redir >Xresult
1074 messages
1075 redir END
1076 qall!
1077 [SCRIPT]
Bram Moolenaar56564962022-10-10 22:39:42 +01001078 call writefile(lines, 'Xscript', 'D')
Bram Moolenaar71223e22022-05-30 15:23:09 +01001079 if RunVim([], [], '-u NONE -S Xscript')
Bram Moolenaarbe990422022-05-30 16:01:42 +01001080 call assert_match('E565: Not allowed to change text or change window',
1081 \ readfile('Xresult')->join('XX'))
Bram Moolenaar71223e22022-05-30 15:23:09 +01001082 endif
1083
Bram Moolenaar71223e22022-05-30 15:23:09 +01001084 call delete('Xresult')
1085endfunc
1086
Bram Moolenaard6211a52022-06-18 19:48:14 +01001087" This was editing a script file from the expression
1088func Test_sub_edit_scriptfile()
1089 new
1090 norm o0000000000000000000000000000000000000000000000000000
1091 func EditScript()
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001092 silent! scr! Xsedfile
Bram Moolenaard6211a52022-06-18 19:48:14 +01001093 endfunc
1094 s/\%')/\=EditScript()
1095
1096 delfunc EditScript
1097 bwipe!
1098endfunc
1099
Bram Moolenaarcc762a42022-11-25 13:03:31 +00001100" This was editing another file from the expression.
1101func Test_sub_expr_goto_other_file()
1102 call writefile([''], 'Xfileone', 'D')
1103 enew!
1104 call setline(1, ['a', 'b', 'c', 'd',
1105 \ 'Xfileone zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz'])
1106
1107 func g:SplitGotoFile()
1108 exe "sil! norm 0\<C-W>gf"
1109 return ''
1110 endfunc
1111
1112 $
1113 s/\%')/\=g:SplitGotoFile()
1114
1115 delfunc g:SplitGotoFile
1116 bwipe!
1117endfunc
1118
Bram Moolenaar3ac1d972023-01-04 17:17:54 +00001119func Test_recursive_expr_substitute()
1120 " this was reading invalid memory
1121 let lines =<< trim END
1122 func Repl(g, n)
1123 s
1124 r%:s000
1125 endfunc
1126 next 0
1127 let caught = 0
1128 s/\%')/\=Repl(0, 0)
1129 qall!
1130 END
1131 call writefile(lines, 'XexprSubst', 'D')
1132 call RunVim([], [], '--clean -S XexprSubst')
1133endfunc
1134
Yegappan Lakshmanan5e877ba2022-03-25 21:19:26 +00001135" Test for the 2-letter and 3-letter :substitute commands
1136func Test_substitute_short_cmd()
1137 new
1138 call setline(1, ['one', 'one one one'])
1139 s/one/two
1140 call cursor(2, 1)
1141
1142 " :sc
1143 call feedkeys(":sc\<CR>y", 'xt')
1144 call assert_equal('two one one', getline(2))
1145
1146 " :scg
1147 call setline(2, 'one one one')
1148 call feedkeys(":scg\<CR>nyq", 'xt')
1149 call assert_equal('one two one', getline(2))
1150
1151 " :sci
1152 call setline(2, 'ONE One onE')
1153 call feedkeys(":sci\<CR>y", 'xt')
1154 call assert_equal('two One onE', getline(2))
1155
1156 " :scI
1157 set ignorecase
1158 call setline(2, 'ONE One one')
1159 call feedkeys(":scI\<CR>y", 'xt')
1160 call assert_equal('ONE One two', getline(2))
1161 set ignorecase&
1162
1163 " :scn
1164 call setline(2, 'one one one')
1165 let t = execute('scn')->split("\n")
1166 call assert_equal(['1 match on 1 line'], t)
1167 call assert_equal('one one one', getline(2))
1168
1169 " :scp
1170 call setline(2, "\tone one one")
1171 redir => output
1172 call feedkeys(":scp\<CR>y", 'xt')
1173 redir END
1174 call assert_equal(' two one one', output->split("\n")[-1])
1175 call assert_equal("\ttwo one one", getline(2))
1176
1177 " :scl
1178 call setline(2, "\tone one one")
1179 redir => output
1180 call feedkeys(":scl\<CR>y", 'xt')
1181 redir END
1182 call assert_equal("^Itwo one one$", output->split("\n")[-1])
1183 call assert_equal("\ttwo one one", getline(2))
1184
1185 " :sgc
1186 call setline(2, 'one one one one one')
1187 call feedkeys(":sgc\<CR>nyyq", 'xt')
1188 call assert_equal('one two two one one', getline(2))
1189
1190 " :sg
1191 call setline(2, 'one one one')
1192 sg
1193 call assert_equal('two two two', getline(2))
1194
1195 " :sgi
1196 call setline(2, 'ONE One onE')
1197 sgi
1198 call assert_equal('two two two', getline(2))
1199
1200 " :sgI
1201 set ignorecase
1202 call setline(2, 'ONE One one')
1203 sgI
1204 call assert_equal('ONE One two', getline(2))
1205 set ignorecase&
1206
1207 " :sgn
1208 call setline(2, 'one one one')
1209 let t = execute('sgn')->split("\n")
1210 call assert_equal(['3 matches on 1 line'], t)
1211 call assert_equal('one one one', getline(2))
1212
1213 " :sgp
1214 call setline(2, "\tone one one")
1215 redir => output
1216 sgp
1217 redir END
1218 call assert_equal(' two two two', output->split("\n")[-1])
1219 call assert_equal("\ttwo two two", getline(2))
1220
1221 " :sgl
1222 call setline(2, "\tone one one")
1223 redir => output
1224 sgl
1225 redir END
1226 call assert_equal("^Itwo two two$", output->split("\n")[-1])
1227 call assert_equal("\ttwo two two", getline(2))
1228
1229 " :sgr
1230 call setline(2, "one one one")
1231 call cursor(2, 1)
1232 s/abc/xyz/e
1233 let @/ = 'one'
1234 sgr
1235 call assert_equal('xyz xyz xyz', getline(2))
1236
1237 " :sic
1238 call cursor(1, 1)
1239 s/one/two/e
1240 call setline(2, "ONE One one")
1241 call cursor(2, 1)
1242 call feedkeys(":sic\<CR>y", 'xt')
1243 call assert_equal('two One one', getline(2))
1244
1245 " :si
1246 call setline(2, "ONE One one")
1247 si
1248 call assert_equal('two One one', getline(2))
1249
1250 " :siI
1251 call setline(2, "ONE One one")
1252 siI
1253 call assert_equal('ONE One two', getline(2))
1254
1255 " :sin
1256 call setline(2, 'ONE One onE')
1257 let t = execute('sin')->split("\n")
1258 call assert_equal(['1 match on 1 line'], t)
1259 call assert_equal('ONE One onE', getline(2))
1260
1261 " :sip
1262 call setline(2, "\tONE One onE")
1263 redir => output
1264 sip
1265 redir END
1266 call assert_equal(' two One onE', output->split("\n")[-1])
1267 call assert_equal("\ttwo One onE", getline(2))
1268
1269 " :sir
1270 call setline(2, "ONE One onE")
1271 call cursor(2, 1)
1272 s/abc/xyz/e
1273 let @/ = 'one'
1274 sir
1275 call assert_equal('xyz One onE', getline(2))
1276
1277 " :sIc
1278 call cursor(1, 1)
1279 s/one/two/e
1280 call setline(2, "ONE One one")
1281 call cursor(2, 1)
1282 call feedkeys(":sIc\<CR>y", 'xt')
1283 call assert_equal('ONE One two', getline(2))
1284
1285 " :sIg
1286 call setline(2, "ONE one onE one")
1287 sIg
1288 call assert_equal('ONE two onE two', getline(2))
1289
1290 " :sIi
1291 call setline(2, "ONE One one")
1292 sIi
1293 call assert_equal('two One one', getline(2))
1294
1295 " :sI
1296 call setline(2, "ONE One one")
1297 sI
1298 call assert_equal('ONE One two', getline(2))
1299
1300 " :sIn
1301 call setline(2, 'ONE One one')
1302 let t = execute('sIn')->split("\n")
1303 call assert_equal(['1 match on 1 line'], t)
1304 call assert_equal('ONE One one', getline(2))
1305
1306 " :sIp
1307 call setline(2, "\tONE One one")
1308 redir => output
1309 sIp
1310 redir END
1311 call assert_equal(' ONE One two', output->split("\n")[-1])
1312 call assert_equal("\tONE One two", getline(2))
1313
1314 " :sIl
1315 call setline(2, "\tONE onE one")
1316 redir => output
1317 sIl
1318 redir END
1319 call assert_equal("^IONE onE two$", output->split("\n")[-1])
1320 call assert_equal("\tONE onE two", getline(2))
1321
1322 " :sIr
1323 call setline(2, "ONE one onE")
1324 call cursor(2, 1)
1325 s/abc/xyz/e
1326 let @/ = 'one'
1327 sIr
1328 call assert_equal('ONE xyz onE', getline(2))
1329
1330 " :src
1331 call setline(2, "ONE one one")
1332 call cursor(2, 1)
1333 s/abc/xyz/e
1334 let @/ = 'one'
1335 call feedkeys(":src\<CR>y", 'xt')
1336 call assert_equal('ONE xyz one', getline(2))
1337
1338 " :srg
1339 call setline(2, "one one one")
1340 call cursor(2, 1)
1341 s/abc/xyz/e
1342 let @/ = 'one'
1343 srg
1344 call assert_equal('xyz xyz xyz', getline(2))
1345
1346 " :sri
1347 call setline(2, "ONE one onE")
1348 call cursor(2, 1)
1349 s/abc/xyz/e
1350 let @/ = 'one'
1351 sri
1352 call assert_equal('xyz one onE', getline(2))
1353
1354 " :srI
1355 call setline(2, "ONE one onE")
1356 call cursor(2, 1)
1357 s/abc/xyz/e
1358 let @/ = 'one'
1359 srI
1360 call assert_equal('ONE xyz onE', getline(2))
1361
1362 " :srn
1363 call setline(2, "ONE one onE")
1364 call cursor(2, 1)
1365 s/abc/xyz/e
1366 let @/ = 'one'
1367 let t = execute('srn')->split("\n")
1368 call assert_equal(['1 match on 1 line'], t)
1369 call assert_equal('ONE one onE', getline(2))
1370
1371 " :srp
1372 call setline(2, "\tONE one onE")
1373 call cursor(2, 1)
1374 s/abc/xyz/e
1375 let @/ = 'one'
1376 redir => output
1377 srp
1378 redir END
1379 call assert_equal(' ONE xyz onE', output->split("\n")[-1])
1380 call assert_equal("\tONE xyz onE", getline(2))
1381
1382 " :srl
1383 call setline(2, "\tONE one onE")
1384 call cursor(2, 1)
1385 s/abc/xyz/e
1386 let @/ = 'one'
1387 redir => output
1388 srl
1389 redir END
1390 call assert_equal("^IONE xyz onE$", output->split("\n")[-1])
1391 call assert_equal("\tONE xyz onE", getline(2))
1392
1393 " :sr
1394 call setline(2, "ONE one onE")
1395 call cursor(2, 1)
1396 s/abc/xyz/e
1397 let @/ = 'one'
1398 sr
1399 call assert_equal('ONE xyz onE', getline(2))
1400
1401 " :sce
1402 s/abc/xyz/e
1403 call assert_fails("sc", 'E486:')
1404 sce
1405 " :sge
1406 call assert_fails("sg", 'E486:')
1407 sge
1408 " :sie
1409 call assert_fails("si", 'E486:')
1410 sie
1411 " :sIe
1412 call assert_fails("sI", 'E486:')
1413 sIe
1414
1415 bw!
1416endfunc
Bram Moolenaar37f47952022-01-29 14:21:51 +00001417
Bram Moolenaarab9a2d82023-05-09 21:15:30 +01001418" Check handling expanding "~" resulting in extremely long text.
Bram Moolenaar916d6dd2023-05-09 21:45:47 +01001419" FIXME: disabled, it takes too long to run on CI
Bram Moolenaara4467c42023-05-09 22:07:11 +01001420"func Test_substitute_tilde_too_long()
1421" enew!
1422"
1423" s/.*/ixxx
1424" s//~~~~~~~~~AAAAAAA@(
1425"
1426" " Either fails with "out of memory" or "text too long".
1427" " This can take a long time.
1428" call assert_fails('sil! norm &&&&&&&&&', ['E1240:\|E342:'])
1429"
1430" bwipe!
1431"endfunc
Bram Moolenaarab9a2d82023-05-09 21:15:30 +01001432
Bram Moolenaar44ddf192022-06-21 22:15:25 +01001433" This should be done last to reveal a memory leak when vim_regsub_both() is
1434" called to evaluate an expression but it is not used in a second call.
1435func Test_z_substitute_expr_leak()
1436 func SubExpr()
1437 ~n
1438 endfunc
1439 silent! s/\%')/\=SubExpr()
1440 delfunc SubExpr
1441endfunc
1442
Christian Brabandt18d27092023-09-06 19:53:36 +02001443func Test_substitute_expr_switch_win()
1444 func R()
1445 wincmd x
1446 return 'XXXX'
1447 endfunc
1448 new Xfoobar
1449 let bufnr = bufnr('%')
1450 put ="abcdef"
1451 silent! s/\%')/\=R()
Christian Brabandtee17b6f2023-09-09 11:23:50 +02001452 call assert_fails(':%s/./\=R()/g', 'E565:')
Christian Brabandt18d27092023-09-06 19:53:36 +02001453 delfunc R
1454 exe bufnr .. "bw!"
1455endfunc
1456
Bram Moolenaar5d98dc22020-01-29 21:57:34 +01001457" vim: shiftwidth=2 sts=2 expandtab