blob: b30464ff1ac9d8637e6db154713518fbe36d7be6 [file] [log] [blame]
Bram Moolenaarcd055da2016-09-02 19:50:48 +02001" Tests for multi-line regexps with ":s".
2
Bram Moolenaar1e115362019-01-09 23:01:02 +01003func Test_multiline_subst()
Bram Moolenaarcd055da2016-09-02 19:50:48 +02004 enew!
5 call append(0, ["1 aa",
6 \ "bb",
7 \ "cc",
8 \ "2 dd",
9 \ "ee",
10 \ "3 ef",
11 \ "gh",
12 \ "4 ij",
13 \ "5 a8",
14 \ "8b c9",
15 \ "9d",
16 \ "6 e7",
17 \ "77f",
18 \ "xxxxx"])
19
20 1
21 " test if replacing a line break works with a back reference
22 /^1/,/^2/s/\n\(.\)/ \1/
23 " test if inserting a line break works with a back reference
24 /^3/,/^4/s/\(.\)$/\r\1/
25 " test if replacing a line break with another line break works
26 /^5/,/^6/s/\(\_d\{3}\)/x\1x/
27 call assert_equal('1 aa bb cc 2 dd ee', getline(1))
28 call assert_equal('3 e', getline(2))
29 call assert_equal('f', getline(3))
30 call assert_equal('g', getline(4))
31 call assert_equal('h', getline(5))
32 call assert_equal('4 i', getline(6))
33 call assert_equal('j', getline(7))
34 call assert_equal('5 ax8', getline(8))
35 call assert_equal('8xb cx9', getline(9))
36 call assert_equal('9xd', getline(10))
37 call assert_equal('6 ex7', getline(11))
38 call assert_equal('7x7f', getline(12))
39 call assert_equal('xxxxx', getline(13))
40 enew!
Bram Moolenaar1e115362019-01-09 23:01:02 +010041endfunc
Bram Moolenaar8c50d502017-02-17 18:28:24 +010042
Bram Moolenaar1e115362019-01-09 23:01:02 +010043func Test_substitute_variants()
Bram Moolenaar8c50d502017-02-17 18:28:24 +010044 " Validate that all the 2-/3-letter variants which embed the flags into the
45 " command name actually work.
46 enew!
47 let ln = 'Testing string'
48 let variants = [
49 \ { 'cmd': ':s/Test/test/c', 'exp': 'testing string', 'prompt': 'y' },
50 \ { 'cmd': ':s/foo/bar/ce', 'exp': ln },
51 \ { 'cmd': ':s/t/r/cg', 'exp': 'Tesring srring', 'prompt': 'a' },
52 \ { 'cmd': ':s/t/r/ci', 'exp': 'resting string', 'prompt': 'y' },
53 \ { 'cmd': ':s/t/r/cI', 'exp': 'Tesring string', 'prompt': 'y' },
54 \ { 'cmd': ':s/t/r/cn', 'exp': ln },
55 \ { 'cmd': ':s/t/r/cp', 'exp': 'Tesring string', 'prompt': 'y' },
56 \ { 'cmd': ':s/t/r/cl', 'exp': 'Tesring string', 'prompt': 'y' },
57 \ { 'cmd': ':s/t/r/gc', 'exp': 'Tesring srring', 'prompt': 'a' },
58 \ { 'cmd': ':s/foo/bar/ge', 'exp': ln },
59 \ { 'cmd': ':s/t/r/g', 'exp': 'Tesring srring' },
60 \ { 'cmd': ':s/t/r/gi', 'exp': 'resring srring' },
61 \ { 'cmd': ':s/t/r/gI', 'exp': 'Tesring srring' },
62 \ { 'cmd': ':s/t/r/gn', 'exp': ln },
63 \ { 'cmd': ':s/t/r/gp', 'exp': 'Tesring srring' },
64 \ { 'cmd': ':s/t/r/gl', 'exp': 'Tesring srring' },
65 \ { 'cmd': ':s//r/gr', 'exp': 'Testr strr' },
66 \ { 'cmd': ':s/t/r/ic', 'exp': 'resting string', 'prompt': 'y' },
67 \ { 'cmd': ':s/foo/bar/ie', 'exp': ln },
68 \ { 'cmd': ':s/t/r/i', 'exp': 'resting string' },
69 \ { 'cmd': ':s/t/r/iI', 'exp': 'Tesring string' },
70 \ { 'cmd': ':s/t/r/in', 'exp': ln },
71 \ { 'cmd': ':s/t/r/ip', 'exp': 'resting string' },
72 \ { 'cmd': ':s//r/ir', 'exp': 'Testr string' },
73 \ { 'cmd': ':s/t/r/Ic', 'exp': 'Tesring string', 'prompt': 'y' },
74 \ { 'cmd': ':s/foo/bar/Ie', 'exp': ln },
75 \ { 'cmd': ':s/t/r/Ig', 'exp': 'Tesring srring' },
76 \ { 'cmd': ':s/t/r/Ii', 'exp': 'resting string' },
77 \ { 'cmd': ':s/t/r/I', 'exp': 'Tesring string' },
78 \ { 'cmd': ':s/t/r/Ip', 'exp': 'Tesring string' },
79 \ { 'cmd': ':s/t/r/Il', 'exp': 'Tesring string' },
80 \ { 'cmd': ':s//r/Ir', 'exp': 'Testr string' },
81 \ { 'cmd': ':s//r/rc', 'exp': 'Testr string', 'prompt': 'y' },
82 \ { 'cmd': ':s//r/rg', 'exp': 'Testr strr' },
83 \ { 'cmd': ':s//r/ri', 'exp': 'Testr string' },
84 \ { 'cmd': ':s//r/rI', 'exp': 'Testr string' },
85 \ { 'cmd': ':s//r/rn', 'exp': 'Testing string' },
86 \ { 'cmd': ':s//r/rp', 'exp': 'Testr string' },
87 \ { 'cmd': ':s//r/rl', 'exp': 'Testr string' },
88 \ { 'cmd': ':s//r/r', 'exp': 'Testr string' },
89 \]
90
91 for var in variants
92 for run in [1, 2]
93 let cmd = var.cmd
94 if run == 2 && cmd =~ "/.*/.*/."
95 " Change :s/from/to/{flags} to :s{flags}
96 let cmd = substitute(cmd, '/.*/', '', '')
97 endif
98 call setline(1, [ln])
99 let msg = printf('using "%s"', cmd)
100 let @/='ing'
101 let v:errmsg = ''
102 call feedkeys(cmd . "\<CR>" . get(var, 'prompt', ''), 'ntx')
103 " No error should exist (matters for testing e flag)
104 call assert_equal('', v:errmsg, msg)
105 call assert_equal(var.exp, getline('.'), msg)
106 endfor
107 endfor
Bram Moolenaar1e115362019-01-09 23:01:02 +0100108endfunc
Bram Moolenaarba748c82017-02-26 14:00:07 +0100109
Bram Moolenaar94747162019-02-10 21:55:26 +0100110" Test the l, p, # flags.
111func Test_substitute_flags_lp()
112 new
113 call setline(1, "abc\tdef\<C-h>ghi")
114
115 let a = execute('s/a/a/p')
116 call assert_equal("\nabc def^Hghi", a)
117
118 let a = execute('s/a/a/l')
119 call assert_equal("\nabc^Idef^Hghi$", a)
120
121 let a = execute('s/a/a/#')
122 call assert_equal("\n 1 abc def^Hghi", a)
123
124 let a = execute('s/a/a/p#')
125 call assert_equal("\n 1 abc def^Hghi", a)
126
127 let a = execute('s/a/a/l#')
128 call assert_equal("\n 1 abc^Idef^Hghi$", a)
129
130 let a = execute('s/a/a/')
131 call assert_equal("", a)
132
133 bwipe!
134endfunc
135
Bram Moolenaarba748c82017-02-26 14:00:07 +0100136func Test_substitute_repeat()
137 " This caused an invalid memory access.
138 split Xfile
139 s/^/x
140 call feedkeys("Qsc\<CR>y", 'tx')
141 bwipe!
142endfunc
Bram Moolenaar1a333bc2017-08-30 20:21:58 +0200143
Bram Moolenaard77aa4d2019-02-10 22:50:14 +0100144" Test %s/\n// which is implemented as a special case to use a
145" more efficient join rather than doing a regular substitution.
146func Test_substitute_join()
147 new
148
149 call setline(1, ["foo\tbar", "bar\<C-H>foo"])
150 let a = execute('%s/\n//')
151 call assert_equal("", a)
152 call assert_equal(["foo\tbarbar\<C-H>foo"], getline(1, '$'))
153 call assert_equal('\n', histget("search", -1))
154
155 call setline(1, ["foo\tbar", "bar\<C-H>foo"])
156 let a = execute('%s/\n//g')
157 call assert_equal("", a)
158 call assert_equal(["foo\tbarbar\<C-H>foo"], getline(1, '$'))
159 call assert_equal('\n', histget("search", -1))
160
161 call setline(1, ["foo\tbar", "bar\<C-H>foo"])
162 let a = execute('%s/\n//p')
163 call assert_equal("\nfoo barbar^Hfoo", a)
164 call assert_equal(["foo\tbarbar\<C-H>foo"], getline(1, '$'))
165 call assert_equal('\n', histget("search", -1))
166
167 call setline(1, ["foo\tbar", "bar\<C-H>foo"])
168 let a = execute('%s/\n//l')
169 call assert_equal("\nfoo^Ibarbar^Hfoo$", a)
170 call assert_equal(["foo\tbarbar\<C-H>foo"], getline(1, '$'))
171 call assert_equal('\n', histget("search", -1))
172
173 call setline(1, ["foo\tbar", "bar\<C-H>foo"])
174 let a = execute('%s/\n//#')
175 call assert_equal("\n 1 foo barbar^Hfoo", a)
176 call assert_equal(["foo\tbarbar\<C-H>foo"], getline(1, '$'))
177 call assert_equal('\n', histget("search", -1))
178
179 bwipe!
180endfunc
181
182func Test_substitute_count()
183 new
184 call setline(1, ['foo foo', 'foo foo', 'foo foo', 'foo foo', 'foo foo'])
185 2
186
187 s/foo/bar/3
188 call assert_equal(['foo foo', 'bar foo', 'bar foo', 'bar foo', 'foo foo'],
189 \ getline(1, '$'))
190
191 call assert_fails('s/foo/bar/0', 'E939:')
192
193 bwipe!
194endfunc
195
196" Test substitute 'n' flag (report number of matches, do not substitute).
197func Test_substitute_flag_n()
198 new
199 let lines = ['foo foo', 'foo foo', 'foo foo', 'foo foo', 'foo foo']
200 call setline(1, lines)
201
202 call assert_equal("\n3 matches on 3 lines", execute('2,4s/foo/bar/n'))
203 call assert_equal("\n6 matches on 3 lines", execute('2,4s/foo/bar/gn'))
204
205 " c flag (confirm) should be ignored when using n flag.
206 call assert_equal("\n3 matches on 3 lines", execute('2,4s/foo/bar/nc'))
207
208 " No substitution should have been done.
209 call assert_equal(lines, getline(1, '$'))
210
211 bwipe!
212endfunc
213
214func Test_substitute_errors()
215 new
216 call setline(1, 'foobar')
217
218 call assert_fails('s/FOO/bar/', 'E486:')
219 call assert_fails('s/foo/bar/@', 'E488:')
220 call assert_fails('s/\(/bar/', 'E476:')
221
222 setl nomodifiable
223 call assert_fails('s/foo/bar/', 'E21:')
224
225 bwipe!
226endfunc
227
Bram Moolenaar1a333bc2017-08-30 20:21:58 +0200228" Test for *sub-replace-special* and *sub-replace-expression* on substitute().
229func Test_sub_replace_1()
230 " Run the tests with 'magic' on
231 set magic
232 set cpo&
233 call assert_equal('AA', substitute('A', 'A', '&&', ''))
234 call assert_equal('&', substitute('B', 'B', '\&', ''))
235 call assert_equal('C123456789987654321', substitute('C123456789', 'C\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)', '\0\9\8\7\6\5\4\3\2\1', ''))
236 call assert_equal('d', substitute('D', 'D', 'd', ''))
237 call assert_equal('~', substitute('E', 'E', '~', ''))
238 call assert_equal('~', substitute('F', 'F', '\~', ''))
239 call assert_equal('Gg', substitute('G', 'G', '\ugg', ''))
240 call assert_equal('Hh', substitute('H', 'H', '\Uh\Eh', ''))
241 call assert_equal('iI', substitute('I', 'I', '\lII', ''))
242 call assert_equal('jJ', substitute('J', 'J', '\LJ\EJ', ''))
243 call assert_equal('Kk', substitute('K', 'K', '\Uk\ek', ''))
244 call assert_equal("l\<C-V>\<C-M>l",
245 \ substitute('lLl', 'L', "\<C-V>\<C-M>", ''))
246 call assert_equal("m\<C-M>m", substitute('mMm', 'M', '\r', ''))
247 call assert_equal("n\<C-V>\<C-M>n",
248 \ substitute('nNn', 'N', "\\\<C-V>\<C-M>", ''))
249 call assert_equal("o\no", substitute('oOo', 'O', '\n', ''))
250 call assert_equal("p\<C-H>p", substitute('pPp', 'P', '\b', ''))
251 call assert_equal("q\tq", substitute('qQq', 'Q', '\t', ''))
252 call assert_equal('r\r', substitute('rRr', 'R', '\\', ''))
253 call assert_equal('scs', substitute('sSs', 'S', '\c', ''))
254 call assert_equal("u\nu", substitute('uUu', 'U', "\n", ''))
255 call assert_equal("v\<C-H>v", substitute('vVv', 'V', "\b", ''))
256 call assert_equal("w\\w", substitute('wWw', 'W', "\\", ''))
257 call assert_equal("x\<C-M>x", substitute('xXx', 'X', "\r", ''))
258 call assert_equal("YyyY", substitute('Y', 'Y', '\L\uyYy\l\EY', ''))
259 call assert_equal("zZZz", substitute('Z', 'Z', '\U\lZzZ\u\Ez', ''))
260endfunc
261
262func Test_sub_replace_2()
263 " Run the tests with 'magic' off
264 set nomagic
265 set cpo&
266 call assert_equal('AA', substitute('A', 'A', '&&', ''))
267 call assert_equal('&', substitute('B', 'B', '\&', ''))
268 call assert_equal('C123456789987654321', substitute('C123456789', 'C\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)', '\0\9\8\7\6\5\4\3\2\1', ''))
269 call assert_equal('d', substitute('D', 'D', 'd', ''))
270 call assert_equal('~', substitute('E', 'E', '~', ''))
271 call assert_equal('~', substitute('F', 'F', '\~', ''))
272 call assert_equal('Gg', substitute('G', 'G', '\ugg', ''))
273 call assert_equal('Hh', substitute('H', 'H', '\Uh\Eh', ''))
274 call assert_equal('iI', substitute('I', 'I', '\lII', ''))
275 call assert_equal('jJ', substitute('J', 'J', '\LJ\EJ', ''))
276 call assert_equal('Kk', substitute('K', 'K', '\Uk\ek', ''))
277 call assert_equal("l\<C-V>\<C-M>l",
278 \ substitute('lLl', 'L', "\<C-V>\<C-M>", ''))
279 call assert_equal("m\<C-M>m", substitute('mMm', 'M', '\r', ''))
280 call assert_equal("n\<C-V>\<C-M>n",
281 \ substitute('nNn', 'N', "\\\<C-V>\<C-M>", ''))
282 call assert_equal("o\no", substitute('oOo', 'O', '\n', ''))
283 call assert_equal("p\<C-H>p", substitute('pPp', 'P', '\b', ''))
284 call assert_equal("q\tq", substitute('qQq', 'Q', '\t', ''))
285 call assert_equal('r\r', substitute('rRr', 'R', '\\', ''))
286 call assert_equal('scs', substitute('sSs', 'S', '\c', ''))
287 call assert_equal("t\<C-M>t", substitute('tTt', 'T', "\r", ''))
288 call assert_equal("u\nu", substitute('uUu', 'U', "\n", ''))
289 call assert_equal("v\<C-H>v", substitute('vVv', 'V', "\b", ''))
290 call assert_equal('w\w', substitute('wWw', 'W', "\\", ''))
291 call assert_equal('XxxX', substitute('X', 'X', '\L\uxXx\l\EX', ''))
292 call assert_equal('yYYy', substitute('Y', 'Y', '\U\lYyY\u\Ey', ''))
293endfunc
294
295func Test_sub_replace_3()
296 set magic&
297 set cpo&
298 call assert_equal('a\a', substitute('aAa', 'A', '\="\\"', ''))
299 call assert_equal('b\\b', substitute('bBb', 'B', '\="\\\\"', ''))
300 call assert_equal("c\rc", substitute('cCc', 'C', "\\=\"\r\"", ''))
301 call assert_equal("d\\\rd", substitute('dDd', 'D', "\\=\"\\\\\r\"", ''))
302 call assert_equal("e\\\\\re", substitute('eEe', 'E', "\\=\"\\\\\\\\\r\"", ''))
303 call assert_equal('f\rf', substitute('fFf', 'F', '\="\\r"', ''))
304 call assert_equal('j\nj', substitute('jJj', 'J', '\="\\n"', ''))
305 call assert_equal("k\<C-M>k", substitute('kKk', 'K', '\="\r"', ''))
306 call assert_equal("l\nl", substitute('lLl', 'L', '\="\n"', ''))
307endfunc
308
309" Test for submatch() on substitute().
310func Test_sub_replace_4()
311 set magic&
312 set cpo&
313 call assert_equal('a\a', substitute('aAa', 'A',
314 \ '\=substitute(submatch(0), ".", "\\", "")', ''))
315 call assert_equal('b\b', substitute('bBb', 'B',
316 \ '\=substitute(submatch(0), ".", "\\\\", "")', ''))
317 call assert_equal("c\<C-V>\<C-M>c", substitute('cCc', 'C', '\=substitute(submatch(0), ".", "\<C-V>\<C-M>", "")', ''))
318 call assert_equal("d\<C-V>\<C-M>d", substitute('dDd', 'D', '\=substitute(submatch(0), ".", "\\\<C-V>\<C-M>", "")', ''))
319 call assert_equal("e\\\<C-V>\<C-M>e", substitute('eEe', 'E', '\=substitute(submatch(0), ".", "\\\\\<C-V>\<C-M>", "")', ''))
320 call assert_equal("f\<C-M>f", substitute('fFf', 'F', '\=substitute(submatch(0), ".", "\\r", "")', ''))
321 call assert_equal("j\nj", substitute('jJj', 'J', '\=substitute(submatch(0), ".", "\\n", "")', ''))
322 call assert_equal("k\rk", substitute('kKk', 'K', '\=substitute(submatch(0), ".", "\r", "")', ''))
323 call assert_equal("l\nl", substitute('lLl', 'L', '\=substitute(submatch(0), ".", "\n", "")', ''))
324endfunc
325
326func Test_sub_replace_5()
327 set magic&
328 set cpo&
329 call assert_equal('A123456789987654321', substitute('A123456789',
330 \ 'A\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)',
331 \ '\=submatch(0) . submatch(9) . submatch(8) . ' .
332 \ 'submatch(7) . submatch(6) . submatch(5) . ' .
333 \ 'submatch(4) . submatch(3) . submatch(2) . submatch(1)',
334 \ ''))
335 call assert_equal("[['A123456789'], ['9'], ['8'], ['7'], ['6'], " .
336 \ "['5'], ['4'], ['3'], ['2'], ['1']]",
337 \ substitute('A123456789',
338 \ 'A\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)',
339 \ '\=string([submatch(0, 1), submatch(9, 1), ' .
Bram Moolenaarf6ed61e2019-09-07 19:05:09 +0200340 \ 'submatch(8, 1), 7->submatch(1), submatch(6, 1), ' .
Bram Moolenaar1a333bc2017-08-30 20:21:58 +0200341 \ 'submatch(5, 1), submatch(4, 1), submatch(3, 1), ' .
342 \ 'submatch(2, 1), submatch(1, 1)])',
343 \ ''))
344endfunc
345
346func Test_sub_replace_6()
347 set magic&
348 set cpo+=/
349 call assert_equal('a', substitute('A', 'A', 'a', ''))
350 call assert_equal('%', substitute('B', 'B', '%', ''))
351 set cpo-=/
352 call assert_equal('c', substitute('C', 'C', 'c', ''))
353 call assert_equal('%', substitute('D', 'D', '%', ''))
354endfunc
355
356func Test_sub_replace_7()
357 set magic&
358 set cpo&
359 call assert_equal('AA', substitute('AA', 'A.', '\=submatch(0)', ''))
360 call assert_equal("B\nB", substitute("B\nB", 'B.', '\=submatch(0)', ''))
361 call assert_equal("['B\n']B", substitute("B\nB", 'B.', '\=string(submatch(0, 1))', ''))
362 call assert_equal('-abab', substitute('-bb', '\zeb', 'a', 'g'))
363 call assert_equal('c-cbcbc', substitute('-bb', '\ze', 'c', 'g'))
364endfunc
365
366" Test for *:s%* on :substitute.
367func Test_sub_replace_8()
368 new
369 set magic&
370 set cpo&
371 $put =',,X'
372 s/\(^\|,\)\ze\(,\|X\)/\1N/g
373 call assert_equal('N,,NX', getline("$"))
374 $put =',,Y'
375 let cmd = ':s/\(^\|,\)\ze\(,\|Y\)/\1N/gc'
376 call feedkeys(cmd . "\<CR>a", "xt")
377 call assert_equal('N,,NY', getline("$"))
378 :$put =',,Z'
379 let cmd = ':s/\(^\|,\)\ze\(,\|Z\)/\1N/gc'
380 call feedkeys(cmd . "\<CR>yy", "xt")
381 call assert_equal('N,,NZ', getline("$"))
382 enew! | close
383endfunc
384
385func Test_sub_replace_9()
386 new
387 set magic&
388 set cpo&
389 $put ='xxx'
390 call feedkeys(":s/x/X/gc\<CR>yyq", "xt")
391 call assert_equal('XXx', getline("$"))
392 enew! | close
393endfunc
394
395func Test_sub_replace_10()
396 set magic&
397 set cpo&
398 call assert_equal('a1a2a3a', substitute('123', '\zs', 'a', 'g'))
399 call assert_equal('aaa', substitute('123', '\zs.', 'a', 'g'))
400 call assert_equal('1a2a3a', substitute('123', '.\zs', 'a', 'g'))
401 call assert_equal('a1a2a3a', substitute('123', '\ze', 'a', 'g'))
402 call assert_equal('a1a2a3', substitute('123', '\ze.', 'a', 'g'))
403 call assert_equal('aaa', substitute('123', '.\ze', 'a', 'g'))
404 call assert_equal('aa2a3a', substitute('123', '1\|\ze', 'a', 'g'))
405 call assert_equal('1aaa', substitute('123', '1\zs\|[23]', 'a', 'g'))
406endfunc
Bram Moolenaar15993ce2017-10-26 20:21:44 +0200407
Bram Moolenaarb0745b22019-11-09 22:28:11 +0100408func SubReplacer(text, submatches)
409 return a:text .. a:submatches[0] .. a:text
410endfunc
411
412func Test_substitute_partial()
413 call assert_equal('1foo2foo3', substitute('123', '2', function('SubReplacer', ['foo']), 'g'))
414endfunc
415
Bram Moolenaar15993ce2017-10-26 20:21:44 +0200416" Tests for *sub-replace-special* and *sub-replace-expression* on :substitute.
417
418" Execute a list of :substitute command tests
419func Run_SubCmd_Tests(tests)
420 enew!
421 for t in a:tests
422 let start = line('.') + 1
423 let end = start + len(t[2]) - 1
Bram Moolenaarbb265962019-10-31 04:38:36 +0100424 " TODO: why is there a one second delay the first time we get here?
Bram Moolenaar15993ce2017-10-26 20:21:44 +0200425 exe "normal o" . t[0]
426 call cursor(start, 1)
427 exe t[1]
428 call assert_equal(t[2], getline(start, end), t[1])
429 endfor
430 enew!
431endfunc
432
433func Test_sub_cmd_1()
434 set magic
435 set cpo&
436
437 " List entry format: [input, cmd, output]
438 let tests = [['A', 's/A/&&/', ['AA']],
439 \ ['B', 's/B/\&/', ['&']],
440 \ ['C123456789', 's/C\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)/\0\9\8\7\6\5\4\3\2\1/', ['C123456789987654321']],
441 \ ['D', 's/D/d/', ['d']],
442 \ ['E', 's/E/~/', ['d']],
443 \ ['F', 's/F/\~/', ['~']],
444 \ ['G', 's/G/\ugg/', ['Gg']],
445 \ ['H', 's/H/\Uh\Eh/', ['Hh']],
446 \ ['I', 's/I/\lII/', ['iI']],
447 \ ['J', 's/J/\LJ\EJ/', ['jJ']],
448 \ ['K', 's/K/\Uk\ek/', ['Kk']],
449 \ ['lLl', "s/L/\<C-V>\<C-M>/", ["l\<C-V>", 'l']],
450 \ ['mMm', 's/M/\r/', ['m', 'm']],
451 \ ['nNn', "s/N/\\\<C-V>\<C-M>/", ["n\<C-V>", 'n']],
452 \ ['oOo', 's/O/\n/', ["o\no"]],
453 \ ['pPp', 's/P/\b/', ["p\<C-H>p"]],
454 \ ['qQq', 's/Q/\t/', ["q\tq"]],
455 \ ['rRr', 's/R/\\/', ['r\r']],
456 \ ['sSs', 's/S/\c/', ['scs']],
457 \ ['tTt', "s/T/\<C-V>\<C-J>/", ["t\<C-V>\<C-J>t"]],
458 \ ['U', 's/U/\L\uuUu\l\EU/', ['UuuU']],
459 \ ['V', 's/V/\U\lVvV\u\Ev/', ['vVVv']]
460 \ ]
461 call Run_SubCmd_Tests(tests)
462endfunc
463
464func Test_sub_cmd_2()
465 set nomagic
466 set cpo&
467
468 " List entry format: [input, cmd, output]
469 let tests = [['A', 's/A/&&/', ['&&']],
470 \ ['B', 's/B/\&/', ['B']],
471 \ ['C123456789', 's/\mC\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)/\0\9\8\7\6\5\4\3\2\1/', ['C123456789987654321']],
472 \ ['D', 's/D/d/', ['d']],
473 \ ['E', 's/E/~/', ['~']],
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_3()
496 set nomagic
497 set cpo&
498
499 " List entry format: [input, cmd, output]
500 let tests = [['aAa', "s/A/\\='\\'/", ['a\a']],
501 \ ['bBb', "s/B/\\='\\\\'/", ['b\\b']],
502 \ ['cCc', "s/C/\\='\<C-V>\<C-M>'/", ["c\<C-V>", 'c']],
503 \ ['dDd', "s/D/\\='\\\<C-V>\<C-M>'/", ["d\\\<C-V>", 'd']],
504 \ ['eEe', "s/E/\\='\\\\\<C-V>\<C-M>'/", ["e\\\\\<C-V>", 'e']],
505 \ ['fFf', "s/F/\\='\r'/", ['f', 'f']],
506 \ ['gGg', "s/G/\\='\<C-V>\<C-J>'/", ["g\<C-V>", 'g']],
507 \ ['hHh', "s/H/\\='\\\<C-V>\<C-J>'/", ["h\\\<C-V>", 'h']],
508 \ ['iIi', "s/I/\\='\\\\\<C-V>\<C-J>'/", ["i\\\\\<C-V>", 'i']],
509 \ ['jJj', "s/J/\\='\n'/", ['j', 'j']],
510 \ ['kKk', 's/K/\="\r"/', ['k', 'k']],
511 \ ['lLl', 's/L/\="\n"/', ['l', 'l']]
512 \ ]
513 call Run_SubCmd_Tests(tests)
514endfunc
515
Bram Moolenaarf1699962019-08-31 17:48:19 +0200516" Test for submatch() on :substitute.
Bram Moolenaar15993ce2017-10-26 20:21:44 +0200517func Test_sub_cmd_4()
518 set magic&
519 set cpo&
520
521 " List entry format: [input, cmd, output]
522 let tests = [ ['aAa', "s/A/\\=substitute(submatch(0), '.', '\\', '')/",
Bram Moolenaar1e115362019-01-09 23:01:02 +0100523 \ ['a\a']],
Bram Moolenaar15993ce2017-10-26 20:21:44 +0200524 \ ['bBb', "s/B/\\=substitute(submatch(0), '.', '\\', '')/",
Bram Moolenaar1e115362019-01-09 23:01:02 +0100525 \ ['b\b']],
Bram Moolenaar15993ce2017-10-26 20:21:44 +0200526 \ ['cCc', "s/C/\\=substitute(submatch(0), '.', '\<C-V>\<C-M>', '')/",
527 \ ["c\<C-V>", 'c']],
528 \ ['dDd', "s/D/\\=substitute(submatch(0), '.', '\\\<C-V>\<C-M>', '')/",
529 \ ["d\<C-V>", 'd']],
530 \ ['eEe', "s/E/\\=substitute(submatch(0), '.', '\\\\\<C-V>\<C-M>', '')/",
531 \ ["e\\\<C-V>", 'e']],
532 \ ['fFf', "s/F/\\=substitute(submatch(0), '.', '\\r', '')/",
533 \ ['f', 'f']],
534 \ ['gGg', 's/G/\=substitute(submatch(0), ".", "\<C-V>\<C-J>", "")/',
535 \ ["g\<C-V>", 'g']],
536 \ ['hHh', 's/H/\=substitute(submatch(0), ".", "\\\<C-V>\<C-J>", "")/',
537 \ ["h\<C-V>", 'h']],
538 \ ['iIi', 's/I/\=substitute(submatch(0), ".", "\\\\\<C-V>\<C-J>", "")/',
539 \ ["i\\\<C-V>", 'i']],
540 \ ['jJj', "s/J/\\=substitute(submatch(0), '.', '\\n', '')/",
541 \ ['j', 'j']],
542 \ ['kKk', "s/K/\\=substitute(submatch(0), '.', '\\r', '')/",
543 \ ['k', 'k']],
544 \ ['lLl', "s/L/\\=substitute(submatch(0), '.', '\\n', '')/",
545 \ ['l', 'l']],
546 \ ]
547 call Run_SubCmd_Tests(tests)
548endfunc
549
550func Test_sub_cmd_5()
551 set magic&
552 set cpo&
553
554 " List entry format: [input, cmd, output]
555 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']],
556 \ ['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']]"]],
557 \ ]
558 call Run_SubCmd_Tests(tests)
559endfunc
560
561" Test for *:s%* on :substitute.
562func Test_sub_cmd_6()
563 set magic&
564 set cpo+=/
565
566 " List entry format: [input, cmd, output]
567 let tests = [ ['A', 's/A/a/', ['a']],
568 \ ['B', 's/B/%/', ['a']],
569 \ ]
570 call Run_SubCmd_Tests(tests)
571
572 set cpo-=/
573 let tests = [ ['C', 's/C/c/', ['c']],
574 \ ['D', 's/D/%/', ['%']],
575 \ ]
576 call Run_SubCmd_Tests(tests)
577
578 set cpo&
579endfunc
580
581" Test for :s replacing \n with line break.
582func Test_sub_cmd_7()
583 set magic&
584 set cpo&
585
586 " List entry format: [input, cmd, output]
587 let tests = [ ["A\<C-V>\<C-M>A", 's/A./\=submatch(0)/', ['A', 'A']],
588 \ ["B\<C-V>\<C-J>B", 's/B./\=submatch(0)/', ['B', 'B']],
589 \ ["C\<C-V>\<C-J>C", 's/C./\=strtrans(string(submatch(0, 1)))/', [strtrans("['C\<C-J>']C")]],
590 \ ["D\<C-V>\<C-J>\nD", 's/D.\nD/\=strtrans(string(submatch(0, 1)))/', [strtrans("['D\<C-J>', 'D']")]],
591 \ ["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']")]],
592 \ ]
593 call Run_SubCmd_Tests(tests)
594
595 exe "normal oQ\nQ\<Esc>k"
596 call assert_fails('s/Q[^\n]Q/\=submatch(0)."foobar"/', 'E486')
597 enew!
598endfunc
599
600func TitleString()
601 let check = 'foo' =~ 'bar'
602 return ""
603endfunc
604
605func Test_sub_cmd_8()
606 set titlestring=%{TitleString()}
607
608 enew!
609 call append(0, ['', 'test_one', 'test_two'])
610 call cursor(1,1)
611 /^test_one/s/.*/\="foo\nbar"/
612 call assert_equal('foo', getline(2))
613 call assert_equal('bar', getline(3))
614 call feedkeys(':/^test_two/s/.*/\="foo\nbar"/c', "t")
615 call feedkeys("\<CR>y", "xt")
616 call assert_equal('foo', getline(4))
617 call assert_equal('bar', getline(5))
618
619 enew!
620 set titlestring&
621endfunc
Bram Moolenaar0e97b942019-03-27 22:53:53 +0100622
Bram Moolenaar80341bc2019-05-20 20:34:51 +0200623func Test_sub_cmd_9()
624 new
625 let input = ['1 aaa', '2 aaa', '3 aaa']
626 call setline(1, input)
627 func Foo()
628 return submatch(0)
629 endfunc
630 %s/aaa/\=Foo()/gn
631 call assert_equal(input, getline(1, '$'))
632 call assert_equal(1, &modifiable)
633
634 delfunc Foo
635 bw!
636endfunc
637
Bram Moolenaar0e97b942019-03-27 22:53:53 +0100638func Test_nocatch_sub_failure_handling()
639 " normal error results in all replacements
Bram Moolenaar80341bc2019-05-20 20:34:51 +0200640 func Foo()
Bram Moolenaar0e97b942019-03-27 22:53:53 +0100641 foobar
642 endfunc
643 new
644 call setline(1, ['1 aaa', '2 aaa', '3 aaa'])
645 %s/aaa/\=Foo()/g
646 call assert_equal(['1 0', '2 0', '3 0'], getline(1, 3))
647
648 " Trow without try-catch causes abort after the first line.
649 " We cannot test this, since it would stop executing the test script.
650
651 " try/catch does not result in any changes
652 func! Foo()
653 throw 'error'
654 endfunc
655 call setline(1, ['1 aaa', '2 aaa', '3 aaa'])
656 let error_caught = 0
657 try
658 %s/aaa/\=Foo()/g
659 catch
660 let error_caught = 1
661 endtry
662 call assert_equal(1, error_caught)
663 call assert_equal(['1 aaa', '2 aaa', '3 aaa'], getline(1, 3))
664
Bram Moolenaar6349e942019-05-18 13:41:22 +0200665 " Same, but using "n" flag so that "sandbox" gets set
666 call setline(1, ['1 aaa', '2 aaa', '3 aaa'])
667 let error_caught = 0
668 try
669 %s/aaa/\=Foo()/gn
670 catch
671 let error_caught = 1
672 endtry
673 call assert_equal(1, error_caught)
674 call assert_equal(['1 aaa', '2 aaa', '3 aaa'], getline(1, 3))
675
Bram Moolenaar80341bc2019-05-20 20:34:51 +0200676 delfunc Foo
Bram Moolenaar0e97b942019-03-27 22:53:53 +0100677 bwipe!
678endfunc
Bram Moolenaarc6b37db2019-04-27 18:00:34 +0200679
680" Test ":s/pat/sub/" with different ~s in sub.
681func Test_replace_with_tilde()
682 new
683 " Set the last replace string to empty
684 s/^$//
685 call append(0, ['- Bug in "vPPPP" on this text:'])
686 normal gg
687 s/u/~u~/
688 call assert_equal('- Bug in "vPPPP" on this text:', getline(1))
689 s/i/~u~/
690 call assert_equal('- Bug uuun "vPPPP" on this text:', getline(1))
691 s/o/~~~/
692 call assert_equal('- Bug uuun "vPPPP" uuuuuuuuun this text:', getline(1))
693 close!
694endfunc
695
696func Test_replace_keeppatterns()
697 new
698 a
699foobar
700
701substitute foo asdf
702
703one two
704.
705
706 normal gg
707 /^substitute
708 s/foo/bar/
709 call assert_equal('foo', @/)
710 call assert_equal('substitute bar asdf', getline('.'))
711
712 /^substitute
713 keeppatterns s/asdf/xyz/
714 call assert_equal('^substitute', @/)
715 call assert_equal('substitute bar xyz', getline('.'))
716
717 exe "normal /bar /e\<CR>"
718 call assert_equal(15, col('.'))
719 normal -
720 keeppatterns /xyz
721 call assert_equal('bar ', @/)
722 call assert_equal('substitute bar xyz', getline('.'))
723 exe "normal 0dn"
724 call assert_equal('xyz', getline('.'))
725
726 close!
727endfunc
Bram Moolenaarbb265962019-10-31 04:38:36 +0100728
729func Test_sub_beyond_end()
730 new
731 call setline(1, '#')
732 let @/ = '^#\n\zs'
733 s///e
734 call assert_equal('#', getline(1))
735 bwipe!
736endfunc