blob: 46ea95513192862229443c821e9945590de99622 [file] [log] [blame]
Yegappan Lakshmanan5e877ba2022-03-25 21:19:26 +00001" Tests for the substitute (:s) command
Bram Moolenaarcd055da2016-09-02 19:50:48 +02002
Bram Moolenaar9f6277b2020-02-11 22:04:02 +01003source shared.vim
Bram Moolenaar7a2217b2021-06-06 12:33:49 +02004source check.vim
Bram Moolenaar9f6277b2020-02-11 22:04:02 +01005
Bram Moolenaar1e115362019-01-09 23:01:02 +01006func Test_multiline_subst()
Bram Moolenaarcd055da2016-09-02 19:50:48 +02007 enew!
8 call append(0, ["1 aa",
9 \ "bb",
10 \ "cc",
11 \ "2 dd",
12 \ "ee",
13 \ "3 ef",
14 \ "gh",
15 \ "4 ij",
16 \ "5 a8",
17 \ "8b c9",
18 \ "9d",
19 \ "6 e7",
20 \ "77f",
21 \ "xxxxx"])
22
23 1
24 " test if replacing a line break works with a back reference
25 /^1/,/^2/s/\n\(.\)/ \1/
26 " test if inserting a line break works with a back reference
27 /^3/,/^4/s/\(.\)$/\r\1/
28 " test if replacing a line break with another line break works
29 /^5/,/^6/s/\(\_d\{3}\)/x\1x/
30 call assert_equal('1 aa bb cc 2 dd ee', getline(1))
31 call assert_equal('3 e', getline(2))
32 call assert_equal('f', getline(3))
33 call assert_equal('g', getline(4))
34 call assert_equal('h', getline(5))
35 call assert_equal('4 i', getline(6))
36 call assert_equal('j', getline(7))
37 call assert_equal('5 ax8', getline(8))
38 call assert_equal('8xb cx9', getline(9))
39 call assert_equal('9xd', getline(10))
40 call assert_equal('6 ex7', getline(11))
41 call assert_equal('7x7f', getline(12))
42 call assert_equal('xxxxx', getline(13))
43 enew!
Bram Moolenaar1e115362019-01-09 23:01:02 +010044endfunc
Bram Moolenaar8c50d502017-02-17 18:28:24 +010045
Bram Moolenaar1e115362019-01-09 23:01:02 +010046func Test_substitute_variants()
Bram Moolenaar8c50d502017-02-17 18:28:24 +010047 " Validate that all the 2-/3-letter variants which embed the flags into the
48 " command name actually work.
49 enew!
50 let ln = 'Testing string'
51 let variants = [
52 \ { 'cmd': ':s/Test/test/c', 'exp': 'testing string', 'prompt': 'y' },
53 \ { 'cmd': ':s/foo/bar/ce', 'exp': ln },
54 \ { 'cmd': ':s/t/r/cg', 'exp': 'Tesring srring', 'prompt': 'a' },
55 \ { 'cmd': ':s/t/r/ci', 'exp': 'resting string', 'prompt': 'y' },
56 \ { 'cmd': ':s/t/r/cI', 'exp': 'Tesring string', 'prompt': 'y' },
Bram Moolenaarea3db912020-02-02 15:32:13 +010057 \ { 'cmd': ':s/t/r/c', 'exp': 'Testing string', 'prompt': 'n' },
Bram Moolenaar8c50d502017-02-17 18:28:24 +010058 \ { 'cmd': ':s/t/r/cn', 'exp': ln },
59 \ { 'cmd': ':s/t/r/cp', 'exp': 'Tesring string', 'prompt': 'y' },
60 \ { 'cmd': ':s/t/r/cl', 'exp': 'Tesring string', 'prompt': 'y' },
61 \ { 'cmd': ':s/t/r/gc', 'exp': 'Tesring srring', 'prompt': 'a' },
Bram Moolenaarea3db912020-02-02 15:32:13 +010062 \ { 'cmd': ':s/i/I/gc', 'exp': 'TestIng string', 'prompt': 'l' },
Bram Moolenaar8c50d502017-02-17 18:28:24 +010063 \ { 'cmd': ':s/foo/bar/ge', 'exp': ln },
64 \ { 'cmd': ':s/t/r/g', 'exp': 'Tesring srring' },
65 \ { 'cmd': ':s/t/r/gi', 'exp': 'resring srring' },
66 \ { 'cmd': ':s/t/r/gI', 'exp': 'Tesring srring' },
67 \ { 'cmd': ':s/t/r/gn', 'exp': ln },
68 \ { 'cmd': ':s/t/r/gp', 'exp': 'Tesring srring' },
69 \ { 'cmd': ':s/t/r/gl', 'exp': 'Tesring srring' },
70 \ { 'cmd': ':s//r/gr', 'exp': 'Testr strr' },
71 \ { 'cmd': ':s/t/r/ic', 'exp': 'resting string', 'prompt': 'y' },
72 \ { 'cmd': ':s/foo/bar/ie', 'exp': ln },
73 \ { 'cmd': ':s/t/r/i', 'exp': 'resting string' },
74 \ { 'cmd': ':s/t/r/iI', 'exp': 'Tesring string' },
75 \ { 'cmd': ':s/t/r/in', 'exp': ln },
76 \ { 'cmd': ':s/t/r/ip', 'exp': 'resting string' },
77 \ { 'cmd': ':s//r/ir', 'exp': 'Testr string' },
78 \ { 'cmd': ':s/t/r/Ic', 'exp': 'Tesring string', 'prompt': 'y' },
79 \ { 'cmd': ':s/foo/bar/Ie', 'exp': ln },
80 \ { 'cmd': ':s/t/r/Ig', 'exp': 'Tesring srring' },
81 \ { 'cmd': ':s/t/r/Ii', 'exp': 'resting string' },
82 \ { 'cmd': ':s/t/r/I', 'exp': 'Tesring string' },
83 \ { 'cmd': ':s/t/r/Ip', 'exp': 'Tesring string' },
84 \ { 'cmd': ':s/t/r/Il', 'exp': 'Tesring string' },
85 \ { 'cmd': ':s//r/Ir', 'exp': 'Testr string' },
86 \ { 'cmd': ':s//r/rc', 'exp': 'Testr string', 'prompt': 'y' },
87 \ { 'cmd': ':s//r/rg', 'exp': 'Testr strr' },
88 \ { 'cmd': ':s//r/ri', 'exp': 'Testr string' },
89 \ { 'cmd': ':s//r/rI', 'exp': 'Testr string' },
90 \ { 'cmd': ':s//r/rn', 'exp': 'Testing string' },
91 \ { 'cmd': ':s//r/rp', 'exp': 'Testr string' },
92 \ { 'cmd': ':s//r/rl', 'exp': 'Testr string' },
93 \ { 'cmd': ':s//r/r', 'exp': 'Testr string' },
Bram Moolenaarea3db912020-02-02 15:32:13 +010094 \ { 'cmd': ':s/i/I/gc', 'exp': 'Testing string', 'prompt': 'q' },
Bram Moolenaar8c50d502017-02-17 18:28:24 +010095 \]
96
97 for var in variants
98 for run in [1, 2]
99 let cmd = var.cmd
100 if run == 2 && cmd =~ "/.*/.*/."
101 " Change :s/from/to/{flags} to :s{flags}
102 let cmd = substitute(cmd, '/.*/', '', '')
103 endif
104 call setline(1, [ln])
105 let msg = printf('using "%s"', cmd)
106 let @/='ing'
107 let v:errmsg = ''
108 call feedkeys(cmd . "\<CR>" . get(var, 'prompt', ''), 'ntx')
109 " No error should exist (matters for testing e flag)
110 call assert_equal('', v:errmsg, msg)
111 call assert_equal(var.exp, getline('.'), msg)
112 endfor
113 endfor
Bram Moolenaar1e115362019-01-09 23:01:02 +0100114endfunc
Bram Moolenaarba748c82017-02-26 14:00:07 +0100115
Bram Moolenaar94747162019-02-10 21:55:26 +0100116" Test the l, p, # flags.
117func Test_substitute_flags_lp()
118 new
119 call setline(1, "abc\tdef\<C-h>ghi")
120
121 let a = execute('s/a/a/p')
122 call assert_equal("\nabc def^Hghi", a)
123
124 let a = execute('s/a/a/l')
125 call assert_equal("\nabc^Idef^Hghi$", a)
126
127 let a = execute('s/a/a/#')
128 call assert_equal("\n 1 abc def^Hghi", a)
129
130 let a = execute('s/a/a/p#')
131 call assert_equal("\n 1 abc def^Hghi", a)
132
133 let a = execute('s/a/a/l#')
134 call assert_equal("\n 1 abc^Idef^Hghi$", a)
135
136 let a = execute('s/a/a/')
137 call assert_equal("", a)
138
139 bwipe!
140endfunc
141
Bram Moolenaarba748c82017-02-26 14:00:07 +0100142func Test_substitute_repeat()
143 " This caused an invalid memory access.
144 split Xfile
145 s/^/x
146 call feedkeys("Qsc\<CR>y", 'tx')
147 bwipe!
148endfunc
Bram Moolenaar1a333bc2017-08-30 20:21:58 +0200149
Bram Moolenaard77aa4d2019-02-10 22:50:14 +0100150" Test %s/\n// which is implemented as a special case to use a
151" more efficient join rather than doing a regular substitution.
152func Test_substitute_join()
153 new
154
155 call setline(1, ["foo\tbar", "bar\<C-H>foo"])
156 let a = execute('%s/\n//')
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//g')
163 call assert_equal("", 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//p')
169 call assert_equal("\nfoo barbar^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//l')
175 call assert_equal("\nfoo^Ibarbar^Hfoo$", a)
176 call assert_equal(["foo\tbarbar\<C-H>foo"], getline(1, '$'))
177 call assert_equal('\n', histget("search", -1))
178
179 call setline(1, ["foo\tbar", "bar\<C-H>foo"])
180 let a = execute('%s/\n//#')
181 call assert_equal("\n 1 foo barbar^Hfoo", a)
182 call assert_equal(["foo\tbarbar\<C-H>foo"], getline(1, '$'))
183 call assert_equal('\n', histget("search", -1))
184
Bram Moolenaarea3db912020-02-02 15:32:13 +0100185 call setline(1, ['foo', 'bar', 'baz', 'qux'])
186 call execute('1,2s/\n//')
187 call assert_equal(['foobarbaz', 'qux'], getline(1, '$'))
188
Bram Moolenaard77aa4d2019-02-10 22:50:14 +0100189 bwipe!
190endfunc
191
192func Test_substitute_count()
193 new
194 call setline(1, ['foo foo', 'foo foo', 'foo foo', 'foo foo', 'foo foo'])
195 2
196
197 s/foo/bar/3
198 call assert_equal(['foo foo', 'bar foo', 'bar foo', 'bar foo', 'foo foo'],
199 \ getline(1, '$'))
200
201 call assert_fails('s/foo/bar/0', 'E939:')
202
Bram Moolenaarea3db912020-02-02 15:32:13 +0100203 call setline(1, ['foo foo', 'foo foo', 'foo foo', 'foo foo', 'foo foo'])
204 2,4s/foo/bar/ 10
205 call assert_equal(['foo foo', 'foo foo', 'foo foo', 'bar foo', 'bar foo'],
206 \ getline(1, '$'))
207
Bram Moolenaard77aa4d2019-02-10 22:50:14 +0100208 bwipe!
209endfunc
210
211" Test substitute 'n' flag (report number of matches, do not substitute).
212func Test_substitute_flag_n()
213 new
214 let lines = ['foo foo', 'foo foo', 'foo foo', 'foo foo', 'foo foo']
215 call setline(1, lines)
216
217 call assert_equal("\n3 matches on 3 lines", execute('2,4s/foo/bar/n'))
218 call assert_equal("\n6 matches on 3 lines", execute('2,4s/foo/bar/gn'))
219
220 " c flag (confirm) should be ignored when using n flag.
221 call assert_equal("\n3 matches on 3 lines", execute('2,4s/foo/bar/nc'))
222
223 " No substitution should have been done.
224 call assert_equal(lines, getline(1, '$'))
225
Bram Moolenaarea3db912020-02-02 15:32:13 +0100226 %delete _
227 call setline(1, ['A', 'Bar', 'Baz'])
228 call assert_equal("\n1 match on 1 line", execute('s/\nB\@=//gn'))
229
Bram Moolenaard77aa4d2019-02-10 22:50:14 +0100230 bwipe!
231endfunc
232
233func Test_substitute_errors()
234 new
235 call setline(1, 'foobar')
236
237 call assert_fails('s/FOO/bar/', 'E486:')
238 call assert_fails('s/foo/bar/@', 'E488:')
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200239 call assert_fails('s/\(/bar/', 'E54:')
Bram Moolenaar5d98dc22020-01-29 21:57:34 +0100240 call assert_fails('s afooabara', 'E146:')
241 call assert_fails('s\\a', 'E10:')
Bram Moolenaard77aa4d2019-02-10 22:50:14 +0100242
243 setl nomodifiable
244 call assert_fails('s/foo/bar/', 'E21:')
245
Bram Moolenaar0e05de42020-03-25 22:23:46 +0100246 call assert_fails("let s=substitute([], 'a', 'A', 'g')", 'E730:')
247 call assert_fails("let s=substitute('abcda', [], 'A', 'g')", 'E730:')
248 call assert_fails("let s=substitute('abcda', 'a', [], 'g')", 'E730:')
249 call assert_fails("let s=substitute('abcda', 'a', 'A', [])", 'E730:')
Bram Moolenaar531be472020-09-23 22:38:05 +0200250 call assert_fails("let s=substitute('abc', '\\%(', 'A', 'g')", 'E53:')
Bram Moolenaar0e05de42020-03-25 22:23:46 +0100251
Bram Moolenaard77aa4d2019-02-10 22:50:14 +0100252 bwipe!
253endfunc
254
Bram Moolenaar1a333bc2017-08-30 20:21:58 +0200255" Test for *sub-replace-special* and *sub-replace-expression* on substitute().
256func Test_sub_replace_1()
257 " Run the tests with 'magic' on
258 set magic
259 set cpo&
260 call assert_equal('AA', substitute('A', 'A', '&&', ''))
261 call assert_equal('&', substitute('B', 'B', '\&', ''))
262 call assert_equal('C123456789987654321', substitute('C123456789', 'C\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)', '\0\9\8\7\6\5\4\3\2\1', ''))
263 call assert_equal('d', substitute('D', 'D', 'd', ''))
264 call assert_equal('~', substitute('E', 'E', '~', ''))
265 call assert_equal('~', substitute('F', 'F', '\~', ''))
266 call assert_equal('Gg', substitute('G', 'G', '\ugg', ''))
267 call assert_equal('Hh', substitute('H', 'H', '\Uh\Eh', ''))
268 call assert_equal('iI', substitute('I', 'I', '\lII', ''))
269 call assert_equal('jJ', substitute('J', 'J', '\LJ\EJ', ''))
270 call assert_equal('Kk', substitute('K', 'K', '\Uk\ek', ''))
271 call assert_equal("l\<C-V>\<C-M>l",
272 \ substitute('lLl', 'L', "\<C-V>\<C-M>", ''))
273 call assert_equal("m\<C-M>m", substitute('mMm', 'M', '\r', ''))
274 call assert_equal("n\<C-V>\<C-M>n",
275 \ substitute('nNn', 'N', "\\\<C-V>\<C-M>", ''))
276 call assert_equal("o\no", substitute('oOo', 'O', '\n', ''))
277 call assert_equal("p\<C-H>p", substitute('pPp', 'P', '\b', ''))
278 call assert_equal("q\tq", substitute('qQq', 'Q', '\t', ''))
279 call assert_equal('r\r', substitute('rRr', 'R', '\\', ''))
280 call assert_equal('scs', substitute('sSs', 'S', '\c', ''))
281 call assert_equal("u\nu", substitute('uUu', 'U', "\n", ''))
282 call assert_equal("v\<C-H>v", substitute('vVv', 'V', "\b", ''))
283 call assert_equal("w\\w", substitute('wWw', 'W', "\\", ''))
284 call assert_equal("x\<C-M>x", substitute('xXx', 'X', "\r", ''))
285 call assert_equal("YyyY", substitute('Y', 'Y', '\L\uyYy\l\EY', ''))
286 call assert_equal("zZZz", substitute('Z', 'Z', '\U\lZzZ\u\Ez', ''))
Bram Moolenaar004a6782020-04-11 17:09:31 +0200287 " \v or \V after $
288 call assert_equal('abxx', substitute('abcd', 'xy$\v|cd$', 'xx', ''))
289 call assert_equal('abxx', substitute('abcd', 'xy$\V\|cd\$', 'xx', ''))
Bram Moolenaar1a333bc2017-08-30 20:21:58 +0200290endfunc
291
292func Test_sub_replace_2()
293 " Run the tests with 'magic' off
294 set nomagic
295 set cpo&
296 call assert_equal('AA', substitute('A', 'A', '&&', ''))
297 call assert_equal('&', substitute('B', 'B', '\&', ''))
298 call assert_equal('C123456789987654321', substitute('C123456789', 'C\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)', '\0\9\8\7\6\5\4\3\2\1', ''))
299 call assert_equal('d', substitute('D', 'D', 'd', ''))
300 call assert_equal('~', substitute('E', 'E', '~', ''))
301 call assert_equal('~', substitute('F', 'F', '\~', ''))
302 call assert_equal('Gg', substitute('G', 'G', '\ugg', ''))
303 call assert_equal('Hh', substitute('H', 'H', '\Uh\Eh', ''))
304 call assert_equal('iI', substitute('I', 'I', '\lII', ''))
305 call assert_equal('jJ', substitute('J', 'J', '\LJ\EJ', ''))
306 call assert_equal('Kk', substitute('K', 'K', '\Uk\ek', ''))
307 call assert_equal("l\<C-V>\<C-M>l",
308 \ substitute('lLl', 'L', "\<C-V>\<C-M>", ''))
309 call assert_equal("m\<C-M>m", substitute('mMm', 'M', '\r', ''))
310 call assert_equal("n\<C-V>\<C-M>n",
311 \ substitute('nNn', 'N', "\\\<C-V>\<C-M>", ''))
312 call assert_equal("o\no", substitute('oOo', 'O', '\n', ''))
313 call assert_equal("p\<C-H>p", substitute('pPp', 'P', '\b', ''))
314 call assert_equal("q\tq", substitute('qQq', 'Q', '\t', ''))
315 call assert_equal('r\r', substitute('rRr', 'R', '\\', ''))
316 call assert_equal('scs', substitute('sSs', 'S', '\c', ''))
317 call assert_equal("t\<C-M>t", substitute('tTt', 'T', "\r", ''))
318 call assert_equal("u\nu", substitute('uUu', 'U', "\n", ''))
319 call assert_equal("v\<C-H>v", substitute('vVv', 'V', "\b", ''))
320 call assert_equal('w\w', substitute('wWw', 'W', "\\", ''))
321 call assert_equal('XxxX', substitute('X', 'X', '\L\uxXx\l\EX', ''))
322 call assert_equal('yYYy', substitute('Y', 'Y', '\U\lYyY\u\Ey', ''))
323endfunc
324
325func Test_sub_replace_3()
326 set magic&
327 set cpo&
328 call assert_equal('a\a', substitute('aAa', 'A', '\="\\"', ''))
329 call assert_equal('b\\b', substitute('bBb', 'B', '\="\\\\"', ''))
330 call assert_equal("c\rc", substitute('cCc', 'C', "\\=\"\r\"", ''))
331 call assert_equal("d\\\rd", substitute('dDd', 'D', "\\=\"\\\\\r\"", ''))
332 call assert_equal("e\\\\\re", substitute('eEe', 'E', "\\=\"\\\\\\\\\r\"", ''))
333 call assert_equal('f\rf', substitute('fFf', 'F', '\="\\r"', ''))
334 call assert_equal('j\nj', substitute('jJj', 'J', '\="\\n"', ''))
335 call assert_equal("k\<C-M>k", substitute('kKk', 'K', '\="\r"', ''))
336 call assert_equal("l\nl", substitute('lLl', 'L', '\="\n"', ''))
337endfunc
338
339" Test for submatch() on substitute().
340func Test_sub_replace_4()
341 set magic&
342 set cpo&
343 call assert_equal('a\a', substitute('aAa', 'A',
344 \ '\=substitute(submatch(0), ".", "\\", "")', ''))
345 call assert_equal('b\b', substitute('bBb', 'B',
346 \ '\=substitute(submatch(0), ".", "\\\\", "")', ''))
347 call assert_equal("c\<C-V>\<C-M>c", substitute('cCc', 'C', '\=substitute(submatch(0), ".", "\<C-V>\<C-M>", "")', ''))
348 call assert_equal("d\<C-V>\<C-M>d", substitute('dDd', 'D', '\=substitute(submatch(0), ".", "\\\<C-V>\<C-M>", "")', ''))
349 call assert_equal("e\\\<C-V>\<C-M>e", substitute('eEe', 'E', '\=substitute(submatch(0), ".", "\\\\\<C-V>\<C-M>", "")', ''))
350 call assert_equal("f\<C-M>f", substitute('fFf', 'F', '\=substitute(submatch(0), ".", "\\r", "")', ''))
351 call assert_equal("j\nj", substitute('jJj', 'J', '\=substitute(submatch(0), ".", "\\n", "")', ''))
352 call assert_equal("k\rk", substitute('kKk', 'K', '\=substitute(submatch(0), ".", "\r", "")', ''))
353 call assert_equal("l\nl", substitute('lLl', 'L', '\=substitute(submatch(0), ".", "\n", "")', ''))
354endfunc
355
356func Test_sub_replace_5()
357 set magic&
358 set cpo&
359 call assert_equal('A123456789987654321', substitute('A123456789',
360 \ 'A\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)',
361 \ '\=submatch(0) . submatch(9) . submatch(8) . ' .
362 \ 'submatch(7) . submatch(6) . submatch(5) . ' .
363 \ 'submatch(4) . submatch(3) . submatch(2) . submatch(1)',
364 \ ''))
365 call assert_equal("[['A123456789'], ['9'], ['8'], ['7'], ['6'], " .
366 \ "['5'], ['4'], ['3'], ['2'], ['1']]",
367 \ substitute('A123456789',
368 \ 'A\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)',
369 \ '\=string([submatch(0, 1), submatch(9, 1), ' .
Bram Moolenaarf6ed61e2019-09-07 19:05:09 +0200370 \ 'submatch(8, 1), 7->submatch(1), submatch(6, 1), ' .
Bram Moolenaar1a333bc2017-08-30 20:21:58 +0200371 \ 'submatch(5, 1), submatch(4, 1), submatch(3, 1), ' .
372 \ 'submatch(2, 1), submatch(1, 1)])',
373 \ ''))
374endfunc
375
376func Test_sub_replace_6()
377 set magic&
378 set cpo+=/
379 call assert_equal('a', substitute('A', 'A', 'a', ''))
380 call assert_equal('%', substitute('B', 'B', '%', ''))
381 set cpo-=/
382 call assert_equal('c', substitute('C', 'C', 'c', ''))
383 call assert_equal('%', substitute('D', 'D', '%', ''))
384endfunc
385
386func Test_sub_replace_7()
387 set magic&
388 set cpo&
389 call assert_equal('AA', substitute('AA', 'A.', '\=submatch(0)', ''))
390 call assert_equal("B\nB", substitute("B\nB", 'B.', '\=submatch(0)', ''))
391 call assert_equal("['B\n']B", substitute("B\nB", 'B.', '\=string(submatch(0, 1))', ''))
392 call assert_equal('-abab', substitute('-bb', '\zeb', 'a', 'g'))
393 call assert_equal('c-cbcbc', substitute('-bb', '\ze', 'c', 'g'))
394endfunc
395
396" Test for *:s%* on :substitute.
397func Test_sub_replace_8()
398 new
399 set magic&
400 set cpo&
401 $put =',,X'
402 s/\(^\|,\)\ze\(,\|X\)/\1N/g
403 call assert_equal('N,,NX', getline("$"))
404 $put =',,Y'
405 let cmd = ':s/\(^\|,\)\ze\(,\|Y\)/\1N/gc'
406 call feedkeys(cmd . "\<CR>a", "xt")
407 call assert_equal('N,,NY', getline("$"))
408 :$put =',,Z'
409 let cmd = ':s/\(^\|,\)\ze\(,\|Z\)/\1N/gc'
410 call feedkeys(cmd . "\<CR>yy", "xt")
411 call assert_equal('N,,NZ', getline("$"))
412 enew! | close
413endfunc
414
415func Test_sub_replace_9()
416 new
417 set magic&
418 set cpo&
419 $put ='xxx'
420 call feedkeys(":s/x/X/gc\<CR>yyq", "xt")
421 call assert_equal('XXx', getline("$"))
422 enew! | close
423endfunc
424
425func Test_sub_replace_10()
426 set magic&
427 set cpo&
428 call assert_equal('a1a2a3a', substitute('123', '\zs', 'a', 'g'))
429 call assert_equal('aaa', substitute('123', '\zs.', 'a', 'g'))
430 call assert_equal('1a2a3a', substitute('123', '.\zs', 'a', 'g'))
431 call assert_equal('a1a2a3a', substitute('123', '\ze', 'a', 'g'))
432 call assert_equal('a1a2a3', substitute('123', '\ze.', 'a', 'g'))
433 call assert_equal('aaa', substitute('123', '.\ze', 'a', 'g'))
434 call assert_equal('aa2a3a', substitute('123', '1\|\ze', 'a', 'g'))
435 call assert_equal('1aaa', substitute('123', '1\zs\|[23]', 'a', 'g'))
436endfunc
Bram Moolenaar15993ce2017-10-26 20:21:44 +0200437
Bram Moolenaarb0745b22019-11-09 22:28:11 +0100438func SubReplacer(text, submatches)
439 return a:text .. a:submatches[0] .. a:text
440endfunc
Bram Moolenaar4c054e92019-11-10 00:13:50 +0100441func SubReplacer20(t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12, t13, t14, t15, t16, t17, t18, t19, submatches)
442 return a:t3 .. a:submatches[0] .. a:t11
443endfunc
Bram Moolenaarb0745b22019-11-09 22:28:11 +0100444
445func Test_substitute_partial()
446 call assert_equal('1foo2foo3', substitute('123', '2', function('SubReplacer', ['foo']), 'g'))
Bram Moolenaar4c054e92019-11-10 00:13:50 +0100447
448 " 19 arguments plus one is just OK
449 let Replacer = function('SubReplacer20', repeat(['foo'], 19))
450 call assert_equal('1foo2foo3', substitute('123', '2', Replacer, 'g'))
451
452 " 20 arguments plus one is too many
453 let Replacer = function('SubReplacer20', repeat(['foo'], 20))
Bram Moolenaare2e40752020-09-04 21:18:46 +0200454 call assert_fails("call substitute('123', '2', Replacer, 'g')", 'E118:')
Bram Moolenaarb0745b22019-11-09 22:28:11 +0100455endfunc
456
Bram Moolenaar7a2217b2021-06-06 12:33:49 +0200457func Test_substitute_float()
458 CheckFeature float
459
460 call assert_equal('number 1.23', substitute('number ', '$', { -> 1.23 }, ''))
461 vim9 assert_equal('number 1.23', substitute('number ', '$', () => 1.23, ''))
462endfunc
463
Bram Moolenaar15993ce2017-10-26 20:21:44 +0200464" Tests for *sub-replace-special* and *sub-replace-expression* on :substitute.
465
466" Execute a list of :substitute command tests
467func Run_SubCmd_Tests(tests)
468 enew!
469 for t in a:tests
470 let start = line('.') + 1
471 let end = start + len(t[2]) - 1
472 exe "normal o" . t[0]
473 call cursor(start, 1)
474 exe t[1]
475 call assert_equal(t[2], getline(start, end), t[1])
476 endfor
477 enew!
478endfunc
479
480func Test_sub_cmd_1()
481 set magic
482 set cpo&
483
484 " List entry format: [input, cmd, output]
485 let tests = [['A', 's/A/&&/', ['AA']],
486 \ ['B', 's/B/\&/', ['&']],
487 \ ['C123456789', 's/C\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)/\0\9\8\7\6\5\4\3\2\1/', ['C123456789987654321']],
488 \ ['D', 's/D/d/', ['d']],
489 \ ['E', 's/E/~/', ['d']],
490 \ ['F', 's/F/\~/', ['~']],
491 \ ['G', 's/G/\ugg/', ['Gg']],
492 \ ['H', 's/H/\Uh\Eh/', ['Hh']],
493 \ ['I', 's/I/\lII/', ['iI']],
494 \ ['J', 's/J/\LJ\EJ/', ['jJ']],
495 \ ['K', 's/K/\Uk\ek/', ['Kk']],
496 \ ['lLl', "s/L/\<C-V>\<C-M>/", ["l\<C-V>", 'l']],
497 \ ['mMm', 's/M/\r/', ['m', 'm']],
498 \ ['nNn', "s/N/\\\<C-V>\<C-M>/", ["n\<C-V>", 'n']],
499 \ ['oOo', 's/O/\n/', ["o\no"]],
500 \ ['pPp', 's/P/\b/', ["p\<C-H>p"]],
501 \ ['qQq', 's/Q/\t/', ["q\tq"]],
502 \ ['rRr', 's/R/\\/', ['r\r']],
503 \ ['sSs', 's/S/\c/', ['scs']],
504 \ ['tTt', "s/T/\<C-V>\<C-J>/", ["t\<C-V>\<C-J>t"]],
505 \ ['U', 's/U/\L\uuUu\l\EU/', ['UuuU']],
zeertzjq3269efd2022-06-12 11:13:05 +0100506 \ ['V', 's/V/\U\lVvV\u\Ev/', ['vVVv']],
507 \ ['\', 's/\\/\\\\/', ['\\']]
Bram Moolenaar15993ce2017-10-26 20:21:44 +0200508 \ ]
509 call Run_SubCmd_Tests(tests)
510endfunc
511
512func Test_sub_cmd_2()
513 set nomagic
514 set cpo&
515
516 " List entry format: [input, cmd, output]
517 let tests = [['A', 's/A/&&/', ['&&']],
518 \ ['B', 's/B/\&/', ['B']],
519 \ ['C123456789', 's/\mC\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)/\0\9\8\7\6\5\4\3\2\1/', ['C123456789987654321']],
520 \ ['D', 's/D/d/', ['d']],
521 \ ['E', 's/E/~/', ['~']],
522 \ ['F', 's/F/\~/', ['~']],
523 \ ['G', 's/G/\ugg/', ['Gg']],
524 \ ['H', 's/H/\Uh\Eh/', ['Hh']],
525 \ ['I', 's/I/\lII/', ['iI']],
526 \ ['J', 's/J/\LJ\EJ/', ['jJ']],
527 \ ['K', 's/K/\Uk\ek/', ['Kk']],
528 \ ['lLl', "s/L/\<C-V>\<C-M>/", ["l\<C-V>", 'l']],
529 \ ['mMm', 's/M/\r/', ['m', 'm']],
530 \ ['nNn', "s/N/\\\<C-V>\<C-M>/", ["n\<C-V>", 'n']],
531 \ ['oOo', 's/O/\n/', ["o\no"]],
532 \ ['pPp', 's/P/\b/', ["p\<C-H>p"]],
533 \ ['qQq', 's/Q/\t/', ["q\tq"]],
534 \ ['rRr', 's/R/\\/', ['r\r']],
535 \ ['sSs', 's/S/\c/', ['scs']],
536 \ ['tTt', "s/T/\<C-V>\<C-J>/", ["t\<C-V>\<C-J>t"]],
537 \ ['U', 's/U/\L\uuUu\l\EU/', ['UuuU']],
zeertzjq3269efd2022-06-12 11:13:05 +0100538 \ ['V', 's/V/\U\lVvV\u\Ev/', ['vVVv']],
539 \ ['\', 's/\\/\\\\/', ['\\']]
Bram Moolenaar15993ce2017-10-26 20:21:44 +0200540 \ ]
541 call Run_SubCmd_Tests(tests)
542endfunc
543
544func Test_sub_cmd_3()
545 set nomagic
546 set cpo&
547
548 " List entry format: [input, cmd, output]
549 let tests = [['aAa', "s/A/\\='\\'/", ['a\a']],
550 \ ['bBb', "s/B/\\='\\\\'/", ['b\\b']],
551 \ ['cCc', "s/C/\\='\<C-V>\<C-M>'/", ["c\<C-V>", 'c']],
552 \ ['dDd', "s/D/\\='\\\<C-V>\<C-M>'/", ["d\\\<C-V>", 'd']],
553 \ ['eEe', "s/E/\\='\\\\\<C-V>\<C-M>'/", ["e\\\\\<C-V>", 'e']],
554 \ ['fFf', "s/F/\\='\r'/", ['f', 'f']],
555 \ ['gGg', "s/G/\\='\<C-V>\<C-J>'/", ["g\<C-V>", 'g']],
556 \ ['hHh', "s/H/\\='\\\<C-V>\<C-J>'/", ["h\\\<C-V>", 'h']],
557 \ ['iIi', "s/I/\\='\\\\\<C-V>\<C-J>'/", ["i\\\\\<C-V>", 'i']],
558 \ ['jJj', "s/J/\\='\n'/", ['j', 'j']],
559 \ ['kKk', 's/K/\="\r"/', ['k', 'k']],
560 \ ['lLl', 's/L/\="\n"/', ['l', 'l']]
561 \ ]
562 call Run_SubCmd_Tests(tests)
563endfunc
564
Bram Moolenaarf1699962019-08-31 17:48:19 +0200565" Test for submatch() on :substitute.
Bram Moolenaar15993ce2017-10-26 20:21:44 +0200566func Test_sub_cmd_4()
567 set magic&
568 set cpo&
569
570 " List entry format: [input, cmd, output]
571 let tests = [ ['aAa', "s/A/\\=substitute(submatch(0), '.', '\\', '')/",
Bram Moolenaar1e115362019-01-09 23:01:02 +0100572 \ ['a\a']],
Bram Moolenaar15993ce2017-10-26 20:21:44 +0200573 \ ['bBb', "s/B/\\=substitute(submatch(0), '.', '\\', '')/",
Bram Moolenaar1e115362019-01-09 23:01:02 +0100574 \ ['b\b']],
Bram Moolenaar15993ce2017-10-26 20:21:44 +0200575 \ ['cCc', "s/C/\\=substitute(submatch(0), '.', '\<C-V>\<C-M>', '')/",
576 \ ["c\<C-V>", 'c']],
577 \ ['dDd', "s/D/\\=substitute(submatch(0), '.', '\\\<C-V>\<C-M>', '')/",
578 \ ["d\<C-V>", 'd']],
579 \ ['eEe', "s/E/\\=substitute(submatch(0), '.', '\\\\\<C-V>\<C-M>', '')/",
580 \ ["e\\\<C-V>", 'e']],
581 \ ['fFf', "s/F/\\=substitute(submatch(0), '.', '\\r', '')/",
582 \ ['f', 'f']],
583 \ ['gGg', 's/G/\=substitute(submatch(0), ".", "\<C-V>\<C-J>", "")/',
584 \ ["g\<C-V>", 'g']],
585 \ ['hHh', 's/H/\=substitute(submatch(0), ".", "\\\<C-V>\<C-J>", "")/',
586 \ ["h\<C-V>", 'h']],
587 \ ['iIi', 's/I/\=substitute(submatch(0), ".", "\\\\\<C-V>\<C-J>", "")/',
588 \ ["i\\\<C-V>", 'i']],
589 \ ['jJj', "s/J/\\=substitute(submatch(0), '.', '\\n', '')/",
590 \ ['j', 'j']],
591 \ ['kKk', "s/K/\\=substitute(submatch(0), '.', '\\r', '')/",
592 \ ['k', 'k']],
593 \ ['lLl', "s/L/\\=substitute(submatch(0), '.', '\\n', '')/",
594 \ ['l', 'l']],
595 \ ]
596 call Run_SubCmd_Tests(tests)
597endfunc
598
599func Test_sub_cmd_5()
600 set magic&
601 set cpo&
602
603 " List entry format: [input, cmd, output]
604 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']],
605 \ ['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']]"]],
606 \ ]
607 call Run_SubCmd_Tests(tests)
608endfunc
609
610" Test for *:s%* on :substitute.
611func Test_sub_cmd_6()
612 set magic&
613 set cpo+=/
614
615 " List entry format: [input, cmd, output]
616 let tests = [ ['A', 's/A/a/', ['a']],
617 \ ['B', 's/B/%/', ['a']],
618 \ ]
619 call Run_SubCmd_Tests(tests)
620
621 set cpo-=/
622 let tests = [ ['C', 's/C/c/', ['c']],
623 \ ['D', 's/D/%/', ['%']],
624 \ ]
625 call Run_SubCmd_Tests(tests)
626
627 set cpo&
628endfunc
629
630" Test for :s replacing \n with line break.
631func Test_sub_cmd_7()
632 set magic&
633 set cpo&
634
635 " List entry format: [input, cmd, output]
636 let tests = [ ["A\<C-V>\<C-M>A", 's/A./\=submatch(0)/', ['A', 'A']],
637 \ ["B\<C-V>\<C-J>B", 's/B./\=submatch(0)/', ['B', 'B']],
638 \ ["C\<C-V>\<C-J>C", 's/C./\=strtrans(string(submatch(0, 1)))/', [strtrans("['C\<C-J>']C")]],
639 \ ["D\<C-V>\<C-J>\nD", 's/D.\nD/\=strtrans(string(submatch(0, 1)))/', [strtrans("['D\<C-J>', 'D']")]],
640 \ ["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']")]],
641 \ ]
642 call Run_SubCmd_Tests(tests)
643
644 exe "normal oQ\nQ\<Esc>k"
Bram Moolenaare2e40752020-09-04 21:18:46 +0200645 call assert_fails('s/Q[^\n]Q/\=submatch(0)."foobar"/', 'E486:')
Bram Moolenaar15993ce2017-10-26 20:21:44 +0200646 enew!
647endfunc
648
649func TitleString()
650 let check = 'foo' =~ 'bar'
651 return ""
652endfunc
653
654func Test_sub_cmd_8()
655 set titlestring=%{TitleString()}
656
657 enew!
658 call append(0, ['', 'test_one', 'test_two'])
659 call cursor(1,1)
660 /^test_one/s/.*/\="foo\nbar"/
661 call assert_equal('foo', getline(2))
662 call assert_equal('bar', getline(3))
663 call feedkeys(':/^test_two/s/.*/\="foo\nbar"/c', "t")
664 call feedkeys("\<CR>y", "xt")
665 call assert_equal('foo', getline(4))
666 call assert_equal('bar', getline(5))
667
668 enew!
669 set titlestring&
670endfunc
Bram Moolenaar0e97b942019-03-27 22:53:53 +0100671
Bram Moolenaar80341bc2019-05-20 20:34:51 +0200672func Test_sub_cmd_9()
673 new
674 let input = ['1 aaa', '2 aaa', '3 aaa']
675 call setline(1, input)
676 func Foo()
677 return submatch(0)
678 endfunc
679 %s/aaa/\=Foo()/gn
680 call assert_equal(input, getline(1, '$'))
681 call assert_equal(1, &modifiable)
682
683 delfunc Foo
684 bw!
685endfunc
686
Bram Moolenaar0e97b942019-03-27 22:53:53 +0100687func Test_nocatch_sub_failure_handling()
688 " normal error results in all replacements
Bram Moolenaar80341bc2019-05-20 20:34:51 +0200689 func Foo()
Bram Moolenaar0e97b942019-03-27 22:53:53 +0100690 foobar
691 endfunc
692 new
693 call setline(1, ['1 aaa', '2 aaa', '3 aaa'])
zeertzjq3269efd2022-06-12 11:13:05 +0100694 " need silent! to avoid a delay when entering Insert mode
695 silent! %s/aaa/\=Foo()/g
Bram Moolenaar0e97b942019-03-27 22:53:53 +0100696 call assert_equal(['1 0', '2 0', '3 0'], getline(1, 3))
697
zeertzjq3269efd2022-06-12 11:13:05 +0100698 " Throw without try-catch causes abort after the first line.
Bram Moolenaar0e97b942019-03-27 22:53:53 +0100699 " We cannot test this, since it would stop executing the test script.
700
701 " try/catch does not result in any changes
702 func! Foo()
703 throw 'error'
704 endfunc
705 call setline(1, ['1 aaa', '2 aaa', '3 aaa'])
706 let error_caught = 0
707 try
708 %s/aaa/\=Foo()/g
709 catch
710 let error_caught = 1
711 endtry
712 call assert_equal(1, error_caught)
713 call assert_equal(['1 aaa', '2 aaa', '3 aaa'], getline(1, 3))
714
Bram Moolenaar6349e942019-05-18 13:41:22 +0200715 " Same, but using "n" flag so that "sandbox" gets set
716 call setline(1, ['1 aaa', '2 aaa', '3 aaa'])
717 let error_caught = 0
718 try
719 %s/aaa/\=Foo()/gn
720 catch
721 let error_caught = 1
722 endtry
723 call assert_equal(1, error_caught)
724 call assert_equal(['1 aaa', '2 aaa', '3 aaa'], getline(1, 3))
725
Bram Moolenaar80341bc2019-05-20 20:34:51 +0200726 delfunc Foo
Bram Moolenaar0e97b942019-03-27 22:53:53 +0100727 bwipe!
728endfunc
Bram Moolenaarc6b37db2019-04-27 18:00:34 +0200729
730" Test ":s/pat/sub/" with different ~s in sub.
731func Test_replace_with_tilde()
732 new
733 " Set the last replace string to empty
734 s/^$//
735 call append(0, ['- Bug in "vPPPP" on this text:'])
736 normal gg
737 s/u/~u~/
738 call assert_equal('- Bug in "vPPPP" on this text:', getline(1))
739 s/i/~u~/
740 call assert_equal('- Bug uuun "vPPPP" on this text:', getline(1))
741 s/o/~~~/
742 call assert_equal('- Bug uuun "vPPPP" uuuuuuuuun this text:', getline(1))
743 close!
744endfunc
745
746func Test_replace_keeppatterns()
747 new
748 a
749foobar
750
751substitute foo asdf
752
753one two
754.
755
756 normal gg
757 /^substitute
758 s/foo/bar/
759 call assert_equal('foo', @/)
760 call assert_equal('substitute bar asdf', getline('.'))
761
762 /^substitute
763 keeppatterns s/asdf/xyz/
764 call assert_equal('^substitute', @/)
765 call assert_equal('substitute bar xyz', getline('.'))
766
767 exe "normal /bar /e\<CR>"
768 call assert_equal(15, col('.'))
769 normal -
770 keeppatterns /xyz
771 call assert_equal('bar ', @/)
772 call assert_equal('substitute bar xyz', getline('.'))
773 exe "normal 0dn"
774 call assert_equal('xyz', getline('.'))
775
776 close!
777endfunc
Bram Moolenaarbb265962019-10-31 04:38:36 +0100778
779func Test_sub_beyond_end()
780 new
781 call setline(1, '#')
782 let @/ = '^#\n\zs'
783 s///e
784 call assert_equal('#', getline(1))
785 bwipe!
786endfunc
Bram Moolenaar5d98dc22020-01-29 21:57:34 +0100787
Bram Moolenaarea3db912020-02-02 15:32:13 +0100788" Test for repeating last substitution using :~ and :&r
789func Test_repeat_last_sub()
790 new
791 call setline(1, ['blue green yellow orange white'])
792 s/blue/red/
793 let @/ = 'yellow'
794 ~
795 let @/ = 'white'
796 :&r
797 let @/ = 'green'
798 s//gray
799 call assert_equal('red gray red orange red', getline(1))
800 close!
801endfunc
802
803" Test for Vi compatible substitution:
804" \/{string}/, \?{string}? and \&{string}&
805func Test_sub_vi_compatibility()
806 new
807 call setline(1, ['blue green yellow orange blue'])
808 let @/ = 'orange'
809 s\/white/
810 let @/ = 'blue'
811 s\?amber?
812 let @/ = 'white'
813 s\&green&
814 call assert_equal('amber green yellow white green', getline(1))
815 close!
Bram Moolenaar9fb7b422022-03-05 21:13:26 +0000816
817 call assert_fails('vim9cmd s\/white/', 'E1270:')
818 call assert_fails('vim9cmd s\?white?', 'E1270:')
819 call assert_fails('vim9cmd s\&white&', 'E1270:')
Bram Moolenaarea3db912020-02-02 15:32:13 +0100820endfunc
821
822" Test for substitute with the new text longer than the original text
823func Test_sub_expand_text()
824 new
825 call setline(1, 'abcabcabcabcabcabcabcabc')
826 s/b/\=repeat('B', 10)/g
827 call assert_equal(repeat('aBBBBBBBBBBc', 8), getline(1))
828 close!
829endfunc
830
Bram Moolenaar07ada5f2020-02-05 20:38:22 +0100831" Test for command failures when the last substitute pattern is not set.
832func Test_sub_with_no_last_pat()
Bram Moolenaar9f6277b2020-02-11 22:04:02 +0100833 let lines =<< trim [SCRIPT]
834 call assert_fails('~', 'E33:')
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200835 call assert_fails('s//abc/g', 'E35:')
836 call assert_fails('s\/bar', 'E35:')
837 call assert_fails('s\&bar&', 'E33:')
Bram Moolenaar9f6277b2020-02-11 22:04:02 +0100838 call writefile(v:errors, 'Xresult')
839 qall!
840 [SCRIPT]
841 call writefile(lines, 'Xscript')
842 if RunVim([], [], '--clean -S Xscript')
843 call assert_equal([], readfile('Xresult'))
844 endif
Bram Moolenaar07ada5f2020-02-05 20:38:22 +0100845
Bram Moolenaar9f6277b2020-02-11 22:04:02 +0100846 let lines =<< trim [SCRIPT]
847 set cpo+=/
848 call assert_fails('s/abc/%/', 'E33:')
849 call writefile(v:errors, 'Xresult')
850 qall!
851 [SCRIPT]
852 call writefile(lines, 'Xscript')
853 if RunVim([], [], '--clean -S Xscript')
854 call assert_equal([], readfile('Xresult'))
855 endif
856
857 call delete('Xscript')
858 call delete('Xresult')
Bram Moolenaar07ada5f2020-02-05 20:38:22 +0100859endfunc
860
Bram Moolenaarca68ae12020-03-30 19:32:53 +0200861func Test_substitute()
862 call assert_equal('a1a2a3a', substitute('123', '\zs', 'a', 'g'))
Bram Moolenaar004a6782020-04-11 17:09:31 +0200863 " Substitute with special keys
864 call assert_equal("a\<End>c", substitute('abc', "a.c", "a\<End>c", ''))
865endfunc
866
867func Test_substitute_expr()
868 let g:val = 'XXX'
869 call assert_equal('XXX', substitute('yyy', 'y*', '\=g:val', ''))
870 call assert_equal('XXX', substitute('yyy', 'y*', {-> g:val}, ''))
871 call assert_equal("-\u1b \uf2-", substitute("-%1b %f2-", '%\(\x\x\)',
872 \ '\=nr2char("0x" . submatch(1))', 'g'))
873 call assert_equal("-\u1b \uf2-", substitute("-%1b %f2-", '%\(\x\x\)',
874 \ {-> nr2char("0x" . submatch(1))}, 'g'))
875
876 call assert_equal('231', substitute('123', '\(.\)\(.\)\(.\)',
877 \ {-> submatch(2) . submatch(3) . submatch(1)}, ''))
878
879 func Recurse()
880 return substitute('yyy', 'y\(.\)y', {-> submatch(1)}, '')
881 endfunc
882 " recursive call works
883 call assert_equal('-y-x-', substitute('xxx', 'x\(.\)x', {-> '-' . Recurse() . '-' . submatch(1) . '-'}, ''))
884
885 call assert_fails("let s=submatch([])", 'E745:')
886 call assert_fails("let s=submatch(2, [])", 'E745:')
887endfunc
888
889func Test_invalid_submatch()
890 " This was causing invalid memory access in Vim-7.4.2232 and older
891 call assert_fails("call substitute('x', '.', {-> submatch(10)}, '')", 'E935:')
892 call assert_fails('eval submatch(-1)', 'E935:')
893 call assert_equal('', submatch(0))
894 call assert_equal('', submatch(1))
895 call assert_equal([], submatch(0, 1))
896 call assert_equal([], submatch(1, 1))
897endfunc
898
Bram Moolenaar8a0dcf42020-09-06 15:14:45 +0200899func Test_submatch_list_concatenate()
900 let pat = 'A\(.\)'
901 let Rep = {-> string([submatch(0, 1)] + [[submatch(1)]])}
902 call substitute('A1', pat, Rep, '')->assert_equal("[['A1'], ['1']]")
903endfunc
904
Bram Moolenaar004a6782020-04-11 17:09:31 +0200905func Test_substitute_expr_arg()
906 call assert_equal('123456789-123456789=', substitute('123456789',
907 \ '\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)',
908 \ {m -> m[0] . '-' . m[1] . m[2] . m[3] . m[4] . m[5] . m[6] . m[7] . m[8] . m[9] . '='}, ''))
909
910 call assert_equal('123456-123456=789', substitute('123456789',
911 \ '\(.\)\(.\)\(.\)\(a*\)\(n*\)\(.\)\(.\)\(.\)\(x*\)',
912 \ {m -> m[0] . '-' . m[1] . m[2] . m[3] . m[4] . m[5] . m[6] . m[7] . m[8] . m[9] . '='}, ''))
913
914 call assert_equal('123456789-123456789x=', substitute('123456789',
915 \ '\(.\)\(.\)\(.*\)',
916 \ {m -> m[0] . '-' . m[1] . m[2] . m[3] . 'x' . m[4] . m[5] . m[6] . m[7] . m[8] . m[9] . '='}, ''))
917
918 call assert_fails("call substitute('xxx', '.', {m -> string(add(m, 'x'))}, '')", 'E742:')
919 call assert_fails("call substitute('xxx', '.', {m -> string(insert(m, 'x'))}, '')", 'E742:')
920 call assert_fails("call substitute('xxx', '.', {m -> string(extend(m, ['x']))}, '')", 'E742:')
921 call assert_fails("call substitute('xxx', '.', {m -> string(remove(m, 1))}, '')", 'E742:')
922endfunc
923
924" Test for using a function to supply the substitute string
925func Test_substitute_using_func()
926 func Xfunc()
927 return '1234'
928 endfunc
929 call assert_equal('a1234f', substitute('abcdef', 'b..e',
930 \ function("Xfunc"), ''))
931 delfunc Xfunc
932endfunc
933
934" Test for using submatch() with a multiline match
935func Test_substitute_multiline_submatch()
936 new
937 call setline(1, ['line1', 'line2', 'line3', 'line4'])
938 %s/^line1\(\_.\+\)line4$/\=submatch(1)/
939 call assert_equal(['', 'line2', 'line3', ''], getline(1, '$'))
940 close!
Bram Moolenaarca68ae12020-03-30 19:32:53 +0200941endfunc
942
Bram Moolenaardf365142021-05-03 20:01:45 +0200943func Test_substitute_skipped_range()
944 new
945 if 0
946 /1/5/2/2/\n
947 endif
948 call assert_equal([0, 1, 1, 0, 1], getcurpos())
949 bwipe!
950endfunc
951
Dominique Pellebfb2bb12021-08-14 21:11:51 +0200952" Test using the 'gdefault' option (when on, flag 'g' is default on).
953func Test_substitute_gdefault()
954 new
955
956 " First check without 'gdefault'
957 call setline(1, 'foo bar foo')
958 s/foo/FOO/
959 call assert_equal('FOO bar foo', getline(1))
960 call setline(1, 'foo bar foo')
961 s/foo/FOO/g
962 call assert_equal('FOO bar FOO', getline(1))
963 call setline(1, 'foo bar foo')
964 s/foo/FOO/gg
965 call assert_equal('FOO bar foo', getline(1))
966
967 " Then check with 'gdefault'
968 set gdefault
969 call setline(1, 'foo bar foo')
970 s/foo/FOO/
971 call assert_equal('FOO bar FOO', getline(1))
972 call setline(1, 'foo bar foo')
973 s/foo/FOO/g
974 call assert_equal('FOO bar foo', getline(1))
975 call setline(1, 'foo bar foo')
976 s/foo/FOO/gg
977 call assert_equal('FOO bar FOO', getline(1))
978
979 " Setting 'compatible' should reset 'gdefault'
980 call assert_equal(1, &gdefault)
981 set compatible
982 call assert_equal(0, &gdefault)
983 set nocompatible
984 call assert_equal(0, &gdefault)
985
986 bw!
987endfunc
988
Bram Moolenaar37f47952022-01-29 14:21:51 +0000989" This was using "old_sub" after it was freed.
990func Test_using_old_sub()
991 set compatible maxfuncdepth=10
992 new
993 call setline(1, 'some text.')
994 func Repl()
995 ~
996 s/
997 endfunc
998 silent! s/\%')/\=Repl()
999
1000 delfunc Repl
1001 bwipe!
1002 set nocompatible
1003endfunc
1004
Bram Moolenaare2bd8602022-05-18 13:11:57 +01001005" This was switching windows in between computing the length and using it.
1006func Test_sub_change_window()
1007 silent! lfile
1008 sil! norm o0000000000000000000000000000000000000000000000000000
1009 func Repl()
1010 lopen
1011 endfunc
1012 silent! s/\%')/\=Repl()
1013 bwipe!
1014 bwipe!
1015 delfunc Repl
1016endfunc
1017
Bram Moolenaar338f1fc2022-05-26 15:56:23 +01001018" This was undoign a change in between computing the length and using it.
1019func Do_Test_sub_undo_change()
1020 new
1021 norm o0000000000000000000000000000000000000000000000000000
1022 silent! s/\%')/\=Repl()
1023 bwipe!
1024endfunc
1025
1026func Test_sub_undo_change()
1027 func Repl()
1028 silent! norm g-
1029 endfunc
1030 call Do_Test_sub_undo_change()
1031
1032 func! Repl()
1033 silent earlier
1034 endfunc
1035 call Do_Test_sub_undo_change()
1036
1037 delfunc Repl
1038endfunc
1039
Bram Moolenaar71223e22022-05-30 15:23:09 +01001040" This was opening a command line window from the expression
1041func Test_sub_open_cmdline_win()
1042 " the error only happens in a very specific setup, run a new Vim instance to
1043 " get a clean starting point.
1044 let lines =<< trim [SCRIPT]
Bram Moolenaarbe990422022-05-30 16:01:42 +01001045 set vb t_vb=
Bram Moolenaar71223e22022-05-30 15:23:09 +01001046 norm o0000000000000000000000000000000000000000000000000000
1047 func Replace()
1048 norm q/
1049 endfunc
1050 s/\%')/\=Replace()
1051 redir >Xresult
1052 messages
1053 redir END
1054 qall!
1055 [SCRIPT]
1056 call writefile(lines, 'Xscript')
1057 if RunVim([], [], '-u NONE -S Xscript')
Bram Moolenaarbe990422022-05-30 16:01:42 +01001058 call assert_match('E565: Not allowed to change text or change window',
1059 \ readfile('Xresult')->join('XX'))
Bram Moolenaar71223e22022-05-30 15:23:09 +01001060 endif
1061
1062 call delete('Xscript')
1063 call delete('Xresult')
1064endfunc
1065
Yegappan Lakshmanan5e877ba2022-03-25 21:19:26 +00001066" Test for the 2-letter and 3-letter :substitute commands
1067func Test_substitute_short_cmd()
1068 new
1069 call setline(1, ['one', 'one one one'])
1070 s/one/two
1071 call cursor(2, 1)
1072
1073 " :sc
1074 call feedkeys(":sc\<CR>y", 'xt')
1075 call assert_equal('two one one', getline(2))
1076
1077 " :scg
1078 call setline(2, 'one one one')
1079 call feedkeys(":scg\<CR>nyq", 'xt')
1080 call assert_equal('one two one', getline(2))
1081
1082 " :sci
1083 call setline(2, 'ONE One onE')
1084 call feedkeys(":sci\<CR>y", 'xt')
1085 call assert_equal('two One onE', getline(2))
1086
1087 " :scI
1088 set ignorecase
1089 call setline(2, 'ONE One one')
1090 call feedkeys(":scI\<CR>y", 'xt')
1091 call assert_equal('ONE One two', getline(2))
1092 set ignorecase&
1093
1094 " :scn
1095 call setline(2, 'one one one')
1096 let t = execute('scn')->split("\n")
1097 call assert_equal(['1 match on 1 line'], t)
1098 call assert_equal('one one one', getline(2))
1099
1100 " :scp
1101 call setline(2, "\tone one one")
1102 redir => output
1103 call feedkeys(":scp\<CR>y", 'xt')
1104 redir END
1105 call assert_equal(' two one one', output->split("\n")[-1])
1106 call assert_equal("\ttwo one one", getline(2))
1107
1108 " :scl
1109 call setline(2, "\tone one one")
1110 redir => output
1111 call feedkeys(":scl\<CR>y", 'xt')
1112 redir END
1113 call assert_equal("^Itwo one one$", output->split("\n")[-1])
1114 call assert_equal("\ttwo one one", getline(2))
1115
1116 " :sgc
1117 call setline(2, 'one one one one one')
1118 call feedkeys(":sgc\<CR>nyyq", 'xt')
1119 call assert_equal('one two two one one', getline(2))
1120
1121 " :sg
1122 call setline(2, 'one one one')
1123 sg
1124 call assert_equal('two two two', getline(2))
1125
1126 " :sgi
1127 call setline(2, 'ONE One onE')
1128 sgi
1129 call assert_equal('two two two', getline(2))
1130
1131 " :sgI
1132 set ignorecase
1133 call setline(2, 'ONE One one')
1134 sgI
1135 call assert_equal('ONE One two', getline(2))
1136 set ignorecase&
1137
1138 " :sgn
1139 call setline(2, 'one one one')
1140 let t = execute('sgn')->split("\n")
1141 call assert_equal(['3 matches on 1 line'], t)
1142 call assert_equal('one one one', getline(2))
1143
1144 " :sgp
1145 call setline(2, "\tone one one")
1146 redir => output
1147 sgp
1148 redir END
1149 call assert_equal(' two two two', output->split("\n")[-1])
1150 call assert_equal("\ttwo two two", getline(2))
1151
1152 " :sgl
1153 call setline(2, "\tone one one")
1154 redir => output
1155 sgl
1156 redir END
1157 call assert_equal("^Itwo two two$", output->split("\n")[-1])
1158 call assert_equal("\ttwo two two", getline(2))
1159
1160 " :sgr
1161 call setline(2, "one one one")
1162 call cursor(2, 1)
1163 s/abc/xyz/e
1164 let @/ = 'one'
1165 sgr
1166 call assert_equal('xyz xyz xyz', getline(2))
1167
1168 " :sic
1169 call cursor(1, 1)
1170 s/one/two/e
1171 call setline(2, "ONE One one")
1172 call cursor(2, 1)
1173 call feedkeys(":sic\<CR>y", 'xt')
1174 call assert_equal('two One one', getline(2))
1175
1176 " :si
1177 call setline(2, "ONE One one")
1178 si
1179 call assert_equal('two One one', getline(2))
1180
1181 " :siI
1182 call setline(2, "ONE One one")
1183 siI
1184 call assert_equal('ONE One two', getline(2))
1185
1186 " :sin
1187 call setline(2, 'ONE One onE')
1188 let t = execute('sin')->split("\n")
1189 call assert_equal(['1 match on 1 line'], t)
1190 call assert_equal('ONE One onE', getline(2))
1191
1192 " :sip
1193 call setline(2, "\tONE One onE")
1194 redir => output
1195 sip
1196 redir END
1197 call assert_equal(' two One onE', output->split("\n")[-1])
1198 call assert_equal("\ttwo One onE", getline(2))
1199
1200 " :sir
1201 call setline(2, "ONE One onE")
1202 call cursor(2, 1)
1203 s/abc/xyz/e
1204 let @/ = 'one'
1205 sir
1206 call assert_equal('xyz One onE', getline(2))
1207
1208 " :sIc
1209 call cursor(1, 1)
1210 s/one/two/e
1211 call setline(2, "ONE One one")
1212 call cursor(2, 1)
1213 call feedkeys(":sIc\<CR>y", 'xt')
1214 call assert_equal('ONE One two', getline(2))
1215
1216 " :sIg
1217 call setline(2, "ONE one onE one")
1218 sIg
1219 call assert_equal('ONE two onE two', getline(2))
1220
1221 " :sIi
1222 call setline(2, "ONE One one")
1223 sIi
1224 call assert_equal('two One one', getline(2))
1225
1226 " :sI
1227 call setline(2, "ONE One one")
1228 sI
1229 call assert_equal('ONE One two', getline(2))
1230
1231 " :sIn
1232 call setline(2, 'ONE One one')
1233 let t = execute('sIn')->split("\n")
1234 call assert_equal(['1 match on 1 line'], t)
1235 call assert_equal('ONE One one', getline(2))
1236
1237 " :sIp
1238 call setline(2, "\tONE One one")
1239 redir => output
1240 sIp
1241 redir END
1242 call assert_equal(' ONE One two', output->split("\n")[-1])
1243 call assert_equal("\tONE One two", getline(2))
1244
1245 " :sIl
1246 call setline(2, "\tONE onE one")
1247 redir => output
1248 sIl
1249 redir END
1250 call assert_equal("^IONE onE two$", output->split("\n")[-1])
1251 call assert_equal("\tONE onE two", getline(2))
1252
1253 " :sIr
1254 call setline(2, "ONE one onE")
1255 call cursor(2, 1)
1256 s/abc/xyz/e
1257 let @/ = 'one'
1258 sIr
1259 call assert_equal('ONE xyz onE', getline(2))
1260
1261 " :src
1262 call setline(2, "ONE one one")
1263 call cursor(2, 1)
1264 s/abc/xyz/e
1265 let @/ = 'one'
1266 call feedkeys(":src\<CR>y", 'xt')
1267 call assert_equal('ONE xyz one', getline(2))
1268
1269 " :srg
1270 call setline(2, "one one one")
1271 call cursor(2, 1)
1272 s/abc/xyz/e
1273 let @/ = 'one'
1274 srg
1275 call assert_equal('xyz xyz xyz', getline(2))
1276
1277 " :sri
1278 call setline(2, "ONE one onE")
1279 call cursor(2, 1)
1280 s/abc/xyz/e
1281 let @/ = 'one'
1282 sri
1283 call assert_equal('xyz one onE', getline(2))
1284
1285 " :srI
1286 call setline(2, "ONE one onE")
1287 call cursor(2, 1)
1288 s/abc/xyz/e
1289 let @/ = 'one'
1290 srI
1291 call assert_equal('ONE xyz onE', getline(2))
1292
1293 " :srn
1294 call setline(2, "ONE one onE")
1295 call cursor(2, 1)
1296 s/abc/xyz/e
1297 let @/ = 'one'
1298 let t = execute('srn')->split("\n")
1299 call assert_equal(['1 match on 1 line'], t)
1300 call assert_equal('ONE one onE', getline(2))
1301
1302 " :srp
1303 call setline(2, "\tONE one onE")
1304 call cursor(2, 1)
1305 s/abc/xyz/e
1306 let @/ = 'one'
1307 redir => output
1308 srp
1309 redir END
1310 call assert_equal(' ONE xyz onE', output->split("\n")[-1])
1311 call assert_equal("\tONE xyz onE", getline(2))
1312
1313 " :srl
1314 call setline(2, "\tONE one onE")
1315 call cursor(2, 1)
1316 s/abc/xyz/e
1317 let @/ = 'one'
1318 redir => output
1319 srl
1320 redir END
1321 call assert_equal("^IONE xyz onE$", output->split("\n")[-1])
1322 call assert_equal("\tONE xyz onE", getline(2))
1323
1324 " :sr
1325 call setline(2, "ONE one onE")
1326 call cursor(2, 1)
1327 s/abc/xyz/e
1328 let @/ = 'one'
1329 sr
1330 call assert_equal('ONE xyz onE', getline(2))
1331
1332 " :sce
1333 s/abc/xyz/e
1334 call assert_fails("sc", 'E486:')
1335 sce
1336 " :sge
1337 call assert_fails("sg", 'E486:')
1338 sge
1339 " :sie
1340 call assert_fails("si", 'E486:')
1341 sie
1342 " :sIe
1343 call assert_fails("sI", 'E486:')
1344 sIe
1345
1346 bw!
1347endfunc
Bram Moolenaar37f47952022-01-29 14:21:51 +00001348
Bram Moolenaar5d98dc22020-01-29 21:57:34 +01001349" vim: shiftwidth=2 sts=2 expandtab