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