blob: 2ac6f45419b5df728a4007823efdf7f8e1d249ad [file] [log] [blame]
Bram Moolenaarcd055da2016-09-02 19:50:48 +02001" Tests for multi-line regexps with ":s".
2
Bram Moolenaar9f6277b2020-02-11 22:04:02 +01003source shared.vim
4
Bram Moolenaar1e115362019-01-09 23:01:02 +01005func Test_multiline_subst()
Bram Moolenaarcd055da2016-09-02 19:50:48 +02006 enew!
7 call append(0, ["1 aa",
8 \ "bb",
9 \ "cc",
10 \ "2 dd",
11 \ "ee",
12 \ "3 ef",
13 \ "gh",
14 \ "4 ij",
15 \ "5 a8",
16 \ "8b c9",
17 \ "9d",
18 \ "6 e7",
19 \ "77f",
20 \ "xxxxx"])
21
22 1
23 " test if replacing a line break works with a back reference
24 /^1/,/^2/s/\n\(.\)/ \1/
25 " test if inserting a line break works with a back reference
26 /^3/,/^4/s/\(.\)$/\r\1/
27 " test if replacing a line break with another line break works
28 /^5/,/^6/s/\(\_d\{3}\)/x\1x/
29 call assert_equal('1 aa bb cc 2 dd ee', getline(1))
30 call assert_equal('3 e', getline(2))
31 call assert_equal('f', getline(3))
32 call assert_equal('g', getline(4))
33 call assert_equal('h', getline(5))
34 call assert_equal('4 i', getline(6))
35 call assert_equal('j', getline(7))
36 call assert_equal('5 ax8', getline(8))
37 call assert_equal('8xb cx9', getline(9))
38 call assert_equal('9xd', getline(10))
39 call assert_equal('6 ex7', getline(11))
40 call assert_equal('7x7f', getline(12))
41 call assert_equal('xxxxx', getline(13))
42 enew!
Bram Moolenaar1e115362019-01-09 23:01:02 +010043endfunc
Bram Moolenaar8c50d502017-02-17 18:28:24 +010044
Bram Moolenaar1e115362019-01-09 23:01:02 +010045func Test_substitute_variants()
Bram Moolenaar8c50d502017-02-17 18:28:24 +010046 " Validate that all the 2-/3-letter variants which embed the flags into the
47 " command name actually work.
48 enew!
49 let ln = 'Testing string'
50 let variants = [
51 \ { 'cmd': ':s/Test/test/c', 'exp': 'testing string', 'prompt': 'y' },
52 \ { 'cmd': ':s/foo/bar/ce', 'exp': ln },
53 \ { 'cmd': ':s/t/r/cg', 'exp': 'Tesring srring', 'prompt': 'a' },
54 \ { 'cmd': ':s/t/r/ci', 'exp': 'resting string', 'prompt': 'y' },
55 \ { 'cmd': ':s/t/r/cI', 'exp': 'Tesring string', 'prompt': 'y' },
Bram Moolenaarea3db912020-02-02 15:32:13 +010056 \ { 'cmd': ':s/t/r/c', 'exp': 'Testing string', 'prompt': 'n' },
Bram Moolenaar8c50d502017-02-17 18:28:24 +010057 \ { 'cmd': ':s/t/r/cn', 'exp': ln },
58 \ { 'cmd': ':s/t/r/cp', 'exp': 'Tesring string', 'prompt': 'y' },
59 \ { 'cmd': ':s/t/r/cl', 'exp': 'Tesring string', 'prompt': 'y' },
60 \ { 'cmd': ':s/t/r/gc', 'exp': 'Tesring srring', 'prompt': 'a' },
Bram Moolenaarea3db912020-02-02 15:32:13 +010061 \ { 'cmd': ':s/i/I/gc', 'exp': 'TestIng string', 'prompt': 'l' },
Bram Moolenaar8c50d502017-02-17 18:28:24 +010062 \ { 'cmd': ':s/foo/bar/ge', 'exp': ln },
63 \ { 'cmd': ':s/t/r/g', 'exp': 'Tesring srring' },
64 \ { 'cmd': ':s/t/r/gi', 'exp': 'resring srring' },
65 \ { 'cmd': ':s/t/r/gI', 'exp': 'Tesring srring' },
66 \ { 'cmd': ':s/t/r/gn', 'exp': ln },
67 \ { 'cmd': ':s/t/r/gp', 'exp': 'Tesring srring' },
68 \ { 'cmd': ':s/t/r/gl', 'exp': 'Tesring srring' },
69 \ { 'cmd': ':s//r/gr', 'exp': 'Testr strr' },
70 \ { 'cmd': ':s/t/r/ic', 'exp': 'resting string', 'prompt': 'y' },
71 \ { 'cmd': ':s/foo/bar/ie', 'exp': ln },
72 \ { 'cmd': ':s/t/r/i', 'exp': 'resting string' },
73 \ { 'cmd': ':s/t/r/iI', 'exp': 'Tesring string' },
74 \ { 'cmd': ':s/t/r/in', 'exp': ln },
75 \ { 'cmd': ':s/t/r/ip', 'exp': 'resting string' },
76 \ { 'cmd': ':s//r/ir', 'exp': 'Testr string' },
77 \ { 'cmd': ':s/t/r/Ic', 'exp': 'Tesring string', 'prompt': 'y' },
78 \ { 'cmd': ':s/foo/bar/Ie', 'exp': ln },
79 \ { 'cmd': ':s/t/r/Ig', 'exp': 'Tesring srring' },
80 \ { 'cmd': ':s/t/r/Ii', 'exp': 'resting string' },
81 \ { 'cmd': ':s/t/r/I', 'exp': 'Tesring string' },
82 \ { 'cmd': ':s/t/r/Ip', 'exp': 'Tesring string' },
83 \ { 'cmd': ':s/t/r/Il', 'exp': 'Tesring string' },
84 \ { 'cmd': ':s//r/Ir', 'exp': 'Testr string' },
85 \ { 'cmd': ':s//r/rc', 'exp': 'Testr string', 'prompt': 'y' },
86 \ { 'cmd': ':s//r/rg', 'exp': 'Testr strr' },
87 \ { 'cmd': ':s//r/ri', 'exp': 'Testr string' },
88 \ { 'cmd': ':s//r/rI', 'exp': 'Testr string' },
89 \ { 'cmd': ':s//r/rn', 'exp': 'Testing string' },
90 \ { 'cmd': ':s//r/rp', 'exp': 'Testr string' },
91 \ { 'cmd': ':s//r/rl', 'exp': 'Testr string' },
92 \ { 'cmd': ':s//r/r', 'exp': 'Testr string' },
Bram Moolenaarea3db912020-02-02 15:32:13 +010093 \ { 'cmd': ':s/i/I/gc', 'exp': 'Testing string', 'prompt': 'q' },
Bram Moolenaar8c50d502017-02-17 18:28:24 +010094 \]
95
96 for var in variants
97 for run in [1, 2]
98 let cmd = var.cmd
99 if run == 2 && cmd =~ "/.*/.*/."
100 " Change :s/from/to/{flags} to :s{flags}
101 let cmd = substitute(cmd, '/.*/', '', '')
102 endif
103 call setline(1, [ln])
104 let msg = printf('using "%s"', cmd)
105 let @/='ing'
106 let v:errmsg = ''
107 call feedkeys(cmd . "\<CR>" . get(var, 'prompt', ''), 'ntx')
108 " No error should exist (matters for testing e flag)
109 call assert_equal('', v:errmsg, msg)
110 call assert_equal(var.exp, getline('.'), msg)
111 endfor
112 endfor
Bram Moolenaar1e115362019-01-09 23:01:02 +0100113endfunc
Bram Moolenaarba748c82017-02-26 14:00:07 +0100114
Bram Moolenaar94747162019-02-10 21:55:26 +0100115" Test the l, p, # flags.
116func Test_substitute_flags_lp()
117 new
118 call setline(1, "abc\tdef\<C-h>ghi")
119
120 let a = execute('s/a/a/p')
121 call assert_equal("\nabc def^Hghi", a)
122
123 let a = execute('s/a/a/l')
124 call assert_equal("\nabc^Idef^Hghi$", a)
125
126 let a = execute('s/a/a/#')
127 call assert_equal("\n 1 abc def^Hghi", a)
128
129 let a = execute('s/a/a/p#')
130 call assert_equal("\n 1 abc def^Hghi", a)
131
132 let a = execute('s/a/a/l#')
133 call assert_equal("\n 1 abc^Idef^Hghi$", a)
134
135 let a = execute('s/a/a/')
136 call assert_equal("", a)
137
138 bwipe!
139endfunc
140
Bram Moolenaarba748c82017-02-26 14:00:07 +0100141func Test_substitute_repeat()
142 " This caused an invalid memory access.
143 split Xfile
144 s/^/x
145 call feedkeys("Qsc\<CR>y", 'tx')
146 bwipe!
147endfunc
Bram Moolenaar1a333bc2017-08-30 20:21:58 +0200148
Bram Moolenaard77aa4d2019-02-10 22:50:14 +0100149" Test %s/\n// which is implemented as a special case to use a
150" more efficient join rather than doing a regular substitution.
151func Test_substitute_join()
152 new
153
154 call setline(1, ["foo\tbar", "bar\<C-H>foo"])
155 let a = execute('%s/\n//')
156 call assert_equal("", a)
157 call assert_equal(["foo\tbarbar\<C-H>foo"], getline(1, '$'))
158 call assert_equal('\n', histget("search", -1))
159
160 call setline(1, ["foo\tbar", "bar\<C-H>foo"])
161 let a = execute('%s/\n//g')
162 call assert_equal("", a)
163 call assert_equal(["foo\tbarbar\<C-H>foo"], getline(1, '$'))
164 call assert_equal('\n', histget("search", -1))
165
166 call setline(1, ["foo\tbar", "bar\<C-H>foo"])
167 let a = execute('%s/\n//p')
168 call assert_equal("\nfoo barbar^Hfoo", a)
169 call assert_equal(["foo\tbarbar\<C-H>foo"], getline(1, '$'))
170 call assert_equal('\n', histget("search", -1))
171
172 call setline(1, ["foo\tbar", "bar\<C-H>foo"])
173 let a = execute('%s/\n//l')
174 call assert_equal("\nfoo^Ibarbar^Hfoo$", a)
175 call assert_equal(["foo\tbarbar\<C-H>foo"], getline(1, '$'))
176 call assert_equal('\n', histget("search", -1))
177
178 call setline(1, ["foo\tbar", "bar\<C-H>foo"])
179 let a = execute('%s/\n//#')
180 call assert_equal("\n 1 foo barbar^Hfoo", a)
181 call assert_equal(["foo\tbarbar\<C-H>foo"], getline(1, '$'))
182 call assert_equal('\n', histget("search", -1))
183
Bram Moolenaarea3db912020-02-02 15:32:13 +0100184 call setline(1, ['foo', 'bar', 'baz', 'qux'])
185 call execute('1,2s/\n//')
186 call assert_equal(['foobarbaz', 'qux'], getline(1, '$'))
187
Bram Moolenaard77aa4d2019-02-10 22:50:14 +0100188 bwipe!
189endfunc
190
191func Test_substitute_count()
192 new
193 call setline(1, ['foo foo', 'foo foo', 'foo foo', 'foo foo', 'foo foo'])
194 2
195
196 s/foo/bar/3
197 call assert_equal(['foo foo', 'bar foo', 'bar foo', 'bar foo', 'foo foo'],
198 \ getline(1, '$'))
199
200 call assert_fails('s/foo/bar/0', 'E939:')
201
Bram Moolenaarea3db912020-02-02 15:32:13 +0100202 call setline(1, ['foo foo', 'foo foo', 'foo foo', 'foo foo', 'foo foo'])
203 2,4s/foo/bar/ 10
204 call assert_equal(['foo foo', 'foo foo', 'foo foo', 'bar foo', 'bar foo'],
205 \ getline(1, '$'))
206
Bram Moolenaard77aa4d2019-02-10 22:50:14 +0100207 bwipe!
208endfunc
209
210" Test substitute 'n' flag (report number of matches, do not substitute).
211func Test_substitute_flag_n()
212 new
213 let lines = ['foo foo', 'foo foo', 'foo foo', 'foo foo', 'foo foo']
214 call setline(1, lines)
215
216 call assert_equal("\n3 matches on 3 lines", execute('2,4s/foo/bar/n'))
217 call assert_equal("\n6 matches on 3 lines", execute('2,4s/foo/bar/gn'))
218
219 " c flag (confirm) should be ignored when using n flag.
220 call assert_equal("\n3 matches on 3 lines", execute('2,4s/foo/bar/nc'))
221
222 " No substitution should have been done.
223 call assert_equal(lines, getline(1, '$'))
224
Bram Moolenaarea3db912020-02-02 15:32:13 +0100225 %delete _
226 call setline(1, ['A', 'Bar', 'Baz'])
227 call assert_equal("\n1 match on 1 line", execute('s/\nB\@=//gn'))
228
Bram Moolenaard77aa4d2019-02-10 22:50:14 +0100229 bwipe!
230endfunc
231
232func Test_substitute_errors()
233 new
234 call setline(1, 'foobar')
235
236 call assert_fails('s/FOO/bar/', 'E486:')
237 call assert_fails('s/foo/bar/@', 'E488:')
238 call assert_fails('s/\(/bar/', 'E476:')
Bram Moolenaar5d98dc22020-01-29 21:57:34 +0100239 call assert_fails('s afooabara', 'E146:')
240 call assert_fails('s\\a', 'E10:')
Bram Moolenaard77aa4d2019-02-10 22:50:14 +0100241
242 setl nomodifiable
243 call assert_fails('s/foo/bar/', 'E21:')
244
245 bwipe!
246endfunc
247
Bram Moolenaar1a333bc2017-08-30 20:21:58 +0200248" Test for *sub-replace-special* and *sub-replace-expression* on substitute().
249func Test_sub_replace_1()
250 " Run the tests with 'magic' on
251 set magic
252 set cpo&
253 call assert_equal('AA', substitute('A', 'A', '&&', ''))
254 call assert_equal('&', substitute('B', 'B', '\&', ''))
255 call assert_equal('C123456789987654321', substitute('C123456789', 'C\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)', '\0\9\8\7\6\5\4\3\2\1', ''))
256 call assert_equal('d', substitute('D', 'D', 'd', ''))
257 call assert_equal('~', substitute('E', 'E', '~', ''))
258 call assert_equal('~', substitute('F', 'F', '\~', ''))
259 call assert_equal('Gg', substitute('G', 'G', '\ugg', ''))
260 call assert_equal('Hh', substitute('H', 'H', '\Uh\Eh', ''))
261 call assert_equal('iI', substitute('I', 'I', '\lII', ''))
262 call assert_equal('jJ', substitute('J', 'J', '\LJ\EJ', ''))
263 call assert_equal('Kk', substitute('K', 'K', '\Uk\ek', ''))
264 call assert_equal("l\<C-V>\<C-M>l",
265 \ substitute('lLl', 'L', "\<C-V>\<C-M>", ''))
266 call assert_equal("m\<C-M>m", substitute('mMm', 'M', '\r', ''))
267 call assert_equal("n\<C-V>\<C-M>n",
268 \ substitute('nNn', 'N', "\\\<C-V>\<C-M>", ''))
269 call assert_equal("o\no", substitute('oOo', 'O', '\n', ''))
270 call assert_equal("p\<C-H>p", substitute('pPp', 'P', '\b', ''))
271 call assert_equal("q\tq", substitute('qQq', 'Q', '\t', ''))
272 call assert_equal('r\r', substitute('rRr', 'R', '\\', ''))
273 call assert_equal('scs', substitute('sSs', 'S', '\c', ''))
274 call assert_equal("u\nu", substitute('uUu', 'U', "\n", ''))
275 call assert_equal("v\<C-H>v", substitute('vVv', 'V', "\b", ''))
276 call assert_equal("w\\w", substitute('wWw', 'W', "\\", ''))
277 call assert_equal("x\<C-M>x", substitute('xXx', 'X', "\r", ''))
278 call assert_equal("YyyY", substitute('Y', 'Y', '\L\uyYy\l\EY', ''))
279 call assert_equal("zZZz", substitute('Z', 'Z', '\U\lZzZ\u\Ez', ''))
280endfunc
281
282func Test_sub_replace_2()
283 " Run the tests with 'magic' off
284 set nomagic
285 set cpo&
286 call assert_equal('AA', substitute('A', 'A', '&&', ''))
287 call assert_equal('&', substitute('B', 'B', '\&', ''))
288 call assert_equal('C123456789987654321', substitute('C123456789', 'C\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)', '\0\9\8\7\6\5\4\3\2\1', ''))
289 call assert_equal('d', substitute('D', 'D', 'd', ''))
290 call assert_equal('~', substitute('E', 'E', '~', ''))
291 call assert_equal('~', substitute('F', 'F', '\~', ''))
292 call assert_equal('Gg', substitute('G', 'G', '\ugg', ''))
293 call assert_equal('Hh', substitute('H', 'H', '\Uh\Eh', ''))
294 call assert_equal('iI', substitute('I', 'I', '\lII', ''))
295 call assert_equal('jJ', substitute('J', 'J', '\LJ\EJ', ''))
296 call assert_equal('Kk', substitute('K', 'K', '\Uk\ek', ''))
297 call assert_equal("l\<C-V>\<C-M>l",
298 \ substitute('lLl', 'L', "\<C-V>\<C-M>", ''))
299 call assert_equal("m\<C-M>m", substitute('mMm', 'M', '\r', ''))
300 call assert_equal("n\<C-V>\<C-M>n",
301 \ substitute('nNn', 'N', "\\\<C-V>\<C-M>", ''))
302 call assert_equal("o\no", substitute('oOo', 'O', '\n', ''))
303 call assert_equal("p\<C-H>p", substitute('pPp', 'P', '\b', ''))
304 call assert_equal("q\tq", substitute('qQq', 'Q', '\t', ''))
305 call assert_equal('r\r', substitute('rRr', 'R', '\\', ''))
306 call assert_equal('scs', substitute('sSs', 'S', '\c', ''))
307 call assert_equal("t\<C-M>t", substitute('tTt', 'T', "\r", ''))
308 call assert_equal("u\nu", substitute('uUu', 'U', "\n", ''))
309 call assert_equal("v\<C-H>v", substitute('vVv', 'V', "\b", ''))
310 call assert_equal('w\w', substitute('wWw', 'W', "\\", ''))
311 call assert_equal('XxxX', substitute('X', 'X', '\L\uxXx\l\EX', ''))
312 call assert_equal('yYYy', substitute('Y', 'Y', '\U\lYyY\u\Ey', ''))
313endfunc
314
315func Test_sub_replace_3()
316 set magic&
317 set cpo&
318 call assert_equal('a\a', substitute('aAa', 'A', '\="\\"', ''))
319 call assert_equal('b\\b', substitute('bBb', 'B', '\="\\\\"', ''))
320 call assert_equal("c\rc", substitute('cCc', 'C', "\\=\"\r\"", ''))
321 call assert_equal("d\\\rd", substitute('dDd', 'D', "\\=\"\\\\\r\"", ''))
322 call assert_equal("e\\\\\re", substitute('eEe', 'E', "\\=\"\\\\\\\\\r\"", ''))
323 call assert_equal('f\rf', substitute('fFf', 'F', '\="\\r"', ''))
324 call assert_equal('j\nj', substitute('jJj', 'J', '\="\\n"', ''))
325 call assert_equal("k\<C-M>k", substitute('kKk', 'K', '\="\r"', ''))
326 call assert_equal("l\nl", substitute('lLl', 'L', '\="\n"', ''))
327endfunc
328
329" Test for submatch() on substitute().
330func Test_sub_replace_4()
331 set magic&
332 set cpo&
333 call assert_equal('a\a', substitute('aAa', 'A',
334 \ '\=substitute(submatch(0), ".", "\\", "")', ''))
335 call assert_equal('b\b', substitute('bBb', 'B',
336 \ '\=substitute(submatch(0), ".", "\\\\", "")', ''))
337 call assert_equal("c\<C-V>\<C-M>c", substitute('cCc', 'C', '\=substitute(submatch(0), ".", "\<C-V>\<C-M>", "")', ''))
338 call assert_equal("d\<C-V>\<C-M>d", substitute('dDd', 'D', '\=substitute(submatch(0), ".", "\\\<C-V>\<C-M>", "")', ''))
339 call assert_equal("e\\\<C-V>\<C-M>e", substitute('eEe', 'E', '\=substitute(submatch(0), ".", "\\\\\<C-V>\<C-M>", "")', ''))
340 call assert_equal("f\<C-M>f", substitute('fFf', 'F', '\=substitute(submatch(0), ".", "\\r", "")', ''))
341 call assert_equal("j\nj", substitute('jJj', 'J', '\=substitute(submatch(0), ".", "\\n", "")', ''))
342 call assert_equal("k\rk", substitute('kKk', 'K', '\=substitute(submatch(0), ".", "\r", "")', ''))
343 call assert_equal("l\nl", substitute('lLl', 'L', '\=substitute(submatch(0), ".", "\n", "")', ''))
344endfunc
345
346func Test_sub_replace_5()
347 set magic&
348 set cpo&
349 call assert_equal('A123456789987654321', substitute('A123456789',
350 \ 'A\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)',
351 \ '\=submatch(0) . submatch(9) . submatch(8) . ' .
352 \ 'submatch(7) . submatch(6) . submatch(5) . ' .
353 \ 'submatch(4) . submatch(3) . submatch(2) . submatch(1)',
354 \ ''))
355 call assert_equal("[['A123456789'], ['9'], ['8'], ['7'], ['6'], " .
356 \ "['5'], ['4'], ['3'], ['2'], ['1']]",
357 \ substitute('A123456789',
358 \ 'A\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)',
359 \ '\=string([submatch(0, 1), submatch(9, 1), ' .
Bram Moolenaarf6ed61e2019-09-07 19:05:09 +0200360 \ 'submatch(8, 1), 7->submatch(1), submatch(6, 1), ' .
Bram Moolenaar1a333bc2017-08-30 20:21:58 +0200361 \ 'submatch(5, 1), submatch(4, 1), submatch(3, 1), ' .
362 \ 'submatch(2, 1), submatch(1, 1)])',
363 \ ''))
364endfunc
365
366func Test_sub_replace_6()
367 set magic&
368 set cpo+=/
369 call assert_equal('a', substitute('A', 'A', 'a', ''))
370 call assert_equal('%', substitute('B', 'B', '%', ''))
371 set cpo-=/
372 call assert_equal('c', substitute('C', 'C', 'c', ''))
373 call assert_equal('%', substitute('D', 'D', '%', ''))
374endfunc
375
376func Test_sub_replace_7()
377 set magic&
378 set cpo&
379 call assert_equal('AA', substitute('AA', 'A.', '\=submatch(0)', ''))
380 call assert_equal("B\nB", substitute("B\nB", 'B.', '\=submatch(0)', ''))
381 call assert_equal("['B\n']B", substitute("B\nB", 'B.', '\=string(submatch(0, 1))', ''))
382 call assert_equal('-abab', substitute('-bb', '\zeb', 'a', 'g'))
383 call assert_equal('c-cbcbc', substitute('-bb', '\ze', 'c', 'g'))
384endfunc
385
386" Test for *:s%* on :substitute.
387func Test_sub_replace_8()
388 new
389 set magic&
390 set cpo&
391 $put =',,X'
392 s/\(^\|,\)\ze\(,\|X\)/\1N/g
393 call assert_equal('N,,NX', getline("$"))
394 $put =',,Y'
395 let cmd = ':s/\(^\|,\)\ze\(,\|Y\)/\1N/gc'
396 call feedkeys(cmd . "\<CR>a", "xt")
397 call assert_equal('N,,NY', getline("$"))
398 :$put =',,Z'
399 let cmd = ':s/\(^\|,\)\ze\(,\|Z\)/\1N/gc'
400 call feedkeys(cmd . "\<CR>yy", "xt")
401 call assert_equal('N,,NZ', getline("$"))
402 enew! | close
403endfunc
404
405func Test_sub_replace_9()
406 new
407 set magic&
408 set cpo&
409 $put ='xxx'
410 call feedkeys(":s/x/X/gc\<CR>yyq", "xt")
411 call assert_equal('XXx', getline("$"))
412 enew! | close
413endfunc
414
415func Test_sub_replace_10()
416 set magic&
417 set cpo&
418 call assert_equal('a1a2a3a', substitute('123', '\zs', 'a', 'g'))
419 call assert_equal('aaa', substitute('123', '\zs.', 'a', 'g'))
420 call assert_equal('1a2a3a', substitute('123', '.\zs', 'a', 'g'))
421 call assert_equal('a1a2a3a', substitute('123', '\ze', 'a', 'g'))
422 call assert_equal('a1a2a3', substitute('123', '\ze.', 'a', 'g'))
423 call assert_equal('aaa', substitute('123', '.\ze', 'a', 'g'))
424 call assert_equal('aa2a3a', substitute('123', '1\|\ze', 'a', 'g'))
425 call assert_equal('1aaa', substitute('123', '1\zs\|[23]', 'a', 'g'))
426endfunc
Bram Moolenaar15993ce2017-10-26 20:21:44 +0200427
Bram Moolenaarb0745b22019-11-09 22:28:11 +0100428func SubReplacer(text, submatches)
429 return a:text .. a:submatches[0] .. a:text
430endfunc
Bram Moolenaar4c054e92019-11-10 00:13:50 +0100431func SubReplacer20(t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12, t13, t14, t15, t16, t17, t18, t19, submatches)
432 return a:t3 .. a:submatches[0] .. a:t11
433endfunc
Bram Moolenaarb0745b22019-11-09 22:28:11 +0100434
435func Test_substitute_partial()
436 call assert_equal('1foo2foo3', substitute('123', '2', function('SubReplacer', ['foo']), 'g'))
Bram Moolenaar4c054e92019-11-10 00:13:50 +0100437
438 " 19 arguments plus one is just OK
439 let Replacer = function('SubReplacer20', repeat(['foo'], 19))
440 call assert_equal('1foo2foo3', substitute('123', '2', Replacer, 'g'))
441
442 " 20 arguments plus one is too many
443 let Replacer = function('SubReplacer20', repeat(['foo'], 20))
444 call assert_fails("call substitute('123', '2', Replacer, 'g')", 'E118')
Bram Moolenaarb0745b22019-11-09 22:28:11 +0100445endfunc
446
Bram Moolenaar15993ce2017-10-26 20:21:44 +0200447" Tests for *sub-replace-special* and *sub-replace-expression* on :substitute.
448
449" Execute a list of :substitute command tests
450func Run_SubCmd_Tests(tests)
451 enew!
452 for t in a:tests
453 let start = line('.') + 1
454 let end = start + len(t[2]) - 1
Bram Moolenaarbb265962019-10-31 04:38:36 +0100455 " TODO: why is there a one second delay the first time we get here?
Bram Moolenaar15993ce2017-10-26 20:21:44 +0200456 exe "normal o" . t[0]
457 call cursor(start, 1)
458 exe t[1]
459 call assert_equal(t[2], getline(start, end), t[1])
460 endfor
461 enew!
462endfunc
463
464func Test_sub_cmd_1()
465 set magic
466 set cpo&
467
468 " List entry format: [input, cmd, output]
469 let tests = [['A', 's/A/&&/', ['AA']],
470 \ ['B', 's/B/\&/', ['&']],
471 \ ['C123456789', 's/C\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)/\0\9\8\7\6\5\4\3\2\1/', ['C123456789987654321']],
472 \ ['D', 's/D/d/', ['d']],
473 \ ['E', 's/E/~/', ['d']],
474 \ ['F', 's/F/\~/', ['~']],
475 \ ['G', 's/G/\ugg/', ['Gg']],
476 \ ['H', 's/H/\Uh\Eh/', ['Hh']],
477 \ ['I', 's/I/\lII/', ['iI']],
478 \ ['J', 's/J/\LJ\EJ/', ['jJ']],
479 \ ['K', 's/K/\Uk\ek/', ['Kk']],
480 \ ['lLl', "s/L/\<C-V>\<C-M>/", ["l\<C-V>", 'l']],
481 \ ['mMm', 's/M/\r/', ['m', 'm']],
482 \ ['nNn', "s/N/\\\<C-V>\<C-M>/", ["n\<C-V>", 'n']],
483 \ ['oOo', 's/O/\n/', ["o\no"]],
484 \ ['pPp', 's/P/\b/', ["p\<C-H>p"]],
485 \ ['qQq', 's/Q/\t/', ["q\tq"]],
486 \ ['rRr', 's/R/\\/', ['r\r']],
487 \ ['sSs', 's/S/\c/', ['scs']],
488 \ ['tTt', "s/T/\<C-V>\<C-J>/", ["t\<C-V>\<C-J>t"]],
489 \ ['U', 's/U/\L\uuUu\l\EU/', ['UuuU']],
490 \ ['V', 's/V/\U\lVvV\u\Ev/', ['vVVv']]
491 \ ]
492 call Run_SubCmd_Tests(tests)
493endfunc
494
495func Test_sub_cmd_2()
496 set nomagic
497 set cpo&
498
499 " List entry format: [input, cmd, output]
500 let tests = [['A', 's/A/&&/', ['&&']],
501 \ ['B', 's/B/\&/', ['B']],
502 \ ['C123456789', 's/\mC\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)/\0\9\8\7\6\5\4\3\2\1/', ['C123456789987654321']],
503 \ ['D', 's/D/d/', ['d']],
504 \ ['E', 's/E/~/', ['~']],
505 \ ['F', 's/F/\~/', ['~']],
506 \ ['G', 's/G/\ugg/', ['Gg']],
507 \ ['H', 's/H/\Uh\Eh/', ['Hh']],
508 \ ['I', 's/I/\lII/', ['iI']],
509 \ ['J', 's/J/\LJ\EJ/', ['jJ']],
510 \ ['K', 's/K/\Uk\ek/', ['Kk']],
511 \ ['lLl', "s/L/\<C-V>\<C-M>/", ["l\<C-V>", 'l']],
512 \ ['mMm', 's/M/\r/', ['m', 'm']],
513 \ ['nNn', "s/N/\\\<C-V>\<C-M>/", ["n\<C-V>", 'n']],
514 \ ['oOo', 's/O/\n/', ["o\no"]],
515 \ ['pPp', 's/P/\b/', ["p\<C-H>p"]],
516 \ ['qQq', 's/Q/\t/', ["q\tq"]],
517 \ ['rRr', 's/R/\\/', ['r\r']],
518 \ ['sSs', 's/S/\c/', ['scs']],
519 \ ['tTt', "s/T/\<C-V>\<C-J>/", ["t\<C-V>\<C-J>t"]],
520 \ ['U', 's/U/\L\uuUu\l\EU/', ['UuuU']],
521 \ ['V', 's/V/\U\lVvV\u\Ev/', ['vVVv']]
522 \ ]
523 call Run_SubCmd_Tests(tests)
524endfunc
525
526func Test_sub_cmd_3()
527 set nomagic
528 set cpo&
529
530 " List entry format: [input, cmd, output]
531 let tests = [['aAa', "s/A/\\='\\'/", ['a\a']],
532 \ ['bBb', "s/B/\\='\\\\'/", ['b\\b']],
533 \ ['cCc', "s/C/\\='\<C-V>\<C-M>'/", ["c\<C-V>", 'c']],
534 \ ['dDd', "s/D/\\='\\\<C-V>\<C-M>'/", ["d\\\<C-V>", 'd']],
535 \ ['eEe', "s/E/\\='\\\\\<C-V>\<C-M>'/", ["e\\\\\<C-V>", 'e']],
536 \ ['fFf', "s/F/\\='\r'/", ['f', 'f']],
537 \ ['gGg', "s/G/\\='\<C-V>\<C-J>'/", ["g\<C-V>", 'g']],
538 \ ['hHh', "s/H/\\='\\\<C-V>\<C-J>'/", ["h\\\<C-V>", 'h']],
539 \ ['iIi', "s/I/\\='\\\\\<C-V>\<C-J>'/", ["i\\\\\<C-V>", 'i']],
540 \ ['jJj', "s/J/\\='\n'/", ['j', 'j']],
541 \ ['kKk', 's/K/\="\r"/', ['k', 'k']],
542 \ ['lLl', 's/L/\="\n"/', ['l', 'l']]
543 \ ]
544 call Run_SubCmd_Tests(tests)
545endfunc
546
Bram Moolenaarf1699962019-08-31 17:48:19 +0200547" Test for submatch() on :substitute.
Bram Moolenaar15993ce2017-10-26 20:21:44 +0200548func Test_sub_cmd_4()
549 set magic&
550 set cpo&
551
552 " List entry format: [input, cmd, output]
553 let tests = [ ['aAa', "s/A/\\=substitute(submatch(0), '.', '\\', '')/",
Bram Moolenaar1e115362019-01-09 23:01:02 +0100554 \ ['a\a']],
Bram Moolenaar15993ce2017-10-26 20:21:44 +0200555 \ ['bBb', "s/B/\\=substitute(submatch(0), '.', '\\', '')/",
Bram Moolenaar1e115362019-01-09 23:01:02 +0100556 \ ['b\b']],
Bram Moolenaar15993ce2017-10-26 20:21:44 +0200557 \ ['cCc', "s/C/\\=substitute(submatch(0), '.', '\<C-V>\<C-M>', '')/",
558 \ ["c\<C-V>", 'c']],
559 \ ['dDd', "s/D/\\=substitute(submatch(0), '.', '\\\<C-V>\<C-M>', '')/",
560 \ ["d\<C-V>", 'd']],
561 \ ['eEe', "s/E/\\=substitute(submatch(0), '.', '\\\\\<C-V>\<C-M>', '')/",
562 \ ["e\\\<C-V>", 'e']],
563 \ ['fFf', "s/F/\\=substitute(submatch(0), '.', '\\r', '')/",
564 \ ['f', 'f']],
565 \ ['gGg', 's/G/\=substitute(submatch(0), ".", "\<C-V>\<C-J>", "")/',
566 \ ["g\<C-V>", 'g']],
567 \ ['hHh', 's/H/\=substitute(submatch(0), ".", "\\\<C-V>\<C-J>", "")/',
568 \ ["h\<C-V>", 'h']],
569 \ ['iIi', 's/I/\=substitute(submatch(0), ".", "\\\\\<C-V>\<C-J>", "")/',
570 \ ["i\\\<C-V>", 'i']],
571 \ ['jJj', "s/J/\\=substitute(submatch(0), '.', '\\n', '')/",
572 \ ['j', 'j']],
573 \ ['kKk', "s/K/\\=substitute(submatch(0), '.', '\\r', '')/",
574 \ ['k', 'k']],
575 \ ['lLl', "s/L/\\=substitute(submatch(0), '.', '\\n', '')/",
576 \ ['l', 'l']],
577 \ ]
578 call Run_SubCmd_Tests(tests)
579endfunc
580
581func Test_sub_cmd_5()
582 set magic&
583 set cpo&
584
585 " List entry format: [input, cmd, output]
586 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']],
587 \ ['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']]"]],
588 \ ]
589 call Run_SubCmd_Tests(tests)
590endfunc
591
592" Test for *:s%* on :substitute.
593func Test_sub_cmd_6()
594 set magic&
595 set cpo+=/
596
597 " List entry format: [input, cmd, output]
598 let tests = [ ['A', 's/A/a/', ['a']],
599 \ ['B', 's/B/%/', ['a']],
600 \ ]
601 call Run_SubCmd_Tests(tests)
602
603 set cpo-=/
604 let tests = [ ['C', 's/C/c/', ['c']],
605 \ ['D', 's/D/%/', ['%']],
606 \ ]
607 call Run_SubCmd_Tests(tests)
608
609 set cpo&
610endfunc
611
612" Test for :s replacing \n with line break.
613func Test_sub_cmd_7()
614 set magic&
615 set cpo&
616
617 " List entry format: [input, cmd, output]
618 let tests = [ ["A\<C-V>\<C-M>A", 's/A./\=submatch(0)/', ['A', 'A']],
619 \ ["B\<C-V>\<C-J>B", 's/B./\=submatch(0)/', ['B', 'B']],
620 \ ["C\<C-V>\<C-J>C", 's/C./\=strtrans(string(submatch(0, 1)))/', [strtrans("['C\<C-J>']C")]],
621 \ ["D\<C-V>\<C-J>\nD", 's/D.\nD/\=strtrans(string(submatch(0, 1)))/', [strtrans("['D\<C-J>', 'D']")]],
622 \ ["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']")]],
623 \ ]
624 call Run_SubCmd_Tests(tests)
625
626 exe "normal oQ\nQ\<Esc>k"
627 call assert_fails('s/Q[^\n]Q/\=submatch(0)."foobar"/', 'E486')
628 enew!
629endfunc
630
631func TitleString()
632 let check = 'foo' =~ 'bar'
633 return ""
634endfunc
635
636func Test_sub_cmd_8()
637 set titlestring=%{TitleString()}
638
639 enew!
640 call append(0, ['', 'test_one', 'test_two'])
641 call cursor(1,1)
642 /^test_one/s/.*/\="foo\nbar"/
643 call assert_equal('foo', getline(2))
644 call assert_equal('bar', getline(3))
645 call feedkeys(':/^test_two/s/.*/\="foo\nbar"/c', "t")
646 call feedkeys("\<CR>y", "xt")
647 call assert_equal('foo', getline(4))
648 call assert_equal('bar', getline(5))
649
650 enew!
651 set titlestring&
652endfunc
Bram Moolenaar0e97b942019-03-27 22:53:53 +0100653
Bram Moolenaar80341bc2019-05-20 20:34:51 +0200654func Test_sub_cmd_9()
655 new
656 let input = ['1 aaa', '2 aaa', '3 aaa']
657 call setline(1, input)
658 func Foo()
659 return submatch(0)
660 endfunc
661 %s/aaa/\=Foo()/gn
662 call assert_equal(input, getline(1, '$'))
663 call assert_equal(1, &modifiable)
664
665 delfunc Foo
666 bw!
667endfunc
668
Bram Moolenaar0e97b942019-03-27 22:53:53 +0100669func Test_nocatch_sub_failure_handling()
670 " normal error results in all replacements
Bram Moolenaar80341bc2019-05-20 20:34:51 +0200671 func Foo()
Bram Moolenaar0e97b942019-03-27 22:53:53 +0100672 foobar
673 endfunc
674 new
675 call setline(1, ['1 aaa', '2 aaa', '3 aaa'])
676 %s/aaa/\=Foo()/g
677 call assert_equal(['1 0', '2 0', '3 0'], getline(1, 3))
678
679 " Trow without try-catch causes abort after the first line.
680 " We cannot test this, since it would stop executing the test script.
681
682 " try/catch does not result in any changes
683 func! Foo()
684 throw 'error'
685 endfunc
686 call setline(1, ['1 aaa', '2 aaa', '3 aaa'])
687 let error_caught = 0
688 try
689 %s/aaa/\=Foo()/g
690 catch
691 let error_caught = 1
692 endtry
693 call assert_equal(1, error_caught)
694 call assert_equal(['1 aaa', '2 aaa', '3 aaa'], getline(1, 3))
695
Bram Moolenaar6349e942019-05-18 13:41:22 +0200696 " Same, but using "n" flag so that "sandbox" gets set
697 call setline(1, ['1 aaa', '2 aaa', '3 aaa'])
698 let error_caught = 0
699 try
700 %s/aaa/\=Foo()/gn
701 catch
702 let error_caught = 1
703 endtry
704 call assert_equal(1, error_caught)
705 call assert_equal(['1 aaa', '2 aaa', '3 aaa'], getline(1, 3))
706
Bram Moolenaar80341bc2019-05-20 20:34:51 +0200707 delfunc Foo
Bram Moolenaar0e97b942019-03-27 22:53:53 +0100708 bwipe!
709endfunc
Bram Moolenaarc6b37db2019-04-27 18:00:34 +0200710
711" Test ":s/pat/sub/" with different ~s in sub.
712func Test_replace_with_tilde()
713 new
714 " Set the last replace string to empty
715 s/^$//
716 call append(0, ['- Bug in "vPPPP" on this text:'])
717 normal gg
718 s/u/~u~/
719 call assert_equal('- Bug in "vPPPP" on this text:', getline(1))
720 s/i/~u~/
721 call assert_equal('- Bug uuun "vPPPP" on this text:', getline(1))
722 s/o/~~~/
723 call assert_equal('- Bug uuun "vPPPP" uuuuuuuuun this text:', getline(1))
724 close!
725endfunc
726
727func Test_replace_keeppatterns()
728 new
729 a
730foobar
731
732substitute foo asdf
733
734one two
735.
736
737 normal gg
738 /^substitute
739 s/foo/bar/
740 call assert_equal('foo', @/)
741 call assert_equal('substitute bar asdf', getline('.'))
742
743 /^substitute
744 keeppatterns s/asdf/xyz/
745 call assert_equal('^substitute', @/)
746 call assert_equal('substitute bar xyz', getline('.'))
747
748 exe "normal /bar /e\<CR>"
749 call assert_equal(15, col('.'))
750 normal -
751 keeppatterns /xyz
752 call assert_equal('bar ', @/)
753 call assert_equal('substitute bar xyz', getline('.'))
754 exe "normal 0dn"
755 call assert_equal('xyz', getline('.'))
756
757 close!
758endfunc
Bram Moolenaarbb265962019-10-31 04:38:36 +0100759
760func Test_sub_beyond_end()
761 new
762 call setline(1, '#')
763 let @/ = '^#\n\zs'
764 s///e
765 call assert_equal('#', getline(1))
766 bwipe!
767endfunc
Bram Moolenaar5d98dc22020-01-29 21:57:34 +0100768
Bram Moolenaarea3db912020-02-02 15:32:13 +0100769" Test for repeating last substitution using :~ and :&r
770func Test_repeat_last_sub()
771 new
772 call setline(1, ['blue green yellow orange white'])
773 s/blue/red/
774 let @/ = 'yellow'
775 ~
776 let @/ = 'white'
777 :&r
778 let @/ = 'green'
779 s//gray
780 call assert_equal('red gray red orange red', getline(1))
781 close!
782endfunc
783
784" Test for Vi compatible substitution:
785" \/{string}/, \?{string}? and \&{string}&
786func Test_sub_vi_compatibility()
787 new
788 call setline(1, ['blue green yellow orange blue'])
789 let @/ = 'orange'
790 s\/white/
791 let @/ = 'blue'
792 s\?amber?
793 let @/ = 'white'
794 s\&green&
795 call assert_equal('amber green yellow white green', getline(1))
796 close!
797endfunc
798
799" Test for substitute with the new text longer than the original text
800func Test_sub_expand_text()
801 new
802 call setline(1, 'abcabcabcabcabcabcabcabc')
803 s/b/\=repeat('B', 10)/g
804 call assert_equal(repeat('aBBBBBBBBBBc', 8), getline(1))
805 close!
806endfunc
807
Bram Moolenaar07ada5f2020-02-05 20:38:22 +0100808" Test for command failures when the last substitute pattern is not set.
809func Test_sub_with_no_last_pat()
Bram Moolenaar9f6277b2020-02-11 22:04:02 +0100810 let lines =<< trim [SCRIPT]
811 call assert_fails('~', 'E33:')
812 call assert_fails('s//abc/g', 'E476:')
813 call assert_fails('s\/bar', 'E476:')
814 call assert_fails('s\&bar&', 'E476:')
815 call writefile(v:errors, 'Xresult')
816 qall!
817 [SCRIPT]
818 call writefile(lines, 'Xscript')
819 if RunVim([], [], '--clean -S Xscript')
820 call assert_equal([], readfile('Xresult'))
821 endif
Bram Moolenaar07ada5f2020-02-05 20:38:22 +0100822
Bram Moolenaar9f6277b2020-02-11 22:04:02 +0100823 let lines =<< trim [SCRIPT]
824 set cpo+=/
825 call assert_fails('s/abc/%/', 'E33:')
826 call writefile(v:errors, 'Xresult')
827 qall!
828 [SCRIPT]
829 call writefile(lines, 'Xscript')
830 if RunVim([], [], '--clean -S Xscript')
831 call assert_equal([], readfile('Xresult'))
832 endif
833
834 call delete('Xscript')
835 call delete('Xresult')
Bram Moolenaar07ada5f2020-02-05 20:38:22 +0100836endfunc
837
Bram Moolenaar5d98dc22020-01-29 21:57:34 +0100838" vim: shiftwidth=2 sts=2 expandtab