blob: 0ae407d8af3f3210588d9065889418c141542d05 [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
144" Test for *sub-replace-special* and *sub-replace-expression* on substitute().
145func Test_sub_replace_1()
146 " Run the tests with 'magic' on
147 set magic
148 set cpo&
149 call assert_equal('AA', substitute('A', 'A', '&&', ''))
150 call assert_equal('&', substitute('B', 'B', '\&', ''))
151 call assert_equal('C123456789987654321', substitute('C123456789', 'C\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)', '\0\9\8\7\6\5\4\3\2\1', ''))
152 call assert_equal('d', substitute('D', 'D', 'd', ''))
153 call assert_equal('~', substitute('E', 'E', '~', ''))
154 call assert_equal('~', substitute('F', 'F', '\~', ''))
155 call assert_equal('Gg', substitute('G', 'G', '\ugg', ''))
156 call assert_equal('Hh', substitute('H', 'H', '\Uh\Eh', ''))
157 call assert_equal('iI', substitute('I', 'I', '\lII', ''))
158 call assert_equal('jJ', substitute('J', 'J', '\LJ\EJ', ''))
159 call assert_equal('Kk', substitute('K', 'K', '\Uk\ek', ''))
160 call assert_equal("l\<C-V>\<C-M>l",
161 \ substitute('lLl', 'L', "\<C-V>\<C-M>", ''))
162 call assert_equal("m\<C-M>m", substitute('mMm', 'M', '\r', ''))
163 call assert_equal("n\<C-V>\<C-M>n",
164 \ substitute('nNn', 'N', "\\\<C-V>\<C-M>", ''))
165 call assert_equal("o\no", substitute('oOo', 'O', '\n', ''))
166 call assert_equal("p\<C-H>p", substitute('pPp', 'P', '\b', ''))
167 call assert_equal("q\tq", substitute('qQq', 'Q', '\t', ''))
168 call assert_equal('r\r', substitute('rRr', 'R', '\\', ''))
169 call assert_equal('scs', substitute('sSs', 'S', '\c', ''))
170 call assert_equal("u\nu", substitute('uUu', 'U', "\n", ''))
171 call assert_equal("v\<C-H>v", substitute('vVv', 'V', "\b", ''))
172 call assert_equal("w\\w", substitute('wWw', 'W', "\\", ''))
173 call assert_equal("x\<C-M>x", substitute('xXx', 'X', "\r", ''))
174 call assert_equal("YyyY", substitute('Y', 'Y', '\L\uyYy\l\EY', ''))
175 call assert_equal("zZZz", substitute('Z', 'Z', '\U\lZzZ\u\Ez', ''))
176endfunc
177
178func Test_sub_replace_2()
179 " Run the tests with 'magic' off
180 set nomagic
181 set cpo&
182 call assert_equal('AA', substitute('A', 'A', '&&', ''))
183 call assert_equal('&', substitute('B', 'B', '\&', ''))
184 call assert_equal('C123456789987654321', substitute('C123456789', 'C\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)', '\0\9\8\7\6\5\4\3\2\1', ''))
185 call assert_equal('d', substitute('D', 'D', 'd', ''))
186 call assert_equal('~', substitute('E', 'E', '~', ''))
187 call assert_equal('~', substitute('F', 'F', '\~', ''))
188 call assert_equal('Gg', substitute('G', 'G', '\ugg', ''))
189 call assert_equal('Hh', substitute('H', 'H', '\Uh\Eh', ''))
190 call assert_equal('iI', substitute('I', 'I', '\lII', ''))
191 call assert_equal('jJ', substitute('J', 'J', '\LJ\EJ', ''))
192 call assert_equal('Kk', substitute('K', 'K', '\Uk\ek', ''))
193 call assert_equal("l\<C-V>\<C-M>l",
194 \ substitute('lLl', 'L', "\<C-V>\<C-M>", ''))
195 call assert_equal("m\<C-M>m", substitute('mMm', 'M', '\r', ''))
196 call assert_equal("n\<C-V>\<C-M>n",
197 \ substitute('nNn', 'N', "\\\<C-V>\<C-M>", ''))
198 call assert_equal("o\no", substitute('oOo', 'O', '\n', ''))
199 call assert_equal("p\<C-H>p", substitute('pPp', 'P', '\b', ''))
200 call assert_equal("q\tq", substitute('qQq', 'Q', '\t', ''))
201 call assert_equal('r\r', substitute('rRr', 'R', '\\', ''))
202 call assert_equal('scs', substitute('sSs', 'S', '\c', ''))
203 call assert_equal("t\<C-M>t", substitute('tTt', 'T', "\r", ''))
204 call assert_equal("u\nu", substitute('uUu', 'U', "\n", ''))
205 call assert_equal("v\<C-H>v", substitute('vVv', 'V', "\b", ''))
206 call assert_equal('w\w', substitute('wWw', 'W', "\\", ''))
207 call assert_equal('XxxX', substitute('X', 'X', '\L\uxXx\l\EX', ''))
208 call assert_equal('yYYy', substitute('Y', 'Y', '\U\lYyY\u\Ey', ''))
209endfunc
210
211func Test_sub_replace_3()
212 set magic&
213 set cpo&
214 call assert_equal('a\a', substitute('aAa', 'A', '\="\\"', ''))
215 call assert_equal('b\\b', substitute('bBb', 'B', '\="\\\\"', ''))
216 call assert_equal("c\rc", substitute('cCc', 'C', "\\=\"\r\"", ''))
217 call assert_equal("d\\\rd", substitute('dDd', 'D', "\\=\"\\\\\r\"", ''))
218 call assert_equal("e\\\\\re", substitute('eEe', 'E', "\\=\"\\\\\\\\\r\"", ''))
219 call assert_equal('f\rf', substitute('fFf', 'F', '\="\\r"', ''))
220 call assert_equal('j\nj', substitute('jJj', 'J', '\="\\n"', ''))
221 call assert_equal("k\<C-M>k", substitute('kKk', 'K', '\="\r"', ''))
222 call assert_equal("l\nl", substitute('lLl', 'L', '\="\n"', ''))
223endfunc
224
225" Test for submatch() on substitute().
226func Test_sub_replace_4()
227 set magic&
228 set cpo&
229 call assert_equal('a\a', substitute('aAa', 'A',
230 \ '\=substitute(submatch(0), ".", "\\", "")', ''))
231 call assert_equal('b\b', substitute('bBb', 'B',
232 \ '\=substitute(submatch(0), ".", "\\\\", "")', ''))
233 call assert_equal("c\<C-V>\<C-M>c", substitute('cCc', 'C', '\=substitute(submatch(0), ".", "\<C-V>\<C-M>", "")', ''))
234 call assert_equal("d\<C-V>\<C-M>d", substitute('dDd', 'D', '\=substitute(submatch(0), ".", "\\\<C-V>\<C-M>", "")', ''))
235 call assert_equal("e\\\<C-V>\<C-M>e", substitute('eEe', 'E', '\=substitute(submatch(0), ".", "\\\\\<C-V>\<C-M>", "")', ''))
236 call assert_equal("f\<C-M>f", substitute('fFf', 'F', '\=substitute(submatch(0), ".", "\\r", "")', ''))
237 call assert_equal("j\nj", substitute('jJj', 'J', '\=substitute(submatch(0), ".", "\\n", "")', ''))
238 call assert_equal("k\rk", substitute('kKk', 'K', '\=substitute(submatch(0), ".", "\r", "")', ''))
239 call assert_equal("l\nl", substitute('lLl', 'L', '\=substitute(submatch(0), ".", "\n", "")', ''))
240endfunc
241
242func Test_sub_replace_5()
243 set magic&
244 set cpo&
245 call assert_equal('A123456789987654321', substitute('A123456789',
246 \ 'A\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)',
247 \ '\=submatch(0) . submatch(9) . submatch(8) . ' .
248 \ 'submatch(7) . submatch(6) . submatch(5) . ' .
249 \ 'submatch(4) . submatch(3) . submatch(2) . submatch(1)',
250 \ ''))
251 call assert_equal("[['A123456789'], ['9'], ['8'], ['7'], ['6'], " .
252 \ "['5'], ['4'], ['3'], ['2'], ['1']]",
253 \ substitute('A123456789',
254 \ 'A\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)',
255 \ '\=string([submatch(0, 1), submatch(9, 1), ' .
256 \ 'submatch(8, 1), submatch(7, 1), submatch(6, 1), ' .
257 \ 'submatch(5, 1), submatch(4, 1), submatch(3, 1), ' .
258 \ 'submatch(2, 1), submatch(1, 1)])',
259 \ ''))
260endfunc
261
262func Test_sub_replace_6()
263 set magic&
264 set cpo+=/
265 call assert_equal('a', substitute('A', 'A', 'a', ''))
266 call assert_equal('%', substitute('B', 'B', '%', ''))
267 set cpo-=/
268 call assert_equal('c', substitute('C', 'C', 'c', ''))
269 call assert_equal('%', substitute('D', 'D', '%', ''))
270endfunc
271
272func Test_sub_replace_7()
273 set magic&
274 set cpo&
275 call assert_equal('AA', substitute('AA', 'A.', '\=submatch(0)', ''))
276 call assert_equal("B\nB", substitute("B\nB", 'B.', '\=submatch(0)', ''))
277 call assert_equal("['B\n']B", substitute("B\nB", 'B.', '\=string(submatch(0, 1))', ''))
278 call assert_equal('-abab', substitute('-bb', '\zeb', 'a', 'g'))
279 call assert_equal('c-cbcbc', substitute('-bb', '\ze', 'c', 'g'))
280endfunc
281
282" Test for *:s%* on :substitute.
283func Test_sub_replace_8()
284 new
285 set magic&
286 set cpo&
287 $put =',,X'
288 s/\(^\|,\)\ze\(,\|X\)/\1N/g
289 call assert_equal('N,,NX', getline("$"))
290 $put =',,Y'
291 let cmd = ':s/\(^\|,\)\ze\(,\|Y\)/\1N/gc'
292 call feedkeys(cmd . "\<CR>a", "xt")
293 call assert_equal('N,,NY', getline("$"))
294 :$put =',,Z'
295 let cmd = ':s/\(^\|,\)\ze\(,\|Z\)/\1N/gc'
296 call feedkeys(cmd . "\<CR>yy", "xt")
297 call assert_equal('N,,NZ', getline("$"))
298 enew! | close
299endfunc
300
301func Test_sub_replace_9()
302 new
303 set magic&
304 set cpo&
305 $put ='xxx'
306 call feedkeys(":s/x/X/gc\<CR>yyq", "xt")
307 call assert_equal('XXx', getline("$"))
308 enew! | close
309endfunc
310
311func Test_sub_replace_10()
312 set magic&
313 set cpo&
314 call assert_equal('a1a2a3a', substitute('123', '\zs', 'a', 'g'))
315 call assert_equal('aaa', substitute('123', '\zs.', 'a', 'g'))
316 call assert_equal('1a2a3a', substitute('123', '.\zs', 'a', 'g'))
317 call assert_equal('a1a2a3a', substitute('123', '\ze', 'a', 'g'))
318 call assert_equal('a1a2a3', substitute('123', '\ze.', 'a', 'g'))
319 call assert_equal('aaa', substitute('123', '.\ze', 'a', 'g'))
320 call assert_equal('aa2a3a', substitute('123', '1\|\ze', 'a', 'g'))
321 call assert_equal('1aaa', substitute('123', '1\zs\|[23]', 'a', 'g'))
322endfunc
Bram Moolenaar15993ce2017-10-26 20:21:44 +0200323
324" Tests for *sub-replace-special* and *sub-replace-expression* on :substitute.
325
326" Execute a list of :substitute command tests
327func Run_SubCmd_Tests(tests)
328 enew!
329 for t in a:tests
330 let start = line('.') + 1
331 let end = start + len(t[2]) - 1
332 exe "normal o" . t[0]
333 call cursor(start, 1)
334 exe t[1]
335 call assert_equal(t[2], getline(start, end), t[1])
336 endfor
337 enew!
338endfunc
339
340func Test_sub_cmd_1()
341 set magic
342 set cpo&
343
344 " List entry format: [input, cmd, output]
345 let tests = [['A', 's/A/&&/', ['AA']],
346 \ ['B', 's/B/\&/', ['&']],
347 \ ['C123456789', 's/C\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)/\0\9\8\7\6\5\4\3\2\1/', ['C123456789987654321']],
348 \ ['D', 's/D/d/', ['d']],
349 \ ['E', 's/E/~/', ['d']],
350 \ ['F', 's/F/\~/', ['~']],
351 \ ['G', 's/G/\ugg/', ['Gg']],
352 \ ['H', 's/H/\Uh\Eh/', ['Hh']],
353 \ ['I', 's/I/\lII/', ['iI']],
354 \ ['J', 's/J/\LJ\EJ/', ['jJ']],
355 \ ['K', 's/K/\Uk\ek/', ['Kk']],
356 \ ['lLl', "s/L/\<C-V>\<C-M>/", ["l\<C-V>", 'l']],
357 \ ['mMm', 's/M/\r/', ['m', 'm']],
358 \ ['nNn', "s/N/\\\<C-V>\<C-M>/", ["n\<C-V>", 'n']],
359 \ ['oOo', 's/O/\n/', ["o\no"]],
360 \ ['pPp', 's/P/\b/', ["p\<C-H>p"]],
361 \ ['qQq', 's/Q/\t/', ["q\tq"]],
362 \ ['rRr', 's/R/\\/', ['r\r']],
363 \ ['sSs', 's/S/\c/', ['scs']],
364 \ ['tTt', "s/T/\<C-V>\<C-J>/", ["t\<C-V>\<C-J>t"]],
365 \ ['U', 's/U/\L\uuUu\l\EU/', ['UuuU']],
366 \ ['V', 's/V/\U\lVvV\u\Ev/', ['vVVv']]
367 \ ]
368 call Run_SubCmd_Tests(tests)
369endfunc
370
371func Test_sub_cmd_2()
372 set nomagic
373 set cpo&
374
375 " List entry format: [input, cmd, output]
376 let tests = [['A', 's/A/&&/', ['&&']],
377 \ ['B', 's/B/\&/', ['B']],
378 \ ['C123456789', 's/\mC\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)/\0\9\8\7\6\5\4\3\2\1/', ['C123456789987654321']],
379 \ ['D', 's/D/d/', ['d']],
380 \ ['E', 's/E/~/', ['~']],
381 \ ['F', 's/F/\~/', ['~']],
382 \ ['G', 's/G/\ugg/', ['Gg']],
383 \ ['H', 's/H/\Uh\Eh/', ['Hh']],
384 \ ['I', 's/I/\lII/', ['iI']],
385 \ ['J', 's/J/\LJ\EJ/', ['jJ']],
386 \ ['K', 's/K/\Uk\ek/', ['Kk']],
387 \ ['lLl', "s/L/\<C-V>\<C-M>/", ["l\<C-V>", 'l']],
388 \ ['mMm', 's/M/\r/', ['m', 'm']],
389 \ ['nNn', "s/N/\\\<C-V>\<C-M>/", ["n\<C-V>", 'n']],
390 \ ['oOo', 's/O/\n/', ["o\no"]],
391 \ ['pPp', 's/P/\b/', ["p\<C-H>p"]],
392 \ ['qQq', 's/Q/\t/', ["q\tq"]],
393 \ ['rRr', 's/R/\\/', ['r\r']],
394 \ ['sSs', 's/S/\c/', ['scs']],
395 \ ['tTt', "s/T/\<C-V>\<C-J>/", ["t\<C-V>\<C-J>t"]],
396 \ ['U', 's/U/\L\uuUu\l\EU/', ['UuuU']],
397 \ ['V', 's/V/\U\lVvV\u\Ev/', ['vVVv']]
398 \ ]
399 call Run_SubCmd_Tests(tests)
400endfunc
401
402func Test_sub_cmd_3()
403 set nomagic
404 set cpo&
405
406 " List entry format: [input, cmd, output]
407 let tests = [['aAa', "s/A/\\='\\'/", ['a\a']],
408 \ ['bBb', "s/B/\\='\\\\'/", ['b\\b']],
409 \ ['cCc', "s/C/\\='\<C-V>\<C-M>'/", ["c\<C-V>", 'c']],
410 \ ['dDd', "s/D/\\='\\\<C-V>\<C-M>'/", ["d\\\<C-V>", 'd']],
411 \ ['eEe', "s/E/\\='\\\\\<C-V>\<C-M>'/", ["e\\\\\<C-V>", 'e']],
412 \ ['fFf', "s/F/\\='\r'/", ['f', 'f']],
413 \ ['gGg', "s/G/\\='\<C-V>\<C-J>'/", ["g\<C-V>", 'g']],
414 \ ['hHh', "s/H/\\='\\\<C-V>\<C-J>'/", ["h\\\<C-V>", 'h']],
415 \ ['iIi', "s/I/\\='\\\\\<C-V>\<C-J>'/", ["i\\\\\<C-V>", 'i']],
416 \ ['jJj', "s/J/\\='\n'/", ['j', 'j']],
417 \ ['kKk', 's/K/\="\r"/', ['k', 'k']],
418 \ ['lLl', 's/L/\="\n"/', ['l', 'l']]
419 \ ]
420 call Run_SubCmd_Tests(tests)
421endfunc
422
423" Test for submatch() on :substitue.
424func Test_sub_cmd_4()
425 set magic&
426 set cpo&
427
428 " List entry format: [input, cmd, output]
429 let tests = [ ['aAa', "s/A/\\=substitute(submatch(0), '.', '\\', '')/",
Bram Moolenaar1e115362019-01-09 23:01:02 +0100430 \ ['a\a']],
Bram Moolenaar15993ce2017-10-26 20:21:44 +0200431 \ ['bBb', "s/B/\\=substitute(submatch(0), '.', '\\', '')/",
Bram Moolenaar1e115362019-01-09 23:01:02 +0100432 \ ['b\b']],
Bram Moolenaar15993ce2017-10-26 20:21:44 +0200433 \ ['cCc', "s/C/\\=substitute(submatch(0), '.', '\<C-V>\<C-M>', '')/",
434 \ ["c\<C-V>", 'c']],
435 \ ['dDd', "s/D/\\=substitute(submatch(0), '.', '\\\<C-V>\<C-M>', '')/",
436 \ ["d\<C-V>", 'd']],
437 \ ['eEe', "s/E/\\=substitute(submatch(0), '.', '\\\\\<C-V>\<C-M>', '')/",
438 \ ["e\\\<C-V>", 'e']],
439 \ ['fFf', "s/F/\\=substitute(submatch(0), '.', '\\r', '')/",
440 \ ['f', 'f']],
441 \ ['gGg', 's/G/\=substitute(submatch(0), ".", "\<C-V>\<C-J>", "")/',
442 \ ["g\<C-V>", 'g']],
443 \ ['hHh', 's/H/\=substitute(submatch(0), ".", "\\\<C-V>\<C-J>", "")/',
444 \ ["h\<C-V>", 'h']],
445 \ ['iIi', 's/I/\=substitute(submatch(0), ".", "\\\\\<C-V>\<C-J>", "")/',
446 \ ["i\\\<C-V>", 'i']],
447 \ ['jJj', "s/J/\\=substitute(submatch(0), '.', '\\n', '')/",
448 \ ['j', 'j']],
449 \ ['kKk', "s/K/\\=substitute(submatch(0), '.', '\\r', '')/",
450 \ ['k', 'k']],
451 \ ['lLl', "s/L/\\=substitute(submatch(0), '.', '\\n', '')/",
452 \ ['l', 'l']],
453 \ ]
454 call Run_SubCmd_Tests(tests)
455endfunc
456
457func Test_sub_cmd_5()
458 set magic&
459 set cpo&
460
461 " List entry format: [input, cmd, output]
462 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']],
463 \ ['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']]"]],
464 \ ]
465 call Run_SubCmd_Tests(tests)
466endfunc
467
468" Test for *:s%* on :substitute.
469func Test_sub_cmd_6()
470 set magic&
471 set cpo+=/
472
473 " List entry format: [input, cmd, output]
474 let tests = [ ['A', 's/A/a/', ['a']],
475 \ ['B', 's/B/%/', ['a']],
476 \ ]
477 call Run_SubCmd_Tests(tests)
478
479 set cpo-=/
480 let tests = [ ['C', 's/C/c/', ['c']],
481 \ ['D', 's/D/%/', ['%']],
482 \ ]
483 call Run_SubCmd_Tests(tests)
484
485 set cpo&
486endfunc
487
488" Test for :s replacing \n with line break.
489func Test_sub_cmd_7()
490 set magic&
491 set cpo&
492
493 " List entry format: [input, cmd, output]
494 let tests = [ ["A\<C-V>\<C-M>A", 's/A./\=submatch(0)/', ['A', 'A']],
495 \ ["B\<C-V>\<C-J>B", 's/B./\=submatch(0)/', ['B', 'B']],
496 \ ["C\<C-V>\<C-J>C", 's/C./\=strtrans(string(submatch(0, 1)))/', [strtrans("['C\<C-J>']C")]],
497 \ ["D\<C-V>\<C-J>\nD", 's/D.\nD/\=strtrans(string(submatch(0, 1)))/', [strtrans("['D\<C-J>', 'D']")]],
498 \ ["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']")]],
499 \ ]
500 call Run_SubCmd_Tests(tests)
501
502 exe "normal oQ\nQ\<Esc>k"
503 call assert_fails('s/Q[^\n]Q/\=submatch(0)."foobar"/', 'E486')
504 enew!
505endfunc
506
507func TitleString()
508 let check = 'foo' =~ 'bar'
509 return ""
510endfunc
511
512func Test_sub_cmd_8()
513 set titlestring=%{TitleString()}
514
515 enew!
516 call append(0, ['', 'test_one', 'test_two'])
517 call cursor(1,1)
518 /^test_one/s/.*/\="foo\nbar"/
519 call assert_equal('foo', getline(2))
520 call assert_equal('bar', getline(3))
521 call feedkeys(':/^test_two/s/.*/\="foo\nbar"/c', "t")
522 call feedkeys("\<CR>y", "xt")
523 call assert_equal('foo', getline(4))
524 call assert_equal('bar', getline(5))
525
526 enew!
527 set titlestring&
528endfunc