blob: a58b56ec895e992e37383f2d333ee11337fa33b7 [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), ' .
340 \ 'submatch(8, 1), submatch(7, 1), submatch(6, 1), ' .
341 \ '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
408" Tests for *sub-replace-special* and *sub-replace-expression* on :substitute.
409
410" Execute a list of :substitute command tests
411func Run_SubCmd_Tests(tests)
412 enew!
413 for t in a:tests
414 let start = line('.') + 1
415 let end = start + len(t[2]) - 1
416 exe "normal o" . t[0]
417 call cursor(start, 1)
418 exe t[1]
419 call assert_equal(t[2], getline(start, end), t[1])
420 endfor
421 enew!
422endfunc
423
424func Test_sub_cmd_1()
425 set magic
426 set cpo&
427
428 " List entry format: [input, cmd, output]
429 let tests = [['A', 's/A/&&/', ['AA']],
430 \ ['B', 's/B/\&/', ['&']],
431 \ ['C123456789', 's/C\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)/\0\9\8\7\6\5\4\3\2\1/', ['C123456789987654321']],
432 \ ['D', 's/D/d/', ['d']],
433 \ ['E', 's/E/~/', ['d']],
434 \ ['F', 's/F/\~/', ['~']],
435 \ ['G', 's/G/\ugg/', ['Gg']],
436 \ ['H', 's/H/\Uh\Eh/', ['Hh']],
437 \ ['I', 's/I/\lII/', ['iI']],
438 \ ['J', 's/J/\LJ\EJ/', ['jJ']],
439 \ ['K', 's/K/\Uk\ek/', ['Kk']],
440 \ ['lLl', "s/L/\<C-V>\<C-M>/", ["l\<C-V>", 'l']],
441 \ ['mMm', 's/M/\r/', ['m', 'm']],
442 \ ['nNn', "s/N/\\\<C-V>\<C-M>/", ["n\<C-V>", 'n']],
443 \ ['oOo', 's/O/\n/', ["o\no"]],
444 \ ['pPp', 's/P/\b/', ["p\<C-H>p"]],
445 \ ['qQq', 's/Q/\t/', ["q\tq"]],
446 \ ['rRr', 's/R/\\/', ['r\r']],
447 \ ['sSs', 's/S/\c/', ['scs']],
448 \ ['tTt', "s/T/\<C-V>\<C-J>/", ["t\<C-V>\<C-J>t"]],
449 \ ['U', 's/U/\L\uuUu\l\EU/', ['UuuU']],
450 \ ['V', 's/V/\U\lVvV\u\Ev/', ['vVVv']]
451 \ ]
452 call Run_SubCmd_Tests(tests)
453endfunc
454
455func Test_sub_cmd_2()
456 set nomagic
457 set cpo&
458
459 " List entry format: [input, cmd, output]
460 let tests = [['A', 's/A/&&/', ['&&']],
461 \ ['B', 's/B/\&/', ['B']],
462 \ ['C123456789', 's/\mC\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)/\0\9\8\7\6\5\4\3\2\1/', ['C123456789987654321']],
463 \ ['D', 's/D/d/', ['d']],
464 \ ['E', 's/E/~/', ['~']],
465 \ ['F', 's/F/\~/', ['~']],
466 \ ['G', 's/G/\ugg/', ['Gg']],
467 \ ['H', 's/H/\Uh\Eh/', ['Hh']],
468 \ ['I', 's/I/\lII/', ['iI']],
469 \ ['J', 's/J/\LJ\EJ/', ['jJ']],
470 \ ['K', 's/K/\Uk\ek/', ['Kk']],
471 \ ['lLl', "s/L/\<C-V>\<C-M>/", ["l\<C-V>", 'l']],
472 \ ['mMm', 's/M/\r/', ['m', 'm']],
473 \ ['nNn', "s/N/\\\<C-V>\<C-M>/", ["n\<C-V>", 'n']],
474 \ ['oOo', 's/O/\n/', ["o\no"]],
475 \ ['pPp', 's/P/\b/', ["p\<C-H>p"]],
476 \ ['qQq', 's/Q/\t/', ["q\tq"]],
477 \ ['rRr', 's/R/\\/', ['r\r']],
478 \ ['sSs', 's/S/\c/', ['scs']],
479 \ ['tTt', "s/T/\<C-V>\<C-J>/", ["t\<C-V>\<C-J>t"]],
480 \ ['U', 's/U/\L\uuUu\l\EU/', ['UuuU']],
481 \ ['V', 's/V/\U\lVvV\u\Ev/', ['vVVv']]
482 \ ]
483 call Run_SubCmd_Tests(tests)
484endfunc
485
486func Test_sub_cmd_3()
487 set nomagic
488 set cpo&
489
490 " List entry format: [input, cmd, output]
491 let tests = [['aAa', "s/A/\\='\\'/", ['a\a']],
492 \ ['bBb', "s/B/\\='\\\\'/", ['b\\b']],
493 \ ['cCc', "s/C/\\='\<C-V>\<C-M>'/", ["c\<C-V>", 'c']],
494 \ ['dDd', "s/D/\\='\\\<C-V>\<C-M>'/", ["d\\\<C-V>", 'd']],
495 \ ['eEe', "s/E/\\='\\\\\<C-V>\<C-M>'/", ["e\\\\\<C-V>", 'e']],
496 \ ['fFf', "s/F/\\='\r'/", ['f', 'f']],
497 \ ['gGg', "s/G/\\='\<C-V>\<C-J>'/", ["g\<C-V>", 'g']],
498 \ ['hHh', "s/H/\\='\\\<C-V>\<C-J>'/", ["h\\\<C-V>", 'h']],
499 \ ['iIi', "s/I/\\='\\\\\<C-V>\<C-J>'/", ["i\\\\\<C-V>", 'i']],
500 \ ['jJj', "s/J/\\='\n'/", ['j', 'j']],
501 \ ['kKk', 's/K/\="\r"/', ['k', 'k']],
502 \ ['lLl', 's/L/\="\n"/', ['l', 'l']]
503 \ ]
504 call Run_SubCmd_Tests(tests)
505endfunc
506
507" Test for submatch() on :substitue.
508func Test_sub_cmd_4()
509 set magic&
510 set cpo&
511
512 " List entry format: [input, cmd, output]
513 let tests = [ ['aAa', "s/A/\\=substitute(submatch(0), '.', '\\', '')/",
Bram Moolenaar1e115362019-01-09 23:01:02 +0100514 \ ['a\a']],
Bram Moolenaar15993ce2017-10-26 20:21:44 +0200515 \ ['bBb', "s/B/\\=substitute(submatch(0), '.', '\\', '')/",
Bram Moolenaar1e115362019-01-09 23:01:02 +0100516 \ ['b\b']],
Bram Moolenaar15993ce2017-10-26 20:21:44 +0200517 \ ['cCc', "s/C/\\=substitute(submatch(0), '.', '\<C-V>\<C-M>', '')/",
518 \ ["c\<C-V>", 'c']],
519 \ ['dDd', "s/D/\\=substitute(submatch(0), '.', '\\\<C-V>\<C-M>', '')/",
520 \ ["d\<C-V>", 'd']],
521 \ ['eEe', "s/E/\\=substitute(submatch(0), '.', '\\\\\<C-V>\<C-M>', '')/",
522 \ ["e\\\<C-V>", 'e']],
523 \ ['fFf', "s/F/\\=substitute(submatch(0), '.', '\\r', '')/",
524 \ ['f', 'f']],
525 \ ['gGg', 's/G/\=substitute(submatch(0), ".", "\<C-V>\<C-J>", "")/',
526 \ ["g\<C-V>", 'g']],
527 \ ['hHh', 's/H/\=substitute(submatch(0), ".", "\\\<C-V>\<C-J>", "")/',
528 \ ["h\<C-V>", 'h']],
529 \ ['iIi', 's/I/\=substitute(submatch(0), ".", "\\\\\<C-V>\<C-J>", "")/',
530 \ ["i\\\<C-V>", 'i']],
531 \ ['jJj', "s/J/\\=substitute(submatch(0), '.', '\\n', '')/",
532 \ ['j', 'j']],
533 \ ['kKk', "s/K/\\=substitute(submatch(0), '.', '\\r', '')/",
534 \ ['k', 'k']],
535 \ ['lLl', "s/L/\\=substitute(submatch(0), '.', '\\n', '')/",
536 \ ['l', 'l']],
537 \ ]
538 call Run_SubCmd_Tests(tests)
539endfunc
540
541func Test_sub_cmd_5()
542 set magic&
543 set cpo&
544
545 " List entry format: [input, cmd, output]
546 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']],
547 \ ['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']]"]],
548 \ ]
549 call Run_SubCmd_Tests(tests)
550endfunc
551
552" Test for *:s%* on :substitute.
553func Test_sub_cmd_6()
554 set magic&
555 set cpo+=/
556
557 " List entry format: [input, cmd, output]
558 let tests = [ ['A', 's/A/a/', ['a']],
559 \ ['B', 's/B/%/', ['a']],
560 \ ]
561 call Run_SubCmd_Tests(tests)
562
563 set cpo-=/
564 let tests = [ ['C', 's/C/c/', ['c']],
565 \ ['D', 's/D/%/', ['%']],
566 \ ]
567 call Run_SubCmd_Tests(tests)
568
569 set cpo&
570endfunc
571
572" Test for :s replacing \n with line break.
573func Test_sub_cmd_7()
574 set magic&
575 set cpo&
576
577 " List entry format: [input, cmd, output]
578 let tests = [ ["A\<C-V>\<C-M>A", 's/A./\=submatch(0)/', ['A', 'A']],
579 \ ["B\<C-V>\<C-J>B", 's/B./\=submatch(0)/', ['B', 'B']],
580 \ ["C\<C-V>\<C-J>C", 's/C./\=strtrans(string(submatch(0, 1)))/', [strtrans("['C\<C-J>']C")]],
581 \ ["D\<C-V>\<C-J>\nD", 's/D.\nD/\=strtrans(string(submatch(0, 1)))/', [strtrans("['D\<C-J>', 'D']")]],
582 \ ["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']")]],
583 \ ]
584 call Run_SubCmd_Tests(tests)
585
586 exe "normal oQ\nQ\<Esc>k"
587 call assert_fails('s/Q[^\n]Q/\=submatch(0)."foobar"/', 'E486')
588 enew!
589endfunc
590
591func TitleString()
592 let check = 'foo' =~ 'bar'
593 return ""
594endfunc
595
596func Test_sub_cmd_8()
597 set titlestring=%{TitleString()}
598
599 enew!
600 call append(0, ['', 'test_one', 'test_two'])
601 call cursor(1,1)
602 /^test_one/s/.*/\="foo\nbar"/
603 call assert_equal('foo', getline(2))
604 call assert_equal('bar', getline(3))
605 call feedkeys(':/^test_two/s/.*/\="foo\nbar"/c', "t")
606 call feedkeys("\<CR>y", "xt")
607 call assert_equal('foo', getline(4))
608 call assert_equal('bar', getline(5))
609
610 enew!
611 set titlestring&
612endfunc
Bram Moolenaar0e97b942019-03-27 22:53:53 +0100613
614func Test_nocatch_sub_failure_handling()
615 " normal error results in all replacements
616 func! Foo()
617 foobar
618 endfunc
619 new
620 call setline(1, ['1 aaa', '2 aaa', '3 aaa'])
621 %s/aaa/\=Foo()/g
622 call assert_equal(['1 0', '2 0', '3 0'], getline(1, 3))
623
624 " Trow without try-catch causes abort after the first line.
625 " We cannot test this, since it would stop executing the test script.
626
627 " try/catch does not result in any changes
628 func! Foo()
629 throw 'error'
630 endfunc
631 call setline(1, ['1 aaa', '2 aaa', '3 aaa'])
632 let error_caught = 0
633 try
634 %s/aaa/\=Foo()/g
635 catch
636 let error_caught = 1
637 endtry
638 call assert_equal(1, error_caught)
639 call assert_equal(['1 aaa', '2 aaa', '3 aaa'], getline(1, 3))
640
641 bwipe!
642endfunc
Bram Moolenaarc6b37db2019-04-27 18:00:34 +0200643
644" Test ":s/pat/sub/" with different ~s in sub.
645func Test_replace_with_tilde()
646 new
647 " Set the last replace string to empty
648 s/^$//
649 call append(0, ['- Bug in "vPPPP" on this text:'])
650 normal gg
651 s/u/~u~/
652 call assert_equal('- Bug in "vPPPP" on this text:', getline(1))
653 s/i/~u~/
654 call assert_equal('- Bug uuun "vPPPP" on this text:', getline(1))
655 s/o/~~~/
656 call assert_equal('- Bug uuun "vPPPP" uuuuuuuuun this text:', getline(1))
657 close!
658endfunc
659
660func Test_replace_keeppatterns()
661 new
662 a
663foobar
664
665substitute foo asdf
666
667one two
668.
669
670 normal gg
671 /^substitute
672 s/foo/bar/
673 call assert_equal('foo', @/)
674 call assert_equal('substitute bar asdf', getline('.'))
675
676 /^substitute
677 keeppatterns s/asdf/xyz/
678 call assert_equal('^substitute', @/)
679 call assert_equal('substitute bar xyz', getline('.'))
680
681 exe "normal /bar /e\<CR>"
682 call assert_equal(15, col('.'))
683 normal -
684 keeppatterns /xyz
685 call assert_equal('bar ', @/)
686 call assert_equal('substitute bar xyz', getline('.'))
687 exe "normal 0dn"
688 call assert_equal('xyz', getline('.'))
689
690 close!
691endfunc