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